diff --git a/cli/tests/unit_node/child_process_test.ts b/cli/tests/unit_node/child_process_test.ts index d4a2a4cc68..f8de5b6f6d 100644 --- a/cli/tests/unit_node/child_process_test.ts +++ b/cli/tests/unit_node/child_process_test.ts @@ -600,6 +600,28 @@ Deno.test( }, ); +Deno.test( + "[node/child_process spawn] supports stdio [0, 1, 2] option", + async () => { + const cmdFinished = deferred(); + let output = ""; + const script = path.join( + path.dirname(path.fromFileUrl(import.meta.url)), + "testdata", + "child_process_stdio_012.js", + ); + const cp = spawn(Deno.execPath(), ["run", "-A", script]); + cp.stdout?.on("data", (data) => { + output += data; + }); + cp.on("close", () => cmdFinished.resolve()); + await cmdFinished; + + assertStringIncludes(output, "foo"); + assertStringIncludes(output, "close"); + }, +); + Deno.test({ name: "[node/child_process spawn] supports SIGIOT signal", ignore: Deno.build.os === "windows", diff --git a/cli/tests/unit_node/testdata/child_process_stdio_012.js b/cli/tests/unit_node/testdata/child_process_stdio_012.js new file mode 100644 index 0000000000..682d8a084a --- /dev/null +++ b/cli/tests/unit_node/testdata/child_process_stdio_012.js @@ -0,0 +1,15 @@ +import childProcess from "node:child_process"; +import process from "node:process"; +import * as path from "node:path"; + +const script = path.join( + path.dirname(path.fromFileUrl(import.meta.url)), + "node_modules", + "foo", + "index.js", +); + +const child = childProcess.spawn(process.execPath, [script], { + stdio: [0, 1, 2], +}); +child.on("close", () => console.log("close")); diff --git a/ext/node/polyfills/internal/child_process.ts b/ext/node/polyfills/internal/child_process.ts index 365af4add6..d4acf1db2a 100644 --- a/ext/node/polyfills/internal/child_process.ts +++ b/ext/node/polyfills/internal/child_process.ts @@ -469,6 +469,13 @@ function normalizeStdioOption( ...Array, ] { if (Array.isArray(stdio)) { + // `[0, 1, 2]` is equivalent to `"inherit"` + if ( + stdio.length === 3 && stdio[0] === 0 && stdio[1] === 1 && stdio[2] === 2 + ) { + return ["inherit", "inherit", "inherit"]; + } + // At least 3 stdio must be created to match node while (stdio.length < 3) { ArrayPrototypePush(stdio, undefined);