diff --git a/apps/server/src/process/NonBlockingSpawner.test.ts b/apps/server/src/process/NonBlockingSpawner.test.ts index 81563cbab70dc..349f138f39d95 100644 --- a/apps/server/src/process/NonBlockingSpawner.test.ts +++ b/apps/server/src/process/NonBlockingSpawner.test.ts @@ -11,6 +11,7 @@ import * as Fiber from "effect/Fiber"; import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Path from "effect/Path"; +import * as Redacted from "effect/Redacted"; import * as ChildProcess from "effect/process/ChildProcess"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; @@ -29,6 +30,11 @@ const outcome = (command: ChildProcess.Command) => onSuccess: (output) => ({ output }), onFailure: (error) => ({ reason: error.reason._tag, message: error.message }), }), + Effect.catchDefect((defect) => + Effect.succeed({ + defect: defect instanceof Error ? { ...defect, message: defect.message } : defect, + }), + ), ); describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpawner", () => { @@ -81,6 +87,20 @@ describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("NonBlockingSpa ChildProcess.pipeTo(ChildProcess.make("tail", ["-n", "1"])), ), "a missing command": ChildProcess.make("t3code-missing-command", ["--version"]), + "a missing command with a regular file in PATH": ChildProcess.make("tool", [], { + env: { PATH: `${root}/fresh/tool:${root}/target` }, + }), + "a missing command with a regular file last in PATH": ChildProcess.make("tool", [], { + env: { PATH: `${root}/target:${root}/fresh/tool` }, + }), + "an explicit path through a regular file": ChildProcess.make(`${root}/fresh/tool/x`, []), + "a NUL argument takes precedence over ENOTDIR": ChildProcess.make(`${root}/fresh/tool/x`, [ + "invalid\0argument", + ]), + "a redacted NUL argument takes precedence over ENOTDIR": ChildProcess.make( + `${root}/fresh/tool/x`, + [Redacted.make("invalid\0argument")], + ), "a missing command in a cwd": ChildProcess.make("t3code-missing-command", [], { cwd: root, }), diff --git a/apps/server/src/process/NonBlockingSpawner.ts b/apps/server/src/process/NonBlockingSpawner.ts index 0807b287cb9cc..e9f7d2544a248 100644 --- a/apps/server/src/process/NonBlockingSpawner.ts +++ b/apps/server/src/process/NonBlockingSpawner.ts @@ -99,8 +99,8 @@ const cwdFailure = (cwd: string, cause: NodeJS.ErrnoException) => /** * Fails as a native spawn does when the command is missing or not executable: callers read - * that error to tell a CLI that is not installed from one that failed. execvp reports EACCES - * when a candidate exists but cannot be run, and ENOENT otherwise. + * that error to tell a CLI that is not installed from one that failed. When PATH is exhausted, + * execvp reports EACCES if any candidate was denied, or the last lookup error otherwise. */ const findExecutable = Effect.fnUntraced(function* ( command: ChildProcess.StandardCommand, @@ -114,18 +114,25 @@ const findExecutable = Effect.fnUntraced(function* ( : (childPath(command.options) ?? DEFAULT_PATH) .split(":") .map((entry) => `${fromCwd(entry || ".")}/${command.command}`); - let denied = false; + let failure: "ENOENT" | "EACCES" | "ENOTDIR" = "ENOENT"; for (const candidate of candidates) { const error = yield* check( candidate.slice(0, candidate.lastIndexOf("/")) || "/", candidate, NodeFS.constants.X_OK, ); + const code = error?.code; // Any other errno is left for `env` to report through its exit code. - if (error === undefined || (error.code !== "ENOENT" && error.code !== "EACCES")) return; - denied ||= error.code === "EACCES"; + if (code !== "ENOENT" && code !== "EACCES" && code !== "ENOTDIR") return; + if (failure !== "EACCES") failure = code; } - return yield* spawnFailure(command, denied ? "EACCES" : "ENOENT"); + if (failure === "ENOTDIR") { + // Node throws synchronously here, and callers recognize ENOTDIR on the defect itself. + return yield* Effect.die( + Object.assign(new Error("spawn ENOTDIR"), { errno: -20, code: "ENOTDIR", syscall: "spawn" }), + ); + } + return yield* spawnFailure(command, failure); }); const make = Effect.gen(function* () { @@ -138,7 +145,14 @@ const make = Effect.gen(function* () { const prepareStandard = Effect.fnUntraced(function* (input: ChildProcess.StandardCommand) { const { cwd: givenCwd, shell, ...options } = input.options; // Effect rejects a shell command with arguments, and a NUL, itself. - if ((shell && input.args.length > 0) || input.command.includes("\0")) return input; + if ( + (shell && input.args.length > 0) || + [input.command, ...input.args].some((arg) => + (Redacted.isRedacted(arg) ? Redacted.value(arg) : arg).includes("\0"), + ) + ) { + return input; + } // Node's shell mode on Unix, spelled out so the trampoline can run it. const command = shell ? ChildProcess.make(typeof shell === "string" ? shell : "/bin/sh", ["-c", input.command])