Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC: input option in child_process.spawnSync require stdio[0] to be pipe #49417

Closed
dr-js opened this issue Aug 31, 2023 · 2 comments · Fixed by #49481
Closed

DOC: input option in child_process.spawnSync require stdio[0] to be pipe #49417

dr-js opened this issue Aug 31, 2023 · 2 comments · Fixed by #49481
Labels
child_process Issues and PRs related to the child_process subsystem. doc Issues and PRs related to the documentations.

Comments

@dr-js
Copy link
Contributor

dr-js commented Aug 31, 2023

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 content

with 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 content

but 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 output

other 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' ] }))"
@dr-js dr-js added the doc Issues and PRs related to the documentations. label Aug 31, 2023
@VoltrexKeyva VoltrexKeyva added the child_process Issues and PRs related to the child_process subsystem. label Aug 31, 2023
@aduh95
Copy link
Contributor

aduh95 commented Sep 3, 2023

Would you like to send a PR to update the docs?

@ariel-weiss
Copy link
Contributor

I'd be happy to take this one

nodejs-github-bot pushed a commit that referenced this issue Sep 6, 2023
PR-URL: #49481
Fixes: #49417
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
ruyadorno pushed a commit that referenced this issue Sep 28, 2023
PR-URL: #49481
Fixes: #49417
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
alexfernandez pushed a commit to alexfernandez/node that referenced this issue Nov 1, 2023
PR-URL: nodejs#49481
Fixes: nodejs#49417
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. doc Issues and PRs related to the documentations.
Projects
None yet
4 participants