Commit Graph
738 Commits
Author SHA1 Message Date
Jordan Harband 55bdfd19dd [Tests] nvm_npm_global_modules: add unit tests 2026-11-30 18:07:27 -08:00
Jordan Harband 90965e3aae [Tests] nvm_npm: add unit tests 2026-11-30 18:07:26 -08:00
Jordan Harband d86011bb0b [Tests] nvm_normalize_version: add unit tests 2026-11-30 18:07:26 -08:00
Jordan Harband 9c71dec09a [Tests] nvm_needs_lts_status: add unit tests 2026-11-30 18:07:26 -08:00
Jordan Harband 9165bf1060 [Tests] nvm_match_version: add unit tests 2026-11-30 18:07:25 -08:00
Jordan Harband a95fabb521 [Tests] nvm_lts_status_path: add unit tests 2026-11-30 18:07:25 -08:00
Jordan Harband 72178b2d67 [Tests] nvm_ls_remote_index_tab: add unit tests 2026-11-30 18:07:25 -08:00
Jordan Harband 08763e43ef [Tests] nvm_list_aliases: add unit tests 2026-11-30 18:07:24 -08:00
Jordan Harband aa20d996b6 [Tests] nvm_is_zsh: add unit tests 2026-11-30 18:07:24 -08:00
Jordan Harband 4b12bf4a90 [Tests] nvm_is_version_selected: add unit tests 2026-11-30 18:07:24 -08:00
Jordan Harband 866a9d64c3 [Tests] nvm_is_version_or_alias: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband fb17d36f31 [Tests] nvm_is_below_min_version: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband 43a4ea2a12 [Tests] nvm_install_npm_if_needed: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband e27de13dfa [Tests] nvm_grep: add unit tests 2026-11-30 18:07:23 -08:00
Jordan Harband e3cc267387 [Tests] nvm_get_remote_aliases: add unit tests 2026-11-30 18:07:22 -08:00
Jordan Harband 10dada17da [Tests] nvm_find_nvmrc: add unit tests 2026-11-30 18:07:22 -08:00
Jordan Harband 0527e34f99 [Tests] nvm_filter_min_versions: add unit tests 2026-11-30 18:07:22 -08:00
Jordan Harband b2ad63095f [Tests] nvm_filter_lts_versions: add unit tests 2026-11-30 18:07:21 -08:00
Jordan Harband 6bdf17fb4d [Tests] nvm_explain_excluded_version: add unit tests 2026-11-30 18:07:21 -08:00
Jordan Harband 300ab22dfb [Tests] nvm_err_with_colors: add unit tests 2026-11-30 18:07:21 -08:00
Jordan Harband 4c227362a4 [Tests] nvm_ensure_lts_status: add unit tests 2026-11-30 18:07:20 -08:00
Jordan Harband a5eef5568e [Tests] nvm_echo_with_colors: add unit tests 2026-11-30 18:07:20 -08:00
Jordan Harband 8baffe92e0 [Tests] nvm_cd_root: add unit tests 2026-11-30 18:07:20 -08:00
Jordan Harband 7773328bb5 [Tests] nvm_cache_dir: add unit tests 2026-11-30 18:07:19 -08:00
Jordan Harband c4bb986185 [Tests] nvm_binary_available: add unit tests 2026-11-30 18:07:19 -08:00
Oikantik Basu 74544c12f4 [Fix] nvm install: explain native Windows curl write failures
Refs #3391.
2026-11-24 19:09:58 +05:30
Hridhin Varkey Joseph 41041a99ca [Tests] nvm_check_file_permissions: symlinked directories and cycles are not traversed 2026-11-22 22:41:58 +05:30
thejesh23 35519ec047 [Fix] nvm reinstall-packages: fail when the version is not installed
`nvm reinstall-packages 99` resolved the version to `N/A` and kept going:
it told the user to run `nvm install N/A`,
printed "Reinstalling global packages from N/A..." and exited 0.
Only the case of an installed version excluded by `--only` returned early.

Now any unresolved version prints "Version '99' is not installed." and returns 3,
like `nvm install --reinstall-packages-from=`,
which already requires an installed version.

Assisted-by: Claude Code <noreply@anthropic.com>
Signed-off-by: thejesh23 <35212698+thejesh23@users.noreply.github.com>
2026-11-22 08:26:23 -08:00
NUAAwqyandJordan Harband a66a5ae5d2 [Fix] nvm.sh: use NVM_-prefixed locals in the sourcing path
Sourcing nvm.sh fails when the caller has made readonly a variable that nvm.sh declares as a local,
such as `readonly VERSION` in a CI build script:
bash rejects the `local`, and dash exits.

Prefix the locals that sourcing and its `nvm use` reach.
The test derives the unprefixed local names from nvm.sh,
so a new collision fails it.

`nvm_install_default_packages` reported an invalid default-packages file by assigning `nvm`'s `EXIT_CODE` without declaring it.
It now declares its own, and `nvm install` uses its return status instead,
except 3, which means npm failed to install the packages and,
as before, does not fail the install.

Fixes #3584

Signed-off-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: NUAAwqy <146524024+NUAAwqy@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-28 06:30:00 +00:00
585b6ec915 [New] add prune command
`nvm prune` uninstalls every version except the newest install per major
(per minor with `--minor`, and always per minor for `v0.x`),
the current version, the `$NVM_DIR/current` symlink target,
and anything an alias resolves to.
It lists what it will remove and asks first;
`--dry-run` only lists, and `--force` skips the prompt.

Co-authored-by: Guillermo Esguerra <gesguerrab@gmail.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-02-23 13:21:38 -05:00
Peter Dave Hello eb0c8b95c8 [Tests] Verify explicit make jobs without redundant source builds
Exercise both argument orders and multiple job counts against the existing fake source fixture,
checking the actual build and install make arguments.
Use an isolated offline cache and a conflicting inherited job count to catch regressions that a successful real compilation alone cannot detect.

Keep a real source build with explicit jobs in each installation suite,
but remove the redundant serial compilation and make installation failures and version mismatches fail the test immediately.
2026-11-26 03:13:13 +08:00
Peter Dave HelloandJordan Harband bea27fbe0f [New] bash_completion: complete options and arguments in context
Parse words before the cursor once to offer flags, versions, and aliases
in the positions that accept them. Distinguish install options before
and after the version, and leave run and exec arguments to the shell.

Match local names literally. Account for Readline splitting at `=` and
quoting common prefixes. Give zsh the same candidates as raw strings so
its line editor can match, quote, and close arguments in its own syntax.

Check the offered flags against nvm help and cover argument boundaries,
including the actual arguments submitted by zsh after Tab completion.

Co-authored-by: Peter Dave Hello <hsu@peterdavehello.org>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-25 13:23:26 -08:00
Jordan Harband 3bf6686090 [Robustness] handle an overridden type
A `type` function or alias defined before nvm is sourced
hijacked `nvm_has` and `nvm_command_info`.
`nvm_has` now uses `command -v`;
`nvm_command_info` needs `type`'s output,
so it uses `builtin type` in zsh,
whose `command type` runs an external `type` that can't see shell functions.

Since `command -v` makes dash hash the command,
`nvm_command_info` also parses dash's and ksh's "tracked alias" output.

Fixes #2352.
2020-11-21 13:33:12 -08:00
thejesh23 adf35d7697 [Fix] nvm_get_default_packages: ignore leading and trailing blanks
A trailing space (or tab) left by an editor, or an indented line,
in `$NVM_DIR/default-packages` tripped the "multiple space-separated values" check,
so the whole file was rejected and no default packages were installed after `nvm install`,
with an error naming the wrong problem.
Trim blanks around each line before checking it,
as is already done for a trailing `\r`;
a line with two values is still rejected.

Assisted-by: Claude Code <noreply@anthropic.com>
Signed-off-by: thejesh23 <35212698+thejesh23@users.noreply.github.com>
2026-11-22 08:26:23 -08:00
谢骐 ee8fa8dc70 [Fix] install.sh: support .bash_login
Detect it after `.bash_profile`, including in completion setup,
and update related documentation and tests.

Assisted-by: Kimi K3.1 Pro <317908118+kimi-agent-bot@users.noreply.github.com>
2026-11-22 04:14:10 +08:00
Jordan Harband 9abb216367 [Fix] install.sh: detect the profile from the login shell that ran the installer
`$SHELL` is the account's login shell,
so when a terminal runs a different shell,
like zsh from Terminal's settings without `chsh`,
the installer wrote to the other shell's profile.
Prefer the shell that ran the installer,
but only when it is a login shell,
since a script that runs the installer,
like a bash dotfiles bootstrap,
isn't the user's shell.

Fixes #592.
Closes #611, whose idea this is.
2026-11-22 12:24:35 -08:00
Jordan HarbandandDivya Chukkapalli 62adf80079 [Fix] --lts=<name>: match codenames containing the name, not as a regex
`nvm_ls_remote_index_tab` compared the codename with awk's `!~`,
so the name was a regex: `--lts=.` matched every LTS line,
and `--lts=i.on` or `--lts=(` didn't mean what they say.
Use `index()` so a name still matches every codename that contains it,
but literally, and do the same in `nvm_filter_lts_versions`,
for `--lts=<name>` in an alias.

Locally, `--lts=<name>` resolved the `lts/<name>` alias,
which only exists for full codenames,
so `nvm use --lts=ir` failed where `nvm install --lts=ir` installed Iron.
Add `nvm_resolve_lts_name`,
which picks the newest codename in `$NVM_DIR/.lts-status` that contains the name,
and resolve `lts/<codename>` with it in `nvm use`, `uninstall`, `exec`, `run`,
and in `nvm install` for its offline lookup and the default and `--alias=` it writes.
Without a recorded status, or when no codename contains the name,
the name is used as given.

Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Co-authored-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-20 10:36:47 -08:00
Jordan Harbandanddajiaohuang dbda4b9387 [Fix] install.sh: update from nvm's remote; nvm_ls: list versions without globs; nvm_ensure_version_installed: do not leak VERSION
`install.sh`: an existing install now fetches from the remote whose URL is nvm's source,
adding one when none matches,
instead of always fetching `origin`;
fresh installs still name it `origin`.
The remote's URL is read with `git config`,
since `git remote get-url` needs git 2.7.

`nvm_ls`: `find` lists each version directory's non-hidden subdirectories itself,
so it works under `set -f`,
which disables the globs it was given;
the directories only need to exist.

`nvm_ensure_version_installed`:
the resolved alias was assigned to `VERSION` without declaring it local.

Co-authored-by: dajiaohuang <mikewushuwen@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-11-19 22:51:28 -08:00
Jordan Harband b0a377a524 [Perf] nvm_ls: check whether a version directory has entries without forking
Each check ran `ls` piped into `grep` in a subshell,
which cost more than the `find` it guards;
a glob finds the same entries in the shell.
2026-11-19 16:05:28 -08:00
Hridhin Varkey Joseph 887c2969b7 [Perf] nvm_version: list installed versions once for node/stable
Once the newest installed version is 1.0 or later, `stable` is that version, so list the installed versions once and take the newest, instead of `nvm_ls stable` listing them three times.

Only 0.x installs (where `stable` is the newest even minor), a user alias named `stable`, and `--lts` filters keep the existing path. A system node, which `nvm_ls` lists last, is skipped.

Part of #3416.
2026-11-19 15:39:47 -08:00
hqzingandJordan Harband e8144d4194 [New] Add support for OpenHarmony platform
also: `nvm_get_os`: set `NVM_DIR` in the OpenHarmony chroots, and skip them without a glibc x86-64 layout

`sudo` resets the environment, and `nvm.sh` returns early when it cannot detect `NVM_DIR`,
so in the chroots `nvm_get_os` was undefined and every check got an empty result.
The chroot setup needs `ldd` and `/lib64/ld-linux-x86-64.so.2`, which macOS does not have.

Co-authored-by: hqzing <hqzing@outlook.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
2026-08-18 17:23:11 +08:00
Jordan Harband 944232943b [Fix] nvm_process_nvmrc_content: a key containing a space is not a duplicate of each word in it
The duplicate-key check kept keys in a space-separated list,
so a key such as `a b` followed by a key `a` or `b` was reported as a duplicate,
unlike in the `nvmrc` package.
Keep the keys newline-separated, since a key cannot contain a newline.
2026-11-18 16:20:53 -08:00
Divya Chukkapalli e8cfc3975d [Tests] nvm_process_nvmrc_content: compare .nvmrc keys literally
The duplicate-key check used to splice each key into an extended regex,
so distinct keys whose characters are regex syntax collided
(`node.dist` matched `node-dist`, `a+b` matched `ab`),
a key with `(` or `[` printed a grep error,
and a repeated `[x` key was not caught.
e4fcf988 now compares keys literally with a `case` pattern;
cover those cases.

Signed-off-by: Divya Chukkapalli <12871391+divya0795@users.noreply.github.com>
2026-11-18 11:59:49 -08:00
Jordan Harband ad42d90860 [Fix] nvm_ls_remote_index_tab, nvm_compare_checksum: also keep non-ASCII controls from the mirror off the terminal
Rejecting only ASCII control characters in the LTS codename still lets through a UTF-8 C1 control
(like U+009B, a CSI) or a bidi override (like U+202E),
which some terminals act on.
A row is now kept only if its codename is `-`, empty, or alias-safe,
which is the same shape the LTS alias step already requires,
and every real codename has.

BusyBox `tr` mishandles `-c` with a character class,
so on Alpine the expected checksum printed as almost all `?`;
an octal range in the C locale works in BusyBox, GNU, and BSD `tr`,
and does not make BSD `tr` abort on invalid UTF-8.
2026-11-16 15:13:13 -08:00
kasparovabi f3ad1d23b7 [Fix] nvm_ls_remote_index_tab, nvm_compare_checksum: keep control characters from the mirror off the terminal
`index.tab` and `SHASUMS256.txt` are untrusted mirror metadata.
An LTS codename (`$10`) carrying ESC/BEL bytes was printed by `nvm ls-remote`,
and a mismatched expected checksum was echoed as received.

Skip `index.tab` rows whose codename has a control character,
next to the existing version check,
and show non-printable bytes of the expected checksum as `?`.
2026-11-12 14:28:16 +03:00
Jordan Harband d66749b21c [Fix] install.sh: run the system npm outside the project
`nvm_check_global_modules` ran `npm --version` and `npm list -g` in `$PWD`,
so the project `.npmrc` applied, eg its `logs-dir` on npm v8.11 through v10.5.
The "npm unavailable" test put its fake npm on a relative `PATH` entry, which no longer resolves from `/`.
2026-11-16 15:31:02 -08:00
Jordan Harband e837e16067 [Fix] run npm outside the project for nvm use, install-latest-npm, and global package installs
These also ran npm in `$PWD`, applying the untrusted project `.npmrc`.
npm v7 through v8.2 apply it even with `-g`,
so its `registry` chose where `nvm install-latest-npm`, `--latest-npm`, `default-packages`, and `reinstall-packages` downloaded globally-installed packages from, and their install scripts then ran.
npm v8.11 through v10.5 honor its `logs-dir` and `cache` for `npm --version`,
so every non-silent `nvm use` created those directories and wrote a debug log there.

Use `nvm_npm`, or `nvm_cd_root` where `xargs` runs npm.
`npm link` still runs in each linked package's own directory.
2026-11-16 15:31:01 -08:00
Jordan Harband f07f4ed8f3 [Fix] nvm debug: do not let a project .npmrc configure npm
`nvm debug` ran `npm config get prefix` in `$PWD`, so npm applied the untrusted project `.npmrc`.
Its `logs-dir` chose where npm wrote its debug log,
and npm 11 copies unknown keys into that log verbatim,
so `logs-dir=~/.bashrc.d` plus a `$(...)` key runs a command in the next shell on Fedora-style setups.
Its `userconfig` makes npm 11 echo the lines of any file into the log and into the `nvm debug` output.

`-g` is not enough: npm v7 through v8.2 load the project `.npmrc` even in global mode.
Add `nvm_npm`, which runs npm from `/`, and use it for both npm commands.
`$NVM_CD_FLAGS` is not used, since a zsh parent exports `-q` to bash and sh children.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ZF4UWQNS.
2026-11-16 15:21:25 -08:00
Jordan Harband 2abfc380ad [Fix] nvm_download: pass a mirror URL with credentials to curl and GNU Wget on stdin, not as an argument
903e3493 moved `$NVM_AUTH_HEADER` out of the downloader's arguments,
but a mirror URL with `user:password@` in it was still one,
so any local user could read the password in `ps` or /proc/<pid>/cmdline
for every index.tab, SHASUMS, and tarball request.
curl now gets such a URL as `url = "..."` in the config it already reads with `--config -`,
with `"` and `\` escaped;
GNU Wget reads it with `--input-file=-`,
including in the same-origin redirect loop.
A URL with whitespace or a control character cannot be one config line, and stays an argument.
BusyBox wget has no way to take a URL other than as an argument; the README says so.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-MNDN98RD.
2026-11-16 15:15:25 -08:00
Jordan Harband cb7b66c729 [Fix] nvm_download_artifact: give each download a path no other process knows
Every download went to the fixed `<archive>.unverified`,
and curl opens its `-o` file only on the first body byte,
so a curl left running by an earlier install of the same version
(past the install lock via `nvm cache clear`, a stale-lock takeover, or a removed lock)
survived the `rm -f`, then opened the same file
and could rewrite it after the checksum passed, while `tar` read it,
and leave it in the cache that `--offline` trusts.

Downloading into a `mktemp -d` directory in the same cache dir
keeps the rename atomic and the file's mode following the umask.
Leftover `.unverified*` paths from interrupted installs are still removed first.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-XUXR5LQ4.
2026-11-16 15:09:20 -08:00