Skip to content

child_process.spawnSync() seems to be truncating arguments containing '&' #6702

Closed

Description

@jasholl
  • Version: 5.10.1, 6.1.0
  • Platform: Win 10 x64
  • Subsystem:

Using the child_process.spawnSync() function call, the args I pass are being cut if they contain an & in them. Here's my snippet:

var child=require('child_process');
var args =[];
args [0]="a";
args [1]="b";
args [2]="\"a&b\"";
var r = child.spawnSync("c:/temp/test.cmd", args);
r.stdout.toString();

Here's the output:

> var child=require('child_process');
undefined
> var args =[];
undefined
> args [0]="a";
'a'
> args [1]="b";
'b'
> args [2]="\"a&b\"";
'"a&b"'
> var r = child.spawnSync("c:/temp/test.cmd", args);
undefined
> r.stdout.toString();
'\r\nC:\\Program Files\\nodejs6.1.0>echo args=a b "\\"a \r\nargs=a b "\\"a\r\n'

Notice that the 3rd arg, "a&b" is passed only as "a

here's the contents of test.cmd:

echo args=%*

And here's the same output from commandline:

C:\temp>test a b "a&b"

C:\temp>echo args=a b "a&b"
args=a b "a&b"

[edit: fixed formatting - bnoordhuis]

Activity

  1. added
    child_process
    windows
    on May 11, 2016
  2. bnoordhuis commented on May 12, 2016

    @bnoordhuis
    Member

    Does it work when you pass { windowsVerbatimArguments: true } as the options?

  3. jasholl commented on May 12, 2016

    @jasholl
    Author

    Yes! Thank you!

  4. bnoordhuis commented on May 12, 2016

    @bnoordhuis
    Member

    @piscisaureus You introduced windowsVerbatimArguments back in 2011 in 567470b. Is there a reason it's not the default on Windows?

  5. piscisaureus commented on May 12, 2016

    @piscisaureus
    Contributor
    • spawning .bat and .cmd files directly has never been reliable in node and isn't supported. The issue is, as you have experienced, that arguments aren't passed reliably. To spawn a bat file, my recommendation would be to explicitly run cmd.exe with the batch file as an argument. If you use the exec() API it'll work.
    • the reason that windowsVerbatimArguments isn't the default is that when you pass arguments with spaces ["foo bar", "baz quux"], I'd like the spawned process to see that list of arguments, rather than ["foo", "bar", "baz", "quux"]. With windowsVerbatimArguments on one would get the latter.
  6. added
    doc
    and removed
    doc
    on May 12, 2016
  7. bnoordhuis commented on May 12, 2016

    @bnoordhuis
    Member

    Fair enough. I see now that the child process documentation has a section on .bat and .cmd files and recommends using { shell: true }. I guess there is nothing left to do but close the issue.

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_processwindows

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions