From 1dca33090cef4e8400e814fdf157b1a831f90098 Mon Sep 17 00:00:00 2001 From: Mehmet Bektas Date: Sat, 9 Mar 2024 20:21:33 -0800 Subject: [PATCH] fix duplicate environments dicovered by registry --- src/main/registry.ts | 45 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/registry.ts b/src/main/registry.ts index cd572e19..3e68eea7 100644 --- a/src/main/registry.ts +++ b/src/main/registry.ts @@ -283,7 +283,7 @@ export class Registry implements IRegistry, IDisposable { sort?: boolean ): Promise { let filteredEnvs = envs.filter(env => this._pathExistsSync(env.path)); - const uniqueEnvs = this._getUniqueObjects(filteredEnvs); + const uniqueEnvs = this._getUniqueEnvs(filteredEnvs); const resolvedEnvs = await Promise.all( uniqueEnvs.map(async env => await this._resolveEnvironment(env.path)) ); @@ -820,7 +820,7 @@ export class Registry implements IRegistry, IDisposable { [], allCondas ); - const uniqueCondaRoots = this._getUniqueObjects(flattenedCondaRoots); + const uniqueCondaRoots = this._getUniquePythonPaths(flattenedCondaRoots); return uniqueCondaRoots.map(condaRootEnvPath => { const path = pythonPathForEnvPath(condaRootEnvPath, true); @@ -1110,29 +1110,30 @@ export class Registry implements IRegistry, IDisposable { } } - // Probably pretty slow, luckily won't ever be used on many values - private _getUniqueObjects(arr: T[], keyFunction?: (value: T) => V) { - if (keyFunction) { - let mappedIndices = arr.map(keyFunction).map((keyValue, index, self) => { - return self.indexOf(keyValue); - }); + private _getUniquePythonPaths(pythonPaths: string[]): string[] { + const uniquePythonPaths = new Set(); - let filteredIndices = mappedIndices.filter( - (mappedIndex, actualIndex, self) => { - return mappedIndex === actualIndex; - } - ); + return pythonPaths.filter(pythonPath => { + if (!uniquePythonPaths.has(pythonPath)) { + uniquePythonPaths.add(pythonPath); + return true; + } - let filteredValues = filteredIndices.map(index => { - return arr[index]; - }); + return false; + }); + } - return filteredValues; - } else { - return arr.filter((value, index, self) => { - return self.indexOf(value) === index; - }); - } + private _getUniqueEnvs(envs: IPythonEnvironment[]): IPythonEnvironment[] { + const uniquePythonPaths = new Set(); + + return envs.filter(env => { + if (!uniquePythonPaths.has(env.path)) { + uniquePythonPaths.add(env.path); + return true; + } + + return false; + }); } get ready(): Promise {