Schniz / fnm

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

Suggestion: disambiguate shell setup steps #351

Open acx0 opened 3 years ago

acx0 commented 3 years ago

Just a suggestion to change mentions of '.bashrc profile' and '.zshrc profile' in the readme to '.bashrc file/.zshrc file' so that there's no ambiguity as to whether it's referring to each shells' profile file (i.e. .bash_profile, .zprofile) or the actual rc file.

I figured it was meant to be the rc files but had to double-check the install.sh script to be sure.

I also noticed install.sh assumes either .profile or .bashrc when $SHELL is bash but just FYI, for login shells bash only sources one of .bash_profile, .bash_login, or .profile, in that order if they exist; see: https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-Startup-Files

https://github.com/Schniz/fnm/blob/02370483718e2b1fc553bac6e983054fe59fdae7/.ci/get_shell_profile.sh#L12-L15 https://github.com/Schniz/fnm/blob/02370483718e2b1fc553bac6e983054fe59fdae7/.ci/install.sh#L178-L181

Schniz commented 3 years ago

Hey, thanks for opening the issue 😄

We have added $HOME/.profile because OSX didn't source .bashrc for some reason: https://github.com/Schniz/fnm/pull/58

Do you think we should always use and suggest .bash_profile?

acx0 commented 3 years ago

because OSX didn't source .bashrc for some reason: #58

That's because macos's Terminal.app invokes your shell as a login shell. If you open Terminal.app > Preferences > General it somewhat hints at this with "Shells open with: [x] Default login shell". If you're quick enough too you can see login (i.e. /usr/bin/login) being invoked on every new Terminal.app window/tab opened (if you have the window configured to show 'Active process name' in Preferences).

The reason .bashrc isn't being read is because when bash is invoked as a login shell (i.e. bash --login which is what /usr/bin/login is probably calling), it only reads /etc/profile and then one of .bash_profile, .bash_login, or .profile, in that order. That's why it's generally recommended to source ~/.bashrc inside your .bash_profile.

This behaviour isn't macos specific; if you ssh into a linux machine without supplying a command arg and have bash as the default shell, it'll also invoke bash as a login shell and you'll run into the same behaviour, i.e. .bashrc not being sourced.

Do you think we should always use and suggest .bash_profile?

I was just hoping to bring attention to the fact that assuming ~/.profile is the file being sourced by bash may lead to some confusion/non-functioning setups if someone is using a ~/.bash_profile or ~/.bash_login file instead.

Ideally I would want to recommend that users place the fnm env call inside their ~/.bashrc and then ensure that they have a correctly setup ~/.bash_profile which sources ~/.bashrc if it exists. Automating this would complicate the install script as you'd then have to modify .bashrc and .bash_profile (or .bash_login) as well as check that the .bashrc isn't already being sourced.

I think a practical solution would be to just stick with the current method of appending to ~/.profile on macos but also add checks for the other two files, and document in the readme (and maybe even in the install script output) what the recommended setup is.

I can create a PR with my suggestions and update the readme to mention the whole login shell issue with macos for you to review if that makes it easier.