Schniz / fnm

🚀 Fast and simple Node.js version manager, built in Rust
https://fnm.vercel.app
GNU General Public License v3.0
18.13k stars 463 forks source link

`eval "$(fnm env --use-on-cd)"` in .zshrc #640

Closed robinloeffel closed 2 years ago

robinloeffel commented 2 years ago

Whenever I use eval "$(fnm env --use-on-cd)" in my .zshrc, I get the following error in my terminal: (eval):16: parse error near add-zsh-hook', and fnm isn't usable.

Screenshot 2022-01-18 at 14 04 56

If I use the old eval "$(fnm env)", however, I can use fnm with no problem.

Screenshot 2022-01-18 at 14 04 30

fnm env prints the following, in both cases:

export PATH="/var/folders/ms/4yxkx48x4150qrgkj4gzm1d00000gn/T/fnm_multishells/55532_1642511393840/bin":$PATH
export FNM_MULTISHELL_PATH="/var/folders/ms/4yxkx48x4150qrgkj4gzm1d00000gn/T/fnm_multishells/55532_1642511393840"
export FNM_VERSION_FILE_STRATEGY="local"
export FNM_DIR="/Users/robin/Library/Application Support/fnm"
export FNM_LOGLEVEL="info"
export FNM_NODE_DIST_MIRROR="https://nodejs.org/dist"
export FNM_ARCH="x64"
rehash

I've installed it via brew install fnm, and tried re-installing it several times. My local machine is running:

Edit: This is my whole .zshrc:

# aliases
## brew
alias bup="brew update && brew upgrade && brew cleanup"
alias bi="brew install"
alias bu="brew uninstall"
alias br="brew reinstall"
alias bci="brew install --cask"
alias bcu="brew uninstall --cask"
alias bcr="brew reinstall --cask"

## yarn
alias ys="yarn start"
alias ya="yarn add"
alias yr="yarn remove"
alias yu="yarn upgrade-interactive --latest && yarn upgrade"

## fnm
alias fu="fnm use"
alias fi="fnm install"

## git
alias gitit="git clone"

## misc
alias ngr="sudo rm -rf $(xcode-select -print-path) && sudo rm -rf /Library/Developer/CommandLineTools && sudo xcode-select --reset && xcode-select --install"

# prompt
export PROMPT="%F{cyan}%T%f > %F{green}%~%f 🔥 "

# brew
export PATH="/usr/local/sbin:$PATH"

# fnm
eval "$(fnm env)"
robinloeffel commented 2 years ago

Figured it out! I've had two aliases defined before evaluating fnm env --use-on-cd. If i move eval "$(fnm env --use-on-cd)" to the top of my .zshrc, all is peachy!

Schniz commented 2 years ago

not really sure why this should fix it or what the diff was, but happy that it works. please feel free to share with more details. maybe it is a bug in the generated env :)

robinloeffel commented 2 years ago

I have no idea why this fixed it, but the following .zshrc works:

# prompt
export PROMPT="%F{cyan}%T%f > %F{green}%~%f 🔥 "

# brew
export PATH="/usr/local/sbin:$PATH"

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

# aliases
## brew
alias bup="brew update && brew upgrade && brew cleanup"
alias bi="brew install"
alias bu="brew uninstall"
alias br="brew reinstall"
alias bci="brew install --cask"
alias bcu="brew uninstall --cask"
alias bcr="brew reinstall --cask"

## yarn
alias ys="yarn start"
alias ya="yarn add"
alias yr="yarn remove"
alias yu="yarn upgrade-interactive --latest && yarn upgrade"

## fnm
alias fu="fnm use"
alias fi="fnm install"

## git
alias gitit="git clone"

## misc
alias ngr="sudo rm -rf $(xcode-select -print-path) && sudo rm -rf /Library/Developer/CommandLineTools && sudo xcode-select --reset && xcode-select --install"

Apart from that, I've changed absolutely nothing.

cweekly commented 2 years ago

Hi @robinloeffel, FTR the problem is your use of fi as an alias. Shell scripts often make use of the if...fi control statement, so fi should be considered a keyword to avoid as an alias. Even if your updated .zshrc noted above isn't triggering obvious errors, I still recommend using a different alias. HTH! :)

Schniz commented 2 years ago

That is unexpected. I wonder if we need to prepend a forward slash to avoid clashing. Great catch!

cweekly commented 2 years ago

Hey @Schniz, I might be missing something, but... I don't think the issue had anything to do with fnm per se. Creating an alias named fi is problematic by definition, and that alias was something robinloeffel added by hand. Removing/renaming the alias eliminates the reported problem with eval "$(fnm env --use-on-cd)". So, IMHO, it's a NOOP for fnm's maintainer! :)