nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
107.39k stars 29.51k forks source link

Cannot spawn command on Windows when path has special characters #31346

Closed joeywatts closed 3 months ago

joeywatts commented 4 years ago

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:

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.

Hakerh400 commented 4 years ago
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
huseyinacacak-janea commented 5 months ago

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?

StefanStojanovic commented 3 months ago

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.