@@ -1053,6 +1053,9 @@ export async function checkForNoPythonCondaEnvironment(
10531053 return environment ;
10541054}
10551055
1056+ // Cache for conda hook paths to avoid redundant filesystem checks
1057+ const condaHookPathCache = new Map < string , Promise < string > > ( ) ;
1058+
10561059/**
10571060 * Returns the best guess path to conda-hook.ps1 given a conda executable path.
10581061 *
@@ -1063,32 +1066,45 @@ export async function checkForNoPythonCondaEnvironment(
10631066 * - etc/profile.d/
10641067 */
10651068async function getCondaHookPs1Path ( condaPath : string ) : Promise < string > {
1066- const condaRoot = path . dirname ( path . dirname ( condaPath ) ) ;
1067-
1068- const condaRootCandidates : string [ ] = [
1069- path . join ( condaRoot , 'shell' , 'condabin' ) ,
1070- path . join ( condaRoot , 'Library' , 'shell' , 'condabin' ) ,
1071- path . join ( condaRoot , 'condabin' ) ,
1072- path . join ( condaRoot , 'etc' , 'profile.d' ) ,
1073- ] ;
1074-
1075- const checks = condaRootCandidates . map ( async ( hookSearchDir ) => {
1076- const candidate = path . join ( hookSearchDir , 'conda-hook.ps1' ) ;
1077- if ( await fse . pathExists ( candidate ) ) {
1078- traceInfo ( `Conda hook found at: ${ candidate } ` ) ;
1079- return candidate ;
1080- }
1081- return undefined ;
1082- } ) ;
1083- const results = await Promise . all ( checks ) ;
1084- const found = results . find ( Boolean ) ;
1085- if ( found ) {
1086- return found as string ;
1069+ // Check cache first
1070+ const cachedPath = condaHookPathCache . get ( condaPath ) ;
1071+ if ( cachedPath ) {
1072+ return cachedPath ;
10871073 }
1088- traceError (
1089- `Conda hook not found in any of the expected locations: ${ condaRootCandidates . join (
1090- ', ' ,
1091- ) } , given conda path: ${ condaPath } `,
1092- ) ;
1093- return path . join ( condaRoot , 'shell' , 'condabin' , 'conda-hook.ps1' ) ;
1074+
1075+ // Create the promise for finding the hook path
1076+ const hookPathPromise = ( async ( ) => {
1077+ const condaRoot = path . dirname ( path . dirname ( condaPath ) ) ;
1078+
1079+ const condaRootCandidates : string [ ] = [
1080+ path . join ( condaRoot , 'shell' , 'condabin' ) ,
1081+ path . join ( condaRoot , 'Library' , 'shell' , 'condabin' ) ,
1082+ path . join ( condaRoot , 'condabin' ) ,
1083+ path . join ( condaRoot , 'etc' , 'profile.d' ) ,
1084+ ] ;
1085+
1086+ const checks = condaRootCandidates . map ( async ( hookSearchDir ) => {
1087+ const candidate = path . join ( hookSearchDir , 'conda-hook.ps1' ) ;
1088+ if ( await fse . pathExists ( candidate ) ) {
1089+ traceInfo ( `Conda hook found at: ${ candidate } ` ) ;
1090+ return candidate ;
1091+ }
1092+ return undefined ;
1093+ } ) ;
1094+ const results = await Promise . all ( checks ) ;
1095+ const found = results . find ( Boolean ) ;
1096+ if ( found ) {
1097+ return found as string ;
1098+ }
1099+ traceError (
1100+ `Conda hook not found in any of the expected locations: ${ condaRootCandidates . join (
1101+ ', ' ,
1102+ ) } , given conda path: ${ condaPath } `,
1103+ ) ;
1104+ return path . join ( condaRoot , 'shell' , 'condabin' , 'conda-hook.ps1' ) ;
1105+ } ) ( ) ;
1106+
1107+ // Store in cache and return
1108+ condaHookPathCache . set ( condaPath , hookPathPromise ) ;
1109+ return hookPathPromise ;
10941110}
0 commit comments