From dcbc225b9312c5e3ed93b555308782051e0f8eaa Mon Sep 17 00:00:00 2001 From: Andreas <183095001+AndreasReichel@users.noreply.github.com> Date: Mon, 23 Nov 2026 16:05:56 +0100 Subject: [PATCH] Fire onDidChangeEnvironment when a refresh auto-selects a project venv When a project has no persisted venv selection, get() returns the global fallback until a refresh finds a venv in the project, e.g. one created after VS Code started. loadEnvMap() then mapped the project to that venv without firing onDidChangeEnvironment, so consumers that cache the interpreter (e.g. Pylint) kept the old one. Fixes #2046 Co-Authored-By: Claude Opus 5.5 --- .../builtin/builtInEnvironmentManager.ts | 11 ++++++ .../venvManager.autoSelect.unit.test.ts | 36 ++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/managers/builtin/builtInEnvironmentManager.ts b/src/managers/builtin/builtInEnvironmentManager.ts index 7a2a61fef..2ebf19769 100644 --- a/src/managers/builtin/builtInEnvironmentManager.ts +++ b/src/managers/builtin/builtInEnvironmentManager.ts @@ -822,6 +822,7 @@ export class BuiltInEnvironmentManager implements EnvironmentManager { : this.baseManager.getEnvironments('global')); await this.loadGlobalEnv(globals); + const previousEnvs = new Map(this.fsPathToEnv); this.fsPathToEnv.forEach((_env, key) => { if (isCurrent(key)) { this.fsPathToEnv.delete(key); @@ -890,6 +891,16 @@ export class BuiltInEnvironmentManager implements EnvironmentManager { ) ?? projectEnvs[0]; if (found) { this.fsPathToEnv.set(normalizedPath, found); + // A venv created in the project after the last refresh replaces the global fallback + // that get() returned so far, so consumers have to be told. + const previousEnv = previousEnvs.get(normalizedPath) ?? this.globalEnv; + if (previousEnv?.envId.id !== found.envId.id) { + events.push(() => { + if (isCurrent(normalizedPath)) { + this._onDidChangeEnvironment.fire({ uri: project.uri, old: previousEnv, new: found }); + } + }); + } } } } diff --git a/src/test/managers/builtin/venvManager.autoSelect.unit.test.ts b/src/test/managers/builtin/venvManager.autoSelect.unit.test.ts index 9c8b0b258..afe0c4fa9 100644 --- a/src/test/managers/builtin/venvManager.autoSelect.unit.test.ts +++ b/src/test/managers/builtin/venvManager.autoSelect.unit.test.ts @@ -5,7 +5,13 @@ import assert from 'assert'; import * as path from 'path'; import * as sinon from 'sinon'; import { LogOutputChannel, Uri } from 'vscode'; -import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi, PythonProject } from '../../../api'; +import { + DidChangeEnvironmentEventArgs, + EnvironmentManager, + PythonEnvironment, + PythonEnvironmentApi, + PythonProject, +} from '../../../api'; import { VENV_MANAGER_ID } from '../../../common/constants'; import { normalizePath } from '../../../common/utils/pathUtils'; import * as windowApis from '../../../common/window.apis'; @@ -102,4 +108,32 @@ suite('VenvManager auto-selection for a project', () => { assert.strictEqual((await manager.get(projectUri))?.envId.id, brokenRoot.envId.id); }); + + test('fires onDidChangeEnvironment when a refresh finds a venv created in the project', async () => { + const rootVenv = makeVenv(venvIn(), '3.12.14'); + const find = sinon.stub(venvUtils, 'findVirtualEnvironments').resolves([]); + const manager = await createManager(); + const events: DidChangeEnvironmentEventArgs[] = []; + manager.onDidChangeEnvironment((e) => events.push(e)); + + find.resolves([rootVenv]); + await manager.refresh(undefined); + + assert.strictEqual(events.length, 1); + assert.strictEqual(events[0].uri?.fsPath, projectUri.fsPath); + assert.strictEqual(events[0].new?.envId.id, rootVenv.envId.id); + assert.strictEqual((await manager.get(projectUri))?.envId.id, rootVenv.envId.id); + }); + + test('does not fire onDidChangeEnvironment when a refresh finds the same venv again', async () => { + const rootVenv = makeVenv(venvIn(), '3.12.14'); + sinon.stub(venvUtils, 'findVirtualEnvironments').resolves([rootVenv]); + const manager = await createManager(); + const events: DidChangeEnvironmentEventArgs[] = []; + manager.onDidChangeEnvironment((e) => events.push(e)); + + await manager.refresh(undefined); + + assert.strictEqual(events.length, 0); + }); });