Skip to content

Commit 6e812b2

Browse files
juliusmarmingecodex
andcommitted
Continue PATH search after probe failures
Co-authored-by: codex <[email protected]>
1 parent c09b5a3 commit 6e812b2

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ 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";
67
import * as PlatformError from "effect/PlatformError";
78
import { describe, expect, it, vi } from "vite-plus/test";
89

@@ -371,7 +372,46 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
371372
}),
372373
);
373374

374-
it.effect("preserves filesystem failures while probing PATH candidates", () =>
375+
it.effect("continues past a failed PATH probe when a later executable exists", () =>
376+
Effect.gen(function* () {
377+
const fileSystem = yield* FileSystem.FileSystem;
378+
const path = yield* Path.Path;
379+
const baseDirectory = yield* fileSystem.makeTempDirectoryScoped({
380+
prefix: "t3-command-resolution-",
381+
});
382+
const blockedDirectory = path.join(baseDirectory, "blocked");
383+
const workingDirectory = path.join(baseDirectory, "working");
384+
const blockedCandidate = path.join(blockedDirectory, "available-command");
385+
const workingCandidate = path.join(workingDirectory, "available-command");
386+
yield* fileSystem.makeDirectory(blockedDirectory);
387+
yield* fileSystem.makeDirectory(workingDirectory);
388+
yield* fileSystem.writeFileString(workingCandidate, "#!/bin/sh\nexit 0\n");
389+
yield* fileSystem.chmod(workingCandidate, 0o755);
390+
391+
const cause = PlatformError.systemError({
392+
_tag: "PermissionDenied",
393+
module: "FileSystem",
394+
method: "stat",
395+
pathOrDescriptor: blockedCandidate,
396+
});
397+
const failingFileSystem = FileSystem.FileSystem.of({
398+
...fileSystem,
399+
stat: (candidate) =>
400+
candidate === blockedCandidate ? Effect.fail(cause) : fileSystem.stat(candidate),
401+
});
402+
403+
const resolved = yield* resolveCommandPath("available-command", {
404+
env: { PATH: `${blockedDirectory}:${workingDirectory}` },
405+
}).pipe(
406+
Effect.provideService(HostProcessPlatform, "linux"),
407+
Effect.provideService(FileSystem.FileSystem, failingFileSystem),
408+
);
409+
410+
expect(resolved).toBe(workingCandidate);
411+
}),
412+
);
413+
414+
it.effect("preserves filesystem failures after exhausting PATH candidates", () =>
375415
Effect.gen(function* () {
376416
const fileSystem = yield* FileSystem.FileSystem;
377417
const cause = PlatformError.systemError({

‎packages/shared/src/shell.ts‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -551,18 +551,26 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
551551
windowsPathExtensions,
552552
path.extname,
553553
);
554+
let firstProbeFailure: PlatformError.PlatformError | undefined;
555+
const probeCandidate = (candidatePath: string) =>
556+
isExecutableFile(candidatePath, platform, windowsPathExtensions).pipe(
557+
Effect.catch((cause) => {
558+
firstProbeFailure ??= cause;
559+
return Effect.succeed(false);
560+
}),
561+
);
554562

555563
if (command.includes("/") || command.includes("\\")) {
556564
for (const candidate of commandCandidates) {
557-
if (
558-
yield* isExecutableFile(candidate, platform, windowsPathExtensions).pipe(
559-
Effect.mapError((cause) => new CommandResolutionError({ command, platform, cause })),
560-
)
561-
) {
565+
if (yield* probeCandidate(candidate)) {
562566
return candidate;
563567
}
564568
}
565-
return yield* new CommandResolutionError({ command, platform });
569+
return yield* new CommandResolutionError({
570+
command,
571+
platform,
572+
...(firstProbeFailure === undefined ? {} : { cause: firstProbeFailure }),
573+
});
566574
}
567575

568576
const pathValue = resolvePathEnvironmentVariable(env);
@@ -580,16 +588,16 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
580588
for (const pathEntry of pathEntries) {
581589
for (const candidate of commandCandidates) {
582590
const candidatePath = path.join(pathEntry, candidate);
583-
if (
584-
yield* isExecutableFile(candidatePath, platform, windowsPathExtensions).pipe(
585-
Effect.mapError((cause) => new CommandResolutionError({ command, platform, cause })),
586-
)
587-
) {
591+
if (yield* probeCandidate(candidatePath)) {
588592
return candidatePath;
589593
}
590594
}
591595
}
592-
return yield* new CommandResolutionError({ command, platform });
596+
return yield* new CommandResolutionError({
597+
command,
598+
platform,
599+
...(firstProbeFailure === undefined ? {} : { cause: firstProbeFailure }),
600+
});
593601
});
594602

595603
export const resolveCommandPath = Effect.fn("shell.resolveCommandPath")(function* (

0 commit comments

Comments
 (0)