Skip to content

Commit 15b971e

Browse files
committed
switch to async
1 parent 6085288 commit 15b971e

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

src/managers/conda/condaUtils.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,13 @@ function isPrefixOf(roots: string[], e: string): boolean {
280280
return false;
281281
}
282282

283-
function getNamedCondaPythonInfo(
283+
async function getNamedCondaPythonInfo(
284284
name: string,
285285
prefix: string,
286286
executable: string,
287287
version: string,
288288
conda: string,
289-
): PythonEnvironmentInfo {
289+
): Promise<PythonEnvironmentInfo> {
290290
const sv = shortVersion(version);
291291
const shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
292292
const shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
@@ -323,7 +323,7 @@ function getNamedCondaPythonInfo(
323323
]);
324324
shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'conda', args: ['deactivate'] }]);
325325
}
326-
const psActivate = getCondaHookPs1Path(conda);
326+
const psActivate = await getCondaHookPs1Path(conda);
327327
shellActivation.set(ShellConstants.PWSH, [
328328
{ executable: '&', args: [psActivate] },
329329
{ executable: 'conda', args: ['activate', name] },
@@ -374,12 +374,12 @@ function getNamedCondaPythonInfo(
374374
};
375375
}
376376

377-
function getPrefixesCondaPythonInfo(
377+
async function getPrefixesCondaPythonInfo(
378378
prefix: string,
379379
executable: string,
380380
version: string,
381381
conda: string,
382-
): PythonEnvironmentInfo {
382+
): Promise<PythonEnvironmentInfo> {
383383
const sv = shortVersion(version);
384384
const shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
385385
const shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map();
@@ -416,7 +416,7 @@ function getPrefixesCondaPythonInfo(
416416
]);
417417
shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'conda', args: ['deactivate'] }]);
418418
}
419-
const psActivate = getCondaHookPs1Path(conda);
419+
const psActivate = await getCondaHookPs1Path(conda);
420420
shellActivation.set(ShellConstants.PWSH, [
421421
{ executable: '&', args: [psActivate] },
422422
{ executable: 'conda', args: ['activate', prefix] },
@@ -487,14 +487,14 @@ function getCondaWithoutPython(name: string, prefix: string, conda: string): Pyt
487487
};
488488
}
489489

490-
function nativeToPythonEnv(
490+
async function nativeToPythonEnv(
491491
e: NativeEnvInfo,
492492
api: PythonEnvironmentApi,
493493
manager: EnvironmentManager,
494494
log: LogOutputChannel,
495495
conda: string,
496496
condaPrefixes: string[],
497-
): PythonEnvironment | undefined {
497+
): Promise<PythonEnvironment | undefined> {
498498
if (!(e.prefix && e.executable && e.version)) {
499499
let name = e.name;
500500
const environment = api.createPythonEnvironmentItem(
@@ -507,14 +507,14 @@ function nativeToPythonEnv(
507507

508508
if (e.name === 'base') {
509509
const environment = api.createPythonEnvironmentItem(
510-
getNamedCondaPythonInfo('base', e.prefix, e.executable, e.version, conda),
510+
await getNamedCondaPythonInfo('base', e.prefix, e.executable, e.version, conda),
511511
manager,
512512
);
513513
log.info(`Found base environment: ${e.prefix}`);
514514
return environment;
515515
} else if (!isPrefixOf(condaPrefixes, e.prefix)) {
516516
const environment = api.createPythonEnvironmentItem(
517-
getPrefixesCondaPythonInfo(e.prefix, e.executable, e.version, conda),
517+
await getPrefixesCondaPythonInfo(e.prefix, e.executable, e.version, conda),
518518
manager,
519519
);
520520
log.info(`Found prefix environment: ${e.prefix}`);
@@ -523,7 +523,7 @@ function nativeToPythonEnv(
523523
const basename = path.basename(e.prefix);
524524
const name = e.name ?? basename;
525525
const environment = api.createPythonEnvironmentItem(
526-
getNamedCondaPythonInfo(name, e.prefix, e.executable, e.version, conda),
526+
await getNamedCondaPythonInfo(name, e.prefix, e.executable, e.version, conda),
527527
manager,
528528
);
529529
log.info(`Found named environment: ${e.prefix}`);
@@ -586,8 +586,8 @@ export async function refreshCondaEnvs(
586586
.filter((e) => e.kind === NativePythonEnvironmentKind.conda);
587587
const collection: PythonEnvironment[] = [];
588588

589-
envs.forEach((e) => {
590-
const environment = nativeToPythonEnv(e, api, manager, log, condaPath, condaPrefixes);
589+
envs.forEach(async (e) => {
590+
const environment = await nativeToPythonEnv(e, api, manager, log, condaPath, condaPrefixes);
591591
if (environment) {
592592
collection.push(environment);
593593
}
@@ -699,7 +699,7 @@ async function createNamedCondaEnvironment(
699699
const version = await getVersion(envPath);
700700

701701
const environment = api.createPythonEnvironmentItem(
702-
getNamedCondaPythonInfo(envName, envPath, path.join(envPath, bin), version, await getConda()),
702+
await getNamedCondaPythonInfo(envName, envPath, path.join(envPath, bin), version, await getConda()),
703703
manager,
704704
);
705705
return environment;
@@ -757,7 +757,7 @@ async function createPrefixCondaEnvironment(
757757
const version = await getVersion(prefix);
758758

759759
const environment = api.createPythonEnvironmentItem(
760-
getPrefixesCondaPythonInfo(prefix, path.join(prefix, bin), version, await getConda()),
760+
await getPrefixesCondaPythonInfo(prefix, path.join(prefix, bin), version, await getConda()),
761761
manager,
762762
);
763763
return environment;
@@ -1062,25 +1062,32 @@ export async function checkForNoPythonCondaEnvironment(
10621062
* - condabin/
10631063
* - etc/profile.d/
10641064
*/
1065-
function getCondaHookPs1Path(condaPath: string): string {
1065+
async function getCondaHookPs1Path(condaPath: string): Promise<string> {
10661066
const condaRoot = path.dirname(path.dirname(condaPath));
10671067

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'));
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+
];
10731074

1074-
for (const hookSearchDir of condaRootCandidates) {
1075+
const checks = condaRootCandidates.map(async (hookSearchDir) => {
10751076
const candidate = path.join(hookSearchDir, 'conda-hook.ps1');
1076-
if (fse.existsSync(candidate)) {
1077+
if (await fse.pathExists(candidate)) {
10771078
traceInfo(`Conda hook found at: ${candidate}`);
10781079
return candidate;
10791080
}
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;
10801087
}
10811088
throw new Error(
10821089
`Conda hook not found in expected locations. Tried: ${condaRootCandidates.join(
10831090
', ',
10841091
)} based on conda executable at ${condaPath}.`,
1085-
); // Throw an error if not found
1092+
);
10861093
}

0 commit comments

Comments
 (0)