From 81a53a629d0146b0287f6247956e9c24cdb98b71 Mon Sep 17 00:00:00 2001 From: Wasim Date: Sat, 28 Nov 2026 17:09:58 +0530 Subject: [PATCH] Wait for active interpreter resolution before reporting invalid interpreter --- .../diagnostics/checks/pythonInterpreter.ts | 4 +-- src/client/interpreter/contracts.ts | 4 +++ src/client/interpreter/interpreterService.ts | 3 ++ .../checks/pythonInterpreter.unit.test.ts | 31 ++++++++++++++----- .../interpreterService.unit.test.ts | 11 +++++++ 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/client/application/diagnostics/checks/pythonInterpreter.ts b/src/client/application/diagnostics/checks/pythonInterpreter.ts index 9167e232a417..b82276712a28 100644 --- a/src/client/application/diagnostics/checks/pythonInterpreter.ts +++ b/src/client/application/diagnostics/checks/pythonInterpreter.ts @@ -158,7 +158,7 @@ export class InvalidPythonInterpreterService extends BaseDiagnosticsService ]; } - const currentInterpreter = await interpreterService.getActiveInterpreter(resource); + const currentInterpreter = await interpreterService.getActiveInterpreter(resource, { waitForResolution: true }); if (!currentInterpreter) { if (useEnvExtension()) { traceWarn(Interpreters.envExtNoActiveEnvironment); @@ -188,7 +188,7 @@ export class InvalidPythonInterpreterService extends BaseDiagnosticsService return []; } const interpreterService = this.serviceContainer.get(IInterpreterService); - const currentInterpreter = await interpreterService.getActiveInterpreter(resource); + const currentInterpreter = await interpreterService.getActiveInterpreter(resource, { waitForResolution: true }); if (currentInterpreter) { return []; } diff --git a/src/client/interpreter/contracts.ts b/src/client/interpreter/contracts.ts index d47f789c7b44..e539d123662d 100644 --- a/src/client/interpreter/contracts.ts +++ b/src/client/interpreter/contracts.ts @@ -77,6 +77,10 @@ export interface GetActiveInterpreterOptions { * Resolve the exact resource without using workspace-scoped in-flight or last-known state. */ exactResource?: boolean; + /** + * Wait for active interpreter resolution to complete instead of returning early on the timeout budget. + */ + waitForResolution?: boolean; } export interface IInterpreterService { diff --git a/src/client/interpreter/interpreterService.ts b/src/client/interpreter/interpreterService.ts index 74d9b7af0ebc..88f3a9b288b4 100644 --- a/src/client/interpreter/interpreterService.ts +++ b/src/client/interpreter/interpreterService.ts @@ -299,6 +299,9 @@ export class InterpreterService implements Disposable, IInterpreterService { // abandoned, so listeners are still notified of the real value once it becomes available: // the env-extension path reports via the environments API (see getActiveInterpreterLegacy), // and the native path reports via `_onConfigChanged` once auto-selection completes. + if (options?.waitForResolution) { + return resolution; + } const result = await Promise.race([ resolution, sleep(GET_ACTIVE_INTERPRETER_TIMEOUT_MS).then(() => ACTIVE_INTERPRETER_TIMED_OUT), diff --git a/src/test/application/diagnostics/checks/pythonInterpreter.unit.test.ts b/src/test/application/diagnostics/checks/pythonInterpreter.unit.test.ts index 2eecf052e433..4afc29f38fa9 100644 --- a/src/test/application/diagnostics/checks/pythonInterpreter.unit.test.ts +++ b/src/test/application/diagnostics/checks/pythonInterpreter.unit.test.ts @@ -148,7 +148,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { interpreterService.reset(); interpreterService.setup((i) => i.hasInterpreters()).returns(() => Promise.resolve(true)); interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => Promise.resolve(({ path: 'interpreterpath' } as unknown) as PythonEnvironment)); const result2 = await triggerFunction!(undefined); expect(result2).to.equal(true); @@ -228,7 +228,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { } // No interpreter should exist if comspec is incorrectly configured. interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => { return Promise.resolve(undefined); }); @@ -252,7 +252,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { } // No interpreter should exist if execution is failing. interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => { return Promise.resolve(undefined); }); @@ -272,7 +272,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { } // No interpreter should exist if execution is failing. interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => { return Promise.resolve(undefined); }); @@ -293,7 +293,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { interpreterService.setup((i) => i.hasInterpreters()).returns(() => Promise.resolve(false)); // No interpreter should exist if execution is failing. interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => { return Promise.resolve(undefined); }); @@ -318,7 +318,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { .returns(() => Promise.resolve(true)) .verifiable(typemoq.Times.once()); interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => { return Promise.resolve(undefined); }); @@ -338,7 +338,7 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { test('Should return empty diagnostics if there are interpreters and a current interpreter', async () => { interpreterService.setup((i) => i.hasInterpreters()).returns(() => Promise.resolve(true)); interpreterService - .setup((i) => i.getActiveInterpreter(typemoq.It.isAny())) + .setup((i) => i.getActiveInterpreter(typemoq.It.isAny(), typemoq.It.isAny())) .returns(() => { return Promise.resolve({ envType: EnvironmentType.Unknown } as any); }); @@ -347,6 +347,23 @@ suite('Application Diagnostics - Checks Python Interpreter', () => { expect(diagnostics).to.be.deep.equal([], 'not the same'); }); + test('Should wait for active interpreter resolution in _manualDiagnose', async () => { + interpreterService.setup((i) => i.hasInterpreters()).returns(() => Promise.resolve(true)); + interpreterService + .setup((i) => + i.getActiveInterpreter( + undefined, + typemoq.It.is((opts) => opts?.waitForResolution === true), + ), + ) + .returns(() => Promise.resolve(({ path: 'interpreterpath' } as unknown) as PythonEnvironment)) + .verifiable(typemoq.Times.once()); + + const diagnostics = await diagnosticService._manualDiagnose(undefined); + expect(diagnostics).to.be.deep.equal([]); + interpreterService.verifyAll(); + }); + test('Handling comspec diagnostic should launch expected browser link', async () => { const diagnostic = new DefaultShellDiagnostic(DiagnosticCodes.InvalidComspecDiagnostic, undefined); const cmd = ({} as any) as IDiagnosticCommand; diff --git a/src/test/interpreters/interpreterService.unit.test.ts b/src/test/interpreters/interpreterService.unit.test.ts index d8bdb5c81ec7..3cf78ec3d43e 100644 --- a/src/test/interpreters/interpreterService.unit.test.ts +++ b/src/test/interpreters/interpreterService.unit.test.ts @@ -480,6 +480,17 @@ suite('Interpreters service', () => { workspace.verify((w) => w.getWorkspaceFolderIdentifier(TypeMoq.It.isAny()), TypeMoq.Times.never()); expect(workspacePersistedValues.size).to.equal(0); }); + + test('waitForResolution waits for slow cold-start resolution instead of timing out', async () => { + const slowEnv = { path: path.join('usr', 'bin', 'slowpy') } as any; + useEnvExtensionStub.returns(true); + sinon.stub(legacyApi, 'getActiveInterpreterLegacy').callsFake(async () => delayed(slowEnv, 200)); + + const service = new InterpreterService(serviceContainer, pyenvs.object); + const result = await service.getActiveInterpreter(undefined, { waitForResolution: true }); + + expect(result).to.equal(slowEnv); + }); }); test('A configuration change re-runs the config change handler to report a background selection', async () => {