Skip to content

Commit be5ee43

Browse files
committed
Add more rules to detect --prefix environment
1 parent 354a73e commit be5ee43

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

src/client/pythonEnvironments/nativeAPI.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { createDeferred, Deferred } from '../common/utils/async';
2323
import { Architecture, getUserHomeDir } from '../common/utils/platform';
2424
import { parseVersion } from './base/info/pythonVersion';
2525
import { cache } from '../common/utils/decorators';
26-
import { traceError, traceLog, traceWarn } from '../logging';
26+
import { traceError, traceInfo, 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';
@@ -157,6 +157,15 @@ function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
157157
}
158158
}
159159

160+
function isSubDir(pathToCheck: string | undefined, parents: string[]): boolean {
161+
return parents.some((prefix) => {
162+
if (pathToCheck) {
163+
return path.normalize(pathToCheck).startsWith(path.normalize(prefix));
164+
}
165+
return false;
166+
});
167+
}
168+
160169
function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind, condaEnvDirs: string[]): string {
161170
if (nativeEnv.name) {
162171
return nativeEnv.name;
@@ -168,14 +177,18 @@ function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind, condaEnvDirs: st
168177
}
169178

170179
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-
) {
180+
if (nativeEnv.name === 'base') {
181+
return 'base';
182+
}
183+
184+
const workspaces = (getWorkspaceFolders() ?? []).map((wf) => wf.uri.fsPath);
185+
if (isSubDir(nativeEnv.prefix, workspaces)) {
186+
traceInfo(`Conda env is --prefix environment: ${nativeEnv.prefix}`);
187+
return '';
188+
}
189+
190+
if (condaEnvDirs.length > 0 && isSubDir(nativeEnv.prefix, condaEnvDirs)) {
191+
traceInfo(`Conda env is --named environment: ${nativeEnv.prefix}`);
179192
return path.basename(nativeEnv.prefix);
180193
}
181194
}

0 commit comments

Comments
 (0)