Skip to content

add workspace folder for debugpy launch.json config #25338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/client/interpreter/interpreterPathCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
'use strict';

import { inject, injectable } from 'inversify';
import { Uri } from 'vscode';
import { Uri, workspace } from 'vscode';
import { IExtensionSingleActivationService } from '../activation/types';
import { Commands } from '../common/constants';
import { IDisposable, IDisposableRegistry } from '../common/types';
import { registerCommand } from '../common/vscodeApis/commandApis';
import { IInterpreterService } from './contracts';
import { useEnvExtension } from '../envExt/api.internal';

@injectable()
export class InterpreterPathCommand implements IExtensionSingleActivationService {
Expand All @@ -26,7 +27,9 @@ export class InterpreterPathCommand implements IExtensionSingleActivationService
);
}

public async _getSelectedInterpreterPath(args: { workspaceFolder: string } | string[]): Promise<string> {
public async _getSelectedInterpreterPath(
args: { workspaceFolder: string; type: string } | string[],
): Promise<string> {
// If `launch.json` is launching this command, `args.workspaceFolder` carries the workspaceFolder
// If `tasks.json` is launching this command, `args[1]` carries the workspaceFolder
let workspaceFolder;
Expand All @@ -35,6 +38,11 @@ export class InterpreterPathCommand implements IExtensionSingleActivationService
} else if (args[1]) {
const [, second] = args;
workspaceFolder = second;
} else if (useEnvExtension() && 'type' in args && args.type === 'debugpy') {
// If using the envsExt and the type is debugpy, we need to add the workspace folder to get the interpreter path.
if (Array.isArray(workspace.workspaceFolders) && workspace.workspaceFolders.length > 0) {
workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
}
} else {
workspaceFolder = undefined;
}
Expand Down
10 changes: 9 additions & 1 deletion src/test/interpreters/interpreterPathCommand.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ import * as commandApis from '../../client/common/vscodeApis/commandApis';
import { InterpreterPathCommand } from '../../client/interpreter/interpreterPathCommand';
import { IInterpreterService } from '../../client/interpreter/contracts';
import { PythonEnvironment } from '../../client/pythonEnvironments/info';
import * as workspaceApis from '../../client/common/vscodeApis/workspaceApis';

suite('Interpreter Path Command', () => {
let interpreterService: IInterpreterService;
let interpreterPathCommand: InterpreterPathCommand;
let registerCommandStub: sinon.SinonStub;
let getConfigurationStub: sinon.SinonStub;

setup(() => {
interpreterService = mock<IInterpreterService>();
registerCommandStub = sinon.stub(commandApis, 'registerCommand');
interpreterPathCommand = new InterpreterPathCommand(instance(interpreterService), []);
getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration');
});

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

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

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

test('If neither of these exists, value of workspace folder is `undefined`', async () => {
getConfigurationStub.withArgs('python').returns({
get: sinon.stub().returns(false),
});

const args = ['command'];

when(interpreterService.getActiveInterpreter(undefined)).thenReturn(
Expand Down