-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathdebuggingManagerBase.ts
206 lines (183 loc) · 7.77 KB
/
debuggingManagerBase.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import {
debug,
NotebookDocument,
workspace,
DebugSession,
DebugSessionOptions,
DebugAdapterDescriptor,
Event,
EventEmitter,
NotebookCell
} from 'vscode';
import { IKernel, IKernelProvider } from '../../kernels/types';
import { IDisposable } from '../../platform/common/types';
import { IApplicationShell, ICommandManager, IVSCodeNotebook } from '../../platform/common/application/types';
import { DebuggingTelemetry } from './constants';
import { sendTelemetryEvent } from '../../telemetry';
import { traceError, traceInfoIfCI } from '../../platform/logging';
import { DataScience } from '../../platform/common/utils/localize';
import { IKernelDebugAdapterConfig } from './debuggingTypes';
import { Debugger } from './debugger';
import { KernelDebugAdapterBase } from './kernelDebugAdapterBase';
import { IpykernelCheckResult, isUsingIpykernel6OrLater } from './helper';
import { noop } from '../../platform/common/utils/misc';
import { IControllerLoader, IControllerSelection } from '../controllers/types';
/**
* The DebuggingManager maintains the mapping between notebook documents and debug sessions.
*/
export abstract class DebuggingManagerBase implements IDisposable {
private notebookToDebugger = new Map<NotebookDocument, Debugger>();
protected notebookToDebugAdapter = new Map<NotebookDocument, KernelDebugAdapterBase>();
protected notebookInProgress = new Set<NotebookDocument>();
protected readonly disposables: IDisposable[] = [];
private _doneDebugging = new EventEmitter<void>();
public constructor(
private kernelProvider: IKernelProvider,
private readonly notebookControllerLoader: IControllerLoader,
private readonly notebookControllerSelection: IControllerSelection,
protected readonly commandManager: ICommandManager,
protected readonly appShell: IApplicationShell,
protected readonly vscNotebook: IVSCodeNotebook
) {}
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.onDidStopDebugging(document);
}
})
);
}
public getDebugCell(notebook: NotebookDocument): NotebookCell | undefined {
return this.notebookToDebugAdapter.get(notebook)?.debugCell;
}
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 getDebugAdapter(notebook: NotebookDocument): KernelDebugAdapterBase | undefined {
return this.notebookToDebugAdapter.get(notebook);
}
protected onDidStopDebugging(_notebook: NotebookDocument): void {
//
}
protected async startDebuggingConfig(
doc: NotebookDocument,
config: IKernelDebugAdapterConfig,
options?: DebugSessionOptions
) {
traceInfoIfCI(`Attempting to start debugging with config ${JSON.stringify(config)}`);
let dbg = this.notebookToDebugger.get(doc);
if (!dbg) {
dbg = new Debugger(doc, config, options);
this.notebookToDebugger.set(doc, dbg);
try {
const session = await dbg.session;
traceInfoIfCI(`Debugger session is ready. Should be debugging ${session.id} now`);
} catch (err) {
traceError(`Can't start debugging (${err})`);
this.appShell.showErrorMessage(DataScience.cantStartDebugging()).then(noop, noop);
}
} else {
traceInfoIfCI(`Not starting debugging because already debugging in this notebook`);
}
}
protected trackDebugAdapter(notebook: NotebookDocument, adapter: KernelDebugAdapterBase) {
this.notebookToDebugAdapter.set(notebook, adapter);
this.disposables.push(adapter.onDidEndSession(this.endSession.bind(this)));
}
protected async endSession(session: DebugSession) {
traceInfoIfCI(`Ending debug session ${session.id}`);
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.onDidStopDebugging(doc);
break;
}
}
}
protected abstract createDebugAdapterDescriptor(session: DebugSession): Promise<DebugAdapterDescriptor | undefined>;
protected getDebuggerByUri(document: NotebookDocument): Debugger | undefined {
for (const [doc, dbg] of this.notebookToDebugger.entries()) {
if (document === doc) {
return dbg;
}
}
}
protected async ensureKernelIsRunning(doc: NotebookDocument): Promise<IKernel | undefined> {
await this.notebookControllerLoader.loaded;
const controller = this.notebookControllerSelection.getSelected(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;
}
protected async checkForIpykernel6(doc: NotebookDocument): Promise<IpykernelCheckResult> {
try {
let kernel = this.kernelProvider.get(doc);
if (!kernel) {
const controller = this.notebookControllerSelection.getSelected(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) {
traceError('Debugging: Could not check for ipykernel 6', ex);
}
return IpykernelCheckResult.Unknown;
}
protected 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);
}
}
}