Skip to content

Commit da2a20f

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: - Temporarily enable nullglob before the find command - Restore nullglob to its previous state after the command completes - This allows the original find syntax to work correctly with empty directories
1 parent 630a01f commit da2a20f

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎nvm.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,16 @@ 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+
# Enable nullglob to avoid errors when directories are empty
1536+
# See: https://github.com/nvm-sh/nvm/issues/3727
1537+
local NVM_NULLGLOB_WAS_SET
1538+
NVM_NULLGLOB_WAS_SET=false
1539+
if nvm_has "shopt"; then
1540+
if shopt -q nullglob; then
1541+
NVM_NULLGLOB_WAS_SET=true
1542+
fi
1543+
shopt -s nullglob
1544+
fi
15351545
VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \
15361546
| command sed -e "
15371547
s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#;
@@ -1547,6 +1557,10 @@ nvm_ls() {
15471557
| command sed -e 's#\(.*\)\.\([^\.]\{1,\}\)$#\2-\1#;' \
15481558
-e "s#^${NVM_NODE_PREFIX}-##;" \
15491559
)"
1560+
# Restore nullglob to its previous state
1561+
if nvm_has "shopt" && [ "${NVM_NULLGLOB_WAS_SET}" = 'false' ]; then
1562+
shopt -u nullglob
1563+
fi
15501564
fi
15511565
fi
15521566

0 commit comments

Comments
 (0)