diff --git a/src/managers/builtin/sysPythonManager.ts b/src/managers/builtin/sysPythonManager.ts index 14895a3d..2f830b57 100644 --- a/src/managers/builtin/sysPythonManager.ts +++ b/src/managers/builtin/sysPythonManager.ts @@ -15,19 +15,19 @@ import { ResolveEnvironmentContext, SetEnvironmentScope, } from '../../api'; -import { refreshPythons, resolveSystemPythonEnvironmentPath } from './utils'; -import { NativePythonFinder } from '../common/nativePythonFinder'; +import { SysManagerStrings } from '../../common/localize'; import { createDeferred, Deferred } from '../../common/utils/deferred'; +import { NativePythonFinder } from '../common/nativePythonFinder'; import { getLatest } from '../common/utils'; -import { SysManagerStrings } from '../../common/localize'; import { - setSystemEnvForWorkspace, - setSystemEnvForGlobal, clearSystemEnvCache, getSystemEnvForGlobal, getSystemEnvForWorkspace, + setSystemEnvForGlobal, + setSystemEnvForWorkspace, setSystemEnvForWorkspaces, } from './cache'; +import { refreshPythons, resolveSystemPythonEnvironmentPath } from './utils'; export class SysPythonManager implements EnvironmentManager { private collection: PythonEnvironment[] = []; @@ -86,6 +86,7 @@ export class SysPythonManager implements EnvironmentManager { async () => { const discard = this.collection.map((c) => c); + // hit here is fine... this.collection = await refreshPythons(hardRefresh, this.nativeFinder, this.api, this.log, this); await this.loadEnvMap(); @@ -198,13 +199,20 @@ export class SysPythonManager implements EnvironmentManager { // This environment is unknown. Resolve it. const resolved = await resolveSystemPythonEnvironmentPath(context.fsPath, this.nativeFinder, this.api, this); if (resolved) { + // HERE IT GOT TOO MANY // This is just like finding a new environment or creating a new one. // Add it to collection, and trigger the added event. // For all other env types we need to ensure that the environment is of the type managed by the manager. // But System is a exception, this is the last resort for resolving. So we don't need to check. // We will just add it and treat it as a non-activatable environment. - this.collection.push(resolved); + const exists = this.collection.some( + (e) => e.environmentPath.toString() === resolved.environmentPath.toString(), + ); + if (!exists) { + // only add it if it is not already in the collection to avoid duplicates + this.collection.push(resolved); + } this._onDidChangeEnvironments.fire([{ environment: resolved, kind: EnvironmentChangeKind.add }]); } @@ -216,7 +224,7 @@ export class SysPythonManager implements EnvironmentManager { } private findEnvironmentByPath(fsPath: string): PythonEnvironment | undefined { - const normalized = path.normalize(fsPath); + const normalized = path.normalize(fsPath); // /opt/homebrew/bin/python3.12 return this.collection.find((e) => { const n = path.normalize(e.environmentPath.fsPath); return n === normalized || path.dirname(n) === normalized || path.dirname(path.dirname(n)) === normalized;