Respect terminal cwd when resolving local venvs - #1708
Merged
Stella Huang (StellaHuang95) merged 1 commit intoAug 13, 2026
Merged
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 2f537bc5-b389-4ee2-aac2-e2b83be47f5c
Stella Huang (StellaHuang95)
requested review from
Eduardo Villalpando Mello (edvilme) and
Eleanor Boyd (eleanorjboyd)
August 12, 2026 19:54
| stopFallback: boolean; | ||
| } | ||
|
|
||
| async function getEnvironmentForCwd( |
Contributor
Author
There was a problem hiding this comment.
Previously, a new terminal could activate a sibling project’s venv because workspace-level selection was checked before terminal cwd. The resolver now uses cwd, rejects sibling venvs, and selects the nearest unambiguous local venv, or activates nothing if unsafe.
Eleanor Boyd (eleanorjboyd)
approved these changes
Aug 12, 2026
Eduardo Villalpando Mello (edvilme)
approved these changes
Aug 13, 2026
Stella Huang (StellaHuang95)
merged commit Aug 13, 2026
6138122
into
microsoft:main
123 of 126 checks passed
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.
Summary
Fixes #1631
Context
The issue uses a single VS Code workspace folder as a container for several independent Python projects:
With the default
workspaceSearchPaths([".venv", "*/.venv"]), all three environments are discovered. However, the workspace root is automatically registered as the only Python project unless the nested projects are explicitly added. A terminal opened in ProjectC could therefore receive ProjectA's activation command.Besides activating the wrong dependencies and Python version, this affects every integrated terminal opened by other extensions because auto-activation runs on the terminal-open event.
Root cause
The failure was caused by two scope-collapsing decisions:
VenvManagercan therefore associate one sorted or persisted sibling venv with that root.getEnvironmentForTerminal()previously preferred project-wide environment consensus before checking terminal cwd. With exactly one registered project, it returned the root's selected environment immediately, so the cwd heuristic never ran.Simply moving the existing cwd lookup earlier is insufficient:
api.getEnvironment(ProjectC)still maps ProjectC back to the root project and returns the same sibling environment.Fix
Cwd-aware terminal resolution
The terminal resolver now:
sysPrefixIf no candidate exists, multiple candidates are equally near, lookup fails, or lookup exceeds one second, the resolver returns no environment rather than activating the wrong one. The losing timeout is cancelled after a successful lookup.
A known cwd project with no selected environment also stops resolution, preventing another project's sole environment from leaking through the previous multi-project fallback.
Compatibility boundaries
This is intentionally terminal-only. It does not change central environment selection, persisted manager state, debugger/Pylance resolution, status-bar selection, task execution, or the public environment API.
workspace/.venvwith a terminal inworkspace/srcTerminal activation UI
Safe resolution can intentionally return
undefined. The activation menu contexts are now cleared in that case instead of retaining the previous terminal's Activate/Deactivate state. Async context updates also verify that the originating terminal is still active, preventing slower lookups or background activation events from overwriting the currently focused terminal's global menu state.Tests
Added direct coverage for:
Validation performed:
npm run compile-testsnpm run compileOut of scope
The shell-execution timeout also reported in #1631 is a separate activation-state issue. This PR addresses only selecting the correct environment for a terminal cwd.