Repository navigation
Fire onDidChangeEnvironment when a refresh auto-selects a project venv - #2048
Open
AndreasReichel wants to merge 1 commit into
Open
AndreasReichel wants to merge 1 commit into
AndreasReichel wants to merge 1 commit into
Conversation
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>
Author
|
@microsoft-github-policy-service agree company="Vector Informatik GmbH" |
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. |
Contributor
There was a problem hiding this comment.
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); |
Contributor
There was a problem hiding this comment.
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; |
Contributor
There was a problem hiding this comment.
Suggested change
| const previousEnv = previousEnvs.get(normalizedPath) ?? this.globalEnv; | |
| const previousEnv = previousProjectEnvs.get(normalizedPath) ?? this.globalEnv; |
Eduardo Villalpando Mello (edvilme)
requested review from
Eduardo Villalpando Mello (edvilme)
and
a balanced review from Copilot
November 24, 2026 18:10
Contributor
There was a problem hiding this comment.
🟡 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #2046
Cause
BuiltInEnvironmentManager.loadEnvMap()runs after every refresh of the venv manager and maps each project to an environment:onDidChangeEnvironmentwhen the environment differs.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. BecauserefreshEnvironment()only runs on a manager event, the publiconDidChangeEnvironmentnever 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
onDidChangeEnvironmentwhen 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 ofloadEnvMap(), 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:Also checked by hand with a local build of 1.44.0 plus this change. I opened a folder with only
main.py, ranpython -m venv .venvin it, and compared the logs:onDidChangeEnvironmentafter.venvis discoveredPython 3.14 -> .venv.venvimmediatelySame result with
python.useEnvironmentsExtensionunset, with it set totrue, and withpython-envs.defaultEnvManagerset toms-python.python:venv.🤖 Generated with Claude Code