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); + }); });