Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1799d75

Browse files
authored
Accomadate trailing commas in parsed launch.json (microsoft#5331)
For microsoft#4543
1 parent c315cc5 commit 1799d75

File tree

3 files changed

+318
-300
lines changed

3 files changed

+318
-300
lines changed

news/2 Fixes/4543.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Accomadate trailing commands in the JSON contents of `launch.json` file.

src/client/testing/common/debugLauncher.ts

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { inject, injectable, named } from 'inversify';
2+
import { parse } from 'jsonc-parser';
23
import * as path from 'path';
3-
import * as stripJsonComments from 'strip-json-comments';
44
import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
55
import { IApplicationShell, IDebugService, IWorkspaceService } from '../../common/application/types';
66
import { EXTENSION_ROOT_DIR } from '../../common/constants';
@@ -12,9 +12,7 @@ import { DebuggerTypeName } from '../../debugger/constants';
1212
import { IDebugConfigurationResolver } from '../../debugger/extension/configuration/types';
1313
import { LaunchRequestArguments } from '../../debugger/types';
1414
import { IServiceContainer } from '../../ioc/types';
15-
import {
16-
ITestDebugConfig, ITestDebugLauncher, LaunchOptions, TestProvider
17-
} from './types';
15+
import { ITestDebugConfig, ITestDebugLauncher, LaunchOptions, TestProvider } from './types';
1816

1917
@injectable()
2018
export class DebugLauncher implements ITestDebugLauncher {
@@ -45,7 +43,26 @@ export class DebugLauncher implements ITestDebugLauncher {
4543
return debugManager.startDebugging(workspaceFolder, launchArgs)
4644
.then(noop, ex => traceError('Failed to start debugging tests', ex));
4745
}
48-
46+
public async readAllDebugConfigs(workspaceFolder: WorkspaceFolder): Promise<DebugConfiguration[]> {
47+
const filename = path.join(workspaceFolder.uri.fsPath, '.vscode', 'launch.json');
48+
if (!(await this.fs.fileExists(filename))) {
49+
return [];
50+
}
51+
try {
52+
const text = await this.fs.readFile(filename);
53+
const parsed = parse(text, [], { allowTrailingComma: true, disallowComments: false });
54+
if (!parsed.version || !parsed.configurations || !Array.isArray(parsed.configurations)) {
55+
throw Error('malformed launch.json');
56+
}
57+
// We do not bother ensuring each item is a DebugConfiguration...
58+
return parsed.configurations;
59+
} catch (exc) {
60+
traceError('could not get debug config', exc);
61+
const appShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
62+
await appShell.showErrorMessage('Could not load unit test config from launch.json');
63+
return [];
64+
}
65+
}
4966
private resolveWorkspaceFolder(cwd: string): WorkspaceFolder {
5067
if (!this.workspaceService.hasWorkspaceFolders) {
5168
throw new Error('Please open a workspace');
@@ -95,30 +112,6 @@ export class DebugLauncher implements ITestDebugLauncher {
95112
}
96113
return undefined;
97114
}
98-
99-
private async readAllDebugConfigs(workspaceFolder: WorkspaceFolder): Promise<DebugConfiguration[]> {
100-
const filename = path.join(workspaceFolder.uri.fsPath, '.vscode', 'launch.json');
101-
let configs: DebugConfiguration[] = [];
102-
if (!(await this.fs.fileExists(filename))) {
103-
return [];
104-
}
105-
try {
106-
let text = await this.fs.readFile(filename);
107-
text = stripJsonComments(text);
108-
const parsed = JSON.parse(text);
109-
if (!parsed.version || !parsed.configurations || !Array.isArray(parsed.configurations)) {
110-
throw Error('malformed launch.json');
111-
}
112-
// We do not bother ensuring each item is a DebugConfiguration...
113-
configs = parsed.configurations;
114-
} catch (exc) {
115-
traceError('could not get debug config', exc);
116-
const appShell = this.serviceContainer.get<IApplicationShell>(IApplicationShell);
117-
await appShell.showErrorMessage('Could not load unit test config from launch.json');
118-
}
119-
return configs;
120-
}
121-
122115
private applyDefaults(
123116
cfg: ITestDebugConfig,
124117
workspaceFolder: WorkspaceFolder,

0 commit comments

Comments
 (0)