Skip to content

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

Description

@dr-js

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' ] }))"

Activity

  1. added
    docIssues and PRs related to Node.js documentation.
    on Aug 31, 2023
  2. aduh95 commented on Sep 3, 2023

    @aduh95
    Contributor

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

  3. ariel-weiss commented on Sep 4, 2023

    @ariel-weiss
    Contributor

    I'd be happy to take this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.docIssues and PRs related to Node.js documentation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions