Skip to content

Commit fea68cd

Browse files
authored
add exp check to disable envs ext if in control (#573)
1 parent 8724148 commit fea68cd

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

package.json

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@
110110
"command": "python-envs.setEnvManager",
111111
"title": "%python-envs.setEnvManager.title%",
112112
"category": "Python",
113-
"icon": "$(gear)"
113+
"icon": "$(gear)",
114+
"when": "config.python.useEnvironmentsExtension != false"
114115
},
115116
{
116117
"command": "python-envs.setPkgManager",
@@ -293,6 +294,22 @@
293294
"command": "python-envs.addPythonProjectGivenResource",
294295
"when": "false"
295296
},
297+
{
298+
"command": "python-envs.setEnvManager",
299+
"when": "config.python.useEnvironmentsExtension != false"
300+
},
301+
{
302+
"command": "python-envs.packages",
303+
"when": "config.python.useEnvironmentsExtension != false"
304+
},
305+
{
306+
"command": "python-envs.set",
307+
"when": "config.python.useEnvironmentsExtension != false"
308+
},
309+
{
310+
"command": "python-envs.setPkgManager",
311+
"when": "config.python.useEnvironmentsExtension != false"
312+
},
296313
{
297314
"command": "python-envs.removePythonProject",
298315
"when": "false"
@@ -307,7 +324,7 @@
307324
},
308325
{
309326
"command": "python-envs.runAsTask",
310-
"when": "true"
327+
"when": "config.python.useEnvironmentsExtension != false"
311328
},
312329
{
313330
"command": "python-envs.terminal.activate",
@@ -336,6 +353,18 @@
336353
{
337354
"command": "python-envs.revealProjectInExplorer",
338355
"when": "false"
356+
},
357+
{
358+
"command": "python-envs.createNewProjectFromTemplate",
359+
"when": "config.python.useEnvironmentsExtension != false"
360+
},
361+
{
362+
"command": "python-envs.terminal.revertStartupScriptChanges",
363+
"when": "config.python.useEnvironmentsExtension != false"
364+
},
365+
{
366+
"command": "python-envs.reportIssue",
367+
"when": "config.python.useEnvironmentsExtension != false"
339368
}
340369
],
341370
"view/item/context": [
@@ -477,7 +506,8 @@
477506
{
478507
"id": "python",
479508
"title": "Python",
480-
"icon": "files/logo.svg"
509+
"icon": "files/logo.svg",
510+
"when": "config.python.useEnvironmentsExtension != false"
481511
}
482512
]
483513
},
@@ -487,13 +517,15 @@
487517
"id": "python-projects",
488518
"name": "Python Projects",
489519
"icon": "files/logo.svg",
490-
"contextualTitle": "Python Projects"
520+
"contextualTitle": "Python Projects",
521+
"when": "config.python.useEnvironmentsExtension != false"
491522
},
492523
{
493524
"id": "env-managers",
494525
"name": "Environment Managers",
495526
"icon": "files/logo.svg",
496-
"contextualTitle": "Environment Managers"
527+
"contextualTitle": "Environment Managers",
528+
"when": "config.python.useEnvironmentsExtension != false"
497529
}
498530
]
499531
},

src/extension.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { commands, ExtensionContext, extensions, LogOutputChannel, Terminal, Uri, window, workspace } from 'vscode';
22
import { PythonEnvironment, PythonEnvironmentApi, PythonProjectCreator } from './api';
33
import { ensureCorrectVersion } from './common/extVersion';
4-
import { registerLogger, traceError, traceInfo } from './common/logging';
4+
import { registerLogger, traceError, traceInfo, traceWarn } from './common/logging';
55
import { clearPersistentState, setPersistentState } from './common/persistentState';
66
import { newProjectSelection } from './common/pickers/managers';
77
import { StopWatch } from './common/stopWatch';
@@ -15,6 +15,7 @@ import {
1515
onDidChangeActiveTerminal,
1616
onDidChangeTerminalShellIntegration,
1717
} from './common/window.apis';
18+
import { getConfiguration } from './common/workspace.apis';
1819
import { createManagerReady } from './features/common/managerReady';
1920
import { AutoFindProjects } from './features/creators/autoFindProjects';
2021
import { ExistingProjects } from './features/creators/existingProjects';
@@ -66,6 +67,7 @@ import { EnvironmentManagers, ProjectCreators, PythonProjectManager } from './in
6667
import { registerSystemPythonFeatures } from './managers/builtin/main';
6768
import { SysPythonManager } from './managers/builtin/sysPythonManager';
6869
import { createNativePythonFinder, NativePythonFinder } from './managers/common/nativePythonFinder';
70+
import { IDisposable } from './managers/common/types';
6971
import { registerCondaFeatures } from './managers/conda/main';
7072
import { registerPoetryFeatures } from './managers/poetry/main';
7173
import { registerPyenvFeatures } from './managers/pyenv/main';
@@ -148,19 +150,16 @@ async function collectEnvironmentInfo(
148150
return info.join('\n');
149151
}
150152

151-
export async function activate(context: ExtensionContext): Promise<PythonEnvironmentApi> {
152-
const start = new StopWatch();
153-
154-
// Attempt to set setting of config.python.useEnvironmentsExtension to true
155-
try {
156-
const config = workspace.getConfiguration('python');
157-
await config.update('useEnvironmentsExtension', true, true);
158-
} catch (err) {
159-
traceError(
160-
'Failed to set config.python.useEnvironmentsExtension to true. Please do so manually in your user settings now to ensure the Python environment extension is enabled during upcoming experimentation.',
161-
err,
153+
export async function activate(context: ExtensionContext): Promise<PythonEnvironmentApi | undefined> {
154+
const useEnvironmentsExtension = getConfiguration('python').get<boolean>('useEnvironmentsExtension', true);
155+
if (!useEnvironmentsExtension) {
156+
traceWarn(
157+
'The Python environments extension has been disabled via a setting. If you would like to opt into using the extension, please add the following to your user settings (note that updating this setting requires a window reload afterwards):\n\n"python.useEnvironmentsExtension": true',
162158
);
159+
await deactivate(context);
160+
return;
163161
}
162+
const start = new StopWatch();
164163

165164
// Logging should be set up before anything else.
166165
const outputChannel: LogOutputChannel = createLogOutputChannel('Python Environments');
@@ -508,4 +507,21 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
508507
return api;
509508
}
510509

511-
export function deactivate() {}
510+
export async function disposeAll(disposables: IDisposable[]): Promise<void> {
511+
await Promise.all(
512+
disposables.map(async (d) => {
513+
try {
514+
return Promise.resolve(d.dispose());
515+
} catch (_err) {
516+
// do nothing
517+
}
518+
return Promise.resolve();
519+
}),
520+
);
521+
}
522+
523+
export async function deactivate(context: ExtensionContext) {
524+
await disposeAll(context.subscriptions);
525+
context.subscriptions.length = 0; // Clear subscriptions to prevent memory leaks
526+
traceInfo('Python Environments extension deactivated.');
527+
}

src/managers/common/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ export interface Installable {
4343
*/
4444
readonly uri?: Uri;
4545
}
46+
export interface IDisposable {
47+
dispose(): void | undefined | Promise<void>;
48+
}

0 commit comments

Comments
 (0)