Don't block on global scope during ext startup#1456
Don't block on global scope during ext startup#1456eleanorjboyd wants to merge 3 commits intomicrosoft:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR optimizes extension startup by avoiding blocking on global-scope environment resolution when at least one workspace folder environment has already been resolved, deferring the global resolution to a background task.
Changes:
- Defer global-scope environment selection to a background task when a workspace environment is available.
- Add telemetry for “blocking time” of initial environment selection, including whether global scope was deferred.
- Expand unit tests to cover error handling, deferral behavior, and additional settings-error warning paths.
Show a summary per file
| File | Description |
|---|---|
| src/features/interpreterSelection.ts | Defers global env resolution when a workspace env is resolved; adds completion telemetry and error-handling adjustments. |
| src/common/telemetry/constants.ts | Introduces a new telemetry event name + GDPR property mapping for env selection completion. |
| src/test/features/interpreterSelection.unit.test.ts | Adds tests for new deferral behavior and error/warning paths in environment selection. |
Copilot's findings
- Files reviewed: 3/3 changed files
- Comments generated: 4
| const resolveGlobalScope = async () => { | ||
| try { | ||
| const globalStopWatch = new StopWatch(); | ||
| const { result, errors: globalErrors } = await resolvePriorityChainCore( | ||
| undefined, | ||
| envManagers, | ||
| undefined, | ||
| nativeFinder, | ||
| api, | ||
| ); | ||
|
|
||
| const isPathA = result.environment !== undefined; | ||
| const isPathA = result.environment !== undefined; | ||
| const env = result.environment ?? (await result.manager.get(undefined)); | ||
|
|
||
| // Get the specific environment if not already resolved | ||
| const env = result.environment ?? (await result.manager.get(undefined)); | ||
| sendTelemetryEvent(EventNames.ENV_SELECTION_RESULT, globalStopWatch.elapsedTime, { | ||
| scope: 'global', | ||
| prioritySource: result.source, | ||
| managerId: result.manager.id, | ||
| resolutionPath: isPathA ? 'envPreResolved' : 'managerDiscovery', | ||
| hasPersistedSelection: env !== undefined, | ||
| }); | ||
|
|
||
| sendTelemetryEvent(EventNames.ENV_SELECTION_RESULT, globalStopWatch.elapsedTime, { | ||
| scope: 'global', | ||
| prioritySource: result.source, | ||
| managerId: result.manager.id, | ||
| resolutionPath: isPathA ? 'envPreResolved' : 'managerDiscovery', | ||
| hasPersistedSelection: env !== undefined, | ||
| }); | ||
| await envManagers.setEnvironments('global', env, false); | ||
|
|
||
| // Cache only — NO settings.json write (shouldPersistSettings = false) | ||
| await envManagers.setEnvironments('global', env, false); | ||
| traceInfo(`[interpreterSelection] global: ${env?.displayName ?? 'none'} (source: ${result.source})`); | ||
|
|
||
| traceInfo(`[interpreterSelection] global: ${env?.displayName ?? 'none'} (source: ${result.source})`); | ||
| } catch (err) { | ||
| traceError(`[interpreterSelection] Failed to set global environment: ${err}`); | ||
| if (globalErrors.length > 0) { | ||
| await notifyUserOfSettingErrors(globalErrors); | ||
| } | ||
| } catch (err) { | ||
| traceError(`[interpreterSelection] Failed to set global environment: ${err}`); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Global-scope setting errors are now always surfaced via notifyUserOfSettingErrors() inside resolveGlobalScope(), while workspace-scope errors are surfaced later via allErrors. This changes user-visible behavior: when global scope is awaited (workspaceFolderResolved=false), users can receive multiple warnings for the same setting (one from globalErrors and one from allErrors), whereas previously errors were aggregated and deduped in a single notifyUserOfSettingErrors(allErrors) call. Consider refactoring resolveGlobalScope to return globalErrors (and not notify internally) when it is awaited, and only notify internally when it is deferred, so awaited flows can aggregate + dedupe workspace/global errors in one place.
| // Wait a tick for the background global scope to complete | ||
| await new Promise((resolve) => setTimeout(resolve, 50)); |
There was a problem hiding this comment.
This test uses a real-time setTimeout to wait for the background task, which can make CI flaky (slow machines, timer throttling) and unnecessarily slows the suite. Prefer deterministic synchronization (e.g., sinon fake timers with clock.tickAsync(), or a deferred promise resolved by the stubbed mockEnvManagers.setEnvironments / showWarningMessage) so the test waits only as long as needed and never relies on wall-clock time.
| // Wait a tick for the background global scope to complete | ||
| await new Promise((resolve) => setTimeout(resolve, 50)); |
There was a problem hiding this comment.
Same flakiness risk as the other background-resolution test: relying on a fixed 50ms timeout can fail intermittently or slow CI. Use fake timers or a promise you control (e.g., have setEnvironments return a deferred promise and await its resolution) to make completion deterministic.
| /* __GDPR__ | ||
| "env_selection.completed": { | ||
| "globalScopeDeferred": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "owner": "eleanorjboyd" }, | ||
| "workspaceFolderCount": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "owner": "eleanorjboyd" }, | ||
| "resolvedFolderCount": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "owner": "eleanorjboyd" }, | ||
| "settingErrorCount": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true, "owner": "eleanorjboyd" } | ||
| } | ||
| */ | ||
| [EventNames.ENV_SELECTION_COMPLETED]: { | ||
| globalScopeDeferred: boolean; | ||
| workspaceFolderCount: number; | ||
| resolvedFolderCount: number; | ||
| settingErrorCount: number; | ||
| }; |
There was a problem hiding this comment.
Please verify the GDPR annotation key string ("env_selection.completed") matches the repository’s established telemetry/GDPR naming convention for the corresponding EventNames value ('ENV_SELECTION.COMPLETED'). If the extractor expects an exact match (or a specific normalization), a mismatch can lead to missing/invalid GDPR metadata for this event.
When at least one workspace folder resolved successfully, don't await the global scope resolution. Run it as a background task instead of blocking startup on it.
For more than 3/4ths of slow sessions where the workspace env resolves fast (via
envPreResolved), this should make the status bar update near-instantly.Files opened outside the workspace folder wouldn't have a Python env immediately available. They would resolve moments later when the background task completes. This is an acceptable trade-off