You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use exact per-file environments for Python files (PEP 723 PR 19) (#26129)
> Part of
[microsoft/vscode-python-environments#1602](microsoft/vscode-python-environments#1602).
Design doc:
[microsoft/vscode-python-environments#1601](microsoft/vscode-python-environments#1601).
### Roadmap context
This is **cross-repository PR 19** in the PEP 723 roadmap. It closes the
Python extension's per-file lookup gaps for debugging and Pylance
configuration.
| Cross-repository integration | PR | Status |
|---|---|---|
| | Environments PR 7: persisted script associations | merged
([#1697](microsoft/vscode-python-environments#1697))
|
| | Environments PR 10: exact script projects |
[microsoft/vscode-python-environments#1744](microsoft/vscode-python-environments#1744)
|
| | PR 17: Pylance per-file Python path lookup |
[microsoft/pyrx#9265](microsoft/pyrx#9265) |
| | **PR 19: exact Python-file lookup and debugger resolution** | **this
PR** |
### Why this PR
`IInterpreterService.getActiveInterpreter(resource)` normally shares
in-flight, timeout, and last-known state by workspace folder. A file URI
can therefore receive the workspace interpreter when a workspace lookup
is already running or when exact environment resolution exceeds the
timeout.
That breaks two per-file consumers:
- debugger launch resolution can select the workspace interpreter
instead of the launch program's interpreter;
- Pylance's file-scoped `workspace/configuration` request can receive a
cached workspace interpreter.
The exact lookup must also avoid publishing a file interpreter as a
workspace-wide interpreter change.
### What this PR does
- Adds an internal `exactResource` option to `IInterpreterService`.
- Bypasses workspace-keyed in-flight, timeout, and last-known state for
exact environment-extension lookups.
- Suppresses workspace-level interpreter-change reporting for those
silent exact reads.
- Resolves debugger programs from:
- absolute paths;
- `${file}`;
- `${workspaceFolder}`;
- `${workspaceFolder:name}`.
- Prefers the program interpreter and falls back to the normal workspace
interpreter only when no exact environment is available.
- Reuses the selected interpreter for both legacy `pythonPath` and
command-valued `python`.
- Applies activation variables when the program interpreter differs from
the workspace interpreter.
- Uses exact lookup only for `.py`-scoped Pylance configuration
requests; workspace-level requests retain the existing cached fast path.
### Lookup semantics
| Condition | Behavior |
|---|---|
| No concrete launch program | Preserve workspace lookup |
| Exact program environment exists | Use it for debugger resolution |
| Exact program lookup returns no environment | Fall back to workspace
interpreter |
| Program and workspace interpreters match | Preserve existing terminal
activation behavior |
| Program interpreter differs | Apply its activation variables |
| Pylance requests `python` config for a `.py` URI | Resolve the exact
file environment |
| Pylance requests workspace-level config | Preserve normal workspace
caching |
| Exact lookup resolves an environment | Do not publish a false
workspace interpreter-change event |
### Performance and safety
- The existing fast workspace cache remains unchanged for normal
consumers.
- Exact lookup is opt-in and used only by debugger program selection and
`.py` configuration scopes.
- The environments extension's own URI-scoped timeout/last-known
behavior remains in effect.
- No public Python or environments API is changed.
### User impact
Users without a per-file environment retain the same interpreter and
debugger behavior. When a Python file has a distinct environment,
Pylance configuration and debugger launch consistently use that file's
interpreter rather than a workspace-cached value.
### Tests
- Prettier check for all changed files
- ESLint for all changed files
- Focused middleware, resolver, launch, environment-adapter, and
interpreter-service tests: **181 passing**, 3 pending
- Repository-wide TypeScript compilation currently also reports two
existing `TelemetryReporter` import errors in untouched files.
- The full unit command was run; its failures were confined to untouched
platform/path, terminal activation, activated-environment, and
native-finder tests.
### Scope and follow-up
This PR does not implement Pylance's open-file rerouting notification.
Live movement and reanalysis after a per-file environment change remain
in PR 18.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
0 commit comments