From 80fa073fcba1354242229abe1e928b6f78af241a Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Thu, 26 Jun 2025 08:59:36 -0700 Subject: [PATCH 1/2] add exp check to disable envs ext if in control --- package.json | 23 ++++++++++++++++---- src/extension.ts | 42 +++++++++++++++++++++++++----------- src/managers/common/types.ts | 3 +++ 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index ef5984a3..278299cc 100644 --- a/package.json +++ b/package.json @@ -307,7 +307,7 @@ }, { "command": "python-envs.runAsTask", - "when": "true" + "when": "config.python.useEnvironmentsExtension != false" }, { "command": "python-envs.terminal.activate", @@ -336,6 +336,18 @@ { "command": "python-envs.revealProjectInExplorer", "when": "false" + }, + { + "command": "python-envs.createNewProjectFromTemplate", + "when": "config.python.useEnvironmentsExtension != false" + }, + { + "command": "python-envs.terminal.revertStartupScriptChanges", + "when": "config.python.useEnvironmentsExtension != false" + }, + { + "command": "python-envs.reportIssue", + "when": "config.python.useEnvironmentsExtension != false" } ], "view/item/context": [ @@ -477,7 +489,8 @@ { "id": "python", "title": "Python", - "icon": "files/logo.svg" + "icon": "files/logo.svg", + "when": "config.python.useEnvironmentsExtension != false" } ] }, @@ -487,13 +500,15 @@ "id": "python-projects", "name": "Python Projects", "icon": "files/logo.svg", - "contextualTitle": "Python Projects" + "contextualTitle": "Python Projects", + "when": "config.python.useEnvironmentsExtension != false" }, { "id": "env-managers", "name": "Environment Managers", "icon": "files/logo.svg", - "contextualTitle": "Environment Managers" + "contextualTitle": "Environment Managers", + "when": "config.python.useEnvironmentsExtension != false" } ] }, diff --git a/src/extension.ts b/src/extension.ts index d6c30276..f79ae157 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,7 @@ import { commands, ExtensionContext, extensions, LogOutputChannel, Terminal, Uri, window, workspace } from 'vscode'; import { PythonEnvironment, PythonEnvironmentApi, PythonProjectCreator } from './api'; import { ensureCorrectVersion } from './common/extVersion'; -import { registerLogger, traceError, traceInfo } from './common/logging'; +import { registerLogger, traceError, traceInfo, traceWarn } from './common/logging'; import { clearPersistentState, setPersistentState } from './common/persistentState'; import { newProjectSelection } from './common/pickers/managers'; import { StopWatch } from './common/stopWatch'; @@ -15,6 +15,7 @@ import { onDidChangeActiveTerminal, onDidChangeTerminalShellIntegration, } from './common/window.apis'; +import { getConfiguration } from './common/workspace.apis'; import { createManagerReady } from './features/common/managerReady'; import { AutoFindProjects } from './features/creators/autoFindProjects'; import { ExistingProjects } from './features/creators/existingProjects'; @@ -66,6 +67,7 @@ import { EnvironmentManagers, ProjectCreators, PythonProjectManager } from './in import { registerSystemPythonFeatures } from './managers/builtin/main'; import { SysPythonManager } from './managers/builtin/sysPythonManager'; import { createNativePythonFinder, NativePythonFinder } from './managers/common/nativePythonFinder'; +import { IDisposable } from './managers/common/types'; import { registerCondaFeatures } from './managers/conda/main'; import { registerPoetryFeatures } from './managers/poetry/main'; import { registerPyenvFeatures } from './managers/pyenv/main'; @@ -148,19 +150,16 @@ async function collectEnvironmentInfo( return info.join('\n'); } -export async function activate(context: ExtensionContext): Promise { - const start = new StopWatch(); - - // Attempt to set setting of config.python.useEnvironmentsExtension to true - try { - const config = workspace.getConfiguration('python'); - await config.update('useEnvironmentsExtension', true, true); - } catch (err) { - traceError( - '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.', - err, +export async function activate(context: ExtensionContext): Promise { + const useEnvironmentsExtension = getConfiguration('python').get('useEnvironmentsExtension', true); + if (!useEnvironmentsExtension) { + traceWarn( + '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', ); + deactivate(context); + return; } + const start = new StopWatch(); // Logging should be set up before anything else. const outputChannel: LogOutputChannel = createLogOutputChannel('Python Environments'); @@ -508,4 +507,21 @@ export async function activate(context: ExtensionContext): Promise { + await Promise.all( + disposables.map(async (d) => { + try { + return Promise.resolve(d.dispose()); + } catch (_err) { + // do nothing + } + return Promise.resolve(); + }), + ); +} + +export async function deactivate(context: ExtensionContext) { + await disposeAll(context.subscriptions); + context.subscriptions.length = 0; // Clear subscriptions to prevent memory leaks + traceInfo('Python Environments extension deactivated.'); +} diff --git a/src/managers/common/types.ts b/src/managers/common/types.ts index b6e518dc..ac1eac68 100644 --- a/src/managers/common/types.ts +++ b/src/managers/common/types.ts @@ -43,3 +43,6 @@ export interface Installable { */ readonly uri?: Uri; } +export interface IDisposable { + dispose(): void | undefined | Promise; +} From c35d55ed526fe9caafaaf5cd523d99e1bd4958a9 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Thu, 26 Jun 2025 10:57:52 -0700 Subject: [PATCH 2/2] update more command palette --- package.json | 19 ++++++++++++++++++- src/extension.ts | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 278299cc..f7956c3d 100644 --- a/package.json +++ b/package.json @@ -110,7 +110,8 @@ "command": "python-envs.setEnvManager", "title": "%python-envs.setEnvManager.title%", "category": "Python", - "icon": "$(gear)" + "icon": "$(gear)", + "when": "config.python.useEnvironmentsExtension != false" }, { "command": "python-envs.setPkgManager", @@ -293,6 +294,22 @@ "command": "python-envs.addPythonProjectGivenResource", "when": "false" }, + { + "command": "python-envs.setEnvManager", + "when": "config.python.useEnvironmentsExtension != false" + }, + { + "command": "python-envs.packages", + "when": "config.python.useEnvironmentsExtension != false" + }, + { + "command": "python-envs.set", + "when": "config.python.useEnvironmentsExtension != false" + }, + { + "command": "python-envs.setPkgManager", + "when": "config.python.useEnvironmentsExtension != false" + }, { "command": "python-envs.removePythonProject", "when": "false" diff --git a/src/extension.ts b/src/extension.ts index f79ae157..195460d8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -156,7 +156,7 @@ export async function activate(context: ExtensionContext): Promise