11import assert from 'assert' ;
2- import { existsSync } from 'fs' ;
3- import path from 'path' ;
42import * as vscode from 'vscode' ;
53import { adaExtState } from './extension' ;
6- import {
7- AdaMain ,
8- exe ,
9- getAdaMains ,
10- getEvaluatedTerminalEnv ,
11- getProjectFile ,
12- getProjectFileRelPath ,
13- } from './helpers' ;
4+ import { AdaMain , getAdaMains , getProjectFile , getProjectFileRelPath } from './helpers' ;
145import { BUILD_PROJECT_TASK_NAME , getBuildTaskName } from './taskProviders' ;
156
167export const ADA_DEBUG_BACKEND_TYPE = 'cppdbg' ;
@@ -40,8 +31,9 @@ export const adaDynamicDebugConfigProvider = {
4031 _folder ?: vscode . WorkspaceFolder ,
4132 ) : Promise < AdaConfig [ ] > {
4233 const mains = await getAdaMains ( ) ;
34+ const target = await adaExtState . getTargetPrefix ( ) ;
4335 const configs : AdaConfig [ ] = mains . flatMap ( ( m ) => {
44- return [ initializeConfig ( m ) , createAttachConfig ( m ) ] ;
36+ return [ initializeConfig ( m , target ) , createAttachConfig ( m , target ) ] ;
4537 } ) ;
4638 if ( configs . length == 0 ) {
4739 /**
@@ -112,55 +104,6 @@ export function initializeDebugging(ctx: vscode.ExtensionContext): {
112104 return { providerInitial : providerInitial , providerDynamic : adaDynamicDebugConfigProvider } ;
113105}
114106
115- let cachedGdb : string | undefined | null = undefined ;
116-
117- /**
118- *
119- * @returns the full path to the `gdb` executable, taking into consideration the
120- * `PATH` variable in the `terminal.integrated.env.*` setting if set. Otherwise,
121- * the `PATH` variable of the current process environment is considered.
122- *
123- * The current process environment is unlikely to change during the lifetime of
124- * the extension, and we already prompt the User to reload the window in case
125- * the `terminal.integrated.env.*` variables change. For this reason, we compute
126- * the value only on the first call, and cache it for subsequent calls to return
127- * it efficiently.
128- */
129- export function getOrFindGdb ( ) : string | undefined {
130- if ( cachedGdb == undefined ) {
131- /**
132- * If undefined yet, try to compute it.
133- */
134- const env = getEvaluatedTerminalEnv ( ) ;
135- let pathVal : string ;
136- if ( env && 'PATH' in env ) {
137- pathVal = env . PATH ?? '' ;
138- } else if ( 'PATH' in process . env ) {
139- pathVal = process . env . PATH ?? '' ;
140- } else {
141- pathVal = '' ;
142- }
143-
144- const gdb = pathVal
145- . split ( path . delimiter )
146- . map < string > ( ( v ) => path . join ( v , 'gdb' + exe ) )
147- . find ( existsSync ) ;
148-
149- if ( gdb ) {
150- // Found
151- cachedGdb = gdb ;
152- return cachedGdb ;
153- } else {
154- // Not found. Assign null to cache to avoid recomputing at every call.
155- cachedGdb = null ;
156- }
157- }
158-
159- // When returning, coerce null to undefined because the distinction doesn't
160- // matter on the caller side.
161- return cachedGdb ?? undefined ;
162- }
163-
164107/**
165108 * Initialize a debug configuration based on 'cppdbg' for the given executable
166109 * if specified. Otherwise the program field includes
@@ -175,7 +118,7 @@ export function getOrFindGdb(): string | undefined {
175118 * 'program' parameter.
176119 * @returns an AdaConfig
177120 */
178- export function initializeConfig ( main : AdaMain , name ?: string ) : AdaConfig {
121+ export function initializeConfig ( main : AdaMain , target : string , name ?: string ) : AdaConfig {
179122 // TODO it would be nice if this and the package.json configuration snippet
180123 // were the same.
181124 const config : AdaConfig = {
@@ -192,7 +135,7 @@ export function initializeConfig(main: AdaMain, name?: string): AdaConfig {
192135 MIMode : 'gdb' ,
193136 preLaunchTask : main ? getBuildTaskName ( main ) : BUILD_PROJECT_TASK_NAME ,
194137 setupCommands : setupCmd ,
195- miDebuggerPath : getOrFindGdb ( ) ,
138+ miDebuggerPath : adaExtState . getOrFindGdb ( target ) ,
196139 } ;
197140
198141 return config ;
@@ -283,9 +226,9 @@ export class AdaInitialDebugConfigProvider implements vscode.DebugConfigurationP
283226 return debugConfiguration ;
284227 } else {
285228 const main = await getOrAskForProgram ( ) ;
286-
229+ const target = await adaExtState . getTargetPrefix ( ) ;
287230 if ( main ) {
288- return initializeConfig ( main ) ;
231+ return initializeConfig ( main , target ) ;
289232 }
290233 }
291234
@@ -449,7 +392,7 @@ async function getAdaMainForSourceFile(
449392 return mains . find ( ( val ) => srcPath == val . mainFullPath ) ;
450393}
451394
452- function createAttachConfig ( adaMain : AdaMain ) : AdaConfig {
395+ function createAttachConfig ( adaMain : AdaMain , target : string ) : AdaConfig {
453396 return {
454397 name : `Ada: Attach debugger to running process - ${ adaMain . mainRelPath ( ) } ` ,
455398 type : ADA_DEBUG_BACKEND_TYPE ,
@@ -463,7 +406,7 @@ function createAttachConfig(adaMain: AdaMain): AdaConfig {
463406 * to trigger an unwanted rebuild, so we don't set a preLaunchTask.
464407 */
465408 // preLaunchTask: adaMain ? getBuildTaskName(adaMain) : BUILD_PROJECT_TASK_NAME,
466- miDebuggerPath : getOrFindGdb ( ) ,
409+ miDebuggerPath : adaExtState . getOrFindGdb ( target ) ,
467410 setupCommands : setupCmd ,
468411 } ;
469412}
0 commit comments