-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Description
Affected URL(s)
https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options
Description of the problem
to use the input option in child_process.spawnSync also need the stdio[0] option to be set as 'pipe'
current doc (v20.5.1) specifies:
input <...> The value which will be passed as stdin to the spawned process. Supplying this value will override
stdio[0].
the actual behavior is more like:
Supplying this value will need
stdio[0]to be'pipe'to be overridden.
test done on Linux, and Win+GitBash with node v18.14.0 and v20.2.0
baseline test command:
echo "text file content" > test.txt
echo "cli content" | cat - test.txt
# will output
# > cli content
# > text file contentwith nodejs input with default stdio ('pipe') works:
echo "text file content" > test.txt
node -p "String(child_process.spawnSync('cat', [ '-', 'test.txt' ], { input: 'cli content\n' }).stdout)"
# will output
# > cli content
# > text file contentbut setting stdio[0] to 'ignore' or 'inherit'
will actually override input option
node -p "String(child_process.spawnSync('cat', [ '-', 'test.txt' ], { input: 'cli content\n', stdio: [ 'ignore' ] }).stdout)"
# will only output
# > text file content
node -p "String(child_process.spawnSync('cat', [ '-', 'test.txt' ], { input: 'cli content\n', stdio: [ 'inherit' ] }).stdout)"
# will wait for user input, use "Ctrl+D" to end input and see partial outputother execFileSync and execSync in synchronous-process-creation also have similar behavior:
# work
node -p "String(child_process.execFileSync('/usr/bin/cat', [ '-', 'test.txt' ], { input: 'cli content\n' }))"
# not work
node -p "String(child_process.execFileSync('/usr/bin/cat', [ '-', 'test.txt' ], { input: 'cli content\n', stdio: [ 'ignore' ] }))"
node -p "String(child_process.execFileSync('/usr/bin/cat', [ '-', 'test.txt' ], { input: 'cli content\n', stdio: [ 'inherit' ] }))"
# work
node -p "String(child_process.execSync('cat - test.txt', { input: 'cli content\n' }))"
# not work
node -p "String(child_process.execSync('cat - test.txt', { input: 'cli content\n', stdio: [ 'ignore' ] }))"
node -p "String(child_process.execSync('cat - test.txt', { input: 'cli content\n', stdio: [ 'inherit' ] }))"