node
node copied to clipboard
Cannot spawn command on Windows when path has special characters
- Version: 10.15.3
- Platform: Windows 10 x64
-
Subsystem:
child_process
I'm attempting to use child_process.spawn
(or spawnSync
) to launch a command directly using an absolute path (bypassing the shell with shell: false
). I was able to successfully launch commands at the following paths:
- "C:\test\test.CMD"
- "C:\test - Copy\test.CMD"
- "C:\test (npm)\test.CMD"
But when I have special characters (like parenthesis) and no spaces - "C:\test(npm)\test.CMD", it fails to launch the command with the following in stderr
:
'C:\test' is not recognized as an internal or external command,
operable program or batch file.
Using procmon, I was able to identify that node was launching cmd.exe
using the /c
flag to start my command. Why is it not just launching my command directly (shell: false
implies to me that a shell is not used, but it's using cmd.exe
here)?
It does not appear to be properly quoting "C:\test(npm)\test.CMD" even though it was able to quote other paths containing parenthesis as long as they also contained spaces.
Outdated comment (click to show the content)
Seems that parentheses need to be escaped using an extra pair of quotes. Potential fix in https://github.com/libuv/libuv/pull/2627
I've reproduced this issue with newer Node versions.
Parenthesis are special characters for cmd.exe
and must be escaped to be used. Adding logic to escape special characters for a specific operating system would increase the complexity of the code and its maintainability. This doesn't seem like something Node.js should be doing and therefore, it is the user's responsibility to escape such characters for their platforms before calling spawn
(or spawnSync
).
Please see https://nodejs.org/docs/latest/api/child_process.html#spawning-bat-and-cmd-files-on-windows
@joeywatts can you please close the issue?
Based on the explanation in the last comment, and since there has been no answer, I'll close this. If you still think this should be addressed, please reopen this issue or open a new one.