Skip to content

Commit 6085288

Browse files
committed
update search for conda-hook.ps1 to include more locations
1 parent fefc113 commit 6085288

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/managers/conda/condaUtils.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function getNamedCondaPythonInfo(
323323
]);
324324
shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'conda', args: ['deactivate'] }]);
325325
}
326-
const psActivate = path.join(path.dirname(path.dirname(conda)), 'shell', 'condabin', 'conda-hook.ps1');
326+
const psActivate = getCondaHookPs1Path(conda);
327327
shellActivation.set(ShellConstants.PWSH, [
328328
{ executable: '&', args: [psActivate] },
329329
{ executable: 'conda', args: ['activate', name] },
@@ -416,7 +416,7 @@ function getPrefixesCondaPythonInfo(
416416
]);
417417
shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'conda', args: ['deactivate'] }]);
418418
}
419-
const psActivate = path.join(path.dirname(path.dirname(conda)), 'shell', 'condabin', 'conda-hook.ps1');
419+
const psActivate = getCondaHookPs1Path(conda);
420420
shellActivation.set(ShellConstants.PWSH, [
421421
{ executable: '&', args: [psActivate] },
422422
{ executable: 'conda', args: ['activate', prefix] },
@@ -1052,3 +1052,35 @@ export async function checkForNoPythonCondaEnvironment(
10521052
}
10531053
return environment;
10541054
}
1055+
1056+
/**
1057+
* Returns the best guess path to conda-hook.ps1 given a conda executable path.
1058+
*
1059+
* Searches for conda-hook.ps1 in these locations (relative to the conda root):
1060+
* - shell/condabin/
1061+
* - Library/shell/condabin/
1062+
* - condabin/
1063+
* - etc/profile.d/
1064+
*/
1065+
function getCondaHookPs1Path(condaPath: string): string {
1066+
const condaRoot = path.dirname(path.dirname(condaPath));
1067+
1068+
let condaRootCandidates: string[] = [];
1069+
condaRootCandidates.push(path.join(condaRoot, 'shell', 'condabin'));
1070+
condaRootCandidates.push(path.join(condaRoot, 'Library', 'shell', 'condabin'));
1071+
condaRootCandidates.push(path.join(condaRoot, 'condabin'));
1072+
condaRootCandidates.push(path.join(condaRoot, 'etc', 'profile.d'));
1073+
1074+
for (const hookSearchDir of condaRootCandidates) {
1075+
const candidate = path.join(hookSearchDir, 'conda-hook.ps1');
1076+
if (fse.existsSync(candidate)) {
1077+
traceInfo(`Conda hook found at: ${candidate}`);
1078+
return candidate;
1079+
}
1080+
}
1081+
throw new Error(
1082+
`Conda hook not found in expected locations. Tried: ${condaRootCandidates.join(
1083+
', ',
1084+
)} based on conda executable at ${condaPath}.`,
1085+
); // Throw an error if not found
1086+
}

0 commit comments

Comments
 (0)