[Fix] nvm current, nvm which system: find node without which

Some systems, like the Fedora and UBI container images, ship no `which`,
so `nvm current` printed `none` with a version in use,
which also let `nvm uninstall` remove the active version,
and `nvm which system` failed.
Look node up with `command -v`, ignoring aliases and functions,
via a new `nvm_executable_path`, which `nvm_has_executable` now uses too.

Co-authored-by: Iain <iain1970@protonmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
This commit is contained in:
IainandJordan Harband authored and Jordan Harband committed 2026-11-23 19:28:17 +00:00
1 parent 16744eb47b
commit 94887140eb
4 files changed
+167 -7

No files matched your search

+13 -7
View File
@@ -83,18 +83,24 @@ nvm_type() {
fi
}
# resolves like `command "${1}"` does: only executables on the PATH,
# ignoring shell functions and aliases
nvm_has_executable() {
# prints the path `command "${1}"` runs: only executables on the PATH,
# ignoring shell functions and aliases; unlike `which`, available everywhere
nvm_executable_path() {
(
# `|| true` so that shells with errexit-style options (eg, zsh's ERR_RETURN)
# do not abort the subshell when the name is not an alias or a function
unalias "${1-}" 2>/dev/null || true
unset -f "${1-}" 2>/dev/null || true
command -v "${1-}" >/dev/null 2>&1
# bash's and dash's `command -v` trust the hash table, even for a removed file
hash -r 2>/dev/null || true
command -v "${1-}" 2>/dev/null
)
}
nvm_has_executable() {
nvm_executable_path "${1-}" >/dev/null
}
nvm_has_non_aliased() {
nvm_has "${1-}" && ! nvm_is_alias "${1-}"
}
@@ -2224,7 +2230,7 @@ nvm_alias() {
nvm_ls_current() {
local NVM_LS_CURRENT_NODE_PATH
if ! NVM_LS_CURRENT_NODE_PATH="$(command which node 2>/dev/null)"; then
if ! NVM_LS_CURRENT_NODE_PATH="$(nvm_executable_path node)"; then
nvm_echo 'none'
elif nvm_tree_contains_path "$(nvm_version_dir iojs)" "${NVM_LS_CURRENT_NODE_PATH}"; then
nvm_add_iojs_prefix "$(iojs --version 2>/dev/null)"
@@ -6975,7 +6981,7 @@ nvm() {
if [ "_${NVM_VERSION}" = '_system' ]; then
if nvm_has_system_iojs >/dev/null 2>&1 || nvm_has_system_node >/dev/null 2>&1; then
local NVM_BIN
NVM_BIN="$(nvm use system >/dev/null 2>&1 && command which node)"
NVM_BIN="$(nvm use system >/dev/null 2>&1 && nvm_executable_path node)"
if [ -n "${NVM_BIN}" ]; then
nvm_echo "${NVM_BIN}"
return
@@ -7366,7 +7372,7 @@ nvm() {
nvm_version_greater nvm_version_greater_than_or_equal_to \
nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \
nvm_has_system_node nvm_has_system_iojs \
nvm_download nvm_get_latest nvm_has nvm_has_executable nvm_install_default_packages nvm_get_default_packages \
nvm_download nvm_get_latest nvm_has nvm_has_executable nvm_executable_path nvm_install_default_packages nvm_get_default_packages \
nvm_curl_use_compression nvm_curl_version \
nvm_auto nvm_supports_xz \
nvm_echo nvm_err nvm_grep nvm_cd nvm_cd_root nvm_npm \
+50
View File
@@ -0,0 +1,50 @@
#!/bin/sh
TEST_DIR=''
cleanup() {
case "${TEST_DIR}" in
/*) rm -rf "${TEST_DIR}" ;;
esac
unset -f nvm_test_tool 2>/dev/null
alias nvm_test_tool='nvm_test_tool'
unalias nvm_test_tool
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
TEST_DIR="$(mktemp -d)" || die 'could not make a temp dir'
PATH="${TEST_DIR}:${PATH}"
OUTPUT="$(nvm_executable_path nvm_test_tool)" && die "with no nvm_test_tool, expected failure, got >${OUTPUT}<"
[ -z "${OUTPUT}" ] || die "with no nvm_test_tool, expected no output, got >${OUTPUT}<"
printf '#!/bin/sh\necho tool\n' > "${TEST_DIR}/nvm_test_tool"
chmod a+x "${TEST_DIR}/nvm_test_tool"
OUTPUT="$(nvm_executable_path nvm_test_tool)" || die "expected success, got >${OUTPUT}<"
[ "_${OUTPUT}" = "_${TEST_DIR}/nvm_test_tool" ] || die "expected >${TEST_DIR}/nvm_test_tool<, got >${OUTPUT}<"
# defined before the alias, so zsh does not expand the alias in the definition
nvm_test_tool() { echo function; }
OUTPUT="$(nvm_executable_path nvm_test_tool)" || die "with a function, expected success, got >${OUTPUT}<"
[ "_${OUTPUT}" = "_${TEST_DIR}/nvm_test_tool" ] || die "with a function, expected >${TEST_DIR}/nvm_test_tool<, got >${OUTPUT}<"
# called directly, not in a command substitution, so that a change to the caller would persist
nvm_executable_path nvm_test_tool >/dev/null || die 'with a function, expected success'
[ "_$(nvm_test_tool)" = '_function' ] || die 'the caller lost its nvm_test_tool function'
unset -f nvm_test_tool
alias nvm_test_tool='echo alias'
OUTPUT="$(nvm_executable_path nvm_test_tool)" || die "with an alias, expected success, got >${OUTPUT}<"
[ "_${OUTPUT}" = "_${TEST_DIR}/nvm_test_tool" ] || die "with an alias, expected >${TEST_DIR}/nvm_test_tool<, got >${OUTPUT}<"
nvm_executable_path nvm_test_tool >/dev/null || die 'with an alias, expected success'
alias nvm_test_tool >/dev/null 2>&1 || die 'the caller lost its nvm_test_tool alias'
unalias nvm_test_tool
# a removed file is gone, even while the shell still has its path hashed
OUTPUT="$(hash nvm_test_tool; command rm "${TEST_DIR}/nvm_test_tool"; nvm_executable_path nvm_test_tool)" && die "with nvm_test_tool removed but hashed, expected failure, got >${OUTPUT}<"
[ -z "${OUTPUT}" ] || die "with nvm_test_tool removed but hashed, expected no output, got >${OUTPUT}<"
cleanup
+47
View File
@@ -0,0 +1,47 @@
#!/bin/sh
TEST_DIR=''
cleanup() {
case "${TEST_DIR}" in
/*) rm -rf "${TEST_DIR}" ;;
esac
unset -f nvm_test_tool 2>/dev/null
alias nvm_test_tool='nvm_test_tool'
unalias nvm_test_tool
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
TEST_DIR="$(mktemp -d)" || die 'could not make a temp dir'
PATH="${TEST_DIR}:${PATH}"
OUTPUT="$(nvm_has_executable nvm_test_tool 2>&1)" && die 'with no nvm_test_tool, expected failure'
[ -z "${OUTPUT}" ] || die "with no nvm_test_tool, expected no output, got >${OUTPUT}<"
# defined before the alias, so zsh does not expand the alias in the definition
nvm_test_tool() { echo function; }
alias nvm_test_tool='echo alias'
OUTPUT="$(nvm_has_executable nvm_test_tool 2>&1)" && die 'with only a function and an alias, expected failure'
[ -z "${OUTPUT}" ] || die "with only a function and an alias, expected no output, got >${OUTPUT}<"
# called directly, not in a command substitution, so that a change to the caller would persist
nvm_has_executable nvm_test_tool && die 'with only a function and an alias, expected failure'
# the backslash skips the alias, but not the function
[ "_$(\nvm_test_tool)" = '_function' ] || die 'the caller lost its nvm_test_tool function'
alias nvm_test_tool >/dev/null 2>&1 || die 'the caller lost its nvm_test_tool alias'
unalias nvm_test_tool
unset -f nvm_test_tool
printf '#!/bin/sh\necho tool\n' > "${TEST_DIR}/nvm_test_tool"
chmod a+x "${TEST_DIR}/nvm_test_tool"
OUTPUT="$(nvm_has_executable nvm_test_tool 2>&1)" || die "expected success, got >${OUTPUT}<"
[ -z "${OUTPUT}" ] || die "expected no output, got >${OUTPUT}<"
# a removed file is gone, even while the shell still has its path hashed
OUTPUT="$(hash nvm_test_tool; command rm "${TEST_DIR}/nvm_test_tool"; nvm_has_executable nvm_test_tool 2>&1)" && die 'with nvm_test_tool removed but hashed, expected failure'
[ -z "${OUTPUT}" ] || die "with nvm_test_tool removed but hashed, expected no output, got >${OUTPUT}<"
cleanup
@@ -0,0 +1,57 @@
#!/bin/sh
TEST_DIR=''
cleanup() {
case "${TEST_DIR}" in
/*) rm -rf "${TEST_DIR}" ;;
esac
unset -f node 2>/dev/null
alias node='node'
unalias node
}
die () { echo "$@" ; cleanup ; exit 1; }
: nvm.sh
\. ../../../nvm.sh
TEST_DIR="$(mktemp -d)" || die 'could not make a temp dir'
# some systems, like Fedora container images, have no `which`
# `command -v` prints only the name of a builtin, so search the PATH for each tool
for TOOL in dirname printf sed awk tr mv; do
TOOL_PATH="$(echo "${PATH}" | tr ':' '\n' | while IFS= read -r DIR; do
if [ -f "${DIR}/${TOOL}" ] && [ -x "${DIR}/${TOOL}" ]; then
echo "${DIR}/${TOOL}"
break
fi
done)"
[ -n "${TOOL_PATH}" ] || die "could not find ${TOOL}"
ln -s "${TOOL_PATH}" "${TEST_DIR}/${TOOL}" || die "could not link ${TOOL}"
done
printf '#!/bin/sh\necho v20.0.0\n' > "${TEST_DIR}/node"
chmod a+x "${TEST_DIR}/node"
OUTPUT="$(PATH="${TEST_DIR}" nvm_ls_current)"
[ "_${OUTPUT}" = '_system' ] || die "without \`which\`, nvm_ls_current printed >${OUTPUT}<, expected >system<"
OUTPUT="$(PATH="${TEST_DIR}" nvm which system 2>&1)"
[ "_${OUTPUT}" = "_${TEST_DIR}/node" ] || die "without \`which\`, nvm which system printed >${OUTPUT}<, expected >${TEST_DIR}/node<"
# defined before the alias, so zsh does not expand the alias in the definition
node() { echo function; }
OUTPUT="$(PATH="${TEST_DIR}" nvm which system 2>&1)"
[ "_${OUTPUT}" = "_${TEST_DIR}/node" ] || die "without \`which\`, with a node function, nvm which system printed >${OUTPUT}<, expected >${TEST_DIR}/node<"
unset -f node
alias node='echo alias'
OUTPUT="$(PATH="${TEST_DIR}" nvm which system 2>&1)"
[ "_${OUTPUT}" = "_${TEST_DIR}/node" ] || die "without \`which\`, with node aliased, nvm which system printed >${OUTPUT}<, expected >${TEST_DIR}/node<"
unalias node
# like `which`, a removed node is gone, even while the shell still has its path hashed
OUTPUT="$(PATH="${TEST_DIR}"; hash node; mv "${TEST_DIR}/node" "${TEST_DIR}/removed-node"; nvm_ls_current)"
mv "${TEST_DIR}/removed-node" "${TEST_DIR}/node" || die 'could not restore the node fixture'
[ "_${OUTPUT}" = '_none' ] || die "with node removed but still hashed, nvm_ls_current printed >${OUTPUT}<, expected >none<"
cleanup