Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3
4const ENV = "/usr/bin/env";
5
6let chdirSupport: Promise<boolean> | undefined;
7
8/** Whether `/usr/bin/env` takes `-C` (GNU coreutils 8.28 and later; BusyBox does not). */
9export const supportsEnvChdir = (): Promise<boolean> =>
10 (chdirSupport ??= new Promise((resolve) =>
11 // Run against `/` and absolute paths, so the check itself cannot wait on a mount.
12 NodeChildProcess.execFile(ENV, ["-C", "/", "--", "/bin/sh", "-c", ":"], (error) =>
13 resolve(error === null),
14 ),
15 ));
16
17/**
18 * libuv's spawn blocks until the child has run chdir(cwd) and the PATH search, which a hung
19 * mount stalls. Through `env` both run after exec, with argv and environment untouched.
20 * Undefined when `env` would read the command as `NAME=value` or `-`, which `--` does not stop.
21 */
22export function envTrampoline<Arg>(
23 command: string,
24 args: ReadonlyArray<Arg>,
25 cwd: string | undefined,
26): { readonly command: string; readonly args: Array<Arg | string> } | undefined {
27 if (command === "" || command === "-" || command.includes("=")) return undefined;
28 return {
29 command: ENV,
30 args: [...(cwd === undefined ? [] : ["-C", cwd]), "--", command, ...args],
31 };
32}