Skip to content
Myr

[Fix] `nvm_hash_reset`: only run `hash -r` where the shell supports it

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

Under bash's `set +h`, `hash -r` errors with "hash: hashing disabled", which is then printed on every `nvm use`, including the one at shell startup. Guarding on `$-` alone would be wrong: `zsh` never puts `h` in `$-` - there `h` is `HIST_IGNORE_DUPS` - and `dash` has no `h` flag at all, so it would skip the call in 3 of the 4 shells the fast suite runs. Gate on the shell instead, and skip only where the call fails: bash under `set +h`, and `ksh93`, whose `hash` takes no `-r` and whose usage error aborts `nvm use` before it can export `NVM_BIN`. `ksh` is detected by probing rather than by version, so `mksh` and `pdksh`, which do accept `-r`, keep getting the call. The `${-#*h}` test is from #3910. Refs #2065 Refs #3247

authored by Jordan Harband · committed by Jordan Harband · parent 80e700e · Browse files · 4 files+117 added−3 removed
This commit

State

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

Diff stats

4 files+117 added−3 removed

  • tests:+101 added−0 removed
  • other:+16 added−3 removed

4 files+117 added−3 removed

added:test/fast/Unit tests/nvm_hash_reset+27 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45: nvm.sh6\. ../../../nvm.sh78# also covers #3247, where `command hash -r` found a `hash` binary, not the builtin9OUTPUT="$(nvm_hash_reset 2>&1)"10RC=$?11[ ${RC} -eq 0 ] || die "nvm_hash_reset returned ${RC}: ${OUTPUT}"12[ -z "${OUTPUT}" ] || die "nvm_hash_reset was not silent: ${OUTPUT}"1314# bash is the only supported shell that can turn hashing off15if [ -n "${BASH_VERSION-}" ]; then16 set +h17 OUTPUT="$(nvm_hash_reset 2>&1)"18 RC=$?19 set -h20 [ ${RC} -eq 0 ] || die "with hashing disabled, nvm_hash_reset returned ${RC}: ${OUTPUT}"21 [ -z "${OUTPUT}" ] || die "with hashing disabled, nvm_hash_reset was not silent: ${OUTPUT}"2223 ls >/dev/null 2>&124 hash -t ls >/dev/null 2>&1 || die 'expected `ls` to be hashed'25 nvm_hash_reset || die 'with hashing enabled, nvm_hash_reset failed'26 ! hash -t ls >/dev/null 2>&1 || die 'nvm_hash_reset did not clear the hash table'27fi
added:test/fast/Running 'nvm use' and 'nvm deactivate' with hashing disabled+45 added
1#!/bin/bash23# the `bash` shebang is deliberate: only bash can exhibit the bug, so pinning4# it keeps this meaningful in every job of the shell matrix56die () { echo "$@" ; cleanup ; exit 1; }7cleanup () {8 set -h9 rm -f "${OUTPUT_FILE-}"10}1112export NVM_DIR="$(cd ../.. && pwd)"1314: nvm.sh15\. ../../nvm.sh16\. ../common.sh1718make_fake_node v0.2.3 || die 'unable to make fake node'1920OUTPUT_FILE="$(mktemp)"2122set +h2324# not in a command substitution, so the `PATH` it sets survives to `nvm deactivate`25nvm use --delete-prefix v0.2.3 > "${OUTPUT_FILE}" 2>&126OUTPUT="$(cat "${OUTPUT_FILE}")"27case "${OUTPUT}" in28 *'hash: '*) die "nvm use complained about hashing: ${OUTPUT}" ;;29esac30case "${OUTPUT}" in31 *'Now using node v0.2.3'*) ;;32 *) die "nvm use did not activate v0.2.3: ${OUTPUT}" ;;33esac3435nvm deactivate > "${OUTPUT_FILE}" 2>&136OUTPUT="$(cat "${OUTPUT_FILE}")"37case "${OUTPUT}" in38 *'hash: '*) die "nvm deactivate complained about hashing: ${OUTPUT}" ;;39esac40case "${OUTPUT}" in41 *'removed from ${PATH}'*) ;;42 *) die "nvm deactivate did not strip the nvm bin dir: ${OUTPUT}" ;;43esac4445cleanup
added:test/fast/Running 'nvm use' does not leave a stale hashed node+29 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45export NVM_DIR="$(cd ../.. && pwd)"67: nvm.sh8\. ../../nvm.sh9\. ../common.sh1011make_fake_node v0.2.3 || die 'unable to make fake node v0.2.3'12make_fake_node v0.10.1 || die 'unable to make fake node v0.10.1'1314nvm use --delete-prefix v0.2.3 >/dev/null 2>&1 || die 'failed to activate v0.2.3'1516# run `node` from this shell, so that the shell caches where it found it17node >/dev/null 2>&1 || die 'fake node v0.2.3 is not runnable'1819nvm use --delete-prefix v0.10.1 >/dev/null 2>&1 || die 'failed to activate v0.10.1'2021ACTUAL="$(node)"22[ "${ACTUAL}" = 'v0.10.1' ] || die "expected node v0.10.1 after switching versions, got '${ACTUAL}'"2324nvm deactivate >/dev/null 2>&1 || die 'failed to deactivate'2526RESOLVED="$(command -v node || true)"27case "${RESOLVED}" in28 "${NVM_DIR}"/*) die "after deactivating, node still resolves inside \${NVM_DIR}: ${RESOLVED}" ;;29esac
modified:nvm.sh+16 added-3 removed
1115 unmodified lines
1116 -e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*#${3-}${2-}#"1117 fi1118}11191120nvm_hash_reset() {1121 # under `set +h` there is no cache to clear, and `hash -r` errors1122 if [ -n "${BASH_VERSION-}" ] && [ "${-#*h}" = "$-" ]; then1123 return 01124 fi1125 # `ksh93`'s `hash` takes no `-r` and its usage error is fatal; probing in a1126 # subshell contains it, and spares the `ksh` variants that do accept `-r`1127 if [ -n "${KSH_VERSION-}" ] && ! ( \hash -r ) >/dev/null 2>&1; then1128 return 01129 fi1130 \hash -r1131}11321133nvm_binary_available() {1134 # binaries started with node 0.8.61135 nvm_version_greater_than_or_equal_to "$(nvm_strip_iojs_prefix "${1-}")" v0.8.61136}
3103 unmodified lines
4240 nvm_err "Could not find ${NVM_DIR}/*/bin in \${PATH}"4241 fi4242 else4243 export PATH="${NEWPATH}"4231 \hash -r4244 nvm_hash_reset4245 if [ "${NVM_SILENT:-0}" -ne 1 ]; then4246 nvm_echo "${NVM_DIR}/*/bin removed from \${PATH}"4247 fi4248 fi
136 unmodified lines
4385 MANPATH="$(nvm_change_path "${MANPATH}" "/share/man" "${NVM_VERSION_DIR}")"4386 export MANPATH4387 fi4388 export PATH4376 \hash -r4389 nvm_hash_reset4390 export NVM_BIN="${NVM_VERSION_DIR}/bin"4391 export NVM_INC="${NVM_VERSION_DIR}/include/node"4392 if [ "${NVM_SYMLINK_CURRENT-}" = true ]; then4393 command rm -f "${NVM_DIR}/current" && ln -s "${NVM_VERSION_DIR}" "${NVM_DIR}/current"
598 unmodified lines
4992 nvm_print_color_legend \4993 nvm_echo_with_colors nvm_err_with_colors \4994 nvm_get_artifact_compression nvm_install_binary_extract nvm_extract_tarball \4995 nvm_process_nvmrc nvm_process_nvmrc_content nvm_nvmrc_invalid_msg \4983 nvm_write_nvmrc \4996 nvm_write_nvmrc nvm_hash_reset \4997 >/dev/null 2>&14998 unset NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \4999 NVM_CD_FLAGS NVM_BIN NVM_INC NVM_MAKE_JOBS NVM_INSTALL_LOCK \5000 NVM_COLORS INSTALLED_COLOR SYSTEM_COLOR \
170 unmodified lines