11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
3-
3+ import * as fs from 'fs' ;
44import { cloneDeep } from 'lodash' ;
5- import { CancellationToken , DebugConfiguration , QuickPickItem , WorkspaceFolder } from 'vscode' ;
5+ import { CancellationToken , DebugConfiguration , QuickPickItem , Uri , WorkspaceFolder } from 'vscode' ;
66import { DebugConfigStrings } from '../../common/utils/localize' ;
77import { IMultiStepInputFactory , InputStep , IQuickPickParameters , MultiStepInput } from '../../common/multiStepInput' ;
88import { AttachRequestArguments , DebugConfigurationArguments , LaunchRequestArguments } from '../../types' ;
@@ -17,6 +17,7 @@ import { buildPyramidLaunchConfiguration } from './providers/pyramidLaunch';
1717import { buildRemoteAttachConfiguration } from './providers/remoteAttach' ;
1818import { IDebugConfigurationResolver } from './types' ;
1919import { buildFileWithArgsLaunchDebugConfiguration } from './providers/fileLaunchWithArgs' ;
20+ import { getInterpreterDetails } from '../../common/python' ;
2021
2122export 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 }
0 commit comments