Skip to content

Commit faa22fc

Browse files
fix(shell): retain case-sensitive executable candidates during PATH deduplication
1 parent 5564f3f commit faa22fc

2 files changed

Lines changed: 69 additions & 32 deletions

File tree

‎packages/shared/src/shell.test.ts‎

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { it as effectIt } from "@effect/vitest";
33
import { HostProcessEnvironment, HostProcessPlatform } from "@t3tools/shared/hostProcess";
44
import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
6+
import * as Path from "effect/Path";
7+
import * as TestClock from "effect/testing/TestClock";
68
import { describe, expect, it, vi } from "vite-plus/test";
79

810
import {
@@ -411,8 +413,8 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
411413
PATHEXT: ".COM;.EXE",
412414
});
413415

414-
// Two distinct directories, two extensions. The repeats cost nothing.
415-
expect(probed).toHaveLength(4);
416+
// Two directories, two extensions, upper and lowercase spellings.
417+
expect(probed).toHaveLength(8);
416418
expect(new Set(probed).size).toBe(probed.length);
417419
}),
418420
);
@@ -427,21 +429,71 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
427429
// Deliberately not folded together. Windows 10+ can mark a directory
428430
// case-sensitive, so the two spellings are not provably one directory and
429431
// skipping the second could hide a command that is really there.
430-
expect(probed).toHaveLength(4);
432+
expect(probed).toHaveLength(8);
431433
}),
432434
);
433435

434-
it.effect("probes one spelling per PATHEXT entry", () =>
436+
it.effect.each(["audit-command", "audit-command.CMD"])(
437+
"resolves lowercase executable files for %s in a case-sensitive Windows PATH directory",
438+
(command) =>
439+
Effect.gen(function* () {
440+
const fs = yield* FileSystem.FileSystem;
441+
const path = yield* Path.Path;
442+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-case-sensitive-path-" });
443+
const executable = path.join(cwd, "audit-command.cmd");
444+
yield* fs.writeFileString(executable, "@echo off\n");
445+
446+
const resolved = yield* resolveCommandPath(command, {
447+
env: { PATH: cwd, PATHEXT: ".CMD" },
448+
}).pipe(
449+
Effect.provideService(HostProcessPlatform, "win32"),
450+
Effect.provideService(CommandResolutionCache, new Map()),
451+
Effect.provideService(FileSystem.FileSystem, {
452+
...fs,
453+
// Keep this case-sensitive fixture portable to case-insensitive hosts.
454+
stat: (filePath) =>
455+
fs.stat(filePath === executable ? filePath : path.join(cwd, "missing")),
456+
}),
457+
);
458+
459+
expect(resolved).toBe(executable);
460+
}),
461+
);
462+
463+
it.effect("keeps cached misses until expiry while allowing explicit paths", () =>
464+
Effect.gen(function* () {
465+
const fs = yield* FileSystem.FileSystem;
466+
const path = yield* Path.Path;
467+
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-path-cache-" });
468+
const executable = path.join(cwd, "appeared.CMD");
469+
const options = { env: { PATH: `${cwd};${cwd}`, PATHEXT: ".CMD" } };
470+
471+
expect((yield* resolveCommandPath("appeared", options).pipe(Effect.result))._tag).toBe(
472+
"Failure",
473+
);
474+
yield* fs.writeFileString(executable, "@echo off\n");
475+
expect((yield* resolveCommandPath("appeared", options).pipe(Effect.result))._tag).toBe(
476+
"Failure",
477+
);
478+
expect(yield* resolveCommandPath(executable, options)).toBe(executable);
479+
yield* TestClock.adjust("30 seconds");
480+
expect(yield* resolveCommandPath("appeared", options)).toBe(executable);
481+
}).pipe(
482+
Effect.provideService(HostProcessPlatform, "win32"),
483+
Effect.provideService(CommandResolutionCache, new Map()),
484+
),
485+
);
486+
487+
it.effect("keeps upper and lowercase PATHEXT candidates", () =>
435488
Effect.gen(function* () {
436489
const probed = yield* recordProbes({
437490
PATH: "C:\\bin",
438491
PATHEXT: ".COM;.EXE;.BAT;.CMD",
439492
});
440493

441-
// Four extensions, not eight: Windows matches file names
442-
// case-insensitively, so the lowercase spellings are the same question.
443-
expect(probed).toHaveLength(4);
494+
expect(probed).toHaveLength(8);
444495
expect(probed.filter((filePath) => /\.(COM|EXE|BAT|CMD)$/.test(filePath))).toHaveLength(4);
496+
expect(probed.filter((filePath) => /\.(com|exe|bat|cmd)$/.test(filePath))).toHaveLength(4);
445497
}),
446498
);
447499
});

‎packages/shared/src/shell.ts‎

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -479,18 +479,19 @@ function resolveCommandCandidates(
479479

480480
if (extension.length > 0 && windowsPathExtensions.includes(normalizedExtension)) {
481481
const commandWithoutExtension = command.slice(0, -extension.length);
482-
return Array.from(new Set([command, `${commandWithoutExtension}${normalizedExtension}`]));
482+
return Array.from(
483+
new Set([
484+
command,
485+
`${commandWithoutExtension}${normalizedExtension}`,
486+
`${commandWithoutExtension}${normalizedExtension.toLowerCase()}`,
487+
]),
488+
);
483489
}
484490

485-
// Windows resolves file names case-insensitively, so probing `code.EXE` and
486-
// `code.exe` costs two syscalls to ask the filesystem one question. Both
487-
// consumers of the resolved path re-normalize the extension themselves -
488-
// `isExecutableFile` uppercases it before the PATHEXT check, and
489-
// `resolveSpawnCommand` lowercases it before the .cmd/.bat check - so the
490-
// uppercase spelling alone is enough.
491491
const candidates: string[] = [];
492492
for (const candidateExtension of windowsPathExtensions) {
493493
candidates.push(`${command}${candidateExtension}`);
494+
candidates.push(`${command}${candidateExtension.toLowerCase()}`);
494495
}
495496
return Array.from(new Set(candidates));
496497
}
@@ -543,11 +544,7 @@ function cacheCommandResolution(
543544
});
544545
}
545546

546-
// Untraced on purpose. This runs once per (PATH directory x candidate name),
547-
// which is tens of thousands of spans for a single editor-discovery pass -
548-
// enough to dominate both the scan and the local trace file it writes to. The
549-
// enclosing `shell.resolveCommandPathForPlatform` span already reports every
550-
// lookup and its outcome.
547+
// Trace each command lookup, not every candidate file it probes.
551548
const isExecutableFile = Effect.fnUntraced(function* (
552549
filePath: string,
553550
platform: NodeJS.Platform,
@@ -609,19 +606,7 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
609606
return cached.resolvedPath;
610607
}
611608

612-
// A Windows PATH routinely names the same directory more than once: the user
613-
// and machine values overlap, and `resolveKnownWindowsCliDirs` appends
614-
// entries that are frequently already present. Visiting a directory twice
615-
// inside one walk cannot produce a different answer, so drop the repeats
616-
// before spending syscalls on them.
617-
//
618-
// Entries are compared verbatim rather than through
619-
// `normalizePathEntryForComparison`. That helper case-folds on Windows, which
620-
// is right when it picks which spelling of an entry to keep, but skipping a
621-
// probe is a stronger claim: Windows 10+ can mark a directory case-sensitive
622-
// (`fsutil file setCaseSensitiveInfo`), so `C:\Tools` and `C:\tools` are not
623-
// provably the same directory. Exact matching still drops 15 of the 75
624-
// entries on my PATH, and it cannot hide a command that exists.
609+
// Keep case variants: Windows can make PATH directories case-sensitive.
625610
const pathEntries: string[] = [];
626611
const seenPathEntries = new Set<string>();
627612
for (const entry of pathValue.split(pathDelimiterForPlatform(platform))) {

0 commit comments

Comments
 (0)