forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoDebugConfiguration.ts
572 lines (531 loc) · 22 KB
/
goDebugConfiguration.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
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-prototype-builtins */
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import { lstatSync } from 'fs';
import path = require('path');
import vscode = require('vscode');
import semver = require('semver');
import { extensionId } from './const';
import { getGoConfig } from './config';
import { toolExecutionEnvironment } from './goEnv';
import {
declinedToolInstall,
inspectGoToolVersion,
installTools,
promptForMissingTool,
promptForUpdatingTool,
shouldUpdateTool
} from './goInstallTools';
import { extensionInfo } from './config';
import { packagePathToGoModPathMap } from './goModules';
import { getToolAtVersion } from './goTools';
import { pickGoProcess, pickProcess, pickProcessByName } from './pickProcess';
import { getFromGlobalState, updateGlobalState } from './stateUtils';
import { getBinPath, getGoVersion } from './util';
import { parseArgsString } from './utils/argsUtil';
import { parseEnvFiles } from './utils/envUtils';
import { resolveHomeDir } from './utils/pathUtils';
import { createRegisterCommand } from './commands';
import { GoExtensionContext } from './context';
let dlvDAPVersionChecked = false;
export class GoDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
static activate(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) {
ctx.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider('go', new GoDebugConfigurationProvider('go'))
);
const registerCommand = createRegisterCommand(ctx, goCtx);
registerCommand('go.debug.pickProcess', () => pickProcess);
registerCommand('go.debug.pickGoProcess', () => pickGoProcess);
}
constructor(private defaultDebugAdapterType: string = 'go') {}
public async provideDebugConfigurations(
_folder: vscode.WorkspaceFolder | undefined,
_token?: vscode.CancellationToken
): Promise<vscode.DebugConfiguration[] | undefined> {
return await this.pickConfiguration();
}
public async pickConfiguration(): Promise<vscode.DebugConfiguration[]> {
const debugConfigurations = [
{
label: 'Go: Launch Package',
description: 'Debug/test the package of the open file',
config: {
name: 'Launch Package',
type: this.defaultDebugAdapterType,
request: 'launch',
mode: 'auto',
program: '${fileDirname}'
}
},
{
label: 'Go: Attach to local process',
description: 'Attach to an existing process by process ID',
config: {
name: 'Attach to Process',
type: 'go',
request: 'attach',
mode: 'local',
processId: 0
}
},
{
label: 'Go: Connect to server',
description: 'Connect to a remote headless debug server',
config: {
name: 'Connect to server',
type: 'go',
request: 'attach',
mode: 'remote',
remotePath: '${workspaceFolder}',
port: 2345,
host: '127.0.0.1'
},
fill: async (config: vscode.DebugConfiguration) => {
const host = await vscode.window.showInputBox({
prompt: 'Enter hostname',
value: '127.0.0.1'
});
if (host) {
config.host = host;
}
const port = Number(
await vscode.window.showInputBox({
prompt: 'Enter port',
value: '2345',
validateInput: (value: string) => {
if (isNaN(Number(value))) {
return 'Please enter a number.';
}
return '';
}
})
);
if (port) {
config.port = port;
}
}
}
];
const choice = await vscode.window.showQuickPick(debugConfigurations, {
placeHolder: 'Choose debug configuration'
});
if (!choice) {
return [];
}
if (choice.fill) {
await choice.fill(choice.config);
}
return [choice.config];
}
public async resolveDebugConfiguration(
folder: vscode.WorkspaceFolder | undefined,
debugConfiguration: vscode.DebugConfiguration,
_token?: vscode.CancellationToken
): Promise<vscode.DebugConfiguration | undefined> {
const activeEditor = vscode.window.activeTextEditor;
if (!debugConfiguration || !debugConfiguration.request) {
// if 'request' is missing interpret this as a missing launch.json
if (!activeEditor || activeEditor.document.languageId !== 'go') {
return;
}
debugConfiguration = Object.assign(debugConfiguration || {}, {
name: 'Launch',
type: this.defaultDebugAdapterType,
request: 'launch',
mode: 'auto',
program: path.dirname(activeEditor.document.fileName) // matches ${fileDirname}
});
}
if (!debugConfiguration.type) {
debugConfiguration['type'] = this.defaultDebugAdapterType;
}
if (!debugConfiguration['mode']) {
if (debugConfiguration.request === 'launch') {
// 'auto' will decide mode by checking file extensions later
debugConfiguration['mode'] = 'auto';
} else if (debugConfiguration.request === 'attach') {
debugConfiguration['mode'] = 'local';
}
}
debugConfiguration['packagePathToGoModPathMap'] = packagePathToGoModPathMap;
const goConfig = getGoConfig(folder && folder.uri);
const dlvConfig = goConfig['delveConfig'];
const defaultConfig = vscode.extensions.getExtension(extensionId)?.packageJSON.contributes.configuration
.properties['go.delveConfig'].properties;
// Figure out which debugAdapter is being used first, so we can use this to send warnings
// for properties that don't apply.
// If debugAdapter is not provided in launch.json, see if it's in settings.json.
if (!debugConfiguration.hasOwnProperty('debugAdapter') && dlvConfig.hasOwnProperty('debugAdapter')) {
const { globalValue, workspaceValue } = goConfig.inspect('delveConfig.debugAdapter') ?? {};
// user configured the default debug adapter through settings.json.
if (globalValue !== undefined || workspaceValue !== undefined) {
debugConfiguration['debugAdapter'] = dlvConfig['debugAdapter'];
}
}
// If neither launch.json nor settings.json gave us the debugAdapter value, we go with the default
// from package.json (dlv-dap).
if (!debugConfiguration['debugAdapter']) {
debugConfiguration['debugAdapter'] = defaultConfig.debugAdapter.default;
if (
debugConfiguration.request === 'attach' &&
debugConfiguration['mode'] === 'remote' &&
!extensionInfo.isPreview
) {
this.showWarning(
'ignoreDefaultDebugAdapterChangeWarning',
"We are using the 'dlv-dap' integration for remote debugging by default. Please comment on [issue 3096](https://github.com/golang/vscode-go/issues/3096) if this impacts your workflows."
);
}
}
if (debugConfiguration['debugAdapter'] === 'legacy') {
this.showWarning(
'ignoreLegacyDADeprecationWarning',
'Legacy debug adapter is deprecated. Please comment on [issue 3096](https://github.com/golang/vscode-go/issues/3096) if this impacts your workflows.'
);
}
if (
debugConfiguration['debugAdapter'] === 'dlv-dap' &&
debugConfiguration.request === 'launch' &&
debugConfiguration['port']
) {
this.showWarning(
'ignorePortUsedInDlvDapWarning',
"`port` with 'dlv-dap' debugAdapter connects to [a `dlv dap` server](https://github.com/golang/vscode-go/wiki/debugging#run-debugee-externally) to launch a program or attach to a process. Remove 'host'/'port' from your launch.json configuration if you have not launched a 'dlv dap' server."
);
}
const debugAdapter = debugConfiguration['debugAdapter'] === 'dlv-dap' ? 'dlv-dap' : 'dlv';
let useApiV1 = false;
if (debugConfiguration.hasOwnProperty('useApiV1')) {
useApiV1 = debugConfiguration['useApiV1'] === true;
} else if (dlvConfig.hasOwnProperty('useApiV1')) {
useApiV1 = dlvConfig['useApiV1'] === true;
}
if (useApiV1) {
debugConfiguration['apiVersion'] = 1;
}
if (!debugConfiguration.hasOwnProperty('apiVersion') && dlvConfig.hasOwnProperty('apiVersion')) {
debugConfiguration['apiVersion'] = dlvConfig['apiVersion'];
}
if (
debugAdapter === 'dlv-dap' &&
(debugConfiguration.hasOwnProperty('dlvLoadConfig') ||
goConfig.inspect('delveConfig.dlvLoadConfig')?.globalValue !== undefined ||
goConfig.inspect('delveConfig.dlvLoadConfig')?.workspaceValue !== undefined)
) {
this.showWarning(
'ignoreDebugDlvConfigWithDlvDapWarning',
"'dlvLoadConfig' is deprecated with dlv-dap debug adapter.\n\nDlv-dap loads composite data on demand and uses increased string limits on source code hover, in Debug Console and via Copy Value. Please file an issue if these are not sufficient for your use case."
);
}
// Reflect the defaults set through go.delveConfig setting.
const dlvProperties = [
'showRegisters',
'showGlobalVariables',
'substitutePath',
'showLog',
'logOutput',
'dlvFlags',
'hideSystemGoroutines'
];
if (debugAdapter !== 'dlv-dap') {
dlvProperties.push('dlvLoadConfig');
}
dlvProperties.forEach((p) => {
if (!debugConfiguration.hasOwnProperty(p)) {
if (dlvConfig.hasOwnProperty(p)) {
debugConfiguration[p] = dlvConfig[p];
} else {
debugConfiguration[p] = defaultConfig[p]?.default;
}
}
});
if (debugAdapter !== 'dlv-dap' && debugConfiguration.request === 'attach' && !debugConfiguration['cwd']) {
debugConfiguration['cwd'] = '${workspaceFolder}';
if (vscode.workspace.workspaceFolders?.length ?? 0 > 1) {
debugConfiguration['cwd'] = '${fileWorkspaceFolder}';
}
}
if (debugConfiguration['cwd']) {
// expand 'cwd' folder path containing '~', which would cause dlv to fail
debugConfiguration['cwd'] = resolveHomeDir(debugConfiguration['cwd']);
}
const dlvToolPath = getBinPath('dlv');
if (!path.isAbsolute(dlvToolPath)) {
// If user has not already declined to install this tool,
// prompt for it. Otherwise continue and have the lack of
// dlv binary be caught later.
if (!declinedToolInstall('dlv')) {
await promptForMissingTool('dlv');
return;
}
}
debugConfiguration['dlvToolPath'] = dlvToolPath;
// Remove any '--gcflags' entries and show a warning
if (debugConfiguration['buildFlags']) {
let flags = await maybeJoinFlags(dlvToolPath, debugConfiguration['buildFlags']);
if (typeof flags === 'string') {
const resp = this.removeGcflags(flags);
if (resp.removed) {
flags = resp.args;
this.showWarning(
'ignoreDebugGCFlagsWarning',
"User specified build flag '-gcflags' in 'buildFlags' is being ignored (see [debugging with build flags](https://github.com/golang/vscode-go/blob/master/docs/debugging.md#specifying-other-build-flags) documentation)"
);
}
}
debugConfiguration['buildFlags'] = flags;
}
if (debugConfiguration['env'] && debugConfiguration['env']['GOFLAGS']) {
const resp = this.removeGcflags(debugConfiguration['env']['GOFLAGS']);
if (resp.removed) {
debugConfiguration['env']['GOFLAGS'] = resp.args;
this.showWarning(
'ignoreDebugGCFlagsWarning',
"User specified build flag '-gcflags' in 'GOFLAGS' is being ignored (see [debugging with build flags](https://github.com/golang/vscode-go/blob/master/docs/debugging.md#specifying-other-build-flags) documentation)"
);
}
}
// For dlv-dap mode, check if the dlv is recent enough to support DAP.
if (debugAdapter === 'dlv-dap' && !dlvDAPVersionChecked) {
const tool = getToolAtVersion('dlv');
if (await shouldUpdateTool(tool, dlvToolPath)) {
// If the user has opted in to automatic tool updates, we can update
// without prompting.
const toolsManagementConfig = getGoConfig()['toolsManagement'];
if (toolsManagementConfig && toolsManagementConfig['autoUpdate'] === true) {
const goVersion = await getGoVersion();
await installTools([tool], goVersion, true);
} else {
await promptForUpdatingTool(tool.name);
}
// installTools could've failed (e.g. no network access) or the user decliend to install dlv
// in promptForUpdatingTool. If dlv doesn't exist or dlv is too old to have MVP features,
// the failure will be visible to users when launching the dlv process (crash or error message).
}
dlvDAPVersionChecked = true;
}
if (debugConfiguration['mode'] === 'auto') {
let filename = activeEditor?.document?.fileName;
if (debugConfiguration['program'] && debugConfiguration['program'].endsWith('.go')) {
// If the 'program' attribute is a file, not a directory, then we will determine the mode from that
// file path instead of the currently active file.
filename = debugConfiguration['program'];
}
debugConfiguration['mode'] = filename?.endsWith('_test.go') ? 'test' : 'debug';
}
if (debugConfiguration['mode'] === 'test' && debugConfiguration['program'].endsWith('_test.go')) {
// Running a test file in file mode does not make sense, so change the program
// to the directory.
debugConfiguration['program'] = path.dirname(debugConfiguration['program']);
}
if (debugConfiguration.request === 'launch' && debugConfiguration['mode'] === 'remote') {
this.showWarning(
'ignoreDebugLaunchRemoteWarning',
"Request type of 'launch' with mode 'remote' is deprecated, please use request type 'attach' with mode 'remote' instead."
);
}
if (
debugAdapter !== 'dlv-dap' &&
debugConfiguration.request === 'attach' &&
debugConfiguration['mode'] === 'remote' &&
debugConfiguration['program']
) {
this.showWarning(
'ignoreUsingRemotePathAndProgramWarning',
"Request type of 'attach' with mode 'remote' does not work with 'program' attribute, please use 'cwd' attribute instead."
);
}
if (debugConfiguration.request === 'attach' && debugConfiguration['mode'] === 'local') {
if (!debugConfiguration['processId'] || debugConfiguration['processId'] === 0) {
// The processId is not valid, offer a quickpick menu of all processes.
debugConfiguration['processId'] = await pickProcess();
} else if (
typeof debugConfiguration['processId'] === 'string' &&
debugConfiguration['processId'] !== '${command:pickProcess}' &&
debugConfiguration['processId'] !== '${command:pickGoProcess}'
) {
debugConfiguration['processId'] = await pickProcessByName(debugConfiguration['processId']);
}
}
return debugConfiguration;
}
public removeGcflags(args: string): { args: string; removed: boolean } {
// From `go help build`
// ...
// -gcflags '[pattern=]arg list'
// arguments to pass on each go tool compile invocation.
//
// The -asmflags, -gccgoflags, -gcflags, and -ldflags flags accept a
// space-separated list of arguments to pass to an underlying tool
// during the build. To embed spaces in an element in the list, surround
// it with either single or double quotes. The argument list may be
// preceded by a package pattern and an equal sign, which restricts
// the use of that argument list to the building of packages matching
// that pattern (see 'go help packages' for a description of package
// patterns). Without a pattern, the argument list applies only to the
// packages named on the command line. The flags may be repeated
// with different patterns in order to specify different arguments for
// different sets of packages. If a package matches patterns given in
// multiple flags, the latest match on the command line wins.
// For example, 'go build -gcflags=-S fmt' prints the disassembly
// only for package fmt, while 'go build -gcflags=all=-S fmt'
// prints the disassembly for fmt and all its dependencies.
// Regexp Explanation:
// 1. (^|\s): the flag is preceded by a white space or is at the start of the line.
// 2. -gcflags: the name of the flag.
// 3. (=| ): the name of the flag is followed by = or a space.
// 4. ('[^']*'|"[^"]*"|[^'"\s]+)+: the value of the flag is a combination of nonwhitespace
// characters and quoted strings which may contain white space.
const gcflagsRegexp = /(^|\s)(-gcflags)(=| )('[^']*'|"[^"]*"|[^'"\s]+)+/;
let removed = false;
while (args.search(gcflagsRegexp) >= 0) {
args = args.replace(gcflagsRegexp, '');
removed = true;
}
return { args, removed };
}
public resolveDebugConfigurationWithSubstitutedVariables(
folder: vscode.WorkspaceFolder | undefined,
debugConfiguration: vscode.DebugConfiguration,
_token?: vscode.CancellationToken
): vscode.DebugConfiguration | null {
const debugAdapter = debugConfiguration['debugAdapter'];
if (debugAdapter === '') {
return null;
}
// Read debugConfiguration.envFile and
// combine the environment variables from all the env files and
// debugConfiguration.env.
// We also unset 'envFile' from the user-suppled debugConfiguration
// because it is already applied.
//
// For legacy mode, we merge the environment variables on top of
// the tools execution environment variables and update the debugConfiguration
// because VS Code directly handles launch of the legacy debug adapter.
// For dlv-dap mode, we do not merge process.env environment
// variables here to reduce the number of environment variables passed
// as launch/attach parameters.
const mergeProcessEnv = debugAdapter === 'legacy';
const goToolsEnvVars = toolExecutionEnvironment(folder?.uri, mergeProcessEnv);
const fileEnvs = debugConfiguration['envFile']
? parseEnvFiles(debugConfiguration['envFile'], toolExecutionEnvironment(folder?.uri))
: parseEnvFiles(debugConfiguration['envFile'], goToolsEnvVars);
const env = debugConfiguration['env'] || {};
debugConfiguration['env'] = Object.assign(goToolsEnvVars, fileEnvs, env);
debugConfiguration['envFile'] = undefined; // unset, since we already processed.
const entriesWithRelativePaths = ['cwd', 'output', 'program'].filter(
(attr) => debugConfiguration[attr] && !path.isAbsolute(debugConfiguration[attr])
);
if (debugAdapter === 'dlv-dap') {
// 1. Relative paths -> absolute paths
if (entriesWithRelativePaths.length > 0) {
const workspaceRoot = folder?.uri.fsPath;
if (workspaceRoot) {
entriesWithRelativePaths.forEach((attr) => {
debugConfiguration[attr] = path.join(workspaceRoot, debugConfiguration[attr]);
});
} else {
this.showWarning(
'relativePathsWithoutWorkspaceFolder',
'Behavior when using relative paths without a workspace folder for `cwd`, `program`, or `output` is undefined.'
);
}
}
// 2. For launch debug/test modes that builds the debug target,
// delve needs to be launched from the right directory (inside the main module of the target).
// Compute the launch dir heuristically, and translate the dirname in program to a path relative to buildDir.
// We skip this step when working with externally launched debug adapter
// because we do not control the adapter's launch process.
if (debugConfiguration.request === 'launch') {
const mode = debugConfiguration['mode'] || 'debug';
if (['debug', 'test', 'auto'].includes(mode)) {
// Massage config to build the target from the package directory
// with a relative path. (https://github.com/golang/vscode-go/issues/1713)
// parseDebugProgramArgSync will throw an error if `program` is invalid.
const { program, dirname, programIsDirectory } = parseDebugProgramArgSync(
debugConfiguration['program']
);
if (
dirname &&
// Presence of the following attributes indicates externally launched debug adapter.
// Don't mess with 'program' if the debug adapter was launched externally.
!debugConfiguration.port &&
!debugConfiguration.debugServer
) {
debugConfiguration['__buildDir'] = dirname;
debugConfiguration['program'] = programIsDirectory
? '.'
: '.' + path.sep + path.relative(dirname, program);
}
}
}
}
// convert args string into string array if needed
if (debugConfiguration.request === 'launch' && typeof debugConfiguration['args'] === 'string') {
const argsOrErrorMsg = parseArgsString(debugConfiguration['args']);
if (typeof argsOrErrorMsg === 'string') {
throw new Error(argsOrErrorMsg);
} else {
debugConfiguration['args'] = argsOrErrorMsg;
}
}
if (debugConfiguration.request === 'attach' && debugConfiguration['mode'] === 'local') {
// processId needs to be an int, but the substituted variables from pickGoProcess and pickProcess
// become a string. Convert any strings to integers.
if (typeof debugConfiguration['processId'] === 'string') {
debugConfiguration['processId'] = parseInt(debugConfiguration['processId'], 10);
}
}
return debugConfiguration;
}
private showWarning(ignoreWarningKey: string, warningMessage: string) {
const ignoreWarning = getFromGlobalState(ignoreWarningKey);
if (ignoreWarning) {
return;
}
const neverAgain = { title: "Don't Show Again" };
vscode.window.showWarningMessage(warningMessage, neverAgain).then((result) => {
if (result === neverAgain) {
updateGlobalState(ignoreWarningKey, true);
}
});
}
}
// exported for testing.
export async function maybeJoinFlags(dlvToolPath: string, flags: string | string[]) {
const { moduleVersion } = await inspectGoToolVersion(dlvToolPath);
const localVersion = semver.parse(moduleVersion, { includePrerelease: true });
if (typeof flags !== 'string' && (!localVersion || semver.lt(localVersion, '1.22.2'))) {
flags = flags.join(' ');
}
return flags;
}
// parseDebugProgramArgSync parses program arg of debug/auto/test launch requests.
export function parseDebugProgramArgSync(
program: string
): { program: string; dirname: string; programIsDirectory: boolean } {
if (!program) {
throw new Error('The program attribute is missing in the debug configuration in launch.json');
}
try {
const pstats = lstatSync(program);
if (pstats.isDirectory()) {
return { program, dirname: program, programIsDirectory: true };
}
const ext = path.extname(program);
if (ext === '.go') {
// TODO(hyangah): .s?
return { program, dirname: path.dirname(program), programIsDirectory: false };
}
} catch (e) {
console.log(`parseDebugProgramArgSync failed: ${e}`);
}
// shouldn't reach here if program was a valid directory or .go file.
throw new Error(
`The program attribute '${program}' must be a valid directory or .go file in debug/test/auto modes.`
);
}