fix: forward process.stdin to child when no explicit input is provided
Fixes #1378
When using $cat`` or any interactive command via zx's $, the child process's stdin was not connected to the parent's stdin. This meant interactive commands could not read from the terminal, unlike standard shell behavior.
Root cause
The default stdio is 'pipe', which creates a new pipe for stdin. However, nothing connected process.stdin to the child's stdin pipe -- the child received an empty VoidStream instead.
Fix
In the ProcessPromise.run() method's start handler, after the child process spawns, process.stdin is now piped to child.stdin when all of the following conditions are met:
- No explicit
$.inputis provided - The process is not running synchronously
child.stdinexists and is not destroyed (stdio[0]is'pipe')process.stdinis readable and has not ended- The process is not receiving piped input from another
ProcessPromise
Cleanup is handled automatically: errors on child.stdin are suppressed via a noop handler, and process.stdin is unpiped when the child closes.
How to reproduce
#!/usr/bin/env zx
await $`cat`
Run the script, type something, press Enter -- previously nothing happened. Now it echoes input and responds to Ctrl+D (EOF) as expected.
Tests
-
Added integration test: spawns a child Node.js process running
$cat``, writes to its stdin, and verifies the output is forwarded correctly. -
All 28 existing stdin and pipe tests continue to pass.
-
[x] Setup Set the latest Node.js LTS version.
-
[x] Build: I've run
npm buildbefore committing and verified the bundle updates correctly. -
[x] Tests: I've run
testand confirmed all tests succeed. Added tests to cover my changes if needed. -
[x] Docs: I've added or updated relevant documentation as needed.
-
[x] Sign Commits have verified signatures and follow conventional commits spec
-
[x] CoC: My changes follow the project's coding guidelines and Code of Conduct.
-
[x] Review: This PR represents original work and is not solely generated by AI tools.