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

`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
This commit is contained in:
Jordan Harband
2026-09-08 11:01:03 -07:00
parent a5ff3a32f4
commit e361b93e4b
4 changed files with 95 additions and 8 deletions
+12 -7
View File
@@ -4257,7 +4257,11 @@ nvm() {
nvm_err "Could not find ${NVM_DIR}/*/share/man in \${MANPATH}"
fi
else
export MANPATH="${NEWPATH}"
# `man` treats both of these as unset anyway, so leave nothing behind
case "${NEWPATH}" in
'' | ':') unset MANPATH ;;
*) export MANPATH="${NEWPATH}" ;;
esac
if [ "${NVM_SILENT:-0}" -ne 1 ]; then
nvm_echo "${NVM_DIR}/*/share/man removed from \${MANPATH}"
fi
@@ -4380,12 +4384,13 @@ nvm() {
# Change current version
PATH="$(nvm_change_path "${PATH}" "/bin" "${NVM_VERSION_DIR}")"
if nvm_has manpath; then
if [ -z "${MANPATH-}" ]; then
local MANPATH
MANPATH=$(manpath)
fi
# Change current version
MANPATH="$(nvm_change_path "${MANPATH}" "/share/man" "${NVM_VERSION_DIR}")"
MANPATH="$(nvm_change_path "${MANPATH-}" "/share/man" "${NVM_VERSION_DIR}")"
# `man` consults its configured default path only where the list has an
# empty entry; a trailing one keeps nvm's directory ahead of it
case "${MANPATH}" in
:* | *::* | *:) ;;
*) MANPATH="${MANPATH}:" ;;
esac
export MANPATH
fi
export PATH