Skip to content

Commit

Permalink
Add more rules to detect --prefix environment
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Dec 9, 2024
1 parent 354a73e commit be5ee43
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/client/pythonEnvironments/nativeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createDeferred, Deferred } from '../common/utils/async';
import { Architecture, getUserHomeDir } from '../common/utils/platform';
import { parseVersion } from './base/info/pythonVersion';
import { cache } from '../common/utils/decorators';
import { traceError, traceLog, traceWarn } from '../logging';
import { traceError, traceInfo, traceLog, traceWarn } from '../logging';
import { StopWatch } from '../common/utils/stopWatch';
import { FileChangeType } from '../common/platform/fileSystemWatcher';
import { categoryToKind, NativePythonEnvironmentKind } from './base/locators/common/nativePythonUtils';
Expand Down Expand Up @@ -157,6 +157,15 @@ function getEnvType(kind: PythonEnvKind): PythonEnvType | undefined {
}
}

function isSubDir(pathToCheck: string | undefined, parents: string[]): boolean {
return parents.some((prefix) => {
if (pathToCheck) {
return path.normalize(pathToCheck).startsWith(path.normalize(prefix));
}
return false;
});
}

function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind, condaEnvDirs: string[]): string {
if (nativeEnv.name) {
return nativeEnv.name;
Expand All @@ -168,14 +177,18 @@ function getName(nativeEnv: NativeEnvInfo, kind: PythonEnvKind, condaEnvDirs: st
}

if (nativeEnv.prefix && envType === PythonEnvType.Conda) {
if (
condaEnvDirs.some((dir) => {
if (nativeEnv.prefix) {
return path.normalize(nativeEnv.prefix).startsWith(path.normalize(dir));
}
return false;
})
) {
if (nativeEnv.name === 'base') {
return 'base';
}

const workspaces = (getWorkspaceFolders() ?? []).map((wf) => wf.uri.fsPath);
if (isSubDir(nativeEnv.prefix, workspaces)) {
traceInfo(`Conda env is --prefix environment: ${nativeEnv.prefix}`);
return '';
}

if (condaEnvDirs.length > 0 && isSubDir(nativeEnv.prefix, condaEnvDirs)) {
traceInfo(`Conda env is --named environment: ${nativeEnv.prefix}`);
return path.basename(nativeEnv.prefix);
}
}
Expand Down

0 comments on commit be5ee43

Please sign in to comment.