Skip to content

Commit

Permalink
Clear cached interpreter on active interpreter change (#5474)
Browse files Browse the repository at this point in the history
* Clear cache on active interpreter change

* Add smoke test but skipped due to changes required in Python extension
  • Loading branch information
joyceerhl committed Apr 9, 2021
1 parent b05a372 commit 7863499
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/2 Fixes/5470.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Invalidate cached interpreters when Python extension active interpreter changes.
9 changes: 8 additions & 1 deletion src/client/api/pythonApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion src/test/smoke/datascience.smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<void>('jupyter.createnewinteractive');
const provider = api.serviceManager.get<IInteractiveWindowProvider>(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>(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<void>('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<void>('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'
);
});
});

0 comments on commit 7863499

Please sign in to comment.