Skip to content
Myr

[Fix] `nvm_alias`: unset zsh's `extendedglob` while reading an alias file

Before adoption: this commit came in with the history the project adopted, so no landing records it.

`#` is a repetition operator under `extendedglob`, so `${VAR%%#*}` is an invalid pattern: zsh aborts the expansion and `nvm_alias` silently prints nothing, leaving the `default` alias unresolved when nvm.sh is sourced. The read loop moves into `nvm_print_alias_file` so that `local_options` scopes the option change to that section alone, rather than to everything `nvm_alias` does, and restores whatever the caller had set. Fixes #3885

authored by debaditya · committed by Jordan Harband · parent 25b3783 · Browse files · 3 files+105 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: debaditya <debaditya2005hait@gmail.com>
  • Co-authored-by: Jordan Harband <ljharb@gmail.com>

Diff stats

3 files+105 added−11 removed

  • tests:+86 added−0 removed
  • other:+19 added−11 removed

3 files+105 added−11 removed

added:test/fast/Unit tests/nvm_print_alias_file+53 added
1#!/bin/sh23die () { echo "$@" ; cleanup ; exit 1; }45cleanup () {6 rm -rf "${TEST_DIR-}"7 unset TEST_DIR OUTPUT EXPECTED8}910: nvm.sh11\. ../../../nvm.sh1213TEST_DIR="${PWD}/nvm_print_alias_file_tmp"14mkdir -p "${TEST_DIR}" || die 'failed to create test dir'1516printf 'v0.10 # inline comment\n' > "${TEST_DIR}/inline"17OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/inline")"18[ "_${OUTPUT}" = '_v0.10' ] || die "inline comment not stripped; got '${OUTPUT}'"1920printf '# leading comment\nv0.11\n' > "${TEST_DIR}/comment-first"21OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/comment-first")"22[ "_${OUTPUT}" = '_v0.11' ] || die "comment-only line not skipped; got '${OUTPUT}'"2324printf 'v0.12\t \nv0.13\n' > "${TEST_DIR}/trailing-space"25OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/trailing-space")"26EXPECTED="$(printf 'v0.12\nv0.13')"27[ "_${OUTPUT}" = "_${EXPECTED}" ] || die "trailing whitespace not stripped, or a line was dropped; got '${OUTPUT}'"2829printf '\n \n\t\n# only a comment\n' > "${TEST_DIR}/nothing-usable"30OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/nothing-usable")"31[ "_${OUTPUT}" = '_' ] || die "blank and comment-only lines should produce no output; got '${OUTPUT}'"3233nvm_print_alias_file "${TEST_DIR}/nothing-usable" >/dev/null 2>&1 \34 || die 'a file with no usable lines should still exit zero'3536printf 'v0.14' > "${TEST_DIR}/no-trailing-newline"37OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/no-trailing-newline")"38[ "_${OUTPUT}" = '_v0.14' ] || die "a final line with no trailing newline should be read; got '${OUTPUT}'"3940# a `#` pattern is a repetition operator under zsh's `extendedglob`, which is what41# made `nvm_alias` print nothing at all: https://github.com/nvm-sh/nvm/issues/388542if [ -n "${ZSH_VERSION-}" ]; then43 setopt extendedglob || die 'enabling extendedglob failed'44 OUTPUT="$(nvm_print_alias_file "${TEST_DIR}/inline")"45 [ "_${OUTPUT}" = '_v0.10' ] || die "with extendedglob set, expected 'v0.10', got '${OUTPUT}'"46 [[ -o extendedglob ]] || die 'nvm_print_alias_file left extendedglob unset'4748 unsetopt extendedglob49 nvm_print_alias_file "${TEST_DIR}/inline" > /dev/null50 [[ -o extendedglob ]] && die 'nvm_print_alias_file left extendedglob set'51fi5253cleanup
added:test/sourcing/Sourcing nvm.sh with zsh extendedglob should use the default+33 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45\. ../common.sh67# `#` is a pattern operator only under zsh's `extendedglob`8[ -n "${ZSH_VERSION-}" ] || exit 0910# We need to unload nvm again first, as by the time this test is run,11# despite being unloaded in setup, the inherited PATH still contains12# an nvm-installed node version. `nvm unload` unsets NVM_DIR, so cache it.13\. ../../nvm.sh || die 'sourcing returned nonzero exit code'14NVM_DIR_CACHED="${NVM_DIR}"15nvm unload || die 'unloading returned nonzero exit code'16NVM_DIR="${NVM_DIR_CACHED}"1718# the comment is the point: stripping it is what needs a `#` pattern19echo '0.10.1 # comment' > "${NVM_DIR}/alias/default" || die 'creation of default alias failed'2021setopt extendedglob || die 'enabling extendedglob failed'2223\. ../../nvm.sh || die 'sourcing returned nonzero exit code'2425[[ -o extendedglob ]] || die 'sourcing nvm.sh disabled extendedglob'2627NVM_LS_CURRENT_NOT_GREPPED="$(nvm ls current | strip_colors)"28NVM_LS_CURRENT="$(nvm ls current | strip_colors | \grep -o 'v0\.10\.1')"29[ "_${NVM_LS_CURRENT}" = '_v0.10.1' ] || die "'nvm ls current' did not return '-> v0.10.1', got '${NVM_LS_CURRENT_NOT_GREPPED}'"3031NVM_ALIAS_DEFAULT="$(nvm alias default | strip_colors)"32[ "_${NVM_ALIAS_DEFAULT}" = '_default -> 0.10.1 (-> v0.10.1 *)' ] \33 || die "'nvm alias default' did not return 'default -> 0.10.1 (-> v0.10.1 *)', got '${NVM_ALIAS_DEFAULT}'"
modified:nvm.sh+19 added-11 removed
1425 unmodified lines
1426 ) | command sort1427 return1428}14291430nvm_print_alias_file() {1431 # a `#` pattern is a repetition operator under zsh's `extendedglob`, so it is1432 # unset for the length of this function and restored by `local_options`1433 nvm_is_zsh && setopt local_options noextendedglob14341435 local NVM_ALIAS_LINE1436 while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do1437 NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"1438 case "${NVM_ALIAS_LINE}" in1439 *[![:space:]]*) ;;1440 *) continue ;;1441 esac1442 NVM_ALIAS_LINE="${NVM_ALIAS_LINE%"${NVM_ALIAS_LINE##*[![:space:]]}"}"1443 nvm_echo "${NVM_ALIAS_LINE}"1444 done < "$1"1445}14461447nvm_alias() {1448 local ALIAS1449 ALIAS="${1-}"1450 if [ -z "${ALIAS}" ]; then
22 unmodified lines
1473 nvm_err "Alias file is not readable: ${NVM_ALIAS_PATH}"1474 return 01475 fi14761460 local NVM_ALIAS_LINE1461 while IFS= read -r NVM_ALIAS_LINE || [ -n "${NVM_ALIAS_LINE}" ]; do1462 NVM_ALIAS_LINE="${NVM_ALIAS_LINE%%#*}"1463 case "${NVM_ALIAS_LINE}" in1464 *[![:space:]]*) ;;1465 *) continue ;;1466 esac1467 NVM_ALIAS_LINE="${NVM_ALIAS_LINE%"${NVM_ALIAS_LINE##*[![:space:]]}"}"1468 nvm_echo "${NVM_ALIAS_LINE}"1469 done < "${NVM_ALIAS_PATH}"1477 nvm_print_alias_file "${NVM_ALIAS_PATH}"1478}14791480nvm_ls_current() {1481 local NVM_LS_CURRENT_NODE_PATH
3486 unmodified lines
4968 nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \4969 nvm_has_solaris_binary nvm_is_merged_node_version \4970 nvm_is_natural_num nvm_is_version_installed nvm_validate_install \4971 nvm_install_lock_name nvm_acquire_install_lock nvm_release_install_lock \4964 nvm_list_aliases nvm_make_alias nvm_print_alias_path \4972 nvm_list_aliases nvm_make_alias nvm_print_alias_file nvm_print_alias_path \4973 nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \4974 nvm_sanitize_path nvm_has_colors nvm_process_parameters \4975 nvm_node_version_has_solaris_binary nvm_iojs_version_has_solaris_binary \4976 nvm_curl_libz_support nvm_command_info nvm_is_zsh nvm_stdout_is_terminal \
181 unmodified lines