Skip to content
Myr

[Fix] `nvm use`: do not clobber `man`'s default search path

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

`man` consults its own configured search path only when MANPATH holds an empty entry; a list without one replaces the default outright. `nvm use` produced exactly that, so activating a version could hide every system man page. Contribute the empty entry as a trailing one, so that nvm's directory keeps precedence over the system's, and only when the list has none already, so that repeated `nvm use` calls are idempotent and an empty entry the user placed stays where they put it. The `$(manpath)` snapshot this replaces went stale the moment man's configuration changed, and cost a subprocess per `nvm use`. It was also dead: `local MANPATH` scoped the assignment to `nvm()`, so `export` did not outlive the call and MANPATH went untouched whenever it started out unset. Only ksh, where `local` is not a builtin, ever ran it. `nvm deactivate` now unsets MANPATH where nvm's was the only real entry, rather than leaving a bare `:` behind. Reported and diagnosed by @al0ksar in #3890, which used a leading empty entry instead. That one accrues one more colon on every `nvm use`, which `nvm deactivate` then leaves behind, and it loses to the system's man pages wherever nvm's bin directory is not first on PATH. Supersedes #2077. Refs #3890 Refs #2077

authored by Jordan Harband · committed by Jordan Harband · parent a5ff3a3 · Browse files · 4 files+95 added−8 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+95 added−8 removed

  • tests:+82 added−0 removed
  • other:+13 added−8 removed

4 files+95 added−8 removed

modified:test/fast/Unit tests/nvm_change_path+14 added
42 unmodified lines
4344[ "$NEW_PATH" = "$NVM_DIR/v0.1.2/bin" ] || die "Not correctly prepended: $NEW_PATH "454647# empty entries select `man`'s own search path, so they have to survive intact48MAN_PATH_WITH_EMPTY_ENTRIES=/opt/foo/man::/opt/bar/man:4950# New version dir51NEW_PATH=`nvm_change_path "$MAN_PATH_WITH_EMPTY_ENTRIES" "/share/man" "$NVM_DIR/versions/node/v7.1.0"`5253[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0/share/man:$MAN_PATH_WITH_EMPTY_ENTRIES" ] || die "Not correctly prepended: $NEW_PATH "5455# Replacing an existing version dir56NEW_PATH=`nvm_change_path "$NVM_DIR/versions/node/v4.5.0/share/man::/opt/bar/man:" "/share/man" "$NVM_DIR/versions/node/v7.1.0"`5758[ "$NEW_PATH" = "$NVM_DIR/versions/node/v7.1.0/share/man::/opt/bar/man:" ] || die "Not correctly changed: $NEW_PATH "596061# https://github.com/nvm-sh/nvm/issues/1652#issuecomment-34257122362MAC_OS_NESTED_SESSION_PATH=/usr/bin:/usr/local/bin:$NVM_DIR/versions/node/v4.5.0/bin6364# New version dir
8 unmodified lines
added:test/fast/Running 'nvm use' should not clobber the default MANPATH+68 added
1#!/bin/sh23set -e45die () { echo "$@" ; exit 1; }67export NVM_DIR="$(cd ../.. && pwd)"89# `--delete-prefix` covers only the npmrc files, so an inherited prefix in the10# environment would still abort `nvm use`11unset PREFIX npm_config_prefix NPM_CONFIG_PREFIX1213: nvm.sh14\. ../../nvm.sh15\. ../common.sh1617# the block under test is gated on `nvm_has manpath`, and the Alpine CI images18# ship no `manpath`; the output is recognizable so that a reintroduced19# `$(manpath)` call fails the assertions instead of passing vacuously20manpath() {21 echo '/usr/share/man'22}2324make_fake_node v0.10.225make_fake_node v0.10.32627MAN_2="$(nvm_version_path v0.10.2)/share/man"28MAN_3="$(nvm_version_path v0.10.3)/share/man"2930# nvm has to contribute the empty entry itself when there is nothing to preserve31unset MANPATH32nvm use --delete-prefix v0.10.2 --silent || die "Failed to activate v0.10.2"33[ "${MANPATH}" = "${MAN_2}:" ] || die "MANPATH should be '${MAN_2}:'; got '${MANPATH}'"3435nvm deactivate --silent || die "Failed to deactivate v0.10.2"36[ -z "${MANPATH-}" ] || die "MANPATH should be unset again; got '${MANPATH}'"3738# nvm's entry first, the empty entry last: the reverse of what a leading empty39# entry produces40MANPATH='/opt/foo/man'41nvm use --delete-prefix v0.10.2 --silent || die "Failed to activate v0.10.2"42[ "${MANPATH}" = "${MAN_2}:/opt/foo/man:" ] || die "MANPATH should be '${MAN_2}:/opt/foo/man:'; got '${MANPATH}'"4344# switching versions replaces nvm's entry without accruing more empty ones45nvm use --delete-prefix v0.10.3 --silent || die "Failed to activate v0.10.3"46[ "${MANPATH}" = "${MAN_3}:/opt/foo/man:" ] || die "MANPATH should be '${MAN_3}:/opt/foo/man:'; got '${MANPATH}'"4748nvm use --delete-prefix v0.10.2 --silent || die "Failed to reactivate v0.10.2"49[ "${MANPATH}" = "${MAN_2}:/opt/foo/man:" ] || die "MANPATH should be '${MAN_2}:/opt/foo/man:'; got '${MANPATH}'"5051nvm deactivate --silent || die "Failed to deactivate v0.10.2"52[ "${MANPATH}" = '/opt/foo/man:' ] || die "MANPATH should be '/opt/foo/man:'; got '${MANPATH}'"5354# a value with no empty entry at all still deactivates to nothing55MANPATH="${MAN_2}"56nvm deactivate --silent || die "Failed to deactivate v0.10.2"57[ -z "${MANPATH-}" ] || die "MANPATH should be unset again; got '${MANPATH}'"5859# an empty entry the user placed themselves stays where they put it60for TEST_MANPATH in ':/opt/foo/man' '/opt/foo/man:' '/a/man::/b/man'; do61 MANPATH="${TEST_MANPATH}"6263 nvm use --delete-prefix v0.10.2 --silent || die "Failed to activate v0.10.2 with MANPATH '${TEST_MANPATH}'"64 [ "${MANPATH}" = "${MAN_2}:${TEST_MANPATH}" ] || die "MANPATH should be '${MAN_2}:${TEST_MANPATH}'; got '${MANPATH}'"6566 nvm deactivate --silent || die "Failed to deactivate v0.10.2 with MANPATH '${TEST_MANPATH}'"67 [ "${MANPATH}" = "${TEST_MANPATH}" ] || die "MANPATH should be '${TEST_MANPATH}'; got '${MANPATH}'"68done
modified:nvm.sh+12 added-7 removed
4255 unmodified lines
4256 if [ "${NVM_SILENT:-0}" -ne 1 ]; then4257 nvm_err "Could not find ${NVM_DIR}/*/share/man in \${MANPATH}"4258 fi4259 else4260 export MANPATH="${NEWPATH}"4260 # `man` treats both of these as unset anyway, so leave nothing behind4261 case "${NEWPATH}" in4262 '' | ':') unset MANPATH ;;4263 *) export MANPATH="${NEWPATH}" ;;4264 esac4265 if [ "${NVM_SILENT:-0}" -ne 1 ]; then4266 nvm_echo "${NVM_DIR}/*/share/man removed from \${MANPATH}"4267 fi4268 fi
114 unmodified lines
43834384 # Change current version4385 PATH="$(nvm_change_path "${PATH}" "/bin" "${NVM_VERSION_DIR}")"4386 if nvm_has manpath; then4383 if [ -z "${MANPATH-}" ]; then4384 local MANPATH4385 MANPATH=$(manpath)4386 fi4387 # Change current version4388 MANPATH="$(nvm_change_path "${MANPATH}" "/share/man" "${NVM_VERSION_DIR}")"4387 MANPATH="$(nvm_change_path "${MANPATH-}" "/share/man" "${NVM_VERSION_DIR}")"4388 # `man` consults its configured default path only where the list has an4389 # empty entry; a trailing one keeps nvm's directory ahead of it4390 case "${MANPATH}" in4391 :* | *::* | *:) ;;4392 *) MANPATH="${MANPATH}:" ;;4393 esac4394 export MANPATH4395 fi4396 export PATH4397 nvm_hash_reset
781 unmodified lines
modified:README.md+1 added-1 removed
868 unmodified lines
869- `NVM_INC` - node's include file directory (useful for building C/C++ addons for node).870- `NVM_CD_FLAGS` - used to maintain compatibility with zsh.871- `NVM_RC_VERSION` - version from .nvmrc file if being used.872873Additionally, nvm modifies `PATH`, and, if present, `MANPATH` and `NODE_PATH` when changing versions.873Additionally, nvm modifies `PATH` when changing versions, along with `MANPATH` wherever a `manpath` command exists, and `NODE_PATH`, if present.874875The following environment variables can be set to configure `nvm install`:876877- `NVM_NO_SOURCE_FALLBACK` - when `1`, a failed binary download aborts instead of silently falling back to a (much slower) from-source compile; the persistent equivalent of the `-b` flag, and mutually exclusive with `-s`.
339 unmodified lines