Skip to content

Commit 464d6e2

Browse files
committed
support per file debug with correct interpreter
1 parent cec4929 commit 464d6e2

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/extension/debugger/configuration/debugConfigurationService.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
3-
3+
import * as fs from 'fs';
44
import { cloneDeep } from 'lodash';
5-
import { CancellationToken, DebugConfiguration, QuickPickItem, WorkspaceFolder } from 'vscode';
5+
import { CancellationToken, DebugConfiguration, QuickPickItem, Uri, WorkspaceFolder } from 'vscode';
66
import { DebugConfigStrings } from '../../common/utils/localize';
77
import { IMultiStepInputFactory, InputStep, IQuickPickParameters, MultiStepInput } from '../../common/multiStepInput';
88
import { AttachRequestArguments, DebugConfigurationArguments, LaunchRequestArguments } from '../../types';
@@ -17,6 +17,7 @@ import { buildPyramidLaunchConfiguration } from './providers/pyramidLaunch';
1717
import { buildRemoteAttachConfiguration } from './providers/remoteAttach';
1818
import { IDebugConfigurationResolver } from './types';
1919
import { buildFileWithArgsLaunchDebugConfiguration } from './providers/fileLaunchWithArgs';
20+
import { getInterpreterDetails } from '../../common/python';
2021

2122
export class PythonDebugConfigurationService implements IDebugConfigurationService {
2223
private cacheDebugConfig: DebugConfiguration | undefined = undefined;
@@ -90,6 +91,18 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
9091
debugConfiguration: DebugConfiguration,
9192
token?: CancellationToken,
9293
): Promise<DebugConfiguration | undefined> {
94+
// now that ${file} is resolved, we can use it to get the interpreter for that file
95+
if (debugConfiguration.program !== undefined) {
96+
if (debugConfiguration.python === undefined) {
97+
// If program is a valid file, get interpreter for that file
98+
if (fs.existsSync(debugConfiguration.program) && fs.statSync(debugConfiguration.program).isFile()) {
99+
const interpreter = await getInterpreterDetails(Uri.file(debugConfiguration.program));
100+
if (interpreter?.path && interpreter.path.length > 0) {
101+
debugConfiguration.python = interpreter.path[0];
102+
}
103+
}
104+
}
105+
}
93106
function resolve<T extends DebugConfiguration>(resolver: IDebugConfigurationResolver<T>) {
94107
return resolver.resolveDebugConfigurationWithSubstitutedVariables(folder, debugConfiguration as T, token);
95108
}

src/extension/debugger/configuration/resolvers/base.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the MIT License.
33

44
'use strict';
5-
5+
import * as fs from 'fs';
66
import * as path from 'path';
77
import { CancellationToken, DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
88
import { sendTelemetryEvent } from '../../../telemetry';
@@ -137,9 +137,22 @@ export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
137137
if (!debugConfiguration) {
138138
return;
139139
}
140+
let interpreterDetailsTarget = workspaceFolder;
140141

141-
// get the interpreter details in the context of the workspace folder
142-
const interpreterDetail = await getInterpreterDetails(workspaceFolder);
142+
if (debugConfiguration.program !== undefined) {
143+
if (debugConfiguration.python === undefined) {
144+
if (debugConfiguration.program === '${file}') {
145+
// If program is ${file}, we cannot determine the interpreter yet
146+
return;
147+
}
148+
// If program is a valid file, get interpreter for that file
149+
if (fs.existsSync(debugConfiguration.program) && fs.statSync(debugConfiguration.program).isFile()) {
150+
interpreterDetailsTarget = Uri.file(debugConfiguration.program);
151+
}
152+
}
153+
}
154+
// get the interpreter details in the context of either the workspace folder or the program file
155+
const interpreterDetail = await getInterpreterDetails(interpreterDetailsTarget);
143156
const interpreterPath = interpreterDetail?.path ?? (await getSettingsPythonPath(workspaceFolder));
144157
const resolvedInterpreterPath = interpreterPath ? interpreterPath[0] : interpreterPath;
145158

0 commit comments

Comments
 (0)