Repository navigation
Description
Describe the bug
With Node.js in managed mode and npm in system-first mode, vp install runs the npm in the managed Node.js bin directory. It does not run the system npm that is first on PATH. In system-first mode, Vite+ prefers system tools (environment modes).
prepend_js_runtime_to_path_env prepends the managed Node.js bin directory, and that directory also contains npm. The system-first lookup for npm then uses this PATH. find_system_tool accepts a managed installation that is first on PATH, so it selects the bundled npm.
Moving the system npm directory to the front is not a safe fix. A directory such as /usr/bin can also contain another node, and that node then hides the managed Node.js. Instead, vp install can look up the system npm before it prepends the managed Node.js bin directory, and run that npm by its absolute path.
I intend to submit a PR for this issue.
Reproduction
The script in the steps below is a self-contained reproduction. It creates its project in a temporary directory.
Steps to reproduce
- Run
vp env on nodeandvp env off npm. - Save the script below as
repro.shand runbash repro.sh. It creates a temporary project that pins Node.js 26.13.1 andnpm@10.9.0. It also creates a fake system npm that printsran: system npm. - Compare the two cases. In both cases, the fake system npm is first on PATH.
repro.sh
#!/usr/bin/env bash
set -euo pipefail
# Run with Node.js managed (`vp env on node`) and npm system-first (`vp env off npm`).
node_version=26.13.1
scratch=$(mktemp -d /tmp/vp-system-npm-repro.XXXXXX)
trap 'rm -rf -- "$scratch"' EXIT
# A system npm that reports which npm ran.
mkdir "$scratch/system-npm-bin"
cat > "$scratch/system-npm-bin/npm" <<'SH'
#!/bin/sh
if [ "${1-}" = "--version" ]; then echo 10.9.0; else echo "ran: system npm"; fi
SH
chmod +x "$scratch/system-npm-bin/npm"
mkdir "$scratch/project"
cd "$scratch/project"
printf '%s\n' "$node_version" > .nvmrc
cat > package.json <<'JSON'
{
"name": "vp-system-npm-repro",
"private": true,
"packageManager": "npm@10.9.0",
"scripts": { "preinstall": "node -e \"console.log('ran: ' + process.env.npm_execpath)\"" }
}
JSON
managed_bin=$(dirname "$(vp env exec --node "$node_version" -- node -p 'process.execPath')")
vp --version | head -n 1
printf '\n%s\n' 'Case 1: system npm on PATH, managed Node.js bin directory not on PATH'
PATH="$scratch/system-npm-bin:$PATH" vp install 2>&1 | grep 'ran:'
printf '\n%s\n' 'Case 2: system npm before the managed Node.js bin directory on PATH'
PATH="$scratch/system-npm-bin:$managed_bin:$PATH" vp install 2>&1 | grep 'ran:'System Info
macOS (arm64)
vp v1.7.0, and main with #3591
Node.js: 26.13.1 from .nvmrc, managed mode
npm: 10.9.0 from packageManager, system-first mode
Used Package Manager
npm
Logs
Case 1: system npm on PATH, managed Node.js bin directory not on PATH
ran: <VP_HOME>/js_runtime/node/26.13.1/lib/node_modules/npm/bin/npm-cli.js
Case 2: system npm before the managed Node.js bin directory on PATH
ran: <VP_HOME>/js_runtime/node/26.13.1/lib/node_modules/npm/bin/npm-cli.js
Expected output in both cases: ran: system npm.
On main before #3591, case 2 runs the system npm, because the managed Node.js bin directory keeps its position on PATH.
Validations
- Read the Contributing Guidelines.
- Check that there isn't already an issue for the same bug.
- Confirm this is a Vite+ issue and not an upstream issue (Vite, Vitest, tsdown, Rolldown, or Oxc).
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Effort
Target date
Start date
Activity
voidzero-bot commentedon Nov 28, 2026
crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path(new snapshot casesystem_npm_first_on_path, verified failing without the fix)vp installruns the npm bundled with the managed Node.js becauseprepend_js_runtime_to_path_env(crates/vp_global_cli/src/commands/mod.rs) puts the managed Node.js bin directory at the front of PATH before the system-first package-manager lookup inrun_package_manager_command(crates/vp_global_cli/src/cli.rs). Since that directory also containsnpm,find_system_tool("npm")(crates/vp_global_cli/src/shim/dispatch.rs) sees the managed npm as the first PATH match and reuses it under the "a parent's selection" rule — mistaking Vite+'s own prepend for a parent-provided environment. This contradictsdocs/guide/env.md("Vite+ prefers system tools" in system-first mode), the merged intent of #3447, and the CLI's ownvp env which npm, which resolves to the real system npm under the same configuration.I found a potential fix for this issue. Following the approach suggested in the report,
vp installandprepend_js_runtime_to_path_envnow resolve the system package manager before the managed Node.js bin directory is prepended and dispatch it by absolute path; a managed npm genuinely placed first on PATH by a parent process is still reused, and the system npm's directory is never moved to the front. The potential fix is on the branch above and needs human review.Full Triage Report
Triage report — issue #3592
Issue summary
With Node.js in managed mode (
vp env on node) and npm in system-first mode (vp env off npm),vp installruns the npm bundled inside the managed Node.js installation instead of the system npm that is first on PATH.vp installruns the system npm that is first on PATH (the report's fake npm printsran: system npm).vp installruns the npm shipped with the managed Node.js (preinstallprocess.env.npm_execpathpoints at<VP_HOME>/js_runtime/node/<version>/lib/node_modules/npm/bin/npm-cli.js).Environment
triagebot/fix-3592, HEAD5cf0b72c8,vp v1.8.0built from this checkout.VP_HOME: fresh and isolated; the runner's default installation was never modified.vp env install node@26.13.1(download from the Node.js distribution registry — required to match the report's managed-runtime setup).Reproduction
Temp project with
.nvmrc26.13.1,package.jsonpinningnpm@10.9.0with apreinstallscript printingnpm_execpath, and a fake system npm (prints10.9.0for--version, otherwiseran: system npm) placed first on PATH.vp install→ preinstall printedran: <VP_HOME>/js_runtime/node/26.13.1/lib/node_modules/npm/bin/npm-cli.js— the bundled npm ran, not the system npm. Reproduced.vp install→ran: system npm(expected). Case 2 passes on this checkout because PR fix(cli): move the selected managed Node.js to the front of PATH #3591 is not merged here; the report itself predicts that case 2 also breaks once fix(cli): move the selected managed Node.js to the front of PATH #3591 (front-moving prepend) lands.vp env which npmunder the same configuration resolved to the fake system npm — the env surface sees the system npm correctly; only the install dispatch picks the bundled npm.Controls
npm installwith the fake npm first on PATH printedran: system npm— the fake npm works and is reachable.vp env on npm(managed mode), same PATH:vp installran the managednpm@10.9.0— expected for managed mode, so the mode setting drives the difference.vp env off npm: bundled npm again — the result is stable, not a one-off.Diagnosis
vp installreachesrun_package_manager_command(crates/vp_global_cli/src/cli.rs:675); a bareinstallis not a managed global command, so it falls through to the dispatch at ~line 730.prepend_js_runtime_to_path_env(crates/vp_global_cli/src/commands/mod.rs:117) resolves the project runtime and prepends the managed Node.js bin directory to the process PATH — that directory also containsnpmandnpx.find_system_tool("npm")(crates/vp_global_cli/src/shim/dispatch.rs:1474) classifies the first PATH match withvp_command::classify_first_path_match(crates/vp_command/src/external_tool.rs:102); the npm under<DATA>/js_runtimeis classifiedVpOwned, andfind_system_tool_inaccepts aVpOwnedfirst match: "A managed install first on PATH is a parent's selection, which system-first reuses."cli.rson the already-mutated PATH;vp_pm_cli::PackageManager::from_system_executablebuilds a manager whose bin prefix is the managed bin dir, anddispatch_with_resolved_package_managerspawns the bundled npm-cli.js.Root cause:
find_system_tool's "parent's selection" rule is correct for a parent process that deliberately placed a managed install first on PATH, butvp installsatisfies that rule with its own PATH mutation.vp env which npmperforms the lookup without the prepend, which is why the two surfaces disagree. Confidence: high — proven by code inspection and observed behavior, including thevp env which npmdisagreement and both controls.Verification
docs/guide/env.md(~line 134): system-first mode means "Vite+ prefers system tools and falls back to managed installations." The observed behavior contradicts the documented preference.0d8beb6cf— "fix(env): preserve system tool PATH precedence (fix(env): preserve system tool PATH precedence #3447)" — asserts in thesystem_first_pathsnapshot that Vite+'s PATH manipulation must not shadow the selected system tool; the same intent covers npm.VpOwnedacceptance infind_system_toolis intentional (introduced with feat(env): add VP_ENV_FORBIDDEN to restrict Vite+ to system Node.js and package managers #3233, codified bytest_find_system_tool_reuses_managed_installs_but_not_shims), but its rationale is a parent process's PATH selection. No test, comment, or doc endorses Vite+'s ownprepend_js_runtime_to_path_envfeeding that rule; the interaction is incidental.Fix
Changed files:
crates/vp_global_cli/src/commands/mod.rs—prepend_js_runtime_to_path_envresolves the system-first package manager before prepending the managed Node.js bin directory, then prepends the recorded system bin directory.crates/vp_global_cli/src/cli.rs—run_package_manager_commandresolves the system package manager (find_system_tool+from_system_executable) before callingprepend_js_runtime_to_path_env, then dispatches that manager by absolute bin prefix.system_first_pathfixture — newsystem-npmscenario inverify.mjs, new casesystem_npm_first_on_pathinsnapshots.toml, and its recorded snapshot.Rationale: exactly the approach proposed in the issue — look up the system npm before the managed Node.js bin directory is prepended and run it by absolute path. A managed npm genuinely placed first on PATH by a parent process is still reused; the system npm's directory is never moved to the front (which could hide the managed Node.js behind another
nodein a shared directory such as/usr/bin).find_system_tool,vp_command, andvp_pm_cliare unchanged. Alternatives rejected: moving the system npm directory to the front (contradicts #3447's "preserve original PATH precedence"), makingfind_system_toolskip managed installs (breaks intentional parent-selection semantics), and fixing onlycli.rs(prepend_js_runtime_to_path_env's internal branch, also used byvpx/vp execPATH preparation, had the same ordering hazard).Regression test
system_first_path::system_npm_first_on_path: withvp env on node+vp env off npm, a project pinningnpm@10.9.0and Node.js22.23.4(.nvmrc), and a fake system npm that reports10.9.0for--versionand printssystem-npm-ranotherwise,vp installmust run the fake system npm — both with the managed Node.js bin directory not on PATH (the reported bug) and with it on PATH behind the system npm (the case PR #3591 would otherwise break when it moves the managed runtime to the front). Verified failing without the fix (AssertionError: The input did not match /system-npm-ran/. Input: 'up to date, audited 1 package...'— the bundled npm ran); it passes with the fix.Validation
VP_HOME: Case 1 →ran: system npm(was bundled npm-cli.js); Case 2 →ran: system npm.npm@10.9.0; a managed bin dir genuinely first on PATH (parent selection) is still reused; no npm on PATH at all still falls back to a managed download.just snapshot-test-global system_first_path→ 4/4 ok (compare mode); related suites re-run green:command_env_off_on,command_env_package_manager_modes,command_env_clean,shim_system_first_fallback,shim_package_manager_setup.cargo test -p vp_global_cli→ 577 passed;-p vp_pm_cli -p vp_command -p vp_shared→ all passed.cargo fmt --all --check,cargo clippy --workspace --all-targets --all-features -- --deny warnings(justfile lint set), andcargo check --workspace --all-features --all-targets --locked→ all clean.just testand the full pre-mergepnpm bootstrap-cli && pnpm test(the change is confined to the Rust global CLI; affected crates, workspace clippy/check, and focused snapshot suites all pass).command_self_setup_handoffandcommand_self_setup_external_bundledsnapshot cases fail on this runner both with and without the change (verified by stashing the fix) — pre-existing environmental failures on this runner (no seededjs_runtime), unrelated to this fix.This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue.
I've opened a pull request with this fix: #3595