Skip to content

Commit 80fa073

Browse files
committed
add exp check to disable envs ext if in control
1 parent 44a683d commit 80fa073

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

package.json

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@
307307
},
308308
{
309309
"command": "python-envs.runAsTask",
310-
"when": "true"
310+
"when": "config.python.useEnvironmentsExtension != false"
311311
},
312312
{
313313
"command": "python-envs.terminal.activate",
@@ -336,6 +336,18 @@
336336
{
337337
"command": "python-envs.revealProjectInExplorer",
338338
"when": "false"
339+
},
340+
{
341+
"command": "python-envs.createNewProjectFromTemplate",
342+
"when": "config.python.useEnvironmentsExtension != false"
343+
},
344+
{
345+
"command": "python-envs.terminal.revertStartupScriptChanges",
346+
"when": "config.python.useEnvironmentsExtension != false"
347+
},
348+
{
349+
"command": "python-envs.reportIssue",
350+
"when": "config.python.useEnvironmentsExtension != false"
339351
}
340352
],
341353
"view/item/context": [
@@ -477,7 +489,8 @@
477489
{
478490
"id": "python",
479491
"title": "Python",
480-
"icon": "files/logo.svg"
492+
"icon": "files/logo.svg",
493+
"when": "config.python.useEnvironmentsExtension != false"
481494
}
482495
]
483496
},
@@ -487,13 +500,15 @@
487500
"id": "python-projects",
488501
"name": "Python Projects",
489502
"icon": "files/logo.svg",
490-
"contextualTitle": "Python Projects"
503+
"contextualTitle": "Python Projects",
504+
"when": "config.python.useEnvironmentsExtension != false"
491505
},
492506
{
493507
"id": "env-managers",
494508
"name": "Environment Managers",
495509
"icon": "files/logo.svg",
496-
"contextualTitle": "Environment Managers"
510+
"contextualTitle": "Environment Managers",
511+
"when": "config.python.useEnvironmentsExtension != false"
497512
}
498513
]
499514
},

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+
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)