11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import { Terminal , Uri } from 'vscode' ;
4+ import { Terminal , Uri , WorkspaceFolder } from 'vscode' ;
55import { getEnvExtApi , getEnvironment } from './api.internal' ;
66import { EnvironmentType , PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info' ;
77import { PythonEnvironment , PythonTerminalOptions } from './types' ;
@@ -10,7 +10,7 @@ import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
1010import { PythonEnvType } from '../pythonEnvironments/base/info' ;
1111import { traceError , traceInfo } from '../logging' ;
1212import { reportActiveInterpreterChanged } from '../environmentApi' ;
13- import { getWorkspaceFolder } from '../common/vscodeApis/workspaceApis' ;
13+ import { getWorkspaceFolder , getWorkspaceFolders } from '../common/vscodeApis/workspaceApis' ;
1414
1515function toEnvironmentType ( pythonEnv : PythonEnvironment ) : EnvironmentType {
1616 if ( pythonEnv . envId . managerId . toLowerCase ( ) . endsWith ( 'system' ) ) {
@@ -106,16 +106,25 @@ export async function getActiveInterpreterLegacy(resource?: Uri): Promise<Python
106106 const pythonEnv = await getEnvironment ( resource ) ;
107107 const oldEnv = previousEnvMap . get ( uri ?. fsPath || '' ) ;
108108 const newEnv = pythonEnv ? toLegacyType ( pythonEnv ) : undefined ;
109- if ( newEnv && oldEnv ?. envId . id !== pythonEnv ?. envId . id ) {
109+
110+ const folders = getWorkspaceFolders ( ) ?? [ ] ;
111+ const shouldReport =
112+ ( folders . length === 0 && resource === undefined ) || ( folders . length > 0 && resource !== undefined ) ;
113+ if ( shouldReport && newEnv && oldEnv ?. envId . id !== pythonEnv ?. envId . id ) {
110114 reportActiveInterpreterChanged ( {
111115 resource : getWorkspaceFolder ( resource ) ,
112116 path : newEnv . path ,
113117 } ) ;
118+ previousEnvMap . set ( uri ?. fsPath || '' , pythonEnv ) ;
114119 }
115120 return pythonEnv ? toLegacyType ( pythonEnv ) : undefined ;
116121}
117122
118- export async function ensureEnvironmentContainsPythonLegacy ( pythonPath : string ) : Promise < void > {
123+ export async function ensureEnvironmentContainsPythonLegacy (
124+ pythonPath : string ,
125+ workspaceFolder : WorkspaceFolder | undefined ,
126+ callback : ( ) => void ,
127+ ) : Promise < void > {
119128 const api = await getEnvExtApi ( ) ;
120129 const pythonEnv = await api . resolveEnvironment ( Uri . file ( pythonPath ) ) ;
121130 if ( ! pythonEnv ) {
@@ -132,6 +141,12 @@ export async function ensureEnvironmentContainsPythonLegacy(pythonPath: string):
132141 traceInfo ( `EnvExt: Python not found in ${ envType } environment ${ pythonPath } ` ) ;
133142 traceInfo ( `EnvExt: Installing Python in ${ envType } environment ${ pythonPath } ` ) ;
134143 await api . installPackages ( pythonEnv , [ 'python' ] ) ;
144+ previousEnvMap . set ( workspaceFolder ?. uri . fsPath || '' , pythonEnv ) ;
145+ reportActiveInterpreterChanged ( {
146+ path : pythonPath ,
147+ resource : workspaceFolder ,
148+ } ) ;
149+ callback ( ) ;
135150 }
136151}
137152
0 commit comments