-
Notifications
You must be signed in to change notification settings - Fork 41
feat: implement fast path resolution for Python environment managers #1408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eleanorjboyd
wants to merge
8
commits into
microsoft:main
Choose a base branch
from
eleanorjboyd:vague-owl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19b990d
feat: implement fast path resolution for Python environment managers
eleanorjboyd 695bea3
add separate function
eleanorjboyd 41359ea
fix startup flow markdown
eleanorjboyd 2ed5c03
fix: address PR 1408 fast-path review comments - race condition, fail…
eleanorjboyd 4b88fb9
docs: clarify fast-path async behavior - deferred registration, race …
eleanorjboyd df7efbe
Merge branch 'main' into vague-owl
eleanorjboyd f78b242
formatting
eleanorjboyd 1bd544d
improvements from comments
eleanorjboyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { Uri } from 'vscode'; | ||
| import { GetEnvironmentScope, PythonEnvironment, PythonEnvironmentApi } from '../../api'; | ||
| import { traceError, traceWarn } from '../../common/logging'; | ||
| import { createDeferred, Deferred } from '../../common/utils/deferred'; | ||
|
|
||
| /** | ||
| * Options for the fast-path resolution in manager.get(). | ||
| */ | ||
| export interface FastPathOptions { | ||
| /** The current _initialized deferred (may be undefined if init hasn't started). */ | ||
| initialized: Deferred<void> | undefined; | ||
| /** Updates the manager's _initialized deferred. */ | ||
| setInitialized: (initialized: Deferred<void> | undefined) => void; | ||
| /** The scope passed to get(). */ | ||
| scope: GetEnvironmentScope; | ||
| /** Label for log messages, e.g. 'venv', 'conda'. */ | ||
| label: string; | ||
| /** Gets the project fsPath for a given Uri scope. */ | ||
| getProjectFsPath: (scope: Uri) => string; | ||
| /** Reads the persisted env path for a workspace fsPath. */ | ||
| getPersistedPath: (workspaceFsPath: string) => Promise<string | undefined>; | ||
| /** Resolves a persisted path to a full PythonEnvironment. */ | ||
| resolve: (persistedPath: string) => Promise<PythonEnvironment | undefined>; | ||
| /** Starts background initialization (full discovery). Returns a promise that completes when init is done. */ | ||
| startBackgroundInit: () => Promise<void> | Thenable<void>; | ||
| } | ||
|
|
||
| /** | ||
| * Result from a successful fast-path resolution. | ||
| */ | ||
| export interface FastPathResult { | ||
| /** The resolved environment. */ | ||
| env: PythonEnvironment; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the fsPath for a scope by preferring the resolved project path when available. | ||
| */ | ||
| export function getProjectFsPathForScope(api: Pick<PythonEnvironmentApi, 'getPythonProject'>, scope: Uri): string { | ||
| return api.getPythonProject(scope)?.uri.fsPath ?? scope.fsPath; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts fast-path resolution for manager.get(): if full initialization hasn't completed yet | ||
| * and there's a persisted environment for the workspace, resolve it directly via nativeFinder | ||
| * instead of waiting for full discovery. | ||
| * | ||
| * Returns the resolved environment (with an optional new deferred) if successful, or undefined | ||
| * to fall through to the normal init path. | ||
| */ | ||
| export async function tryFastPathGet(opts: FastPathOptions): Promise<FastPathResult | undefined> { | ||
| if (!(opts.scope instanceof Uri)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (opts.initialized?.completed) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let deferred = opts.initialized; | ||
| if (!deferred) { | ||
| // Register deferred before any await to avoid concurrent callers starting duplicate inits. | ||
| deferred = createDeferred<void>(); | ||
| opts.setInitialized(deferred); | ||
| const deferredRef = deferred; | ||
| try { | ||
| Promise.resolve(opts.startBackgroundInit()).then( | ||
| () => deferredRef.resolve(), | ||
| (err) => { | ||
| traceError(`[${opts.label}] Background initialization failed:`, err); | ||
| // Allow subsequent get()/initialize() calls to retry after a background init failure. | ||
| opts.setInitialized(undefined); | ||
| deferredRef.resolve(); | ||
| }, | ||
| ); | ||
| } catch (syncErr) { | ||
| traceError(`[${opts.label}] Background initialization threw synchronously:`, syncErr); | ||
| opts.setInitialized(undefined); | ||
| deferredRef.resolve(); | ||
| } | ||
| } | ||
|
|
||
| const fsPath = opts.getProjectFsPath(opts.scope); | ||
| const persistedPath = await opts.getPersistedPath(fsPath); | ||
|
|
||
| if (persistedPath) { | ||
| try { | ||
| const resolved = await opts.resolve(persistedPath); | ||
| if (resolved) { | ||
| return { env: resolved }; | ||
| } | ||
| } catch (err) { | ||
| traceWarn(`[${opts.label}] Fast path resolve failed for '${persistedPath}', falling back to full init:`, err); | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.