Skip to content

Commit 9449392

Browse files
Andrew LengAndrew Leng
authored andcommitted
fix: enable nullglob to avoid glob expansion errors when no node versions installed
Fixes nvm-sh#3727 When no node versions are installed, nvm ls produces errors like: `find: /path/.nvm/versions/node/*: No such file or directory` This happens because bash expands `*` literally when nullglob is not set and the directory is empty. Solution: - For bash: temporarily enable nullglob before the find command - For other shells (sh, dash, zsh): suppress stderr (2>/dev/null) - Restore nullglob to its previous state after the command completes - Uses BASH_VERSION check to detect bash vs other shells
1 parent 630a01f commit 9449392

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

‎nvm.sh‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,20 @@ nvm_ls() {
15321532
SEARCH_PATTERN="$(nvm_echo "${PATTERN}" | command sed 's#\.#\\\.#g;')"
15331533
fi
15341534
if [ -n "${NVM_DIRS_TO_SEARCH1}${NVM_DIRS_TO_SEARCH2}${NVM_DIRS_TO_SEARCH3}" ]; then
1535-
VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \
1535+
# Enable nullglob (bash-only) to avoid errors when directories are empty
1536+
# For other shells, stderr is suppressed to handle the case gracefully
1537+
# See: https://github.com/nvm-sh/nvm/issues/3727
1538+
if [ -n "${BASH_VERSION-}" ]; then
1539+
# shellcheck disable=SC3044
1540+
if shopt -q nullglob 2>/dev/null; then
1541+
NVM_NULLGLOB_WAS_SET=true
1542+
else
1543+
NVM_NULLGLOB_WAS_SET=false
1544+
# shellcheck disable=SC3044
1545+
shopt -s nullglob 2>/dev/null
1546+
fi
1547+
fi
1548+
VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" 2>/dev/null \
15361549
| command sed -e "
15371550
s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#;
15381551
s#^${NVM_DIR}/##;
@@ -1547,6 +1560,11 @@ nvm_ls() {
15471560
| command sed -e 's#\(.*\)\.\([^\.]\{1,\}\)$#\2-\1#;' \
15481561
-e "s#^${NVM_NODE_PREFIX}-##;" \
15491562
)"
1563+
# Restore nullglob to its previous state (bash-only)
1564+
if [ -n "${BASH_VERSION-}" ] && [ "${NVM_NULLGLOB_WAS_SET-}" = 'false' ]; then
1565+
# shellcheck disable=SC3044
1566+
shopt -u nullglob 2>/dev/null
1567+
fi
15501568
fi
15511569
fi
15521570

0 commit comments

Comments
 (0)