-
Notifications
You must be signed in to change notification settings - Fork 288
/
nodeDebugConfigurationResolver.ts
496 lines (435 loc) · 15.3 KB
/
nodeDebugConfigurationResolver.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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import { promises as fs } from 'fs';
import { inject, injectable } from 'inversify';
import * as path from 'path';
import * as vscode from 'vscode';
import { CancellationToken } from 'vscode';
import { writeToConsole } from '../../common/console';
import { DebugType } from '../../common/contributionUtils';
import { EnvironmentVars } from '../../common/environmentVars';
import { findOpenPort } from '../../common/findOpenPort';
import { existsInjected, IFsUtils, LocalFsUtils } from '../../common/fsUtils';
import { forceForwardSlashes, isSubpathOrEqualTo } from '../../common/pathUtils';
import { some } from '../../common/promiseUtil';
import { getNormalizedBinaryName, nearestDirectoryWhere } from '../../common/urlUtils';
import {
AnyNodeConfiguration,
applyNodeDefaults,
baseDefaults,
breakpointLanguages,
resolveVariableInConfig,
ResolvingNodeAttachConfiguration,
ResolvingNodeLaunchConfiguration,
} from '../../configuration';
import { ExtensionContext } from '../../ioc-extras';
import { INvmResolver } from '../../targets/node/nvmResolver';
import { fixInspectFlags } from '../configurationUtils';
import { resolveProcessId } from '../processPicker';
import { BaseConfigurationResolver } from './baseConfigurationResolver';
type ResolvingNodeConfiguration =
| ResolvingNodeAttachConfiguration
| ResolvingNodeLaunchConfiguration;
/**
* Configuration provider for node debugging. In order to allow for a
* close to 1:1 drop-in, this is nearly identical to the original vscode-
* node-debug, with support for some legacy options (mern, useWSL) removed.
*/
@injectable()
export class NodeConfigurationResolver extends BaseConfigurationResolver<AnyNodeConfiguration> {
constructor(
@inject(ExtensionContext) context: vscode.ExtensionContext,
@inject(INvmResolver) private readonly nvmResolver: INvmResolver,
@inject(IFsUtils) private readonly fsUtils: LocalFsUtils,
) {
super(context);
}
/**
* @inheritdoc
*/
public async resolveDebugConfigurationWithSubstitutedVariables(
folder: vscode.WorkspaceFolder | undefined,
rawConfig: vscode.DebugConfiguration,
): Promise<vscode.DebugConfiguration | undefined> {
const config = rawConfig as AnyNodeConfiguration;
if (
config.type === DebugType.Node &&
config.request === 'attach' &&
typeof config.processId === 'string'
) {
await resolveProcessId(this.fsUtils, config);
}
// check that the cwd is valid to avoid mysterious ENOENTs (vscode#133310)
if (config.cwd) {
const stats = await existsInjected(fs, config.cwd);
if (!stats) {
vscode.window.showErrorMessage(
l10n.t('The configured `cwd` {0} does not exist.', config.cwd),
{ modal: true },
);
return;
}
if (!stats.isDirectory()) {
vscode.window.showErrorMessage(
l10n.t('The configured `cwd` {0} is not a folder.', config.cwd),
{ modal: true },
);
return;
}
}
return config;
}
/**
* @override
*/
protected async resolveDebugConfigurationAsync(
folder: vscode.WorkspaceFolder | undefined,
config: ResolvingNodeConfiguration,
cancellationToken: CancellationToken,
): Promise<AnyNodeConfiguration | undefined> {
if (!config.name && !config.type && !config.request) {
config = await createLaunchConfigFromContext(folder, true, config);
if (config.request === 'launch' && !config.program) {
vscode.window.showErrorMessage(l10n.t('Cannot find a program to debug'), { modal: true });
return;
}
}
// make sure that config has a 'cwd' attribute set
if (!config.cwd) {
config.cwd =
config.localRoot || // https://github.com/microsoft/vscode-js-debug/issues/894#issuecomment-745449195
guessWorkingDirectory(config.request === 'launch' ? config.program : undefined, folder);
}
// if a 'remoteRoot' is specified without a corresponding 'localRoot', set 'localRoot' to the workspace folder.
// see https://github.com/Microsoft/vscode/issues/63118
if (config.remoteRoot && !config.localRoot) {
config.localRoot = '${workspaceFolder}';
}
if (config.request === 'launch') {
// custom node install
this.applyDefaultRuntimeExecutable(config);
// Deno does not support NODE_OPTIONS, so if we see it, try to set the
// necessary options automatically.
if (
config.runtimeExecutable &&
getNormalizedBinaryName(config.runtimeExecutable) === 'deno'
) {
// If the user manually set up attachSimplePort, do nothing.
if (!config.attachSimplePort) {
const port = await findOpenPort();
config.attachSimplePort = port;
config.continueOnAttach ??= true;
const runtimeArgs = [`--inspect-brk=127.0.0.1:${port}`, '--allow-all'];
if (!config.runtimeArgs) {
config.runtimeArgs = ['run', ...runtimeArgs];
} else if (!config.runtimeArgs.includes('run')) {
config.runtimeArgs = ['run', ...runtimeArgs, ...config.runtimeArgs];
} else {
config.runtimeArgs = [...config.runtimeArgs, ...runtimeArgs];
}
}
}
// nvm support
const nvmVersion = config.runtimeVersion;
if (typeof nvmVersion === 'string' && nvmVersion !== 'default') {
const { directory, binary } = await this.nvmResolver.resolveNvmVersionPath(nvmVersion);
config.env = new EnvironmentVars(config.env).addToPath(directory, 'prepend', true).value;
config.runtimeExecutable =
!config.runtimeExecutable || config.runtimeExecutable === 'node'
? binary
: config.runtimeExecutable;
}
// when using "integratedTerminal" ensure that debug console doesn't get activated; see https://github.com/Microsoft/vscode/issues/43164
if (config.console === 'integratedTerminal' && !config.internalConsoleOptions) {
config.internalConsoleOptions = 'neverOpen';
}
// assign a random debug port if requested, otherwise remove manual
// --inspect-brk flags, which are no longer needed and interfere
if (config.attachSimplePort === null || config.attachSimplePort === undefined) {
fixInspectFlags(config);
} else {
if (config.attachSimplePort === 0) {
config.attachSimplePort = await findOpenPort(undefined, cancellationToken);
const arg = `--inspect-brk=${config.attachSimplePort}`;
config.runtimeArgs = config.runtimeArgs ? [...config.runtimeArgs, arg] : [arg];
}
config.continueOnAttach = !config.stopOnEntry;
config.stopOnEntry = false; // handled by --inspect-brk
}
// update outfiles to the nearest package root
await guessOutFiles(this.fsUtils, folder, config);
}
return applyNodeDefaults(config);
}
protected getType() {
return DebugType.Node as const;
}
/**
* @override
*/
protected getSuggestedWorkspaceFolders(config: AnyNodeConfiguration) {
return [config.rootPath, config.cwd];
}
}
export function guessWorkingDirectory(program?: string, folder?: vscode.WorkspaceFolder): string {
if (folder) {
return folder.uri.fsPath;
}
// no folder -> config is a user or workspace launch config
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
return vscode.workspace.workspaceFolders[0].uri.fsPath;
}
// no folder case
if (program) {
if (program === '${file}') {
return '${fileDirname}';
}
// program is some absolute path
if (path.isAbsolute(program)) {
// derive 'cwd' from 'program'
return path.dirname(program);
}
}
// last resort
return '${workspaceFolder}';
}
function getAbsoluteLocation(folder: vscode.WorkspaceFolder | undefined, relpath: string) {
if (folder) {
relpath = resolveVariableInConfig(relpath, 'workspaceFolder', folder.uri.fsPath);
}
if (vscode.workspace.workspaceFolders?.length) {
relpath = resolveVariableInConfig(
relpath,
'workspaceRoot',
vscode.workspace.workspaceFolders[0].uri.fsPath,
);
}
if (path.isAbsolute(relpath)) {
return relpath;
}
if (folder) {
return path.join(folder.uri.fsPath, relpath);
}
return undefined;
}
/**
* Set the outFiles to the nearest package.json-containing folder relative
* to the program, if it's not already included in the workspace folder.
*
* This used to narrow (#326), but I think this is undesirable behavior for
* most users (vscode#142641), so now it only widens the `outFiles`.
*/
async function guessOutFiles(
fsUtils: LocalFsUtils,
folder: vscode.WorkspaceFolder | undefined,
config: ResolvingNodeLaunchConfiguration,
) {
if (config.outFiles || !folder) {
return;
}
let programLocation: string | undefined;
if (config.program) {
programLocation = getAbsoluteLocation(folder, config.program);
if (programLocation) {
programLocation = path.dirname(programLocation);
}
} else if (config.cwd) {
programLocation = getAbsoluteLocation(folder, config.cwd);
}
if (!programLocation || isSubpathOrEqualTo(folder.uri.fsPath, programLocation)) {
return;
}
const root = await nearestDirectoryWhere(
programLocation,
async p => !p.includes('node_modules') && (await fsUtils.exists(path.join(p, 'package.json'))),
);
if (root) {
const rel = forceForwardSlashes(path.relative(folder.uri.fsPath, root));
if (rel.length) {
config.outFiles = [
...baseDefaults.outFiles,
`\${workspaceFolder}/${rel}/**/*.js`,
`!\${workspaceFolder}/${rel}/**/node_modules/**`,
];
}
}
}
interface ITSConfig {
compilerOptions?: {
outDir: string;
};
}
interface IPartialPackageJson {
name?: string;
main?: string;
scripts?: { [key: string]: string };
}
const commonEntrypoints = ['index.js', 'main.js'];
export async function createLaunchConfigFromContext(
folder: vscode.WorkspaceFolder | undefined,
resolve: boolean,
existingConfig?: ResolvingNodeConfiguration,
): Promise<ResolvingNodeConfiguration> {
const config: ResolvingNodeConfiguration = {
type: DebugType.Node,
request: 'launch',
name: l10n.t('Launch Program'),
skipFiles: ['<node_internals>/**'],
};
if (existingConfig && existingConfig.noDebug) {
config.noDebug = true;
}
const pkg = await loadJSON<IPartialPackageJson>(folder, 'package.json');
let program: string | undefined;
let useSourceMaps = false;
if (pkg && pkg.name === 'mern-starter') {
if (resolve) {
writeToConsole(l10n.t("Launch configuration for '{0}' project created.", 'Mern Starter'));
}
configureMern(config);
return config;
}
if (pkg) {
// try to find a value for 'program' by analysing package.json
program = await guessProgramFromPackage(folder, pkg, resolve);
if (program && resolve) {
writeToConsole(l10n.t("Launch configuration created based on 'package.json'."));
}
}
if (!program) {
// try to use file open in editor
const editor = vscode.window.activeTextEditor;
if (editor && breakpointLanguages.includes(editor.document.languageId)) {
useSourceMaps = editor.document.languageId !== 'javascript';
program = folder
? path.relative(folder.uri.fsPath, editor.document.uri.fsPath)
: editor.document.uri.fsPath;
if (!path.isAbsolute(program)) {
// we don't use path.join here since it destroys the workspaceFolder with ../ (vscode#125796)
program = '${workspaceFolder}' + path.sep + program;
}
}
}
if (!program && folder) {
const basePath = folder.uri.fsPath;
program = await some(
commonEntrypoints.map(
async file =>
(await existsInjected(fs, path.join(basePath, file))) &&
'${workspaceFolder}' + path.sep + file,
),
);
}
// just use `${file}` which'll prompt the user to open an active file
if (!program) {
program = '${file}';
}
if (program) {
config.program = program;
if (!folder) {
config.__workspaceFolder = path.dirname(program);
}
}
// prepare for source maps by adding 'outFiles' if typescript or coffeescript is detected
if (
useSourceMaps ||
vscode.workspace.textDocuments.some(document => isTranspiledLanguage(document.languageId))
) {
if (resolve) {
writeToConsole(
l10n.t(
"Adjust glob pattern(s) in the 'outFiles' attribute so that they cover the generated JavaScript.",
),
);
}
let dir = '';
const tsConfig = await loadJSON<ITSConfig>(folder, 'tsconfig.json');
if (tsConfig?.compilerOptions?.outDir && canDetectTsBuildTask()) {
const outDir = tsConfig.compilerOptions.outDir;
if (!path.isAbsolute(outDir)) {
dir = outDir;
if (dir.indexOf('./') === 0) {
dir = dir.substr(2);
}
if (dir[dir.length - 1] !== '/') {
dir += '/';
}
}
config.preLaunchTask = 'tsc: build - tsconfig.json';
}
config['outFiles'] = ['${workspaceFolder}/' + dir + '**/*.js'];
}
return config;
}
function canDetectTsBuildTask() {
const value = vscode.workspace.getConfiguration().get('typescript.tsc.autoDetect');
return value !== 'off' && value !== 'watch';
}
function configureMern(config: ResolvingNodeConfiguration) {
if (config.request !== 'launch') {
return;
}
config.runtimeExecutable = 'nodemon';
config.program = '${workspaceFolder}/index.js';
config.restart = true;
config.env = { BABEL_DISABLE_CACHE: '1', NODE_ENV: 'development' };
config.console = 'integratedTerminal';
config.internalConsoleOptions = 'neverOpen';
}
function isTranspiledLanguage(languagId: string): boolean {
return languagId === 'typescript' || languagId === 'coffeescript';
}
async function loadJSON<T>(
folder: vscode.WorkspaceFolder | undefined,
file: string,
): Promise<T | undefined> {
if (folder) {
try {
const content = await fs.readFile(path.join(folder.uri.fsPath, file), 'utf8');
return JSON.parse(content);
} catch (error) {
// silently ignore
}
}
return undefined;
}
/*
* try to find the entry point ('main') from the package.json
*/
async function guessProgramFromPackage(
folder: vscode.WorkspaceFolder | undefined,
packageJson: IPartialPackageJson,
resolve: boolean,
): Promise<string | undefined> {
let program: string | undefined;
try {
if (packageJson.main) {
program = packageJson.main;
} else if (packageJson.scripts && typeof packageJson.scripts.start === 'string') {
// assume a start script of the form 'node server.js'
program = packageJson.scripts.start.split(' ').pop();
}
if (program) {
let targetPath: string | undefined;
if (path.isAbsolute(program)) {
targetPath = program;
} else {
targetPath = folder ? path.join(folder.uri.fsPath, program) : undefined;
program = path.join('${workspaceFolder}', program);
}
if (
resolve &&
targetPath &&
!(await existsInjected(fs, targetPath)) &&
!(await existsInjected(fs, targetPath + '.js'))
) {
return undefined;
}
}
} catch (error) {
// silently ignore
}
return program;
}