From c8dc8c9b1c19849a71d7fbf3d6c387027afa1d66 Mon Sep 17 00:00:00 2001 From: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com> Date: Thu, 29 Oct 2026 16:59:39 +0000 Subject: [PATCH] [Fix] `nvm_ls`: treat a version dir with only hidden entries as empty `nvm_ls` decides whether a version dir is worth searching with `ls -1qA`, which also counts dotfiles, but then collects versions with `find "${DIR}"/*`, which never matches them. A dir holding nothing but hidden entries passes the guard, so when `nullglob` is not set `find` is handed the literal `*` and `nvm ls` prints "No such file or directory" to stderr. Drop `-A` from the guard so both halves agree on what counts as an entry. Fixes #3727. --- nvm.sh | 6 +-- ... on a version dir with only hidden entries | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100755 test/fast/Listing versions/Running 'nvm ls' should not error on a version dir with only hidden entries diff --git a/nvm.sh b/nvm.sh index 0011b517..db16abe7 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2487,13 +2487,13 @@ nvm_ls() { fi fi - if ! [ -d "${NVM_DIRS_TO_SEARCH1}" ] || ! (command ls -1qA "${NVM_DIRS_TO_SEARCH1}" | nvm_grep -q .); then + if ! [ -d "${NVM_DIRS_TO_SEARCH1}" ] || ! (command ls -1q "${NVM_DIRS_TO_SEARCH1}" | nvm_grep -q .); then NVM_DIRS_TO_SEARCH1='' fi - if ! [ -d "${NVM_DIRS_TO_SEARCH2}" ] || ! (command ls -1qA "${NVM_DIRS_TO_SEARCH2}" | nvm_grep -q .); then + if ! [ -d "${NVM_DIRS_TO_SEARCH2}" ] || ! (command ls -1q "${NVM_DIRS_TO_SEARCH2}" | nvm_grep -q .); then NVM_DIRS_TO_SEARCH2="${NVM_DIRS_TO_SEARCH1}" fi - if ! [ -d "${NVM_DIRS_TO_SEARCH3}" ] || ! (command ls -1qA "${NVM_DIRS_TO_SEARCH3}" | nvm_grep -q .); then + if ! [ -d "${NVM_DIRS_TO_SEARCH3}" ] || ! (command ls -1q "${NVM_DIRS_TO_SEARCH3}" | nvm_grep -q .); then NVM_DIRS_TO_SEARCH3="${NVM_DIRS_TO_SEARCH2}" fi diff --git a/test/fast/Listing versions/Running 'nvm ls' should not error on a version dir with only hidden entries b/test/fast/Listing versions/Running 'nvm ls' should not error on a version dir with only hidden entries new file mode 100755 index 00000000..74f3c5e2 --- /dev/null +++ b/test/fast/Listing versions/Running 'nvm ls' should not error on a version dir with only hidden entries @@ -0,0 +1,39 @@ +#!/bin/sh + +cleanup() { + [ -n "${NVM_DIR-}" ] && [ -d "${NVM_DIR}" ] && rm -rf "${NVM_DIR}" + unset -f die cleanup + unset NVM_DIR +} + +die() { echo "$@" ; cleanup ; exit 1; } + +: nvm.sh +\. ../../../nvm.sh +\. ../../common.sh + +NVM_DIR="$(mktemp -d)" +[ -n "${NVM_DIR}" ] || die 'unable to create temp NVM_DIR' + +# A directory holding only hidden entries holds no versions, so it must be +# treated as empty: otherwise, with nullglob unset, `find` is handed the +# literal `*` pattern and errors out (#3727). +mkdir -p "$(nvm_version_dir new)" +touch "$(nvm_version_dir new)/.hidden" + +try_err nvm ls +[ -z "${CAPTURED_STDERR-}" ] || die "nvm ls should not write to stderr: ${CAPTURED_STDERR-}" + +# Hidden entries must not hide installed versions. +make_fake_node v0.0.1 + +try_err nvm ls +[ -z "${CAPTURED_STDERR-}" ] || die "nvm ls should not write to stderr: ${CAPTURED_STDERR-}" + +try nvm_ls +case "${CAPTURED_STDOUT-}" in + *v0.0.1*) ;; + *) die "expected v0.0.1 in >${CAPTURED_STDOUT-}<" ;; +esac + +cleanup