Skip to content

Commit 1a95084

Browse files
Andrew LengAndrew Leng
authored andcommitted
fix: avoid glob expansion errors when no node versions installed
Fixes #3727 When no node versions are installed, would produce errors like: `find: /path/.nvm/versions/node/*: No such file or directory` This happened because bash expands `*` literally when nullglob is not set and the directory is empty. Solution: - Build find arguments dynamically without shell glob expansion - Use `find DIR -mindepth 1 -maxdepth 1` instead of `find DIR/*` - Suppress stderr from find to handle edge cases gracefully - Deduplicate directories before passing to find
1 parent 630a01f commit 1a95084

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

nvm.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,22 @@ 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+
# Build find arguments without shell glob expansion to avoid errors when directories are empty
1536+
# See: https://github.com/nvm-sh/nvm/issues/3727
1537+
local NVM_FIND_ARGS
1538+
NVM_FIND_ARGS=''
1539+
if [ -n "${NVM_DIRS_TO_SEARCH1}" ]; then
1540+
NVM_FIND_ARGS="${NVM_DIRS_TO_SEARCH1}"
1541+
fi
1542+
if [ -n "${NVM_DIRS_TO_SEARCH2}" ] && [ "${NVM_DIRS_TO_SEARCH2}" != "${NVM_DIRS_TO_SEARCH1}" ]; then
1543+
NVM_FIND_ARGS="${NVM_FIND_ARGS} ${NVM_DIRS_TO_SEARCH2}"
1544+
fi
1545+
if [ -n "${NVM_DIRS_TO_SEARCH3}" ] && [ "${NVM_DIRS_TO_SEARCH3}" != "${NVM_DIRS_TO_SEARCH2}" ] && [ "${NVM_DIRS_TO_SEARCH3}" != "${NVM_DIRS_TO_SEARCH1}" ]; then
1546+
NVM_FIND_ARGS="${NVM_FIND_ARGS} ${NVM_DIRS_TO_SEARCH3}"
1547+
fi
1548+
# Use -mindepth 1 to exclude the directories themselves and avoid needing shell glob expansion
1549+
# shellcheck disable=SC2086
1550+
VERSIONS="$(command find ${NVM_FIND_ARGS} -mindepth 1 -maxdepth 1 -type d -path "*${PATTERN}*" 2>/dev/null \
15361551
| command sed -e "
15371552
s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#;
15381553
s#^${NVM_DIR}/##;

0 commit comments

Comments
 (0)