Skip to content

Fire onDidChangeEnvironment when a refresh auto-selects a project venv - #2048

Open
AndreasReichel wants to merge 1 commit into
microsoft:mainfrom
AndreasReichel:fix-2046
Open

AndreasReichel wants to merge 1 commit into
microsoft:mainfrom
AndreasReichel:fix-2046

Conversation

@AndreasReichel

Copy link
Copy Markdown

Fixes #2046

Cause

BuiltInEnvironmentManager.loadEnvMap() runs after every refresh of the venv manager and maps each project to an environment:

  • If the project has a persisted venv, it maps the project to it and fires onDidChangeEnvironment when the environment differs.
  • If not, it picks the project's own venv from the discovered environments and only calls fsPathToEnv.set().

Until then, get() returns the global fallback for such a project. When a venv is created in the folder while VS Code runs, the next refresh maps the project to it: getEnvironment() switches to the venv, but no manager event fires. Because refreshEnvironment() only runs on a manager event, the public onDidChangeEnvironment never fires either. Consumers that cache the interpreter keep the old one; Pylint, for example, keeps linting with the global Python until it is restarted.

Fix

In the second case, fire onDidChangeEnvironment when the picked environment differs from the project's previous entry, or from the global fallback if there was none. The map is cleared at the start of loadEnvMap(), so its previous entries are copied first. refreshEnvironment() still drops unchanged environments, so refreshes that find the same venv stay quiet.

Tests

Two unit tests in venvManager.autoSelect.unit.test.ts:

  • A refresh that finds a newly created venv in the project fires the event. This test fails without the fix.
  • A refresh that finds the same venv again fires nothing.

Also checked by hand with a local build of 1.44.0 plus this change. I opened a folder with only main.py, ran python -m venv .venv in it, and compared the logs:

Without the fix With the fix
onDidChangeEnvironment after .venv is discovered none fires, Python 3.14 -> .venv
Pylint 2026.8.0 stays on the global Python until "Pylint: Restart Server" restarts on .venv immediately

Same result with python.useEnvironmentsExtension unset, with it set to true, and with python-envs.defaultEnvManager set to ms-python.python:venv.

🤖 Generated with Claude Code

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 microsoft#2046

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AndreasReichel

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Vector Informatik GmbH"

@edvilme Eduardo Villalpando Mello (edvilme) added the bug Issue identified by VS Code Team member as probable bug label Nov 24, 2026
Comment on lines +894 to +895
// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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.
// A venv created in the project after the last refresh replaces the global fallback
// that get() returned so far, so consumers have to be notified.

: this.baseManager.getEnvironments('global'));
await this.loadGlobalEnv(globals);

const previousEnvs = new Map(this.fsPathToEnv);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const previousEnvs = new Map(this.fsPathToEnv);
const previousProjectEnvs = new Map(this.fsPathToEnv);

Maybe this shows why it is a map more clearly

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const previousEnv = previousEnvs.get(normalizedPath) ?? this.globalEnv;
const previousEnv = previousProjectEnvs.get(normalizedPath) ?? this.globalEnv;

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Refresh handling can report the wrong previous environment and emit spurious change events for an unchanged interpreter, potentially restarting consumers unnecessarily.

2 open issues
What changed in this PR

Addresses #2046 by notifying consumers when a refresh automatically selects a newly discovered project virtual environment, allowing tools such as Pylint to update their interpreter.

Changes:

  • Preserves previous project selections before rebuilding the environment map.
  • Emits a manager selection event when an auto-selected environment differs from the previous selection or fallback.
  • Adds unit tests for newly discovered venvs and repeated discovery of the same venv.
File Description
src/​test/​managers/​builtin/​venvManager.autoSelect.unit.test.ts Adds event-emission and unchanged-selection regression tests.
src/​managers/​builtin/​builtInEnvironmentManager.ts Captures previous project mappings and queues auto-selection change events.

Review effort: Balanced

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;
// 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) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getEnvironment() switches to a newly created venv without firing onDidChangeEnvironment

3 participants