1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT License.
3
3
4
- import { Terminal , Uri } from 'vscode' ;
4
+ import { Terminal , Uri , WorkspaceFolder } from 'vscode' ;
5
5
import { getEnvExtApi , getEnvironment } from './api.internal' ;
6
6
import { EnvironmentType , PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info' ;
7
7
import { PythonEnvironment , PythonTerminalOptions } from './types' ;
@@ -10,7 +10,7 @@ import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
10
10
import { PythonEnvType } from '../pythonEnvironments/base/info' ;
11
11
import { traceError , traceInfo } from '../logging' ;
12
12
import { reportActiveInterpreterChanged } from '../environmentApi' ;
13
- import { getWorkspaceFolder } from '../common/vscodeApis/workspaceApis' ;
13
+ import { getWorkspaceFolder , getWorkspaceFolders } from '../common/vscodeApis/workspaceApis' ;
14
14
15
15
function toEnvironmentType ( pythonEnv : PythonEnvironment ) : EnvironmentType {
16
16
if ( pythonEnv . envId . managerId . toLowerCase ( ) . endsWith ( 'system' ) ) {
@@ -106,16 +106,25 @@ export async function getActiveInterpreterLegacy(resource?: Uri): Promise<Python
106
106
const pythonEnv = await getEnvironment ( resource ) ;
107
107
const oldEnv = previousEnvMap . get ( uri ?. fsPath || '' ) ;
108
108
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 ) {
110
114
reportActiveInterpreterChanged ( {
111
115
resource : getWorkspaceFolder ( resource ) ,
112
116
path : newEnv . path ,
113
117
} ) ;
118
+ previousEnvMap . set ( uri ?. fsPath || '' , pythonEnv ) ;
114
119
}
115
120
return pythonEnv ? toLegacyType ( pythonEnv ) : undefined ;
116
121
}
117
122
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 > {
119
128
const api = await getEnvExtApi ( ) ;
120
129
const pythonEnv = await api . resolveEnvironment ( Uri . file ( pythonPath ) ) ;
121
130
if ( ! pythonEnv ) {
@@ -132,6 +141,12 @@ export async function ensureEnvironmentContainsPythonLegacy(pythonPath: string):
132
141
traceInfo ( `EnvExt: Python not found in ${ envType } environment ${ pythonPath } ` ) ;
133
142
traceInfo ( `EnvExt: Installing Python in ${ envType } environment ${ pythonPath } ` ) ;
134
143
await api . installPackages ( pythonEnv , [ 'python' ] ) ;
144
+ previousEnvMap . set ( workspaceFolder ?. uri . fsPath || '' , pythonEnv ) ;
145
+ reportActiveInterpreterChanged ( {
146
+ path : pythonPath ,
147
+ resource : workspaceFolder ,
148
+ } ) ;
149
+ callback ( ) ;
135
150
}
136
151
}
137
152
0 commit comments