fnm icon indicating copy to clipboard operation
fnm copied to clipboard

Git Bash issues with CLI tools

Open alumni opened this issue 4 years ago • 5 comments

The setup for Git Bash (Windows) cannot run cli tools that are installed using npm install -g.

$ fnm env
export PATH="C:\\Users\\XXX\\AppData\\Local\\Temp\\fnm_multishell_15584_1612875966568":$PATH
export FNM_MULTISHELL_PATH="C:\\Users\\XXX\\AppData\\Local\\Temp\\fnm_multishell_15584_1612875966568"
...

Running node works:

node -v
v15.8.0

But bash is somehow confused about the actual path:

$ which node
/c/Users/XXX/Work/neo-backend/\Users\XXX\AppData\Local\Temp\fnm_multishell_17812_1612875949353/node

And if I use any cli tool installed with npm, I'll get this:

$ npm install -g yarn
added 1 package

$ yarn 
node:internal/modules/cjs/loader:928
  throw err;
  ^

Error: Cannot find module 'C:\Program Files\Git\Users\XXX\AppData\Local\Temp\fnm_multishell_17812_1612875949353\node_modules\yarn\bin\yarn.js'

This can be fixed by making sure that the PATH env variable is using Git-Bash paths:

export PATH="/c/Users/XXX/AppData/Local/Temp/fnm_multishell_15584_1612875966568":$PATH

alumni avatar Feb 09 '21 13:02 alumni

I've been playing with fnm today and I must say it's quite fast, I like it a lot ;)

I made a workaround for this invalid PATH issue. Here's my startup (.bashrc etc) script:

eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH

if [[ -f .node-version || -f .nvmrc ]]; then
    fnm use
fi

alumni avatar Feb 09 '21 21:02 alumni

what is cygpath 😨 can we fix it upstream?

Schniz avatar Feb 10 '21 07:02 Schniz

I mean, currently the path is windows-like if you are on a Windows build, maybe it should differ based on the shell instead? And if that so, should we use cygpath? Will it be supported on every Bash on Windows? or should we differentiate between Bash and Git Bash?

Schniz avatar Feb 10 '21 07:02 Schniz

On MSYS and CygWin, cygpath converts between Windows <-> POSIX paths (replaces the path separators and drive letter: X:\some\path <-> /x/some/path), but might also map some common paths like /tmp ~~or home~~ or system folders (/usr/bin is C:\Program Files\Git\usr\bin. In this specific case: C:\Users\XXX\AppData\Local\Temp\fnm_multishell_9452_1612950115921 -> /tmp/fnm_multishell_9452_1612950115921

Git for Windows provides a stripped-down version of MSYS. The thing is that MSYS internally uses POSIX paths, and it maintains its own PATH variable in the POSIX format. It also converts CLI arguments - when executing fnm --fnm-dir ~/.fnm, fnm will think it's called like this: fnm.exe --fnm-dir C:\Users\XXX\.fnm.

On Windows, every cli command that is installed from node has two versions, e.g. yarn (shell script) and yarn.cmd (Windows Command Script). If node is started from Bash, spawning a yarn process from node will call the shell script, if it's started somehow else, it calls the cmd file.

I don't think zsh, fish or bash can ever use Windows paths. At the moment, you can only run them on Windows if you use MSYS, CygWin and WSL. I am not aware of any "native" port (well, MSYS is the only one that is running Windows-native executable code but with this path conversion).

PowerShell is probably the only one that adapts to the OS (POSIX paths on Linux).

alumni avatar Feb 10 '21 12:02 alumni

Was this issue fixed? I've been unable to get fnm working in Git Bash (it works fine in CMD and PowerShell). Even the command 'node' isn't found.

Upon checking the output of fnm env, fnm tries to mix Windows style paths and unix style paths, which can be solved with cygpath as @alumni mentions.

rashil2000 avatar Jun 17 '21 14:06 rashil2000

I've been playing with fnm today and I must say it's quite fast, I like it a lot ;)

I made a workaround for this invalid PATH issue. Here's my startup (.bashrc etc) script:

eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH

if [[ -f .node-version || -f .nvmrc ]]; then
    fnm use
fi

Thanks so much for this workaround; it worked perfectly for me.

cweekly avatar Oct 24 '22 15:10 cweekly

I've been playing with fnm today and I must say it's quite fast, I like it a lot ;)

I made a workaround for this invalid PATH issue. Here's my startup (.bashrc etc) script:

eval $(fnm env | sed 1d)
export PATH=$(cygpath $FNM_MULTISHELL_PATH):$PATH

if [[ -f .node-version || -f .nvmrc ]]; then
    fnm use
fi

Just a note. eval "$(fnm env | sed 1d)" works for me but eval $(fnm env | sed 1d) doesn't. Thanks so much for this workaround.

ModyQyW avatar Feb 28 '23 01:02 ModyQyW

I can confirm that everything is fine on Git Bash, no need for workarounds:

eval "$(fnm env --use-on-cd)"

alumni avatar Jul 03 '23 10:07 alumni

Confirmed same: as of v1.34.0, git-bash in Win10 works as designed and documented, making workarounds moot.

cweekly avatar Aug 30 '23 02:08 cweekly