TypeScript
fix(server): report spawn ENOTDIR as a spawn error instead of a defect (#14345)
Gabriel Rojasbafadf324c9f
diff --git a/apps/server/src/processRunner.test.ts b/apps/server/src/processRunner.test.tsindex ac752da167b..13e811baedf 100644--- a/apps/server/src/processRunner.test.ts+++ b/apps/server/src/processRunner.test.ts@@ -202,6 +202,33 @@ describe("runProcess", () => {}),);+ it.effect("types spawn failures that the platform spawner throws synchronously", () =>+ Effect.gen(function* () {+ // A non-directory entry on PATH makes node's spawn throw ENOTDIR+ // synchronously. The platform spawner calls it inside `Effect.callback`,+ // so the throw arrives as a defect rather than a typed error.+ const defect = Object.assign(new Error("spawn jj ENOTDIR"), { code: "ENOTDIR" });+ const spawner = makeSpawner(() =>+ Effect.callback<never, never>(() => {+ throw defect;+ }),+ );++ const error = yield* runWith(spawner)({+ command: "jj",+ args: ["--version"],+ cwd: "/repo",+ }).pipe(Effect.flip);++ expect(error._tag).toBe("ProcessSpawnError");+ if (error._tag !== "ProcessSpawnError") {+ return expect.fail("Expected ProcessSpawnError");+ }+ expect(error).toMatchObject({ command: "jj", argumentCount: 1, cwd: "/repo" });+ expect(error.cause).toBe(defect);+ }),+ );+it.effect("fails when output exceeds max buffer in default mode", () =>Effect.gen(function* () {const spawner = makeSpawner(() => Effect.succeed(makeHandle({ stdout: "x".repeat(2048) })));diff --git a/apps/server/src/processRunner.ts b/apps/server/src/processRunner.tsindex 7c6de82e17a..3ee8232688c 100644--- a/apps/server/src/processRunner.ts+++ b/apps/server/src/processRunner.ts@@ -281,6 +281,18 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (input.env === undefined ? {} : { env: input.env, extendEnv },);+ const toSpawnError = (cause: unknown) =>+ new ProcessSpawnError({+ command: input.command,+ argumentCount: input.args.length,+ cwd: input.cwd,+ spawnCwd: input.spawnCwd,+ resolvedCommand: spawnCommand.command,+ resolvedArgumentCount: spawnCommand.args.length,+ shell: spawnCommand.shell,+ cause,+ });+const child = yield* spawner.spawn(ChildProcess.make(spawnCommand.command, spawnCommand.args, {@@ -295,19 +307,11 @@ const runProcessCore = Effect.fn("processRunner.runProcessCore")(function* (}),).pipe(- Effect.mapError(- (cause) =>- new ProcessSpawnError({- command: input.command,- argumentCount: input.args.length,- cwd: input.cwd,- spawnCwd: input.spawnCwd,- resolvedCommand: spawnCommand.command,- resolvedArgumentCount: spawnCommand.args.length,- shell: spawnCommand.shell,- cause,- }),- ),+ Effect.mapError(toSpawnError),+ // Node's spawn can also throw synchronously, e.g. ENOTDIR when a PATH+ // entry is a file. That arrives as a defect, which `mapError` never sees,+ // so callers that recover from spawn failures would be bypassed.+ Effect.catchDefect((cause) => Effect.fail(toSpawnError(cause))),);const stdin = input.stdin;