Skip to content

Commit 6827afb

Browse files
authored
add workspace folder for debugpy launch.json config (#25338)
fixes microsoft/vscode-python-environments#645
1 parent 3a729b4 commit 6827afb

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/client/interpreter/interpreterPathCommand.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
'use strict';
55

66
import { inject, injectable } from 'inversify';
7-
import { Uri } from 'vscode';
7+
import { Uri, workspace } from 'vscode';
88
import { IExtensionSingleActivationService } from '../activation/types';
99
import { Commands } from '../common/constants';
1010
import { IDisposable, IDisposableRegistry } from '../common/types';
1111
import { registerCommand } from '../common/vscodeApis/commandApis';
1212
import { IInterpreterService } from './contracts';
13+
import { useEnvExtension } from '../envExt/api.internal';
1314

1415
@injectable()
1516
export class InterpreterPathCommand implements IExtensionSingleActivationService {
@@ -26,7 +27,9 @@ export class InterpreterPathCommand implements IExtensionSingleActivationService
2627
);
2728
}
2829

29-
public async _getSelectedInterpreterPath(args: { workspaceFolder: string } | string[]): Promise<string> {
30+
public async _getSelectedInterpreterPath(
31+
args: { workspaceFolder: string; type: string } | string[],
32+
): Promise<string> {
3033
// If `launch.json` is launching this command, `args.workspaceFolder` carries the workspaceFolder
3134
// If `tasks.json` is launching this command, `args[1]` carries the workspaceFolder
3235
let workspaceFolder;
@@ -35,6 +38,11 @@ export class InterpreterPathCommand implements IExtensionSingleActivationService
3538
} else if (args[1]) {
3639
const [, second] = args;
3740
workspaceFolder = second;
41+
} else if (useEnvExtension() && 'type' in args && args.type === 'debugpy') {
42+
// If using the envsExt and the type is debugpy, we need to add the workspace folder to get the interpreter path.
43+
if (Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0) {
44+
workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
45+
}
3846
} else {
3947
workspaceFolder = undefined;
4048
}

src/test/interpreters/interpreterPathCommand.unit.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ import * as commandApis from '../../client/common/vscodeApis/commandApis';
1313
import { InterpreterPathCommand } from '../../client/interpreter/interpreterPathCommand';
1414
import { IInterpreterService } from '../../client/interpreter/contracts';
1515
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
16+
import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis';
1617

1718
suite('Interpreter Path Command', () => {
1819
let interpreterService: IInterpreterService;
1920
let interpreterPathCommand: InterpreterPathCommand;
2021
let registerCommandStub: sinon.SinonStub;
22+
let getConfigurationStub: sinon.SinonStub;
23+
2124
setup(() => {
2225
interpreterService = mock<IInterpreterService>();
2326
registerCommandStub = sinon.stub(commandApis, 'registerCommand');
2427
interpreterPathCommand = new InterpreterPathCommand(instance(interpreterService), []);
28+
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
2529
});
2630

2731
teardown(() => {
@@ -43,7 +47,7 @@ suite('Interpreter Path Command', () => {
4347
});
4448

4549
test('If `workspaceFolder` property exists in `args`, it is used to retrieve setting from config', async () => {
46-
const args = { workspaceFolder: 'folderPath' };
50+
const args = { workspaceFolder: 'folderPath', type: 'debugpy' };
4751
when(interpreterService.getActiveInterpreter(anything())).thenCall((arg) => {
4852
assert.deepEqual(arg, Uri.file('folderPath'));
4953

@@ -76,6 +80,10 @@ suite('Interpreter Path Command', () => {
7680
});
7781

7882
test('If neither of these exists, value of workspace folder is `undefined`', async () => {
83+
getConfigurationStub.withArgs('python').returns({
84+
get: sinon.stub().returns(false),
85+
});
86+
7987
const args = ['command'];
8088

8189
when(interpreterService.getActiveInterpreter(undefined)).thenReturn(

0 commit comments

Comments
 (0)