-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathvscodeNotebookController.ts
804 lines (758 loc) · 35.6 KB
/
vscodeNotebookController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import {
CancellationError,
commands,
Disposable,
EventEmitter,
ExtensionMode,
languages,
NotebookCell,
NotebookCellExecution,
NotebookCellKind,
NotebookController,
NotebookDocument,
NotebookEditor,
NotebookRendererScript,
notebooks,
Uri,
window,
workspace
} from 'vscode';
import { IPythonExtensionChecker } from '../../platform/api/types';
import { Exiting, InteractiveWindowView, JupyterNotebookView, PYTHON_LANGUAGE } from '../../platform/common/constants';
import { dispose } from '../../platform/common/utils/lifecycle';
import { logger } from '../../platform/logging';
import { getDisplayPath } from '../../platform/common/platform/fs-paths';
import {
IConfigurationService,
IDisplayOptions,
IDisposable,
IDisposableRegistry,
IExtensionContext
} from '../../platform/common/types';
import { createDeferred } from '../../platform/common/utils/async';
import { DataScience, Common } from '../../platform/common/utils/localize';
import { noop, swallowExceptions } from '../../platform/common/utils/misc';
import { sendKernelTelemetryEvent } from '../../kernels/telemetry/sendKernelTelemetryEvent';
import { IServiceContainer } from '../../platform/ioc/types';
import { Commands } from '../../platform/common/constants';
import { Telemetry } from '../../telemetry';
import { WrappedError } from '../../platform/errors/types';
import { IPyWidgetMessages } from '../../messageTypes';
import {
getDisplayNameOrNameOfKernelConnection,
isPythonKernelConnection,
areKernelConnectionsEqual
} from '../../kernels/helpers';
import {
IKernel,
IKernelController,
IKernelProvider,
isLocalConnection,
KernelConnectionMetadata
} from '../../kernels/types';
import { KernelDeadError } from '../../kernels/errors/kernelDeadError';
import { DisplayOptions } from '../../kernels/displayOptions';
import { getNotebookMetadata, isJupyterNotebook, updateNotebookMetadata } from '../../platform/common/utils';
import { ConsoleForegroundColors } from '../../platform/logging/types';
import { KernelConnector } from './kernelConnector';
import { IConnectionDisplayData, IConnectionDisplayDataProvider, IVSCodeNotebookController } from './types';
import { isCancellationError } from '../../platform/common/cancellation';
import { CellExecutionCreator } from '../../kernels/execution/cellExecutionCreator';
import {
traceCellMessage,
endCellAndDisplayErrorsInCell,
updateNotebookMetadataWithSelectedKernel
} from '../../kernels/execution/helpers';
import type { KernelMessage } from '@jupyterlab/services';
import { initializeInteractiveOrNotebookTelemetryBasedOnUserAction } from '../../kernels/telemetry/helper';
import { NotebookCellLanguageService } from '../languages/cellLanguageService';
import { IDataScienceErrorHandler } from '../../kernels/errors/types';
import { ITrustedKernelPaths } from '../../kernels/raw/finder/types';
import { KernelController } from '../../kernels/kernelController';
import { RemoteKernelReconnectBusyIndicator } from './remoteKernelReconnectBusyIndicator';
import { LastCellExecutionTracker } from '../../kernels/execution/lastCellExecutionTracker';
import type { IAnyMessageArgs } from '@jupyterlab/services/lib/kernel/kernel';
import { getParentHeaderMsgId } from '../../kernels/execution/cellExecutionMessageHandler';
import { DisposableStore } from '../../platform/common/utils/lifecycle';
import { openInBrowser } from '../../platform/common/net/browser';
import { KernelError } from '../../kernels/errors/kernelError';
import { getVersion } from '../../platform/interpreter/helpers';
import { getNotebookTelemetryTracker, trackControllerCreation } from '../../kernels/telemetry/notebookTelemetry';
import { IJupyterVariablesProvider } from '../../kernels/variables/types';
import type { INotebookMetadata } from '@jupyterlab/nbformat';
/**
* Our implementation of the VSCode Notebook Controller. Called by VS code to execute cells in a notebook. Also displayed
* in the kernel picker by VS code.
*/
export class VSCodeNotebookController implements Disposable, IVSCodeNotebookController {
private readonly _onNotebookControllerSelectionChanged = new EventEmitter<{
selected: boolean;
notebook: NotebookDocument;
controller: VSCodeNotebookController;
}>();
private readonly _onConnecting = new EventEmitter<void>();
private pendingCellAdditions = new WeakMap<NotebookDocument, Promise<void>>();
private readonly _onDidDispose = new EventEmitter<void>();
private readonly disposables: IDisposable[] = [];
private notebookKernels = new WeakMap<NotebookDocument, IKernel>();
public readonly controller: NotebookController;
/**
* Used purely for testing purposes.
*/
public static kernelAssociatedWithDocument?: boolean;
private isDisposed = false;
private runningCellExecutions = new WeakMap<NotebookDocument, NotebookCellExecution>();
get id() {
return this.controller.id;
}
get label() {
return this.controller.label;
}
get connection() {
return this.kernelConnection;
}
get viewType() {
return this._viewType as typeof InteractiveWindowView | typeof JupyterNotebookView;
}
get onNotebookControllerSelectionChanged() {
return this._onNotebookControllerSelectionChanged.event;
}
get onConnecting() {
return this._onConnecting.event;
}
get onDidReceiveMessage() {
return this.controller.onDidReceiveMessage;
}
get onDidDispose() {
return this._onDidDispose.event;
}
public isAssociatedWithDocument(doc: NotebookDocument) {
return this.associatedDocuments.has(doc);
}
private readonly displayData: IConnectionDisplayData;
private readonly associatedDocuments = new WeakMap<NotebookDocument, Promise<void>>();
public static create(
kernelConnection: KernelConnectionMetadata,
id: string,
_viewType: string,
kernelProvider: IKernelProvider,
context: IExtensionContext,
disposableRegistry: IDisposableRegistry,
languageService: NotebookCellLanguageService,
configuration: IConfigurationService,
extensionChecker: IPythonExtensionChecker,
serviceContainer: IServiceContainer,
displayDataProvider: IConnectionDisplayDataProvider,
jupyterVairablesProvider: IJupyterVariablesProvider
): IVSCodeNotebookController {
const controller = new VSCodeNotebookController(
kernelConnection,
id,
_viewType,
kernelProvider,
context,
disposableRegistry,
languageService,
configuration,
extensionChecker,
serviceContainer,
displayDataProvider
);
try {
controller.controller.variableProvider = jupyterVairablesProvider;
} catch (ex) {
logger.warn('Failed to attach variable provider', ex);
}
return controller;
}
constructor(
private kernelConnection: KernelConnectionMetadata,
id: string,
private _viewType: string,
private readonly kernelProvider: IKernelProvider,
private readonly context: IExtensionContext,
disposableRegistry: IDisposableRegistry,
private readonly languageService: NotebookCellLanguageService,
private readonly configuration: IConfigurationService,
private readonly extensionChecker: IPythonExtensionChecker,
private serviceContainer: IServiceContainer,
private readonly displayDataProvider: IConnectionDisplayDataProvider
) {
trackControllerCreation(kernelConnection.id, kernelConnection.interpreter?.id);
disposableRegistry.push(this);
this.displayData = this.displayDataProvider.getDisplayData(this.connection);
this.controller = notebooks.createNotebookController(
id,
_viewType,
this.displayData.label,
this.handleExecution.bind(this),
this.getRendererScripts()
);
this.displayData.onDidChange(this.updateDisplayData, this, this.disposables);
this.updateDisplayData();
// Fill in extended info for our controller
this.controller.interruptHandler = this.handleInterrupt.bind(this);
this.controller.supportsExecutionOrder = true;
this.controller.supportedLanguages = this.languageService.getSupportedLanguages(kernelConnection);
// Hook up to see when this NotebookController is selected by the UI
this.controller.onDidChangeSelectedNotebooks(this.onDidChangeSelectedNotebooks, this, this.disposables);
workspace.onDidCloseNotebookDocument(
(n) => {
this.associatedDocuments.delete(n);
},
this,
this.disposables
);
}
private readonly restoredConnections = new WeakSet<NotebookDocument>();
public async restoreConnection(notebook: NotebookDocument) {
if (this.restoredConnections.has(notebook)) {
return;
}
this.restoredConnections.add(notebook);
const kernel = await this.connectToKernel(notebook, new DisplayOptions(true));
if (this.kernelConnection.kind === 'connectToLiveRemoteKernel') {
const indicator = new RemoteKernelReconnectBusyIndicator(kernel, this.controller, notebook);
this.disposables.push(indicator);
indicator.initialize();
}
const kernelExecution = this.kernelProvider.getKernelExecution(kernel);
const lastCellExecutionTracker = this.serviceContainer.get<LastCellExecutionTracker>(LastCellExecutionTracker);
const info = await lastCellExecutionTracker.getLastTrackedCellExecution(notebook, kernel);
if (
!kernel.session?.kernel ||
kernelExecution.pendingCells.length ||
!info ||
notebook.cellCount < info.cellIndex ||
notebook.cellAt(info.cellIndex).kind !== NotebookCellKind.Code
) {
return;
}
void initializeInteractiveOrNotebookTelemetryBasedOnUserAction(
kernel.resourceUri,
kernel.kernelConnectionMetadata
);
// If we're connected to the same kernel session and the same cell is still getting executed,
// then ensure to mark the cell as busy and attach the outputs of the execution to the cell.
let resumed = false;
const localDisposables: IDisposable[] = [];
let disposeAnyHandler: IDisposable | undefined;
const anyMessageHandler = (_: unknown, msg: IAnyMessageArgs) => {
if (msg.direction === 'send' || resumed) {
return;
}
if (getParentHeaderMsgId(msg.msg as KernelMessage.IMessage) === info.msg_id) {
// If we have an idle state, then the request is done.
if (
'msg_type' in msg.msg &&
msg.msg.msg_type === 'status' &&
'execution_state' in msg.msg.content &&
msg.msg.content.execution_state === 'idle'
) {
return;
}
resumed = true;
kernelExecution
.resumeCellExecution(notebook.cellAt(info.cellIndex), {
msg_id: info.msg_id,
startTime: info.startTime,
executionCount: info.executionCount
})
.catch(noop);
dispose(localDisposables);
}
};
// Check if we're still getting messages for the previous execution.
kernel.session.kernel.anyMessage.connect(anyMessageHandler);
disposeAnyHandler = new Disposable(() => {
swallowExceptions(() => kernel.session?.kernel?.anyMessage.disconnect(anyMessageHandler));
});
localDisposables.push(disposeAnyHandler);
this.disposables.push(disposeAnyHandler);
}
public updateConnection(kernelConnection: KernelConnectionMetadata) {
if (kernelConnection.kind !== 'connectToLiveRemoteKernel') {
this.controller.label = getDisplayNameOrNameOfKernelConnection(kernelConnection);
}
}
public asWebviewUri(localResource: Uri): Uri {
return this.controller.asWebviewUri(localResource);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public postMessage(message: any, editor?: NotebookEditor): Thenable<boolean> {
const messageType = message && 'message' in message ? message.message : '';
logger.ci(`${ConsoleForegroundColors.Green}Posting message to Notebook UI ${messageType}`);
return this.controller.postMessage(message, editor);
}
/**
* A cell has been added to the notebook, so wait for the execution to be queued before handling any more execution requests.
* This only applies to the Interactive Window since cells are added from both the extension and core.
* @param promise A promise that resolves when the notebook is ready to handle more executions.
*/
public setPendingCellAddition(notebook: NotebookDocument, promise: Promise<void>): void {
if (this.viewType !== InteractiveWindowView) {
throw new Error('setPendingCellAddition only applies to the Interactive Window');
}
this.pendingCellAdditions.set(notebook, promise);
}
public dispose() {
if (this.isDisposed) {
return;
}
const nbDocumentUris = workspace.notebookDocuments
.filter((item) => this.associatedDocuments.has(item))
.map((item) => item.uri.toString());
logger.debug(
`Disposing controller ${this.id} associated with connection ${this.connection.id} ${
nbDocumentUris.length ? 'and documents ' + nbDocumentUris.join(', ') : ''
}`
);
logger.ci(
`Disposing controller ${this.id} associated with connection ${this.connection.id} ${
nbDocumentUris.length ? 'and documents ' + nbDocumentUris.join(', ') : ''
} called from ${new Error('').stack}`
);
this.isDisposed = true;
this._onNotebookControllerSelectionChanged.dispose();
this._onConnecting.dispose();
this.controller.dispose();
this._onDidDispose.fire();
this._onDidDispose.dispose();
dispose(this.disposables);
}
private updateDisplayData() {
this.controller.label = this.displayData.label;
// Do not set descriptions for the live kernels,
// Descriptions contains date/time, and the controller never gets updated every second,
// Hence having the date time is not going to work.
let description = this.connection.kind === 'connectToLiveRemoteKernel' ? '' : this.displayData.description;
this.controller.description = description;
if (this.displayData.serverDisplayName) {
// MRU kernel picker doesn't show controller kind/category, so add server name to description
this.controller.description = description
? `${description} (${this.displayData.serverDisplayName})`
: this.displayData.serverDisplayName;
}
}
private async handleExecution(cells: NotebookCell[], notebook: NotebookDocument) {
if (cells.length < 1) {
return;
}
const tracker = getNotebookTelemetryTracker(notebook);
tracker?.cellExecutionCount(cells.length);
const telemetryTracker = tracker?.preExecuteCellTelemetry();
if (this.pendingCellAdditions.has(notebook)) {
await this.pendingCellAdditions.get(notebook);
}
// Found on CI that sometimes VS Code calls this with old deleted cells.
// See here https://github.com/microsoft/vscode-jupyter/runs/5581627878?check_suite_focus=true
cells = cells.filter((cell) => {
if (cell.index < 0) {
logger.warn(
`Attempting to run a cell with index ${cell.index}, kind ${
cell.kind
}, text = ${cell.document.getText()}`
);
return false;
}
return true;
});
// When we receive a cell execute request, first ensure that the notebook is trusted.
// If it isn't already trusted, block execution until the user trusts it.
if (!workspace.isTrusted) {
return;
}
logger.debug(`Handle Execution of Cells ${cells.map((c) => c.index)} for ${getDisplayPath(notebook.uri)}`);
await initializeInteractiveOrNotebookTelemetryBasedOnUserAction(notebook.uri, this.connection);
telemetryTracker?.stop();
const queue = this.cellQueue.get(notebook) || [];
this.cellQueue.set(notebook, queue.concat(cells));
// Notebook is trusted. Continue to execute cells
await this.executeQueuedCells(notebook);
}
private async onDidChangeSelectedNotebooks(event: { notebook: NotebookDocument; selected: boolean }) {
logger.ci(
`NotebookController selection event called for notebook ${event.notebook.uri.toString()} & controller ${
this.connection.kind
}:${this.id}. Selected ${event.selected} `
);
if (this.associatedDocuments.has(event.notebook) && event.selected) {
// Possible it gets called again in our tests (due to hacks for testing purposes).
return;
}
if (!event.selected) {
// If user has selected another controller, then kill the current kernel.
// Possible user selected a controller that's not contributed by us at all.
const kernel = this.kernelProvider.get(event.notebook);
if (kernel?.kernelConnectionMetadata.id === this.kernelConnection.id) {
logger.info(
`Disposing kernel ${this.kernelConnection.id} for notebook ${getDisplayPath(
event.notebook.uri
)} due to selection of another kernel or closing of the notebook`
);
kernel.dispose().catch(noop);
}
this.associatedDocuments.delete(event.notebook);
this._onNotebookControllerSelectionChanged.fire({
controller: this,
notebook: event.notebook,
selected: event.selected
});
return;
}
// We're only interested in our Notebooks.
if (!isJupyterNotebook(event.notebook) && event.notebook.notebookType !== InteractiveWindowView) {
return;
}
if (!workspace.isTrusted) {
return;
}
getNotebookTelemetryTracker(event.notebook)?.kernelSelected(
this.kernelConnection.id,
this.kernelConnection.interpreter?.id
);
void warnWhenUsingOutdatedPython(this.kernelConnection);
const deferred = createDeferred<void>();
logger.ci(
`Controller ${this.connection.kind}:${this.id} associated with nb ${getDisplayPath(event.notebook.uri)}`
);
this.associatedDocuments.set(event.notebook, deferred.promise);
await this.onDidSelectController(event.notebook);
await this.updateCellLanguages(event.notebook);
// If this NotebookController was selected, fire off the event
this._onNotebookControllerSelectionChanged.fire({
controller: this,
notebook: event.notebook,
selected: event.selected
});
logger.debug(`Controller selection change completed`);
deferred.resolve();
}
/**
* Scenario 1:
* Assume user opens a notebook and language is C++ or .NET Interactive, they start writing python code.
* Next users hits the run button, next user will be prompted to select a kernel.
* User now selects a Python kernel.
* Nothing happens, that's right nothing happens.
* This is because C++ is not a languages supported by the python kernel.
* Hence VS Code will not send the execution call to the extension.
*
* Solution, go through the cells and change the language to something that's supported.
*
* Scenario 2:
* User has .NET extension installed.
* User opens a Python notebook and runs a cell with a .NET kernel (accidentally or deliberately).
* User gets errors in output & realizes mistake & changes the kernel.
* Now user runs a cell & nothing happens again.
*/
private async updateCellLanguages(notebook: NotebookDocument) {
const supportedLanguages = this.controller.supportedLanguages;
// If the controller doesn't have any preferred languages, then get out.
if (!supportedLanguages || supportedLanguages?.length === 0) {
return;
}
const isPythonKernel = isPythonKernelConnection(this.kernelConnection);
const preferredLanguage = isPythonKernel ? PYTHON_LANGUAGE : supportedLanguages[0];
await Promise.all(
notebook
.getCells()
.filter((cell) => cell.kind === NotebookCellKind.Code)
.map(async (cell) => {
if (!supportedLanguages.includes(cell.document.languageId)) {
await languages.setTextDocumentLanguage(cell.document, preferredLanguage).then(noop, noop);
}
})
);
}
private getRendererScripts(): NotebookRendererScript[] {
// Only used in tests & while debugging.
if (
this.context.extensionMode === ExtensionMode.Development ||
this.context.extensionMode === ExtensionMode.Test
) {
return [
new NotebookRendererScript(
Uri.joinPath(
this.context.extensionUri,
'dist',
'webviews',
'webview-side',
'widgetTester',
'widgetTester.js'
)
)
];
} else {
return [];
}
}
private handleInterrupt(notebook: NotebookDocument) {
logger.debug(`VS Code interrupted kernel for ${getDisplayPath(notebook.uri)}`);
notebook.getCells().forEach((cell) => traceCellMessage(cell, 'Cell cancellation requested'));
commands
.executeCommand(Commands.InterruptKernel, { notebookEditor: { notebookUri: notebook.uri } })
.then(noop, (ex) => logger.error('Failed to interrupt', ex));
}
private createCellExecutionIfNecessary(cell: NotebookCell, controller: IKernelController) {
// Only have one cell in the 'running' state for this notebook
let currentExecution = this.runningCellExecutions.get(cell.notebook);
if (!currentExecution || currentExecution.cell === cell) {
currentExecution?.end(undefined, undefined);
currentExecution = CellExecutionCreator.getOrCreate(cell, controller, true);
this.runningCellExecutions.set(cell.notebook, currentExecution);
// When this execution ends, we don't have a current one anymore.
const originalEnd = currentExecution.end.bind(currentExecution);
currentExecution.end = (success: boolean | undefined, endTime?: number | undefined) => {
this.runningCellExecutions.delete(cell.notebook);
originalEnd(success, endTime);
};
}
return currentExecution;
}
private cellQueue = new WeakMap<NotebookDocument, NotebookCell[]>();
private async executeQueuedCells(doc: NotebookDocument) {
if (!this.cellQueue.has(doc)) {
return;
}
// Start execution now (from the user's point of view)
// Creating these execution objects marks the cell as queued for execution (vscode will update cell UI).
type CellExec = { cell: NotebookCell; exec: NotebookCellExecution };
const cellExecs: CellExec[] = (this.cellQueue.get(doc) || []).map((cell) => {
const exec = this.createCellExecutionIfNecessary(cell, new KernelController(this.controller));
return { cell, exec };
});
this.cellQueue.delete(doc);
const firstCell = cellExecs.length ? cellExecs[0].cell : undefined;
if (!firstCell) {
return;
}
logger.trace(`Execute Notebook ${getDisplayPath(doc.uri)}. Step 1`);
// Connect to a matching kernel if possible (but user may pick a different one)
let currentContext: 'start' | 'execution' = 'start';
let controller: IKernelController = new KernelController(this.controller);
const lastCellExecutionTracker = this.serviceContainer.get<LastCellExecutionTracker>(LastCellExecutionTracker);
let kernel: IKernel | undefined;
try {
logger.trace(`Connect to Kernel ${getDisplayPath(doc.uri)}. Step 2`);
kernel = await this.connectToKernel(doc, new DisplayOptions(false));
logger.trace(`Connected to Kernel ${getDisplayPath(doc.uri)}. Step 3`);
if (kernel.disposing) {
throw new CancellationError();
}
// If the controller changed, then ensure to create a new cell execution object.
if (kernel && kernel.controller.id !== controller.id) {
controller = kernel.controller;
cellExecs.forEach(
(cellExec) => (cellExec.exec = this.createCellExecutionIfNecessary(cellExec.cell, controller))
);
}
currentContext = 'execution';
if (kernel.controller.id === this.id) {
this.updateKernelInfoInNotebookWhenAvailable(kernel, doc);
}
} catch (ex) {
if (ex instanceof KernelError) {
// Kernel errors would have been handled and displayed
return;
}
if (!isCancellationError(ex)) {
logger.error(`Error in notebook cell execution`, ex);
}
cellExecs.forEach(({ cell, exec }) => {
exec.start();
exec.clearOutput(cell).then(noop, noop);
});
const errorHandler = this.serviceContainer.get<IDataScienceErrorHandler>(IDataScienceErrorHandler);
ex = WrappedError.unwrap(ex);
const isCancelled = isCancellationError(ex) || ex instanceof KernelDeadError;
// If there was a failure connecting or executing the kernel, stick it in this cell
await endCellAndDisplayErrorsInCell(
firstCell,
controller,
await errorHandler.getErrorMessageForDisplayInCellOutput(ex, currentContext, doc.uri),
isCancelled
);
}
if (!kernel) {
return;
}
const kernelExecution = this.kernelProvider.getKernelExecution(kernel);
const disposables = new DisposableStore();
await Promise.all(
cellExecs.map(async ({ cell }) => {
try {
// Track the information so we can restore execution upon reloading vs code or the like.
const cellExecTracker = getNotebookTelemetryTracker(doc)?.executeCell();
if (cellExecTracker) {
disposables.add(new Disposable(() => cellExecTracker.stop()));
}
lastCellExecutionTracker.trackCellExecution(cell, kernel);
logger.trace(`executeCell ${cell.index}. Step 4`);
await kernelExecution.executeCell(cell);
// If we complete execution, then there is nothing to be restored.
if (!Exiting.isExiting) {
// If we're exiting vs code, then no need to clear the last execution info, we need to preserve that.
lastCellExecutionTracker.deleteTrackedCellExecution(cell, kernel);
}
} catch (ex) {
if (ex instanceof KernelError) {
// Kernel errors would have been handled and displayed
return;
}
if (!isCancellationError(ex)) {
logger.error(`Error in cell execution`, ex);
}
const errorHandler = this.serviceContainer.get<IDataScienceErrorHandler>(IDataScienceErrorHandler);
ex = WrappedError.unwrap(ex);
const isCancelled = isCancellationError(ex) || ex instanceof KernelDeadError;
// If there was a failure connecting or executing the kernel, stick it in this cell
await endCellAndDisplayErrorsInCell(
cell,
controller,
await errorHandler.getErrorMessageForDisplayInCellOutput(ex, currentContext, doc.uri),
isCancelled
);
}
})
).catch(noop);
}
private async connectToKernel(doc: NotebookDocument, options: IDisplayOptions): Promise<IKernel> {
const tracker = getNotebookTelemetryTracker(doc)?.startKernel();
this._onConnecting.fire();
return KernelConnector.connectToNotebookKernel(
this.kernelConnection,
this.serviceContainer,
{ resource: doc.uri, notebook: doc, controller: this.controller },
options,
this.disposables
).finally(() => tracker?.stop());
}
private updateKernelInfoInNotebookWhenAvailable(kernel: IKernel, doc: NotebookDocument) {
if (this.notebookKernels.get(doc) === kernel) {
return;
}
this.notebookKernels.set(doc, kernel);
const handlerDisposables: IDisposable[] = [];
// If the notebook is closed, dispose everything.
workspace.onDidCloseNotebookDocument(
(e) => {
if (e === doc) {
dispose(handlerDisposables);
}
},
this,
handlerDisposables
);
const kernelDisposedDisposable = kernel.onDisposed(() => dispose(handlerDisposables));
const statusChangeDisposable = kernel.onStatusChanged(async () => {
if (kernel.disposed || !kernel.info) {
return;
}
// Disregard if we've changed kernels (i.e. if this controller is no longer associated with the document)
if (!this.associatedDocuments.has(doc)) {
return;
}
await updateNotebookDocumentMetadata(doc, kernel.kernelConnectionMetadata, kernel.info);
if (kernel.info.status === 'ok') {
dispose(handlerDisposables);
}
});
handlerDisposables.push({ dispose: () => statusChangeDisposable.dispose() });
handlerDisposables.push({ dispose: () => kernelDisposedDisposable?.dispose() });
}
private async onDidSelectController(document: NotebookDocument) {
const selectedKernelConnectionMetadata = this.connection;
const existingKernel = this.kernelProvider.get(document);
if (
existingKernel &&
areKernelConnectionsEqual(existingKernel.kernelConnectionMetadata, selectedKernelConnectionMetadata)
) {
logger.info('Switch kernel did not change kernel.');
return;
}
// Send our SwitchKernel telemetry
sendKernelTelemetryEvent(document.uri, Telemetry.SwitchKernel);
// If we have an existing kernel, then we know for a fact the user is changing the kernel.
// Else VSC is just setting a kernel for a notebook after it has opened.
if (existingKernel) {
window.visibleNotebookEditors
.filter((editor) => editor.notebook === document)
.forEach((editor) =>
this.postMessage(
{ message: IPyWidgetMessages.IPyWidgets_onKernelChanged, payload: undefined },
editor
)
);
}
// Before we start the notebook, make sure the metadata is set to this new kernel.
await updateNotebookDocumentMetadata(document, selectedKernelConnectionMetadata);
if (document.notebookType === InteractiveWindowView) {
// Possible its an interactive window, in that case we'll create the kernel manually.
return;
}
// Make this the new kernel (calling this method will associate the new kernel with this Uri).
// Calling `getOrCreate` will ensure a kernel is created and it is mapped to the Uri provided.
// This will dispose any existing (older kernels) associated with this notebook.
// This way other parts of extension have access to this kernel immediately after event is handled.
// Unlike webview notebooks we cannot revert to old kernel if kernel switching fails.
const newKernel = this.kernelProvider.getOrCreate(document, {
metadata: selectedKernelConnectionMetadata,
controller: this.controller,
resourceUri: document.uri // In the case of interactive window, we cannot pass the Uri of notebook, it must be the Py file or undefined.
});
logger.debug(`KernelProvider switched kernel to id = ${newKernel.kernelConnectionMetadata.id}`);
// If this is a Python notebook and Python isn't installed, then don't auto-start the kernel.
if (isPythonKernelConnection(this.kernelConnection) && !this.extensionChecker.isPythonExtensionInstalled) {
return;
}
// Auto start the local kernels.
const trustedKernelPaths = this.serviceContainer.get<ITrustedKernelPaths>(ITrustedKernelPaths);
if (
!this.configuration.getSettings(undefined).disableJupyterAutoStart &&
isLocalConnection(this.kernelConnection) &&
this.kernelConnection.kernelSpec.specFile &&
trustedKernelPaths.isTrusted(Uri.file(this.kernelConnection.kernelSpec.specFile))
) {
// Startup could fail due to missing dependencies or the like.
void this.connectToKernel(document, new DisplayOptions(true));
}
}
}
async function updateNotebookDocumentMetadata(
document: NotebookDocument,
kernelConnection?: KernelConnectionMetadata,
kernelInfo?: Partial<KernelMessage.IInfoReplyMsg['content']>
) {
const metadata: INotebookMetadata = getNotebookMetadata(document) || {};
const { changed } = await updateNotebookMetadataWithSelectedKernel(metadata, kernelConnection, kernelInfo);
if (changed) {
await updateNotebookMetadata(document, metadata);
}
}
export async function warnWhenUsingOutdatedPython(kernelConnection: KernelConnectionMetadata) {
const pyVersion = await getVersion(kernelConnection.interpreter, true);
const major = pyVersion?.major || 0;
const minor = pyVersion?.minor || 0;
if (
!pyVersion ||
major >= 4 ||
major <= 0 || // Invalid versions from Python extension
minor <= -1 || // Invalid versions from Python extension
(kernelConnection.kind !== 'startUsingLocalKernelSpec' &&
kernelConnection.kind !== 'startUsingPythonInterpreter')
) {
return;
}
if (major < 3 || (major === 3 && minor <= 5)) {
window
.showWarningMessage(DataScience.warnWhenSelectingKernelWithUnSupportedPythonVersion, Common.learnMore)
.then((selection) => {
if (selection !== Common.learnMore) {
return;
}
return openInBrowser('https://aka.ms/jupyterUnSupportedPythonKernelVersions');
}, noop);
}
}