Skip to content

Re-apply environment selection when a configured manager registers late - #1998

Open
Siddharth S Shah (SIDLAD) wants to merge 2 commits into
microsoft:mainfrom
SIDLAD:fix/reselect-env-on-late-manager-registration
Open

Siddharth S Shah (SIDLAD) wants to merge 2 commits into
microsoft:mainfrom
SIDLAD:fix/reselect-env-on-late-manager-registration

Conversation

@SIDLAD

Copy link
Copy Markdown

Written with Claude Code (AI-assisted) and reviewed by me.

Fixes #1997.

Problem

Startup selection (applyInitialEnvironmentSelection) doesn't wait for environment managers contributed by other extensions. When python-envs.defaultEnvManager or a pythonProjects[] entry names such a manager (Pixi, Hatch, …) and it registers after the priority chain ran, selection falls back to another environment and is never corrected. #1533 suppressed the warning for this case, but the selection stays on the fallback.

Change

  • After each workspace folder is selected, applyInitialEnvironmentSelection records whether it fell back because a manager named by pythonProjects[] / defaultEnvManager wasn't registered (fallbackSelections, built from the existing managerNotRegistered errors).
  • New registerLateManagerRegistrationListener subscribes to envManagers.onDidChangeEnvironmentManager. When a manager registers, it re-selects only the folders that fell back for that manager, using the same priority chain:
    • other folders and the global scope are left alone,
    • corrections only apply within MANAGER_READY_TIMEOUT_MS (30s, the window managerReady already uses) of the fallback, so a manager that shows up much later (e.g. an extension installed mid-session) doesn't switch the interpreter under a running session,
    • registrations of managers no folder is waiting for are ignored.
  • Selection runs (startup, settings changes, late re-selection) are serialized through a promise queue. Without it, a re-selection triggered mid-startup can be overwritten by the still-running startup selection. The logs in the issue show exactly that overwrite.
  • In extension.ts the listener is registered before startup selection, so a manager that registers while selection is still running is picked up too (the queued re-selection runs after startup has recorded which folders fell back).

This doesn't add any waiting or deadline to startup, and a genuinely misconfigured manager id still warns immediately, which addresses the concerns in the #1533 description.

Tests

New suite Interpreter Selection - late manager registration:

  • re-applies selection when the configured manager registers after startup selection,
  • picks up a manager that registers while startup selection is still running (the queued re-run wins),
  • ignores registrations of managers no setting is waiting for,
  • does not switch the environment when the manager registers after the timeout,
  • only re-selects the workspace folders that fell back for the registered manager (and not the global scope).

I checked that each test catches the behaviour it covers: with the listener disabled, tests 1, 2 and 5 fail; with the time check removed, test 4 fails; with the queue removed, test 2 fails.

  • npm run unittest: 3527 passing, 9 pending (Node 22.21.1, per .nvmrc).
  • npm run lint clean, npm run compile succeeds.

Manual verification

Windows 11, Pixi Code 0.2.0, defaultEnvManager set to renan-r-santos.pixi-code:pixi, running the first iteration of this branch via F5. That iteration re-ran full selection; the second commit narrows it to the fallen-back folders and adds the time window, and is covered by the unit tests above.

12:32:46.894 [warning] [priorityChain] d:\Projects\LearningMLDL defaultEnvManager 'renan-r-santos.pixi-code:pixi' not found, trying next priority
12:32:46.894 [interpreterSelection] Configured manager 'renan-r-santos.pixi-code:pixi' registered after it was skipped, re-applying environment selection
12:32:57.167 [interpreterSelection] LearningMLDL: base (3.13.5) (source: defaultInterpreterPath)
12:32:57.172 [interpreterSelection] Applying initial environment selection for 1 workspace folder(s)
12:32:57.203 [interpreterSelection] LearningMLDL: LearningMLDL:default (3.15.2) (source: defaultEnvManager)

The workspace ends on the Pixi environment after a reload, with no user action.

Open question for reviewers

Within the 30s window, if the user manually picks an interpreter for a fallen-back folder before the configured manager registers, the re-selection replaces that pick with the configured manager's environment. That matches how the settings-change listener already behaves (settings win), but I'm happy to skip folders with a user-made selection if you'd prefer. That would need a small accessor on EnvironmentManagers for the existing userSelected flag, which I didn't want to add without asking.

Notes

  • The global scope isn't re-selected; files outside workspace folders keep the fallback for the session.
  • Prettier reports existing style differences in the touched files on main. I left those untouched to keep the diff focused. The new lines are Prettier-formatted.

🤖 Generated with Claude Code

Startup selection does not wait for environment managers contributed by
other extensions. When python-envs.defaultEnvManager or a pythonProjects[]
entry names such a manager (e.g. Pixi, Hatch) and it registers after the
priority chain ran, selection fell back to another environment and was
never corrected. microsoft#1533 only suppressed the resulting warning.

Remember configured manager ids that were missing when the priority chain
ran, and re-apply selection once one of them registers. Selection runs are
serialized so the re-run cannot be overwritten by the in-flight startup run.
The listener is registered before startup selection so registrations that
happen while it is running are picked up too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e window

- Only re-select the workspace folders that fell back because the
  registered manager was missing; other folders and the global scope are
  left alone.
- Only correct a fallback within MANAGER_READY_TIMEOUT_MS, so a manager
  that registers much later (e.g. an extension installed mid-session)
  does not switch the interpreter under a running session.
- Tests for both, plus the queue ordering.

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

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Startup selection stays on a fallback environment when the configured defaultEnvManager registers late

1 participant