From 95787858335e744dd152a5901ab70978350821f6 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Wed, 5 Feb 2025 17:33:40 -0800 Subject: [PATCH] Fix event duplication when using Python Environments (#24786) Partial fix for https://github.com/microsoft/vscode-python/issues/24783 --- src/client/envExt/api.legacy.ts | 23 ++++++++++++++++---- src/client/interpreter/interpreterService.ts | 7 ++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/client/envExt/api.legacy.ts b/src/client/envExt/api.legacy.ts index 1d9d94ccc98f..7546a429c76a 100644 --- a/src/client/envExt/api.legacy.ts +++ b/src/client/envExt/api.legacy.ts @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import { Terminal, Uri } from 'vscode'; +import { Terminal, Uri, WorkspaceFolder } from 'vscode'; import { getEnvExtApi, getEnvironment } from './api.internal'; import { EnvironmentType, PythonEnvironment as PythonEnvironmentLegacy } from '../pythonEnvironments/info'; import { PythonEnvironment, PythonTerminalOptions } from './types'; @@ -10,7 +10,7 @@ import { parseVersion } from '../pythonEnvironments/base/info/pythonVersion'; import { PythonEnvType } from '../pythonEnvironments/base/info'; import { traceError, traceInfo } from '../logging'; import { reportActiveInterpreterChanged } from '../environmentApi'; -import { getWorkspaceFolder } from '../common/vscodeApis/workspaceApis'; +import { getWorkspaceFolder, getWorkspaceFolders } from '../common/vscodeApis/workspaceApis'; function toEnvironmentType(pythonEnv: PythonEnvironment): EnvironmentType { if (pythonEnv.envId.managerId.toLowerCase().endsWith('system')) { @@ -106,16 +106,25 @@ export async function getActiveInterpreterLegacy(resource?: Uri): Promise 0 && resource !== undefined); + if (shouldReport && newEnv && oldEnv?.envId.id !== pythonEnv?.envId.id) { reportActiveInterpreterChanged({ resource: getWorkspaceFolder(resource), path: newEnv.path, }); + previousEnvMap.set(uri?.fsPath || '', pythonEnv); } return pythonEnv ? toLegacyType(pythonEnv) : undefined; } -export async function ensureEnvironmentContainsPythonLegacy(pythonPath: string): Promise { +export async function ensureEnvironmentContainsPythonLegacy( + pythonPath: string, + workspaceFolder: WorkspaceFolder | undefined, + callback: () => void, +): Promise { const api = await getEnvExtApi(); const pythonEnv = await api.resolveEnvironment(Uri.file(pythonPath)); if (!pythonEnv) { @@ -132,6 +141,12 @@ export async function ensureEnvironmentContainsPythonLegacy(pythonPath: string): traceInfo(`EnvExt: Python not found in ${envType} environment ${pythonPath}`); traceInfo(`EnvExt: Installing Python in ${envType} environment ${pythonPath}`); await api.installPackages(pythonEnv, ['python']); + previousEnvMap.set(workspaceFolder?.uri.fsPath || '', pythonEnv); + reportActiveInterpreterChanged({ + path: pythonPath, + resource: workspaceFolder, + }); + callback(); } } diff --git a/src/client/interpreter/interpreterService.ts b/src/client/interpreter/interpreterService.ts index 628a25d6b3b1..3a1aaed312ff 100644 --- a/src/client/interpreter/interpreterService.ts +++ b/src/client/interpreter/interpreterService.ts @@ -290,11 +290,8 @@ export class InterpreterService implements Disposable, IInterpreterService { @cache(-1, true) private async ensureEnvironmentContainsPython(pythonPath: string, workspaceFolder: WorkspaceFolder | undefined) { if (useEnvExtension()) { - await ensureEnvironmentContainsPythonLegacy(pythonPath); - this.didChangeInterpreterEmitter.fire(workspaceFolder?.uri); - reportActiveInterpreterChanged({ - path: pythonPath, - resource: workspaceFolder, + await ensureEnvironmentContainsPythonLegacy(pythonPath, workspaceFolder, () => { + this.didChangeInterpreterEmitter.fire(workspaceFolder?.uri); }); return; }