Skip to content

Commit 354a73e

Browse files
committed
Fix for prefix conda environments
1 parent eab8794 commit 354a73e

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/client/pythonEnvironments/common/environmentManagers/conda.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,15 @@ export class Conda {
495495
);
496496
}
497497

498+
/**
499+
* Retrieves list of directories where conda environments are stored.
500+
*/
501+
@cache(30_000, true, 10_000)
502+
public async getEnvDirs(): Promise<string[]> {
503+
const info = await this.getInfo();
504+
return info.envs_dirs ?? [];
505+
}
506+
498507
public async getName(prefix: string, info?: CondaInfo): Promise<string | undefined> {
499508
info = info ?? (await this.getInfo(true));
500509
if (info.root_prefix && arePathsSame(prefix, info.root_prefix)) {
@@ -619,3 +628,8 @@ export class Conda {
619628
export function setCondaBinary(executable: string): void {
620629
Conda.setConda(executable);
621630
}
631+
632+
export async function getCondaEnvDirs(): Promise<string[] | undefined> {
633+
const conda = await Conda.getConda();
634+
return conda?.getEnvDirs();
635+
}

src/client/pythonEnvironments/nativeAPI.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { traceError, traceLog, traceWarn } from '../logging';
2727
import { StopWatch } from '../common/utils/stopWatch';
2828
import { FileChangeType } from '../common/platform/fileSystemWatcher';
2929
import { categoryToKind, NativePythonEnvironmentKind } from './base/locators/common/nativePythonUtils';
30-
import { setCondaBinary } from './common/environmentManagers/conda';
30+
import { getCondaEnvDirs, setCondaBinary } from './common/environmentManagers/conda';
3131
import { setPyEnvBinary } from './common/environmentManagers/pyenv';
3232
import {
3333
createPythonWatcher,
@@ -157,26 +157,40 @@ function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
157157
}
158158
}
159159

160-
function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind): string {
160+
function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind, condaEnvDirs: string[]): string {
161161
if (nativeEnv.name) {
162162
return nativeEnv.name;
163163
}
164164

165165
const envType = getEnvType(kind);
166-
if (nativeEnv.prefix && (envType === PythonEnvType.Conda || envType === PythonEnvType.Virtual)) {
166+
if (nativeEnv.prefix && envType === PythonEnvType.Virtual) {
167167
return path.basename(nativeEnv.prefix);
168168
}
169+
170+
if (nativeEnv.prefix && envType === PythonEnvType.Conda) {
171+
if (
172+
condaEnvDirs.some((dir) => {
173+
if (nativeEnv.prefix) {
174+
return path.normalize(nativeEnv.prefix).startsWith(path.normalize(dir));
175+
}
176+
return false;
177+
})
178+
) {
179+
return path.basename(nativeEnv.prefix);
180+
}
181+
}
182+
169183
return '';
170184
}
171185

172-
function toPythonEnvInfo(nativeEnv: NativeEnvInfo): PythonEnvInfo | undefined {
186+
function toPythonEnvInfo(nativeEnv: NativeEnvInfo, condaEnvDirs: string[]): PythonEnvInfo | undefined {
173187
if (!validEnv(nativeEnv)) {
174188
return undefined;
175189
}
176190
const kind = categoryToKind(nativeEnv.kind);
177191
const arch = toArch(nativeEnv.arch);
178192
const version: PythonVersion = parseVersion(nativeEnv.version ?? '');
179-
const name = getName(nativeEnv, kind);
193+
const name = getName(nativeEnv, kind, condaEnvDirs);
180194
const displayName = nativeEnv.version
181195
? getDisplayName(version, kind, arch, name)
182196
: nativeEnv.displayName ?? 'Python';
@@ -211,6 +225,9 @@ function toPythonEnvInfo(nativeEnv: NativeEnvInfo): PythonEnvInfo | undefined {
211225
}
212226

213227
function hasChanged(old: PythonEnvInfo, newEnv: PythonEnvInfo): boolean {
228+
if (old.name !== newEnv.name) {
229+
return true;
230+
}
214231
if (old.executable.filename !== newEnv.executable.filename) {
215232
return true;
216233
}
@@ -247,6 +264,8 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
247264

248265
private _disposables: Disposable[] = [];
249266

267+
private _condaEnvDirs: string[] = [];
268+
250269
constructor(private readonly finder: NativePythonFinder) {
251270
this._onProgress = new EventEmitter<ProgressNotificationEvent>();
252271
this._onChanged = new EventEmitter<PythonEnvCollectionChangedEvent>();
@@ -381,7 +400,7 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
381400
}
382401

383402
private addEnv(native: NativeEnvInfo, searchLocation?: Uri): PythonEnvInfo | undefined {
384-
const info = toPythonEnvInfo(native);
403+
const info = toPythonEnvInfo(native, this._condaEnvDirs);
385404
if (info) {
386405
const old = this._envs.find((item) => item.executable.filename === info.executable.filename);
387406
if (old) {
@@ -417,6 +436,9 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable {
417436
}
418437
const native = await this.finder.resolve(envPath);
419438
if (native) {
439+
if (native.kind === NativePythonEnvironmentKind.Conda && this._condaEnvDirs.length === 0) {
440+
this._condaEnvDirs = (await getCondaEnvDirs()) ?? [];
441+
}
420442
return this.addEnv(native);
421443
}
422444
return undefined;

0 commit comments

Comments
 (0)