11import { commands , ExtensionContext , extensions , LogOutputChannel , Terminal , Uri , window , workspace } from 'vscode' ;
22import { PythonEnvironment , PythonEnvironmentApi , PythonProjectCreator } from './api' ;
33import { ensureCorrectVersion } from './common/extVersion' ;
4- import { registerLogger , traceError , traceInfo } from './common/logging' ;
4+ import { registerLogger , traceError , traceInfo , traceWarn } from './common/logging' ;
55import { clearPersistentState , setPersistentState } from './common/persistentState' ;
66import { newProjectSelection } from './common/pickers/managers' ;
77import { 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' ;
1819import { createManagerReady } from './features/common/managerReady' ;
1920import { AutoFindProjects } from './features/creators/autoFindProjects' ;
2021import { ExistingProjects } from './features/creators/existingProjects' ;
@@ -66,6 +67,7 @@ import { EnvironmentManagers, ProjectCreators, PythonProjectManager } from './in
6667import { registerSystemPythonFeatures } from './managers/builtin/main' ;
6768import { SysPythonManager } from './managers/builtin/sysPythonManager' ;
6869import { createNativePythonFinder , NativePythonFinder } from './managers/common/nativePythonFinder' ;
70+ import { IDisposable } from './managers/common/types' ;
6971import { registerCondaFeatures } from './managers/conda/main' ;
7072import { registerPoetryFeatures } from './managers/poetry/main' ;
7173import { 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+ }
0 commit comments