Skip to content

Commit 9578785

Browse files
authored
Fix event duplication when using Python Environments (#24786)
Partial fix for #24783
1 parent bf38fc5 commit 9578785

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/client/envExt/api.legacy.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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';
55
import { getEnvExtApi, getEnvironment } from './api.internal';
66
import { EnvironmentType, PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info';
77
import { PythonEnvironment, PythonTerminalOptions } from './types';
@@ -10,7 +10,7 @@ import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion';
1010
import { PythonEnvType } from '../pythonEnvironments/base/info';
1111
import { traceError, traceInfo } from '../logging';
1212
import { reportActiveInterpreterChanged } from '../environmentApi';
13-
import { getWorkspaceFolder } from '../common/vscodeApis/workspaceApis';
13+
import { getWorkspaceFolder, getWorkspaceFolders } from '../common/vscodeApis/workspaceApis';
1414

1515
function 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

src/client/interpreter/interpreterService.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,8 @@ export class InterpreterService implements Disposable, IInterpreterService {
290290
@cache(-1, true)
291291
private async ensureEnvironmentContainsPython(pythonPath: string, workspaceFolder: WorkspaceFolder | undefined) {
292292
if (useEnvExtension()) {
293-
await ensureEnvironmentContainsPythonLegacy(pythonPath);
294-
this.didChangeInterpreterEmitter.fire(workspaceFolder?.uri);
295-
reportActiveInterpreterChanged({
296-
path: pythonPath,
297-
resource: workspaceFolder,
293+
await ensureEnvironmentContainsPythonLegacy(pythonPath, workspaceFolder, () => {
294+
this.didChangeInterpreterEmitter.fire(workspaceFolder?.uri);
298295
});
299296
return;
300297
}

0 commit comments

Comments
 (0)