Skip to content

Commit 1c53df7

Browse files
fix(server): retain shell fallback after synchronous spawn failures
1 parent 57518b4 commit 1c53df7

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Exercise synchronous spawn failures at the native probe boundary.
2+
import * as NodeChildProcess from "node:child_process";
13
import * as NodeServices from "@effect/platform-node/NodeServices";
24
import { it as effectIt } from "@effect/vitest";
35
import * as HostProcess from "@t3tools/shared/HostProcess";
@@ -37,6 +39,11 @@ import {
3739
type WindowsShellEnvironmentReader,
3840
} from "./shell.ts";
3941

42+
vi.mock("node:child_process", async (importOriginal) => {
43+
const original = await importOriginal<typeof NodeChildProcess>();
44+
return { ...original, execFile: vi.fn(original.execFile) };
45+
});
46+
4047
const withWindowsEnvironmentMocks = <A, E, R>(
4148
effect: Effect.Effect<A, E, R>,
4249
readEnvironment: (
@@ -246,6 +253,33 @@ describe("readEnvironmentFromWindowsShell", () => {
246253
}),
247254
);
248255

256+
effectIt.layer(NodeServices.layer)("synchronous spawn failures", (it) => {
257+
it.effect("tries both shells and preserves baseline PATH repair", () =>
258+
Effect.gen(function* () {
259+
const execFile = vi.mocked(NodeChildProcess.execFile);
260+
execFile.mockImplementationOnce(() => {
261+
throw new Error("spawn EPERM");
262+
});
263+
execFile.mockImplementationOnce(() => {
264+
throw new Error("spawn EINVAL");
265+
});
266+
try {
267+
const patch = yield* resolveWindowsEnvironment({
268+
PATH: "C:\\Windows",
269+
APPDATA: "C:\\Roaming",
270+
}).pipe(Effect.provideService(CommandAvailability, () => Effect.succeed(true)));
271+
expect(patch).toEqual({ PATH: "C:\\Windows;C:\\Roaming\\npm" });
272+
expect(execFile.mock.calls.slice(-2).map(([file]) => file)).toEqual([
273+
"pwsh.exe",
274+
"powershell.exe",
275+
]);
276+
} finally {
277+
execFile.mockReset();
278+
}
279+
}),
280+
);
281+
});
282+
249283
effectIt.effect("returns an empty environment after both shell candidates fail", () =>
250284
Effect.gen(function* () {
251285
const execFile = vi.fn(() => Effect.succeed(Option.none<string>()));

‎packages/shared/src/shell.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,20 @@ const execWindowsEnvironmentProbe = (
309309
options: { encoding: "utf8"; timeout: number },
310310
) =>
311311
Effect.callback<Option.Option<string>>((resume) => {
312-
const child = NodeChildProcess.execFile(
313-
file,
314-
args,
315-
{ ...options, windowsHide: true },
316-
(error, stdout) => {
317-
resume(Effect.succeed(error ? Option.none() : Option.some(stdout)));
318-
},
319-
);
312+
let child: NodeChildProcess.ChildProcess;
313+
try {
314+
child = NodeChildProcess.execFile(
315+
file,
316+
args,
317+
{ ...options, windowsHide: true },
318+
(error, stdout) => {
319+
resume(Effect.succeed(error ? Option.none() : Option.some(stdout)));
320+
},
321+
);
322+
} catch {
323+
resume(Effect.succeedNone);
324+
return;
325+
}
320326
return Effect.sync(() => {
321327
child.kill();
322328
});

0 commit comments

Comments
 (0)