Skip to content

Commit 052d5b0

Browse files
authored
Fix issue with kernel preselection being overridden by view state (microsoft#154968)
* Fix view state overriding selected kernel * Add test to verify correct kernel is used
1 parent f649124 commit 052d5b0

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

extensions/vscode-api-tests/src/singlefolder-tests/interactiveWindow.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function createInteractiveWindow(kernel: Kernel) {
1717
// Keep focus on the owning file if there is one
1818
{ viewColumn: vscode.ViewColumn.Beside, preserveFocus: false },
1919
undefined,
20-
kernel.controller.id,
20+
`vscode.vscode-api-tests/${kernel.controller.id}`,
2121
undefined
2222
)) as unknown as INativeInteractiveWindow;
2323

@@ -45,11 +45,13 @@ async function addCellAndRun(code: string, notebook: vscode.NotebookDocument, i:
4545

4646
const testDisposables: vscode.Disposable[] = [];
4747
let defaultKernel: Kernel;
48+
let secondKernel: Kernel;
4849

4950
setup(async function () {
50-
// there should be ONE default kernel in this suite
5151
defaultKernel = new Kernel('mainKernel', 'Notebook Default Kernel', 'interactive');
52+
secondKernel = new Kernel('secondKernel', 'Notebook Secondary Kernel', 'interactive');
5253
testDisposables.push(defaultKernel.controller);
54+
testDisposables.push(secondKernel.controller);
5355
await saveAllFilesAndCloseAll();
5456
});
5557

@@ -85,4 +87,22 @@ async function addCellAndRun(code: string, notebook: vscode.NotebookDocument, i:
8587
assert.strictEqual(notebookEditor.visibleRanges[notebookEditor.visibleRanges.length - 1].end, notebookEditor.notebook.cellCount, `Last cell is not visible`);
8688

8789
});
90+
91+
test('Interactive window has the correct kernel', async () => {
92+
assert.ok(vscode.workspace.workspaceFolders);
93+
const notebookEditor = await createInteractiveWindow(defaultKernel);
94+
assert.ok(notebookEditor);
95+
96+
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
97+
98+
// Create a new interactive window with a different kernel
99+
const notebookEditor2 = await createInteractiveWindow(secondKernel);
100+
assert.ok(notebookEditor2);
101+
102+
// Verify the kernel is the secondary one
103+
await addCellAndRun(`print`, notebookEditor2.notebook, 0);
104+
105+
assert.strictEqual(secondKernel.associatedNotebooks.has(notebookEditor2.notebook.uri.toString()), true, `Secondary kernel was not set as the kernel for the interactive window`);
106+
107+
});
88108
});

src/vs/workbench/contrib/notebook/browser/notebookEditorWidget.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,8 +1693,11 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
16931693

16941694
private _restoreSelectedKernel(viewState: INotebookEditorViewState | undefined): void {
16951695
if (viewState?.selectedKernelId && this.textModel) {
1696-
const kernel = this.notebookKernelService.getMatchingKernel(this.textModel).all.find(k => k.id === viewState.selectedKernelId);
1697-
if (kernel) {
1696+
const matching = this.notebookKernelService.getMatchingKernel(this.textModel);
1697+
const kernel = matching.all.find(k => k.id === viewState.selectedKernelId);
1698+
// Selected kernel may have already been picked prior to the view state loading
1699+
// If so, don't overwrite it with the saved kernel.
1700+
if (kernel && !matching.selected) {
16981701
this.notebookKernelService.selectKernelForNotebook(kernel, this.textModel);
16991702
}
17001703
}

0 commit comments

Comments
 (0)