From 96d41163536f0f44b6b93a52d40077bba6659645 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Mon, 14 Oct 2024 09:22:31 -0700 Subject: [PATCH] Fix for conda activation during testing (#24295) --- src/client/pythonEnvironments/nativeAPI.ts | 32 ++++++++++++++++++- .../pythonEnvironments/nativeAPI.unit.test.ts | 2 +- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/client/pythonEnvironments/nativeAPI.ts b/src/client/pythonEnvironments/nativeAPI.ts index 2d13500fdcd8..31ad80608283 100644 --- a/src/client/pythonEnvironments/nativeAPI.ts +++ b/src/client/pythonEnvironments/nativeAPI.ts @@ -55,6 +55,10 @@ function toArch(a: string | undefined): Architecture { } function getLocation(nativeEnv: NativeEnvInfo, executable: string): string { + if (nativeEnv.kind === NativePythonEnvironmentKind.Conda) { + return nativeEnv.prefix ?? path.dirname(executable); + } + if (nativeEnv.executable) { return nativeEnv.executable; } @@ -206,6 +210,32 @@ function toPythonEnvInfo(nativeEnv: NativeEnvInfo): PythonEnvInfo | undefined { }; } +function hasChanged(old: PythonEnvInfo, newEnv: PythonEnvInfo): boolean { + if (old.executable.filename !== newEnv.executable.filename) { + return true; + } + if (old.version.major !== newEnv.version.major) { + return true; + } + if (old.version.minor !== newEnv.version.minor) { + return true; + } + if (old.version.micro !== newEnv.version.micro) { + return true; + } + if (old.location !== newEnv.location) { + return true; + } + if (old.kind !== newEnv.kind) { + return true; + } + if (old.arch !== newEnv.arch) { + return true; + } + + return false; +} + class NativePythonEnvironments implements IDiscoveryAPI, Disposable { private _onProgress: EventEmitter; @@ -354,7 +384,7 @@ class NativePythonEnvironments implements IDiscoveryAPI, Disposable { const info = toPythonEnvInfo(native); if (info) { const old = this._envs.find((item) => item.executable.filename === info.executable.filename); - if (old) { + if (old && hasChanged(old, info)) { this._envs = this._envs.filter((item) => item.executable.filename !== info.executable.filename); this._envs.push(info); this._onChanged.fire({ type: FileChangeType.Changed, old, new: info, searchLocation }); diff --git a/src/test/pythonEnvironments/nativeAPI.unit.test.ts b/src/test/pythonEnvironments/nativeAPI.unit.test.ts index 088a44a0c1a3..f7f956c7a20e 100644 --- a/src/test/pythonEnvironments/nativeAPI.unit.test.ts +++ b/src/test/pythonEnvironments/nativeAPI.unit.test.ts @@ -108,7 +108,7 @@ suite('Native Python API', () => { mtime: -1, }, kind: PythonEnvKind.Conda, - location: '/home/user/.conda/envs/conda_python/python', + location: '/home/user/.conda/envs/conda_python', source: [], name: 'conda_python', type: PythonEnvType.Conda,