diff --git a/news/2 Fixes/5470.md b/news/2 Fixes/5470.md new file mode 100644 index 00000000000..69784512669 --- /dev/null +++ b/news/2 Fixes/5470.md @@ -0,0 +1 @@ +Invalidate cached interpreters when Python extension active interpreter changes. \ No newline at end of file diff --git a/src/client/api/pythonApi.ts b/src/client/api/pythonApi.ts index 0a8e0d470a4..daf8227e3b7 100644 --- a/src/client/api/pythonApi.ts +++ b/src/client/api/pythonApi.ts @@ -382,7 +382,14 @@ export class InterpreterService implements IInterpreterService { .then((api) => { if (!this.eventHandlerAdded) { this.eventHandlerAdded = true; - api.onDidChangeInterpreter(() => this.didChangeInterpreter.fire(), this, this.disposables); + api.onDidChangeInterpreter( + () => { + this.didChangeInterpreter.fire(); + this.workspaceCachedActiveInterpreter.clear(); + }, + this, + this.disposables + ); } }) .catch(noop); diff --git a/src/test/smoke/datascience.smoke.test.ts b/src/test/smoke/datascience.smoke.test.ts index 5907f1cb158..348664a2f73 100644 --- a/src/test/smoke/datascience.smoke.test.ts +++ b/src/test/smoke/datascience.smoke.test.ts @@ -9,7 +9,8 @@ import { assert } from 'chai'; import * as fs from 'fs-extra'; import * as path from 'path'; import * as vscode from 'vscode'; -import { ISystemPseudoRandomNumberGenerator } from '../../client/datascience/types'; +import { IInteractiveWindowProvider, ISystemPseudoRandomNumberGenerator } from '../../client/datascience/types'; +import { IInterpreterService } from '../../client/interpreter/contracts'; import { IExtensionTestApi, openFile, setAutoSaveDelayInWorkspaceRoot, waitForCondition } from '../common'; import { EXTENSION_ROOT_DIR_FOR_TESTS, IS_SMOKE_TEST } from '../constants'; import { sleep } from '../core'; @@ -100,4 +101,34 @@ suite('Smoke Tests', () => { // Give time for the file to be saved before we shutdown await sleep(300); }).timeout(timeoutForCellToRun); + + test('Interactive window should always pick up current active interpreter', async function () { + return this.skip(); // See https://github.com/microsoft/vscode-jupyter/issues/5478 + + // Make an interactive window + await vscode.commands.executeCommand('jupyter.createnewinteractive'); + const provider = api.serviceManager.get(IInteractiveWindowProvider); + assert.ok(provider.windows.length === 1, 'Unexpected number of interactive windows created'); + const currentWindow = provider.windows[0]; + const interpreterForCurrentWindow = currentWindow.notebook?.getMatchingInterpreter(); + assert.ok(interpreterForCurrentWindow !== undefined, 'Unable to get matching interpreter for current window'); + + // Now change active interpreter + const interpreterService = api.serviceManager.get(IInterpreterService); + const allInterpreters = await interpreterService.getInterpreters(); + assert.ok(allInterpreters.length > 1, 'Not enough interpreters to run interactive window smoke test'); + const differentInterpreter = allInterpreters.find((interpreter) => interpreter !== interpreterForCurrentWindow); + await vscode.commands.executeCommand('python.setInterpreter', differentInterpreter); // Requires change to Python extension + + // Now make another interactive window and confirm it's using the newly selected interpreter + await vscode.commands.executeCommand('jupyter.createnewinteractive'); + assert.ok(provider.windows.length === 2, 'Unexpected number of interactive windows created'); + const newWindow = provider.windows.find((window) => window !== currentWindow); + const interpreterForNewWindow = newWindow?.notebook?.getMatchingInterpreter(); + assert.ok(interpreterForNewWindow !== undefined, 'Unable to get matching interpreter for current window'); + assert.ok( + interpreterForNewWindow === differentInterpreter, + 'Interactive window not created with newly selected interpreter' + ); + }); });