Skip to content

Don't block on global scope during ext startup#1456

Draft
eleanorjboyd wants to merge 3 commits intomicrosoft:mainfrom
eleanorjboyd:able-badger
Draft

Don't block on global scope during ext startup#1456
eleanorjboyd wants to merge 3 commits intomicrosoft:mainfrom
eleanorjboyd:able-badger

Conversation

@eleanorjboyd
Copy link
Copy Markdown
Member

@eleanorjboyd eleanorjboyd commented Apr 14, 2026

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

@eleanorjboyd eleanorjboyd changed the title Able badger Don't block on global scope during ext startup Apr 14, 2026
@eleanorjboyd eleanorjboyd requested a review from Copilot April 14, 2026 21:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment on lines +357 to +389
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}`);
}
};
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +881 to +882
// Wait a tick for the background global scope to complete
await new Promise((resolve) => setTimeout(resolve, 50));
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +910 to +911
// Wait a tick for the background global scope to complete
await new Promise((resolve) => setTimeout(resolve, 50));
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +399 to +412
/* __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;
};
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
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.

2 participants