Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/managers/builtin/builtInEnvironmentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
}
});
}
}
}
}
Expand Down
36 changes: 35 additions & 1 deletion src/test/managers/builtin/venvManager.autoSelect.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
Loading