Skip to content

Commit e1c50ae

Browse files
21249 read launch config in workspace file (#21426)
Closed: #21249
1 parent f5acb54 commit e1c50ae

File tree

2 files changed

+96
-3
lines changed

2 files changed

+96
-3
lines changed

src/client/debugger/extension/configuration/launch.json/launchJsonReader.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ import * as path from 'path';
55
import * as fs from 'fs-extra';
66
import { parse } from 'jsonc-parser';
77
import { DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
8-
import { getWorkspaceFolder } from '../../../../common/vscodeApis/workspaceApis';
8+
import { getConfiguration, getWorkspaceFolder } from '../../../../common/vscodeApis/workspaceApis';
9+
import { traceLog } from '../../../../logging';
910

1011
export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder): Promise<DebugConfiguration[]> {
1112
const filename = path.join(workspace.uri.fsPath, '.vscode', 'launch.json');
12-
1313
if (!(await fs.pathExists(filename))) {
14-
return [];
14+
// Check launch config in the workspace file
15+
const codeWorkspaceConfig = getConfiguration('launch');
16+
if (!codeWorkspaceConfig.configurations || !Array.isArray(codeWorkspaceConfig.configurations)) {
17+
return [];
18+
}
19+
traceLog(`Using launch configuration in workspace folder.`);
20+
return codeWorkspaceConfig.configurations;
1521
}
1622

1723
const text = await fs.readFile(filename, 'utf-8');
@@ -23,6 +29,7 @@ export async function getConfigurationsForWorkspace(workspace: WorkspaceFolder):
2329
throw Error('Missing field in launch.json: version');
2430
}
2531
// We do not bother ensuring each item is a DebugConfiguration...
32+
traceLog(`Using launch configuration in launch.json file.`);
2633
return parsed.configurations;
2734
}
2835

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
'use strict';
5+
6+
import * as sinon from 'sinon';
7+
import * as fs from 'fs-extra';
8+
import * as path from 'path';
9+
import { Uri } from 'vscode';
10+
import { assert } from 'chai';
11+
import { getConfigurationsForWorkspace } from '../../../../../client/debugger/extension/configuration/launch.json/launchJsonReader';
12+
import * as vscodeApis from '../../../../../client/common/vscodeApis/workspaceApis';
13+
14+
suite('Launch Json Reader', () => {
15+
let pathExistsStub: sinon.SinonStub;
16+
let readFileStub: sinon.SinonStub;
17+
let getConfigurationStub: sinon.SinonStub;
18+
const workspacePath = 'path/to/workspace';
19+
const workspaceFolder = {
20+
name: 'workspace',
21+
uri: Uri.file(workspacePath),
22+
index: 0,
23+
};
24+
25+
setup(() => {
26+
pathExistsStub = sinon.stub(fs, 'pathExists');
27+
readFileStub = sinon.stub(fs, 'readFile');
28+
getConfigurationStub = sinon.stub(vscodeApis, 'getConfiguration');
29+
});
30+
31+
teardown(() => {
32+
sinon.restore();
33+
});
34+
35+
test('Return the config in the launch.json file', async () => {
36+
const launchPath = path.join(workspaceFolder.uri.fsPath, '.vscode', 'launch.json');
37+
pathExistsStub.withArgs(launchPath).resolves(true);
38+
const launchJson = `{
39+
"version": "0.1.0",
40+
"configurations": [
41+
{
42+
"name": "Python: Launch.json",
43+
"type": "python",
44+
"request": "launch",
45+
"purpose": ["debug-test"],
46+
},
47+
]
48+
}`;
49+
readFileStub.withArgs(launchPath, 'utf-8').returns(launchJson);
50+
51+
const config = await getConfigurationsForWorkspace(workspaceFolder);
52+
53+
assert.deepStrictEqual(config, [
54+
{
55+
name: 'Python: Launch.json',
56+
type: 'python',
57+
request: 'launch',
58+
purpose: ['debug-test'],
59+
},
60+
]);
61+
});
62+
63+
test('If there is no launch.json return the config in the workspace file', async () => {
64+
getConfigurationStub.withArgs('launch').returns({
65+
configurations: [
66+
{
67+
name: 'Python: Workspace File',
68+
type: 'python',
69+
request: 'launch',
70+
purpose: ['debug-test'],
71+
},
72+
],
73+
});
74+
75+
const config = await getConfigurationsForWorkspace(workspaceFolder);
76+
77+
assert.deepStrictEqual(config, [
78+
{
79+
name: 'Python: Workspace File',
80+
type: 'python',
81+
request: 'launch',
82+
purpose: ['debug-test'],
83+
},
84+
]);
85+
});
86+
});

0 commit comments

Comments
 (0)