5

I'm using Git Bash for Windows (as in, I right-click in some directory and select "Git Bash here" from the context menu). Node.js v5.10.1.

Here's my complete Node.js script, example.js:

console.log(process.argv);

Here's my command line and the output:

$ node example.js "https://example.com"
[ 'C:\\...\\node.exe',
  'C:\\...\\example.js',
  'https:\\example.com' ]

Notice how the input argument "https://example.com" has two literal forward slashes, but the resulting string "https:\\example.com" has one literal backslash.

I have found that no amount of backslash-escaping (e.g. "https:\\/example.com") or additional forward slashes (e.g. "https:////////example.com") in the command line can stop this from happening. The result is always one literal backslash.

Using the same Node.js executable to run the same script from an ordinary Windows cmd.exe window does not cause this behaviour. Running an equivalent Python script from Git Bash for Windows also does not cause this behaviour.

Update

Watch this:

$ node.exe -e "console.log(process.argv)" "https://example.com"
[ 'C:\\...\\node.exe',
  'https://example.com' ]

$ node -e "console.log(process.argv)" "https://example.com"
[ 'C:\\...\\node.exe',
  'https:\\example.com' ]

Why does removing ".exe" from the end of the call cause the slashes to reverse? I am using NVM 1.1.0, if that is relevant. which node.exe and which node return identical results, both pointing to that EXE file. There is no separate file named node in that directory. This happens on both Node.js 5.10.1 and Node.js 6.0.0.

qntm
  • 4,147
  • 4
  • 27
  • 41
  • Just tested on Windows with node 6.0.0 and git bash. Nothing odd here: `node.exe -e 'console.log(process.argv)' 'http://example.com'` – Andreas Louv May 05 '16 at 13:25
  • I think this might be a problem with my environment. It's intermittent. – qntm May 05 '16 at 15:01
  • What happens when you run the command I provided? – Andreas Louv May 05 '16 at 15:07
  • It works okay. But I think `nvm use 6.0.0` followed by `nvm use 5.10.1` has made the problem go away entirely somehow. If I could, I'd hide this question until I know more. – qntm May 05 '16 at 15:35
  • It ssems to vary depending on whether I run `node.exe` or just `node`. I've updated the question to highlight this. – qntm May 05 '16 at 15:54
  • I think you found a "bug" (unexpected behavior) which you should address to the developers. – Andreas Louv May 05 '16 at 16:01
  • I do have the same behaviour using node 4.4.3 and git bash version 2.8.1.windows.1. Did you get any working solution or just stay with the workaround of using the windows cmd? – phwa4563 May 09 '16 at 09:01
  • 1
    The workaround I found is to use `node.exe` at the Git Bash for Windows command line, instead of using `node`. – qntm May 20 '16 at 18:31
  • This is one of those posts where I'm so thankful I found it because it means I'm not going crazy. Ran into this exact issue when trying to use `yarn` instead of `npm` for node package management. The 'exe' suffix on the node.exe allows it to accept cmd line args with forward slashes when running under Git Bash – Twicetimes Mar 01 '17 at 22:50
  • Got the same issue when using redis-cli and .sh scripts in git bash for windows. I was passing in an argument to redis-cli of "redis-cli set /keyvalue/stuff "myvalue", and then git bash would prepend c:\mygitdirectory to the /keyvalue/stuff when I viewed the output in redis-cli keys *. – joezen777 Apr 27 '17 at 16:04
  • This may apply to some http://stackoverflow.com/questions/7250130/how-to-stop-mingw-and-msys-from-mangling-path-names-given-at-the-command-line – joezen777 Apr 27 '17 at 16:29
  • Have you tried using single quotes (`'https://example.com'`)? – Shammel Lee Jun 03 '17 at 21:44

1 Answers1

1

Git Bash is based on MSYS2, which does a "POSIX path conversion" like MinGW does. This conversion windows-ifys paths with slashes when calling windows-looking executables. MSYS2 might have done some "Windows-looking" detection in a way different from MinGW.

The question joezen777 mentioned in comments points to a solution: just add ;https:;http: to MSYS2_ARG_CONV_EXCL. The documentation is available at https://github.com/msys2/msys2/wiki/Porting.

Mingye Wang
  • 1,107
  • 9
  • 32