Repository navigation
Conversation
`nvm_change_path` prepends the new version (instead of replacing the existing nvm path) when a system bin dir precedes an nvm path, to handle macOS `path_helper` reordering the `PATH` of nested sessions (nvm-sh#1652). However, the check matched a system dir followed by *any* nvm path. Once a stale nvm path sits after the system dirs (e.g. inherited by a nested shell, tmux, or an editor's terminal), the check stays true forever, so every `nvm use` prepends yet another entry and the previously used version is never removed from the `PATH`. Only consider the first nvm path: if it is not preceded by a system bin dir, replace it in place as usual. Fixes nvm-sh#3561
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3561.
Problem
nvm usekeeps prepending toPATHinstead of replacing the previously used version, soPATHgrows on every switch and old versions are never removed. ThePATHin #3561 shows it:Reproduction:
Root cause
Since #1830 (for #1652),
nvm_change_pathprepends instead of replacing when a system bin dir shadows an nvm path, which happens when macOSpath_helperreorders thePATHof a nested session. The check ((^|:)(/usr(/local)?)?/bin:.*$NVM_DIR/...) matches a system dir followed by any nvm path. Once a stale nvm path is left after the system dirs (inherited by a nested shell, tmux, or an editor's terminal), the check is true on every call, even though the nvm path that is actually used already comes before the system dirs. So everynvm useprepends one more entry.Fix
Only look at the part of
PATHbefore the first nvm path (${1%%"${NVM_DIR}"/*}). If it contains a system bin dir, prepend as before, so the #1652 case is unchanged. Otherwise, replace the first nvm path in place, as in the normal case. This also turns the twogreppipelines in that branch into one.Tests
test/fast/Unit tests/nvm_change_pathwhere an nvm path was already prepended ahead of the system dirs. It fails before this change and passes after. The existing NVM fails to load within nested shell #1652 cases are unchanged and still pass.nvm_change_path,nvm_change_path special characters, andnvm_strip_pathpass under sh, bash, dash, zsh, and ksh. AnNVM_DIRcontaining glob characters (/tmp/a*b[c]) was also checked under bash, dash, zsh, and ksh.masterhas in that environment (running as root, no tty). The fix adds no new failures.npm run eclintpasses, andshellcheck -s {bash,sh,dash,ksh}reports nothing for the changed function.