[Fix] nvm_ls: match NVM_DIR literally

`nvm_ls` strips `$NVM_DIR` and the io.js version dir from each installed version's path with sed,
but put both into the regex as-is,
so an `NVM_DIR` containing a backslash, `[`, or `*` listed no installed versions,
and one containing `#` made sed fail.

Escape both before use, as `SEARCH_PATTERN` already is.
This commit is contained in:
Jordan Harband committed 2026-11-02 13:53:00 -06:00
1 parent bfabdbc278
commit 03f74a8666
2 files changed
+44 -2

No files matched your search

+7 -2
View File
@@ -2479,10 +2479,15 @@ nvm_ls() {
*-*) NVM_PRERELEASE_PATTERN="${PATTERN}" ;;
esac
if [ -n "${NVM_DIRS_TO_SEARCH1}${NVM_DIRS_TO_SEARCH2}${NVM_DIRS_TO_SEARCH3}" ]; then
# the directories are matched literally, so escape characters that are special in sed
local NVM_DIR_SED
NVM_DIR_SED="$(nvm_echo "${NVM_DIR}" | command sed 's/[][\.*^$#]/\\&/g')"
local NVM_VERSION_DIR_IOJS_SED
NVM_VERSION_DIR_IOJS_SED="$(nvm_echo "${NVM_VERSION_DIR_IOJS}" | command sed 's/[][\.*^$#]/\\&/g')"
VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \
| command sed -e "
s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#;
s#^${NVM_DIR}/##;
s#${NVM_VERSION_DIR_IOJS_SED}/#versions/${NVM_IOJS_PREFIX}/#;
s#^${NVM_DIR_SED}/##;
\\#^[^v]# d;
\\#^versions\$# d;
s#^versions/##;
+37
View File
@@ -0,0 +1,37 @@
#!/bin/sh
die () { echo "$@" ; cleanup ; exit 1; }
cleanup() {
rm -rf "${TEST_DIR-}"
unset TEST_DIR NVM_DIR DIR
}
: nvm.sh
\. ../../../nvm.sh
\. ../../common.sh
TEST_DIR="${PWD}/nvm_ls_literal_nvm_dir_tmp"
# each of these made sed match no installed versions, or fail to compile
for DIR in 'nvm\test' 'a[x]b' 'h#sh' 'st*r'; do
NVM_DIR="${TEST_DIR}/${DIR}"
for VERSION in versions/node/v24.11.0 v0.10.5 versions/io.js/v1.0.0; do
mkdir -p "${NVM_DIR}/${VERSION}/bin" || die "failed to create ${VERSION} dir under ${NVM_DIR}"
make_echo "${NVM_DIR}/${VERSION}/bin/node" "${VERSION##*/}" || die "failed to create ${VERSION} node binary under ${NVM_DIR}"
done
# one version per layout; no pattern, or `v`, would also list a system node
try nvm_ls 0.10
[ "_${CAPTURED_STDOUT}" = '_v0.10.5' ] || die "\`nvm_ls 0.10\` under \`${NVM_DIR}\` should output >v0.10.5<; got >${CAPTURED_STDOUT}<"
try nvm_ls 24
[ "_${CAPTURED_STDOUT}" = '_v24.11.0' ] || die "\`nvm_ls 24\` under \`${NVM_DIR}\` should output >v24.11.0<; got >${CAPTURED_STDOUT}<"
try nvm_ls iojs
[ "_${CAPTURED_STDOUT}" = '_iojs-v1.0.0' ] || die "\`nvm_ls iojs\` under \`${NVM_DIR}\` should output >iojs-v1.0.0<; got >${CAPTURED_STDOUT}<"
done
unset VERSION
cleanup