-
Notifications
You must be signed in to change notification settings - Fork 7.3k
child_process.spawn fails on Windows given a space in both the command and an argument #25895
Comments
I think this happens because Windows tries to execute spawn('"command with spaces.cmd"', ['nospaces']); |
Attempting to use You can see this for yourself by updating the repro above with: var cmd = spaceInCmd ? '"command file.cmd"' : 'commandfile.cmd'; Which yields:
Note that |
But aside that problem, why would you even need spaces in the arguments? Could you provide an example? |
Sure, an example command that works on the command line might look like:
Which would most naturally translate to: spawn('C:\path to test runner\run-tests.cmd',
['--input', 'C:\path to source\tests\test-file.js'],
{ stdio: 'inherit' }); You can potentially hack around this in a number of ways, depending on the circumstances -- in this instance in particular, spawn('run-tests.cmd',
['--input', 'C:\path to source\tests\test-file.js'],
{ stdio: 'inherit', cwd: 'C:\path to test runner' }); ...apart from any effects of having a different working directory when executing the command file. However, I have not been able to imagine a workaround that would work with this situation if the actual filenames contained spaces, not just the directories. Well, other than renaming the files, if that's within your control. More specifically, I encountered this problem within |
I've similarly run into this issue using |
Another sort-of workaround is to use "PROGRA~1" instead of "Program Files" in paths. This probably doesn't work in every Windows configuration, though. |
I could not figure out how to use spawn() to call Maybe my issue is different because I'm using If I call spawn with arguments
I get
It seems, Windows treats entire string as a command, but has stripped the start quote and the end quote, so the command instead of being
becomes
If I remove If I try single quotes At first I thought that the /s flag is the culprit, but removing it didn't help at all. It turned out that /s strips redundant quotes only inside the string but the outer quotes are never preserved. MSDN docs say:
So, finally it came to me that I should always enforce additional quotes like this:
Now finally I can execute files on paths with spaces. Later I found superspawn library, which is using similar approach: Yeah, Windows is weird. But I guess this workaround with piping everything through cmd shell and enforcing additional quotes around entire command string should work with any command, theoretically. I've yet to port my node.js scripts to Linux soon, so I'll see what challenges it will bring... |
I see exactly the same issue in Node.js v5.10.1, but I see that this is the |
Repro: https://gist.github.com/smrq/f028b22bc748af9e68a7
The gist of this issue is that on Windows,
child_process.spawn
handles the command incorrectly when both it and one of its arguments contains a space. So, this works fine:But this yields
'command' is not recognized as an internal or external command, operable program or batch file.
:Tested on Windows 7 with node 0.12.7.
The text was updated successfully, but these errors were encountered: