forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
368 lines (335 loc) · 12.6 KB
/
utils.ts
File metadata and controls
368 lines (335 loc) · 12.6 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
// tslint:disable:max-classes-per-file
import { expect } from 'chai';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as vscode from 'vscode';
import { DebugProtocol } from 'vscode-debugprotocol';
import { EXTENSION_ROOT_DIR } from '../../client/common/constants';
import { sleep } from '../../client/common/utils/async';
import { getDebugpyLauncherArgs } from '../../client/debugger/extension/adapter/remoteLaunchers';
import { PythonFixture } from '../fixtures';
import { Proc, ProcOutput, ProcResult } from '../proc';
const launchJSON = path.join(EXTENSION_ROOT_DIR, 'src', 'test', '.vscode', 'launch.json');
export function getConfig(name: string): vscode.DebugConfiguration {
const configs = fs.readJSONSync(launchJSON);
for (const config of configs.configurations) {
if (config.name === name) {
return config;
}
}
throw Error(`debug config "${name}" not found`);
}
type DAPSource = 'vscode' | 'debugpy';
type DAPHandler = (src: DAPSource, msg: DebugProtocol.ProtocolMessage) => void;
type TrackedDebugger = {
id: number;
output: ProcOutput;
dapHandler?: DAPHandler;
session?: vscode.DebugSession;
exitCode?: number;
};
class DebugAdapterTracker {
constructor(
// This contains all the state.
private readonly tracked: TrackedDebugger
) {}
// debugpy -> VS Code
// tslint:disable-next-line:no-any
public onDidSendMessage(message: any): void {
this.onDAPMessage('debugpy', message as DebugProtocol.ProtocolMessage);
}
// VS Code -> debugpy
// tslint:disable-next-line:no-any
public onWillReceiveMessage(message: any): void {
this.onDAPMessage('vscode', message as DebugProtocol.ProtocolMessage);
}
public onExit(code: number | undefined, signal: string | undefined): void {
if (code) {
this.tracked.exitCode = code;
} else if (signal) {
this.tracked.exitCode = 1;
} else {
this.tracked.exitCode = 0;
}
}
// The following vscode.DebugAdapterTracker methods are not implemented:
//
// * onWillStartSession(): void;
// * onWillStopSession(): void;
// * onError(error: Error): void;
private onDAPMessage(src: DAPSource, msg: DebugProtocol.ProtocolMessage) {
// Unomment this to see the DAP messages sent between VS Code and debugpy:
//console.log(`| DAP (${src === 'vscode' ? 'VS Code -> debugpy' : 'debugpy -> VS Code'})\n`, msg, '\n| DAP');
// See: https://microsoft.github.io/debug-adapter-protocol/specification
if (this.tracked.dapHandler) {
this.tracked.dapHandler(src, msg);
}
if (msg.type === 'event') {
const event = ((msg as unknown) as DebugProtocol.Event).event;
if (event === 'output') {
this.onOutputEvent((msg as unknown) as DebugProtocol.OutputEvent);
}
}
}
private onOutputEvent(msg: DebugProtocol.OutputEvent) {
if (msg.body.category === undefined) {
msg.body.category = 'stdout';
}
const data = Buffer.from(msg.body.output, 'utf-8');
if (msg.body.category === 'stdout') {
this.tracked.output.addStdout(data);
} else if (msg.body.category === 'stderr') {
this.tracked.output.addStderr(data);
} else {
// Ignore it.
}
}
}
class Debuggers {
private nextID = 0;
private tracked: { [id: number]: TrackedDebugger } = {};
private results: { [id: number]: ProcResult } = {};
public track(config: vscode.DebugConfiguration, output?: ProcOutput): number {
if (this.nextID === 0) {
vscode.debug.registerDebugAdapterTrackerFactory('python', this);
}
if (output === undefined) {
output = new ProcOutput();
}
this.nextID += 1;
const id = this.nextID;
this.tracked[id] = { id, output };
config._test_session_id = id;
return id;
}
public setDAPHandler(id: number, handler: DAPHandler) {
const tracked = this.tracked[id];
if (tracked !== undefined) {
tracked.dapHandler = handler;
}
}
public getSession(id: number): vscode.DebugSession | undefined {
const tracked = this.tracked[id];
if (tracked === undefined) {
return undefined;
} else {
return tracked.session;
}
}
public async waitUntilDone(id: number): Promise<ProcResult> {
const cachedResult = this.results[id];
if (cachedResult !== undefined) {
return cachedResult;
}
const tracked = this.tracked[id];
if (tracked === undefined) {
throw Error(`untracked debugger ${id}`);
} else {
while (tracked.exitCode === undefined) {
await sleep(10); // milliseconds
}
const result = {
exitCode: tracked.exitCode,
stdout: tracked.output.stdout
};
this.results[id] = result;
return result;
}
}
// This is for DebugAdapterTrackerFactory:
public createDebugAdapterTracker(session: vscode.DebugSession): vscode.ProviderResult<vscode.DebugAdapterTracker> {
const id = session.configuration._test_session_id;
const tracked = this.tracked[id];
if (tracked !== undefined) {
tracked.session = session;
return new DebugAdapterTracker(tracked);
} else if (id !== undefined) {
// This should not have happened, but we don't worry about
// it for now.
}
return undefined;
}
}
const debuggers = new Debuggers();
class DebuggerSession {
private started: boolean = false;
private raw: vscode.DebugSession | undefined;
private stopped: { breakpoint: boolean; threadId: number } | undefined;
constructor(
public readonly id: number,
public readonly config: vscode.DebugConfiguration,
private readonly wsRoot?: vscode.WorkspaceFolder,
private readonly proc?: Proc
) {}
public async start() {
if (this.started) {
throw Error('already started');
}
this.started = true;
// Un-comment this to see the debug config used in this session:
//console.log('|', session.config, '|');
const started = await vscode.debug.startDebugging(this.wsRoot, this.config);
expect(started).to.be.equal(true, 'Debugger did not sart');
this.raw = debuggers.getSession(this.id);
expect(this.raw).to.not.equal(undefined, 'session not set');
}
public async waitUntilDone(): Promise<ProcResult> {
if (this.proc) {
return this.proc.waitUntilDone();
} else {
return debuggers.waitUntilDone(this.id);
}
}
public addBreakpoint(filename: string, line: number, ch?: number): vscode.Breakpoint {
// The arguments are 1-indexed.
const loc = new vscode.Location(
vscode.Uri.file(filename),
// VS Code wants 0-indexed line and column numbers.
// We default to the beginning of the line.
new vscode.Position(line - 1, ch ? ch - 1 : 0)
);
const bp = new vscode.SourceBreakpoint(loc);
vscode.debug.addBreakpoints([bp]);
return bp;
}
public async waitForBreakpoint(bp: vscode.Breakpoint, opts: { clear: boolean } = { clear: true }) {
while (!this.stopped || !this.stopped.breakpoint) {
await sleep(10); // milliseconds
}
if (opts.clear) {
vscode.debug.removeBreakpoints([bp]);
await this.raw!.customRequest('continue', { threadId: this.stopped.threadId });
this.stopped = undefined;
}
}
public handleDAPMessage(_src: DAPSource, baseMsg: DebugProtocol.ProtocolMessage) {
if (baseMsg.type === 'event') {
const event = ((baseMsg as unknown) as DebugProtocol.Event).event;
if (event === 'stopped') {
const msg = (baseMsg as unknown) as DebugProtocol.StoppedEvent;
this.stopped = {
breakpoint: msg.body.reason === 'breakpoint',
threadId: (msg.body.threadId as unknown) as number
};
} else {
// For now there aren't any other events we care about.
}
} else if (baseMsg.type === 'request') {
// For now there aren't any requests we care about.
} else if (baseMsg.type === 'response') {
// For now there aren't any responses we care about.
} else {
// This shouldn't happen but for now we don't worry about it.
}
}
// The old debug adapter tests used
// 'vscode-debugadapter-testsupport'.DebugClient to interact with
// the debugger. This is helpful info when we are considering
// additional debugger-related tests. Here are the methods/props
// the old tests used:
//
// * defaultTimeout
// * start()
// * stop()
// * initializeRequest()
// * configurationSequence()
// * launch()
// * attachRequest()
// * waitForEvent()
// * assertOutput()
// * threadsRequest()
// * continueRequest()
// * scopesRequest()
// * variablesRequest()
// * setBreakpointsRequest()
// * setExceptionBreakpointsRequest()
// * assertStoppedLocation()
}
export class DebuggerFixture extends PythonFixture {
public resolveDebugger(
configName: string,
file: string,
scriptArgs: string[],
wsRoot?: vscode.WorkspaceFolder
): DebuggerSession {
const config = getConfig(configName);
let proc: Proc | undefined;
if (config.request === 'launch') {
config.program = file;
config.args = scriptArgs;
config.redirectOutput = false;
// XXX set the file in the current vscode editor?
} else if (config.request === 'attach') {
if (config.port) {
proc = this.runDebugger(config.port, file, ...scriptArgs);
if (wsRoot && config.name === 'attach to a local port') {
config.pathMappings.localRoot = wsRoot.uri.fsPath;
}
} else if (config.processId) {
proc = this.runScript(file, ...scriptArgs);
config.processId = proc.pid;
} else {
throw Error(`unsupported attach config "${configName}"`);
}
if (wsRoot && config.pathMappings) {
config.pathMappings.localRoot = wsRoot.uri.fsPath;
}
} else {
throw Error(`unsupported request type "${config.request}"`);
}
const id = debuggers.track(config);
const session = new DebuggerSession(id, config, wsRoot, proc);
debuggers.setDAPHandler(id, (src, msg) => session.handleDAPMessage(src, msg));
return session;
}
public getLaunchTarget(filename: string, args: string[]): vscode.DebugConfiguration {
return {
type: 'python',
name: 'debug',
request: 'launch',
program: filename,
args: args,
console: 'integratedTerminal'
};
}
public getAttachTarget(filename: string, args: string[], port?: number): vscode.DebugConfiguration {
if (port) {
this.runDebugger(port, filename, ...args);
return {
type: 'python',
name: 'debug',
request: 'attach',
port: port,
host: 'localhost',
pathMappings: [
{
// tslint:disable-next-line:no-invalid-template-strings
localRoot: '${workspaceFolder}',
remoteRoot: '.'
}
]
};
} else {
const proc = this.runScript(filename, ...args);
return {
type: 'python',
name: 'debug',
request: 'attach',
processId: proc.pid
};
}
}
public runDebugger(port: number, filename: string, ...scriptArgs: string[]) {
const args = getDebugpyLauncherArgs({
host: 'localhost',
port: port,
// This causes problems if we set it to true.
waitUntilDebuggerAttaches: false
});
args.push(filename, ...scriptArgs);
return this.runScript(args[0], ...args.slice(1));
}
}