Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git Bash issues with CLI tools #390

Closed
alumni opened this issue Feb 9, 2021 · 9 comments · Fixed by #960
Closed

Git Bash issues with CLI tools #390

alumni opened this issue Feb 9, 2021 · 9 comments · Fixed by #960

Comments

@alumni
Copy link

alumni commented Feb 9, 2021

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
Copy link
Author

alumni commented Feb 9, 2021

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

@Schniz
Copy link
Owner

Schniz commented Feb 10, 2021

what is cygpath 😨 can we fix it upstream?

@Schniz
Copy link
Owner

Schniz commented Feb 10, 2021

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?

@alumni
Copy link
Author

alumni commented Feb 10, 2021

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).

@rashil2000
Copy link

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.

@cweekly
Copy link

cweekly commented Oct 24, 2022

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.

@ModyQyW
Copy link

ModyQyW commented Feb 28, 2023

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.

Schniz added a commit that referenced this issue May 17, 2023
Schniz added a commit that referenced this issue Jun 29, 2023
* allow to automatically enable corepack

* update command docs

* add changeset

* fix clippy

* don't show the value of corepack

* fix test

* test against more shells

* Make exec handle errors more nicely

* nicer i think

* set corepack as debug

* fix windows-style paths in Bash #390

* fix clippy

* run cygpath on `use` validation too

* trim whitespace

* fix test?

* fix test

* add changeset
@alumni
Copy link
Author

alumni commented Jul 3, 2023

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

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

@cweekly
Copy link

cweekly commented Aug 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants