diff --git a/nvm.sh b/nvm.sh index cee89bd9..6101b461 100755 --- a/nvm.sh +++ b/nvm.sh @@ -1359,8 +1359,11 @@ nvm_remote_version() { if [ -n "${PATTERN}" ] && [ "_${VERSION}" != "_N/A" ] && ! nvm_validate_implicit_alias "${PATTERN}" 2>/dev/null; then local VERSION_NUM VERSION_NUM="$(nvm_echo "${VERSION}" | command awk '{print $1}')" - # the `v` prefix and the trailing non-digit keep `20` from matching `v10.20.0` or `v200.0.0` - if ! nvm_echo "${VERSION_NUM} " | nvm_grep -q -e "$(nvm_ensure_version_prefix "${PATTERN%.}")[^0-9]"; then + # the `v` prefix and the trailing non-digit keep `20` from matching `v10.20.0` or `v200.0.0`, + # and escaping the dots between version components keeps `20.1.1` from matching `v20.111.1` + local VERSION_RE + VERSION_RE="$(nvm_ensure_version_prefix "${PATTERN%.}" | command sed 's/\.\([0-9]\)/\\.\1/g')" + if ! nvm_echo "${VERSION_NUM} " | nvm_grep -q -e "${VERSION_RE}[^0-9]"; then VERSION='N/A' fi fi @@ -2770,12 +2773,17 @@ nvm_ls_remote_index_tab() { fi local VERSIONS + local PATTERN_RE + PATTERN_RE='' if [ -n "${PATTERN}" ] && [ "${PATTERN}" != '*' ]; then if [ "${FLAVOR}" = 'iojs' ]; then PATTERN="$(nvm_ensure_version_prefix "$(nvm_strip_iojs_prefix "${PATTERN}")")" else PATTERN="$(nvm_ensure_version_prefix "${PATTERN}")" fi + # match the dots between version components literally, so that `20.1.1` does not also + # match `v20.111.1`; other regex syntax in the pattern (like `v[68]` or `.*`) is kept + PATTERN_RE="$(nvm_echo "${PATTERN}" | command sed 's/\.\([0-9]\)/\\.\1/g')" else unset PATTERN fi @@ -2849,7 +2857,7 @@ EOF } prev=$10; }' \ - | nvm_grep -w -e "${PATTERN:-.*}" \ + | nvm_grep -w -e "${PATTERN_RE:-.*}" \ | $SORT_COMMAND; } << EOF $VERSION_LIST EOF diff --git a/test/fast/Unit tests/nvm_ls_remote matches version components b/test/fast/Unit tests/nvm_ls_remote matches version components new file mode 100755 index 00000000..52305c00 --- /dev/null +++ b/test/fast/Unit tests/nvm_ls_remote matches version components @@ -0,0 +1,45 @@ +#!/bin/sh + +die () { echo "$@" ; cleanup ; exit 1; } + +cleanup() { + unset -f nvm_download nvm_record_lts_status +} + +: nvm.sh +\. ../../../nvm.sh + +# these mocks must not be recorded as the LTS status of $NVM_DIR's versions +nvm_record_lts_status() { :; } + +nvm_download() { + command printf '%s\n' \ + 'version date files npm v8 uv zlib openssl modules lts security' \ + 'v2041.0.0 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \ + 'v20.111.1 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \ + 'v20.10.0 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \ + 'v20.1.1 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' \ + 'v20.1.0 2026-01-01 linux-x64 10.0.0 1 1 1 1 115 - -' +} + +\. ../../common.sh + +# Dots in the pattern are literal, so they must not match other digits. +OUTPUT="$(nvm_ls_remote 20.1)" +EXPECTED_OUTPUT='v20.1.0 +v20.1.1' +[ "_${OUTPUT}" = "_${EXPECTED_OUTPUT}" ] || die "nvm_ls_remote 20.1: expected >${EXPECTED_OUTPUT}<, got >${OUTPUT}<" + +OUTPUT="$(nvm_ls_remote 20.1.1)" +[ "_${OUTPUT}" = '_v20.1.1' ] || die "nvm_ls_remote 20.1.1: expected >v20.1.1<, got >${OUTPUT}<" + +try nvm_ls_remote 20.1.2 +[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "nvm_ls_remote 20.1.2: expected >N/A<, got >${CAPTURED_STDOUT}<" + +OUTPUT="$(nvm_remote_version 20.1.1)" +[ "_${OUTPUT}" = '_v20.1.1' ] || die "nvm_remote_version 20.1.1: expected >v20.1.1<, got >${OUTPUT}<" + +try nvm_remote_version 20.1.2 +[ "_${CAPTURED_STDOUT}" = '_N/A' ] || die "nvm_remote_version 20.1.2: expected >N/A<, got >${CAPTURED_STDOUT}<" + +cleanup