-
Notifications
You must be signed in to change notification settings - Fork 297
/
debuggingManager.ts
472 lines (420 loc) · 19 KB
/
debuggingManager.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable } from 'inversify';
import {
debug,
NotebookDocument,
workspace,
DebugAdapterInlineImplementation,
DebugSession,
NotebookCell,
DebugSessionOptions,
DebugAdapterDescriptor,
Event,
EventEmitter,
NotebookEditor
} from 'vscode';
import * as path from 'path';
import { IKernel, IKernelProvider } from '../../datascience/jupyter/kernels/types';
import { IConfigurationService, IDisposable } from '../../common/types';
import { KernelDebugAdapter } from './kernelDebugAdapter';
import { IExtensionSingleActivationService } from '../../activation/types';
import { INotebookControllerManager } from '../../datascience/notebook/types';
import { ContextKey } from '../../common/contextKey';
import { EditorContexts } from '../../datascience/constants';
import { IApplicationShell, ICommandManager, IVSCodeNotebook } from '../../common/application/types';
import { traceError } from '../../common/logger';
import { DataScience } from '../../common/utils/localize';
import { Commands as DSCommands } from '../../datascience/constants';
import { IFileSystem } from '../../common/platform/types';
import { IDebuggingManager, IKernelDebugAdapterConfig, KernelDebugMode } from '../types';
import { DebuggingTelemetry, pythonKernelDebugAdapter } from '../constants';
import { sendTelemetryEvent } from '../../telemetry';
import { DebugCellController, RunByLineController } from './debugControllers';
import { assertIsDebugConfig, IpykernelCheckResult, isUsingIpykernel6OrLater } from './helper';
import { Debugger } from './debugger';
import { IpyKernelNotInstalledError } from '../../datascience/kernel-launcher/types';
/**
* The DebuggingManager maintains the mapping between notebook documents and debug sessions.
*/
@injectable()
export class DebuggingManager implements IExtensionSingleActivationService, IDebuggingManager, IDisposable {
private debuggingInProgress: ContextKey;
private runByLineInProgress: ContextKey;
private notebookToDebugger = new Map<NotebookDocument, Debugger>();
private notebookToDebugAdapter = new Map<NotebookDocument, KernelDebugAdapter>();
private notebookToRunByLineController = new Map<NotebookDocument, RunByLineController>();
private notebookInProgress = new Set<NotebookDocument>();
private readonly disposables: IDisposable[] = [];
private _doneDebugging = new EventEmitter<void>();
public constructor(
@inject(IKernelProvider) private kernelProvider: IKernelProvider,
@inject(INotebookControllerManager) private readonly notebookControllerManager: INotebookControllerManager,
@inject(ICommandManager) private readonly commandManager: ICommandManager,
@inject(IApplicationShell) private readonly appShell: IApplicationShell,
@inject(IVSCodeNotebook) private readonly vscNotebook: IVSCodeNotebook,
@inject(IFileSystem) private fs: IFileSystem,
@inject(IConfigurationService) private settings: IConfigurationService
) {
this.debuggingInProgress = new ContextKey(EditorContexts.DebuggingInProgress, this.commandManager);
this.runByLineInProgress = new ContextKey(EditorContexts.RunByLineInProgress, this.commandManager);
this.updateToolbar(false);
this.updateCellToolbar(false);
this.disposables.push(
this.vscNotebook.onDidChangeActiveNotebookEditor(
(e?: NotebookEditor) => {
if (e) {
this.updateCellToolbar(this.isDebugging(e.document));
this.updateToolbar(this.isDebugging(e.document));
}
},
this,
this.disposables
)
);
}
public async activate() {
this.disposables.push(
// track termination of debug sessions
debug.onDidTerminateDebugSession(this.endSession.bind(this)),
// track closing of notebooks documents
workspace.onDidCloseNotebookDocument(async (document) => {
const dbg = this.notebookToDebugger.get(document);
if (dbg) {
await dbg.stop();
this.updateToolbar(false);
this.updateCellToolbar(false);
}
}),
// factory for kernel debug adapters
debug.registerDebugAdapterDescriptorFactory(pythonKernelDebugAdapter, {
createDebugAdapterDescriptor: async (session) => this.createDebugAdapterDescriptor(session)
}),
this.commandManager.registerCommand(DSCommands.DebugNotebook, async () => {
const editor = this.vscNotebook.activeNotebookEditor;
await this.tryToStartDebugging(KernelDebugMode.Everything, editor);
}),
this.commandManager.registerCommand(DSCommands.RunByLine, async (cell: NotebookCell | undefined) => {
sendTelemetryEvent(DebuggingTelemetry.clickedRunByLine);
const editor = this.vscNotebook.activeNotebookEditor;
if (!cell) {
const range = editor?.selections[0];
if (range) {
cell = editor?.document.cellAt(range.start);
}
}
if (!cell) {
return;
}
await this.tryToStartDebugging(KernelDebugMode.RunByLine, editor, cell);
}),
this.commandManager.registerCommand(DSCommands.RunByLineNext, (cell: NotebookCell | undefined) => {
if (!cell) {
const editor = this.vscNotebook.activeNotebookEditor;
const range = editor?.selections[0];
if (range) {
cell = editor?.document.cellAt(range.start);
}
}
if (!cell) {
return;
}
if (this.notebookInProgress.has(cell.notebook)) {
return;
}
const controller = this.notebookToRunByLineController.get(cell.notebook);
if (controller && controller.debugCell.document.uri.toString() === cell.document.uri.toString()) {
controller.continue();
}
}),
this.commandManager.registerCommand(DSCommands.RunByLineStop, () => {
const editor = this.vscNotebook.activeNotebookEditor;
if (editor) {
const controller = this.notebookToRunByLineController.get(editor.document);
if (controller) {
sendTelemetryEvent(DebuggingTelemetry.endedSession, undefined, {
reason: 'withKeybinding'
});
controller.stop();
}
}
}),
this.commandManager.registerCommand(DSCommands.RunAndDebugCell, async (cell: NotebookCell | undefined) => {
sendTelemetryEvent(DebuggingTelemetry.clickedRunAndDebugCell);
const editor = this.vscNotebook.activeNotebookEditor;
if (!cell) {
const range = editor?.selections[0];
if (range) {
cell = editor?.document.cellAt(range.start);
}
}
if (!cell) {
return;
}
await this.tryToStartDebugging(KernelDebugMode.Cell, editor, cell);
})
);
}
public get onDoneDebugging(): Event<void> {
return this._doneDebugging.event;
}
public dispose() {
this.disposables.forEach((d) => d.dispose());
}
public isDebugging(notebook: NotebookDocument): boolean {
return this.notebookToDebugger.has(notebook);
}
public getDebugSession(notebook: NotebookDocument): Promise<DebugSession> | undefined {
const dbg = this.notebookToDebugger.get(notebook);
if (dbg) {
return dbg.session;
}
}
public getDebugMode(notebook: NotebookDocument): KernelDebugMode | undefined {
const controller = this.notebookToRunByLineController.get(notebook);
return controller?.getMode();
}
public getDebugCell(notebook: NotebookDocument): NotebookCell | undefined {
const controller = this.notebookToRunByLineController.get(notebook);
return controller?.debugCell;
}
public getDebugAdapter(notebook: NotebookDocument): KernelDebugAdapter | undefined {
return this.notebookToDebugAdapter.get(notebook);
}
private updateToolbar(debugging: boolean) {
this.debuggingInProgress.set(debugging).ignoreErrors();
}
private updateCellToolbar(runningByLine: boolean) {
this.runByLineInProgress.set(runningByLine).ignoreErrors();
}
private async tryToStartDebugging(mode: KernelDebugMode, editor?: NotebookEditor, cell?: NotebookCell) {
if (!editor) {
void this.appShell.showErrorMessage(DataScience.noNotebookToDebug());
return;
}
if (this.notebookInProgress.has(editor.document)) {
return;
}
if (this.isDebugging(editor.document)) {
this.updateToolbar(true);
if (mode === KernelDebugMode.RunByLine) {
this.updateCellToolbar(true);
}
return;
}
const checkIpykernelAndStart = async (allowSelectKernel = true): Promise<void> => {
const ipykernelResult = await this.checkForIpykernel6(editor.document);
switch (ipykernelResult) {
case IpykernelCheckResult.NotInstalled:
// User would have been notified about this, nothing more to do.
return;
case IpykernelCheckResult.Outdated:
case IpykernelCheckResult.Unknown: {
void this.promptInstallIpykernel6();
return;
}
case IpykernelCheckResult.Ok: {
switch (mode) {
case KernelDebugMode.Everything: {
await this.startDebugging(editor.document);
this.updateToolbar(true);
return;
}
case KernelDebugMode.Cell:
if (cell) {
await this.startDebuggingCell(editor.document, KernelDebugMode.Cell, cell);
this.updateToolbar(true);
}
return;
case KernelDebugMode.RunByLine:
if (cell) {
await this.startDebuggingCell(editor.document, KernelDebugMode.RunByLine, cell);
this.updateToolbar(true);
this.updateCellToolbar(true);
}
return;
default:
return;
}
}
case IpykernelCheckResult.ControllerNotSelected: {
if (allowSelectKernel) {
await this.commandManager.executeCommand('notebook.selectKernel', { notebookEditor: editor });
await checkIpykernelAndStart(false);
}
}
}
};
try {
this.notebookInProgress.add(editor.document);
await checkIpykernelAndStart();
} finally {
this.notebookInProgress.delete(editor.document);
}
}
private async startDebuggingCell(
doc: NotebookDocument,
mode: KernelDebugMode.Cell | KernelDebugMode.RunByLine,
cell: NotebookCell
) {
const config: IKernelDebugAdapterConfig = {
type: pythonKernelDebugAdapter,
name: path.basename(doc.uri.toString()),
request: 'attach',
justMyCode: true,
// add a property to the config to know if the session is runByLine
__mode: mode,
__cellIndex: cell.index
};
const opts: DebugSessionOptions | undefined =
mode === KernelDebugMode.RunByLine
? { debugUI: { simple: true }, suppressSaveBeforeStart: true }
: { suppressSaveBeforeStart: true };
return this.startDebuggingConfig(doc, config, opts);
}
private async startDebugging(doc: NotebookDocument) {
const config: IKernelDebugAdapterConfig = {
type: pythonKernelDebugAdapter,
name: path.basename(doc.uri.toString()),
request: 'attach',
internalConsoleOptions: 'neverOpen',
justMyCode: false,
__mode: KernelDebugMode.Everything
};
return this.startDebuggingConfig(doc, config);
}
private async startDebuggingConfig(
doc: NotebookDocument,
config: IKernelDebugAdapterConfig,
options?: DebugSessionOptions
) {
let dbg = this.notebookToDebugger.get(doc);
if (!dbg) {
dbg = new Debugger(doc, config, options);
this.notebookToDebugger.set(doc, dbg);
try {
await dbg.session;
} catch (err) {
traceError(`Can't start debugging (${err})`);
void this.appShell.showErrorMessage(DataScience.cantStartDebugging());
}
}
}
private async endSession(session: DebugSession) {
this._doneDebugging.fire();
for (const [doc, dbg] of this.notebookToDebugger.entries()) {
if (dbg && session.id === (await dbg.session).id) {
this.notebookToDebugger.delete(doc);
this.notebookToDebugAdapter.delete(doc);
this.updateToolbar(false);
this.updateCellToolbar(false);
break;
}
}
}
private async createDebugAdapterDescriptor(session: DebugSession): Promise<DebugAdapterDescriptor | undefined> {
const config = session.configuration;
assertIsDebugConfig(config);
if (this.vscNotebook.activeNotebookEditor) {
const activeDoc = this.vscNotebook.activeNotebookEditor.document;
// TODO we apparently always have a kernel here, clean up typings
const kernel = await this.ensureKernelIsRunning(activeDoc);
const debug = this.getDebuggerByUri(activeDoc);
if (debug) {
if (kernel?.session) {
debug.resolve(session);
const adapter = new KernelDebugAdapter(session, debug.document, kernel.session, this.fs, kernel);
if (config.__mode === KernelDebugMode.RunByLine && typeof config.__cellIndex === 'number') {
const cell = activeDoc.cellAt(config.__cellIndex);
const controller = new RunByLineController(
adapter,
cell,
this.commandManager,
kernel!,
this.settings
);
adapter.setDebuggingDelegate(controller);
this.notebookToRunByLineController.set(debug.document, controller);
} else if (config.__mode === KernelDebugMode.Cell && typeof config.__cellIndex === 'number') {
const cell = activeDoc.cellAt(config.__cellIndex);
const controller = new DebugCellController(adapter, cell, kernel!, this.commandManager);
adapter.setDebuggingDelegate(controller);
}
this.notebookToDebugAdapter.set(debug.document, adapter);
this.disposables.push(adapter.onDidEndSession(this.endSession.bind(this)));
return new DebugAdapterInlineImplementation(adapter);
} else {
void this.appShell.showInformationMessage(DataScience.kernelWasNotStarted());
}
}
}
traceError('Debug sessions should start only from the cell toolbar command');
return;
}
private getDebuggerByUri(document: NotebookDocument): Debugger | undefined {
for (const [doc, dbg] of this.notebookToDebugger.entries()) {
if (document === doc) {
return dbg;
}
}
}
private async ensureKernelIsRunning(doc: NotebookDocument): Promise<IKernel | undefined> {
await this.notebookControllerManager.loadNotebookControllers();
const controller = this.notebookControllerManager.getSelectedNotebookController(doc);
let kernel = this.kernelProvider.get(doc);
if (!kernel && controller) {
kernel = this.kernelProvider.getOrCreate(doc, {
metadata: controller.connection,
controller: controller?.controller,
resourceUri: doc.uri
});
}
if (kernel && kernel.status === 'unknown') {
await kernel.start();
}
return kernel;
}
private async checkForIpykernel6(doc: NotebookDocument): Promise<IpykernelCheckResult> {
try {
let kernel = this.kernelProvider.get(doc);
if (!kernel) {
const controller = this.notebookControllerManager.getSelectedNotebookController(doc);
if (!controller) {
return IpykernelCheckResult.ControllerNotSelected;
}
kernel = this.kernelProvider.getOrCreate(doc, {
metadata: controller.connection,
controller: controller?.controller,
resourceUri: doc.uri
});
}
const result = await isUsingIpykernel6OrLater(kernel);
sendTelemetryEvent(DebuggingTelemetry.ipykernel6Status, undefined, {
status: result === IpykernelCheckResult.Ok ? 'installed' : 'notInstalled'
});
return result;
} catch (ex) {
if (ex instanceof IpyKernelNotInstalledError) {
return IpykernelCheckResult.NotInstalled;
}
traceError('Debugging: Could not check for ipykernel 6', ex);
}
return IpykernelCheckResult.Unknown;
}
private async promptInstallIpykernel6() {
const response = await this.appShell.showInformationMessage(
DataScience.needIpykernel6(),
{ modal: true },
DataScience.setup()
);
if (response === DataScience.setup()) {
sendTelemetryEvent(DebuggingTelemetry.clickedOnSetup);
this.appShell.openUrl(
'https://github.com/microsoft/vscode-jupyter/wiki/Setting-Up-Run-by-Line-and-Debugging-for-Notebooks'
);
} else {
sendTelemetryEvent(DebuggingTelemetry.closedModal);
}
}
}