Skip to content

Commit 956b533

Browse files
committed
Fix case with duplicate folders in PATH
1 parent 92467d7 commit 956b533

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

extensions/terminal-suggest/src/env/pathExecutableCache.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export class PathExecutableCache implements vscode.Disposable {
7171
// Extract executables from PATH
7272
const paths = pathValue.split(isWindows ? ';' : ':');
7373
const pathSeparator = isWindows ? '\\' : '/';
74+
const promisePaths: string[] = [];
7475
const promises: Promise<Set<ICompletionResource> | undefined>[] = [];
7576
const labels: Set<string> = new Set<string>();
7677

@@ -84,26 +85,30 @@ export class PathExecutableCache implements vscode.Disposable {
8485
}
8586
} else {
8687
// Not cached, need to scan this directory
88+
promisePaths.push(pathDir);
8789
promises.push(this._getExecutablesInPath(pathDir, pathSeparator, labels));
8890
}
8991
}
9092

9193
// Process uncached directories
9294
if (promises.length > 0) {
9395
const resultSets = await Promise.all(promises);
94-
let uncachedPathIndex = 0;
95-
96-
for (const pathDir of paths) {
96+
for (const [i, resultSet] of resultSets.entries()) {
97+
const pathDir = promisePaths[i];
9798
if (!this._cachedExes.has(pathDir)) {
98-
const resultSet = resultSets[uncachedPathIndex++];
9999
this._cachedExes.set(pathDir, resultSet || new Set());
100100
}
101101
}
102102
}
103103

104104
// Merge all results from all directories
105105
const executables = new Set<ICompletionResource>();
106+
const processedPaths: Set<string> = new Set();
106107
for (const pathDir of paths) {
108+
if (processedPaths.has(pathDir)) {
109+
continue;
110+
}
111+
processedPaths.add(pathDir);
107112
const dirExecutables = this._cachedExes.get(pathDir);
108113
if (dirExecutables) {
109114
for (const executable of dirExecutables) {

extensions/terminal-suggest/src/terminalSuggestMain.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,9 @@ export async function activate(context: vscode.ExtensionContext) {
252252
return;
253253
}
254254

255-
const [commandsInPath, shellGlobals] = await Promise.all([
256-
pathExecutableCache.getExecutablesInPath(terminal.shellIntegration?.env?.value, terminalShellType),
257-
(async () => {
258-
const executables = await pathExecutableCache.getExecutablesInPath(terminal.shellIntegration?.env?.value, terminalShellType);
259-
return getShellGlobals(terminalShellType, executables?.labels, machineId, remoteAuthority);
260-
})()
261-
]);
255+
const commandsInPath = await pathExecutableCache.getExecutablesInPath(terminal.shellIntegration?.env?.value, terminalShellType);
256+
const shellGlobals = await getShellGlobals(terminalShellType, commandsInPath?.labels, machineId, remoteAuthority);
257+
262258
const shellGlobalsArr = shellGlobals ?? [];
263259
if (!commandsInPath?.completionResources) {
264260
console.debug('#terminalCompletions No commands found in path');

extensions/terminal-suggest/src/test/env/pathExecutableCache.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import 'mocha';
7-
import { strictEqual } from 'node:assert';
7+
import { deepStrictEqual, strictEqual } from 'node:assert';
88
import type { MarkdownString } from 'vscode';
99
import { PathExecutableCache } from '../../env/pathExecutableCache';
1010

@@ -16,12 +16,12 @@ suite('PathExecutableCache', () => {
1616
strictEqual(Array.from(result!.labels!).length, 0);
1717
});
1818

19-
test('caching is working on successive calls', async () => {
19+
test('results are the same on successive calls', async () => {
2020
const cache = new PathExecutableCache();
2121
const env = { PATH: process.env.PATH };
2222
const result = await cache.getExecutablesInPath(env);
2323
const result2 = await cache.getExecutablesInPath(env);
24-
strictEqual(result, result2);
24+
deepStrictEqual(result!.labels, result2!.labels);
2525
});
2626

2727
test('refresh clears the cache', async () => {

0 commit comments

Comments
 (0)