From ee8fa8dc70f2b0fa8f22059867a431b966dd57f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E9=AA=90?= Date: Sun, 22 Nov 2026 04:14:10 +0800 Subject: [PATCH] [Fix] `install.sh`: support `.bash_login` Detect it after `.bash_profile`, including in completion setup, and update related documentation and tests. Assisted-by: Kimi K3.1 Pro <317908118+kimi-agent-bot@users.noreply.github.com> --- .github/ISSUE_TEMPLATE/issue_template.md | 2 +- README.md | 12 ++-- install.sh | 8 ++- .../nvm_detect_profile_bash_login | 39 +++++++++++ ...ash_login_profile_without_trailing_newline | 65 +++++++++++++++++++ .../nvm_do_install_tried_profile_bash_login | 44 +++++++++++++ .../nvm_profile_is_bash_or_zsh_bash_login | 8 +++ 7 files changed, 168 insertions(+), 10 deletions(-) create mode 100755 test/install_script/nvm_detect_profile_bash_login create mode 100755 test/install_script/nvm_do_install_bash_login_profile_without_trailing_newline create mode 100755 test/install_script/nvm_do_install_tried_profile_bash_login create mode 100755 test/install_script/nvm_profile_is_bash_or_zsh_bash_login diff --git a/.github/ISSUE_TEMPLATE/issue_template.md b/.github/ISSUE_TEMPLATE/issue_template.md index 5d735df4..5f791f52 100644 --- a/.github/ISSUE_TEMPLATE/issue_template.md +++ b/.github/ISSUE_TEMPLATE/issue_template.md @@ -39,7 +39,7 @@ assignees: '' #### What did you expect to happen? #### Is there anything in any of your profile files that modifies the `PATH`? - + #### If you are having installation issues, or getting "N/A", what does `curl -I --compressed -v https://nodejs.org/dist/` print out? diff --git a/README.md b/README.md index 5bee7743..eaa7f89f 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.41.0/install.sh | bash ``` -Running either of the above commands downloads a script and runs it. The script clones the nvm repository to `~/.nvm`, and attempts to add the source lines from the snippet below to the correct profile file (`~/.bashrc`, `~/.bash_profile`, `~/.zshrc`, or `~/.profile`). If you find the install script is updating the wrong profile file, set the `$PROFILE` env var to the profile file’s path, and then rerun the installation script. +Running either of the above commands downloads a script and runs it. The script clones the nvm repository to `~/.nvm`, and attempts to add the source lines from the snippet below to the correct profile file (`~/.bashrc`, `~/.bash_profile`, `~/.bash_login`, `~/.zshrc`, or `~/.profile`). If you find the install script is updating the wrong profile file, set the `$PROFILE` env var to the profile file’s path, and then rerun the installation script. ```sh @@ -236,7 +236,7 @@ If you get `nvm: command not found` after running the install script, one of the - Since macOS 10.15, the default shell is `zsh` and nvm will look for `.zshrc` to update, none is installed by default. Create one with `touch ~/.zshrc` and run the install script again. - - If you use bash, the previous default shell, your system may not have `.bash_profile` or `.bashrc` files where the command is set up. Create one of them with `touch ~/.bash_profile` or `touch ~/.bashrc` and run the install script again. Then, run `. ~/.bash_profile` or `. ~/.bashrc` to pick up the `nvm` command. + - If you use bash, the previous default shell, your system may not have `.bash_profile`, `.bash_login`, or `.bashrc` files where the command is set up. Create one of them with `touch ~/.bash_profile` or `touch ~/.bashrc` and run the install script again. Then, run `. ~/.bash_profile` or `. ~/.bashrc` to pick up the `nvm` command. - You have previously used `bash`, but you have `zsh` installed. You need to manually add [these lines](#manual-install) to `~/.zshrc` and run `. ~/.zshrc`. @@ -246,9 +246,9 @@ If you get `nvm: command not found` after running the install script, one of the If the above doesn't fix the problem, you may try the following: - - If you use bash, it may be that your `.bash_profile` (or `~/.profile`) does not source your `~/.bashrc` properly. You could fix this by adding `source ~/` to it or following the next step below. + - If you use bash, it may be that your `.bash_profile` (or `~/.bash_login`, or `~/.profile`) does not source your `~/.bashrc` properly. You could fix this by adding `source ~/` to it or following the next step below. - - Try adding [the snippet from the install section](#profile_snippet), that finds the correct nvm directory and loads nvm, to your usual profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`). + - Try adding [the snippet from the install section](#profile_snippet), that finds the correct nvm directory and loads nvm, to your usual profile (`~/.bash_profile`, `~/.bash_login`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`). - For more information about this issue and possible workarounds, please [refer here](https://github.com/nvm-sh/nvm/issues/576) @@ -793,7 +793,7 @@ If you prefer a lighter-weight solution, the recipes below have been contributed #### Calling `nvm use` automatically in a directory with a `.nvmrc` file -In your profile (`~/.bash_profile`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`), add the following to `nvm use` whenever you enter a new directory: +In your profile (`~/.bash_profile`, `~/.bash_login`, `~/.zshrc`, `~/.profile`, or `~/.bashrc`), add the following to `nvm use` whenever you enter a new directory: ##### bash @@ -996,7 +996,7 @@ To activate, you need to source `bash_completion`: [[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion ``` -Put the above sourcing line just below the sourcing line for nvm in your profile (`.bashrc`, `.bash_profile`). +Put the above sourcing line just below the sourcing line for nvm in your profile (`.bashrc`, `.bash_profile`, `.bash_login`). ### Usage diff --git a/install.sh b/install.sh index 9220631e..cd60d6e7 100755 --- a/install.sh +++ b/install.sh @@ -57,7 +57,7 @@ nvm_profile_is_bash_or_zsh() { local TEST_PROFILE TEST_PROFILE="${1-}" case "${TEST_PROFILE-}" in - *"/.bashrc" | *"/.bash_profile" | *"/.zshrc" | *"/.zprofile") + *"/.bashrc" | *"/.bash_profile" | *"/.bash_login" | *"/.zshrc" | *"/.zprofile") return ;; *) @@ -393,6 +393,8 @@ nvm_detect_profile() { DETECTED_PROFILE="$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then DETECTED_PROFILE="$HOME/.bash_profile" + elif [ -f "$HOME/.bash_login" ]; then + DETECTED_PROFILE="$HOME/.bash_login" fi elif [ "${NVM_PROFILE_SHELL#*zsh}" != "${NVM_PROFILE_SHELL}" ]; then if [ -f "${ZDOTDIR:-${HOME}}/.zshrc" ]; then @@ -403,7 +405,7 @@ nvm_detect_profile() { fi if [ -z "$DETECTED_PROFILE" ]; then - for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zprofile" ".zshrc" + for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".bash_login" ".zprofile" ".zshrc" do if DETECTED_PROFILE="$(nvm_try_profile "${ZDOTDIR:-${HOME}}/${EACH_PROFILE}")"; then break @@ -560,7 +562,7 @@ nvm_do_install() { if [ -n "${PROFILE}" ]; then TRIED_PROFILE="${PROFILE} (as defined in \$PROFILE), " fi - nvm_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zprofile, ~/.zshrc, and ~/.profile." + nvm_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.bash_login, ~/.zprofile, ~/.zshrc, and ~/.profile." nvm_echo "=> Create one of them and run this script again" nvm_echo " OR" nvm_echo "=> Append the following lines to the correct file yourself:" diff --git a/test/install_script/nvm_detect_profile_bash_login b/test/install_script/nvm_detect_profile_bash_login new file mode 100755 index 00000000..23f5a0e4 --- /dev/null +++ b/test/install_script/nvm_detect_profile_bash_login @@ -0,0 +1,39 @@ +#!/bin/sh + +cleanup () { + rm -rf "${HOME}" + HOME="${ORIGINAL_HOME}" + unset -f setup cleanup die + unset ORIGINAL_HOME NVM_ENV NVM_DETECT_PROFILE SHELL ZDOTDIR +} + +die () { echo "$@" '$NVM_DETECT_PROFILE:' "$NVM_DETECT_PROFILE"; cleanup; exit 1; } + +setup () { + ORIGINAL_HOME="${HOME}" + HOME="$(mktemp -d)" + NVM_ENV=testing \. ../../install.sh + # the shell running these tests is not the one being simulated + nvm_invoking_login_shell() { return 1; } + unset ZDOTDIR + touch "${HOME}/.bash_profile" "${HOME}/.bash_login" "${HOME}/.profile" "${HOME}/.zprofile" +} + +setup + +# bash reads .bash_login only when there is no .bash_profile +NVM_DETECT_PROFILE="$(SHELL="/bin/bash" PROFILE='' nvm_detect_profile)" +[ "$NVM_DETECT_PROFILE" = "${HOME}/.bash_profile" ] || die "nvm_detect_profile should prefer .bash_profile over .bash_login for bash" + +NVM_DETECT_PROFILE="$(SHELL='' PROFILE='' nvm_detect_profile)" +[ "$NVM_DETECT_PROFILE" = "${HOME}/.profile" ] || die "nvm_detect_profile should still prefer .profile when \$SHELL is not set" + +rm "${HOME}/.bash_profile" +NVM_DETECT_PROFILE="$(SHELL="/bin/bash" PROFILE='' nvm_detect_profile)" +[ "$NVM_DETECT_PROFILE" = "${HOME}/.bash_login" ] || die "nvm_detect_profile should pick .bash_login for bash when there is no .bashrc or .bash_profile" + +rm "${HOME}/.profile" +NVM_DETECT_PROFILE="$(SHELL='' PROFILE='' nvm_detect_profile)" +[ "$NVM_DETECT_PROFILE" = "${HOME}/.bash_login" ] || die "nvm_detect_profile should prefer .bash_login over .zprofile when \$SHELL is not set" + +cleanup diff --git a/test/install_script/nvm_do_install_bash_login_profile_without_trailing_newline b/test/install_script/nvm_do_install_bash_login_profile_without_trailing_newline new file mode 100755 index 00000000..3fefacd9 --- /dev/null +++ b/test/install_script/nvm_do_install_bash_login_profile_without_trailing_newline @@ -0,0 +1,65 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + unset -f install_nvm_from_git install_nvm_as_script nvm_detect_profile nvm_has + unset -f nvm_check_global_modules nvm_install_node + unset -f setup cleanup die assert_profile + unset NVM_ENV METHOD PROFILE + rm -rf "${TMPDIR_FOR_TEST-}" 2>/dev/null +} + +setup() { + NVM_ENV=testing \. ../../install.sh + + # Mock installation functions to do nothing + install_nvm_from_git() { :; } + install_nvm_as_script() { :; } + nvm_check_global_modules() { :; } + nvm_install_node() { :; } + + # Mock nvm_has to return true for git + nvm_has() { + case "$1" in + git) return 0 ;; + xcode-select) return 1 ;; + *) return 1 ;; + esac + } +} + +setup + +TMPDIR_FOR_TEST="$(mktemp -d)" +TEST_PROFILE="${TMPDIR_FOR_TEST}/.bash_login" +EXPECTED_PROFILE="${TMPDIR_FOR_TEST}/expected" +COMPLETION_LINE='[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion' + +# runs the installer against $TEST_PROFILE, and compares the whole file, byte for byte, with $EXPECTED_PROFILE. +# `nvm_do_install` unsets every `nvm_` function when it finishes, so each run sources the installer and mocks afresh. +assert_profile() { + setup + nvm_detect_profile() { + echo "${TEST_PROFILE}" + } + PROFILE='' METHOD='' NVM_DIR="$(cd ../.. && pwd)" nvm_do_install > /dev/null 2>&1 + command cmp -s "${TEST_PROFILE}" "${EXPECTED_PROFILE}" || die "$1; got: >$(command cat "${TEST_PROFILE}")<" +} + +# +# Test: when the profile already sources nvm.sh but does not end with a newline, +# the bash_completion line must be appended on its own line, and the profile must end with a newline +# +command printf '%s\n%s' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" > "${TEST_PROFILE}" +command printf '%s\n%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" "${COMPLETION_LINE}" > "${EXPECTED_PROFILE}" +assert_profile 'bash_completion line was not appended on its own line, ending with a newline' + +# +# Test: when the profile already ends with a newline, no blank line is added before the bash_completion line +# +command printf '%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" > "${TEST_PROFILE}" +command printf '%s\n%s\n%s\n' '. "$HOME/.nvm/nvm.sh"' "alias ll='ls -l'" "${COMPLETION_LINE}" > "${EXPECTED_PROFILE}" +assert_profile 'a profile ending with a newline got an extra blank line before the bash_completion line' + +cleanup diff --git a/test/install_script/nvm_do_install_tried_profile_bash_login b/test/install_script/nvm_do_install_tried_profile_bash_login new file mode 100755 index 00000000..1c1d0e08 --- /dev/null +++ b/test/install_script/nvm_do_install_tried_profile_bash_login @@ -0,0 +1,44 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + unset -f install_nvm_from_git install_nvm_as_script nvm_detect_profile nvm_has + unset -f nvm_check_global_modules nvm_install_node + unset -f setup cleanup die + unset NVM_ENV METHOD PROFILE +} + +setup() { + NVM_ENV=testing \. ../../install.sh + + # Mock installation functions to do nothing + install_nvm_from_git() { :; } + install_nvm_as_script() { :; } + nvm_check_global_modules() { :; } + nvm_install_node() { :; } + + # Mock nvm_has to return true for git + nvm_has() { + case "$1" in + git) return 0 ;; + xcode-select) return 1 ;; + *) return 1 ;; + esac + } + + # Mock nvm_detect_profile to return empty (profile not found) + nvm_detect_profile() { + echo "" + } +} + +setup + +OUTPUT="$(PROFILE='' METHOD='' NVM_DIR="$(mktemp -d)" nvm_do_install 2>&1)" + +if ! echo "${OUTPUT}" | grep -q 'Profile not found\. Tried .*~/\.bash_login'; then + die "Expected the 'Profile not found' message to mention ~/.bash_login, got: ${OUTPUT}" +fi + +cleanup diff --git a/test/install_script/nvm_profile_is_bash_or_zsh_bash_login b/test/install_script/nvm_profile_is_bash_or_zsh_bash_login new file mode 100755 index 00000000..a5662f87 --- /dev/null +++ b/test/install_script/nvm_profile_is_bash_or_zsh_bash_login @@ -0,0 +1,8 @@ +#!/bin/sh + +die () { echo "$@" ; exit 1; } + +NVM_ENV=testing \. ../../install.sh + +nvm_profile_is_bash_or_zsh "/home/nvm/.bash_login" || die '/home/nvm/.bash_login is bash profile' +if nvm_profile_is_bash_or_zsh "/home/nvm/.bash_logout"; then die '/home/nvm/.bash_logout is not bash nor zsh profile'; fi