Skip to content

vp install runs the npm bundled with managed Node.js when npm is system-first #3592

In review

Description

@naokihaba

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

  1. Run vp env on node and vp env off npm.
  2. Save the script below as repro.sh and run bash repro.sh. It creates a temporary project that pins Node.js 26.13.1 and npm@10.9.0. It also creates a fake system npm that prints ran: system npm.
  3. 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

Activity

  1. self-assigned this
    on Nov 28, 2026
  2. added theissue type on Nov 28, 2026
  3. voidzero-bot commented on Nov 28, 2026

    @voidzero-bot
    Contributor
    • Reproduced: Yes
    • Exploration: Yes — View branch
    • Unit Test: Yes — crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path (new snapshot case system_npm_first_on_path, verified failing without the fix)
    • Priority P2: Has Workaround. This only affects the mixed configuration (Node.js managed + npm system-first) and does not break installs outright; the system npm still works when invoked directly or when npm is switched to managed mode. It does contradict documented system-first behavior, so it is more than a cosmetic edge case.

    vp install runs the npm bundled with the managed Node.js because prepend_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 in run_package_manager_command (crates/vp_global_cli/src/cli.rs). Since that directory also contains npm, 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 contradicts docs/guide/env.md ("Vite+ prefers system tools" in system-first mode), the merged intent of #3447, and the CLI's own vp 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 install and prepend_js_runtime_to_path_env now 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 install runs the npm bundled inside the managed Node.js installation instead of the system npm that is first on PATH.

    • Expected: in system-first mode, vp install runs the system npm that is first on PATH (the report's fake npm prints ran: system npm).
    • Actual: vp install runs the npm shipped with the managed Node.js (preinstall process.env.npm_execpath points at <VP_HOME>/js_runtime/node/<version>/lib/node_modules/npm/bin/npm-cli.js).

    Environment

    • OS: Ubuntu x86_64 (GitHub runner) — the report was macOS arm64, but the reproduction is a portable bash script, so it is not host-specific.
    • Checkout: branch triagebot/fix-3592, HEAD 5cf0b72c8, vp v1.8.0 built from this checkout.
    • Test VP_HOME: fresh and isolated; the runner's default installation was never modified.
    • Modes: Node.js managed, npm system-first; managed Node.js 26.13.1 installed via 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 .nvmrc 26.13.1, package.json pinning npm@10.9.0 with a preinstall script printing npm_execpath, and a fake system npm (prints 10.9.0 for --version, otherwise ran: system npm) placed first on PATH.

    1. Case 1 (system npm first on PATH, managed Node.js bin dir not on PATH): vp install → preinstall printed ran: <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.
    2. Case 2 (system npm before the managed Node.js bin dir on PATH): 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.
    3. vp env which npm under 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

    • Plain npm install with the fake npm first on PATH printed ran: system npm — the fake npm works and is reachable.
    • vp env on npm (managed mode), same PATH: vp install ran the managed npm@10.9.0 — expected for managed mode, so the mode setting drives the difference.
    • Re-ran case 1 after restoring vp env off npm: bundled npm again — the result is stable, not a one-off.

    Diagnosis

    1. vp install reaches run_package_manager_command (crates/vp_global_cli/src/cli.rs:675); a bare install is not a managed global command, so it falls through to the dispatch at ~line 730.
    2. 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 contains npm and npx.
    3. find_system_tool("npm") (crates/vp_global_cli/src/shim/dispatch.rs:1474) classifies the first PATH match with vp_command::classify_first_path_match (crates/vp_command/src/external_tool.rs:102); the npm under <DATA>/js_runtime is classified VpOwned, and find_system_tool_in accepts a VpOwned first match: "A managed install first on PATH is a parent's selection, which system-first reuses."
    4. The same lookup runs again in cli.rs on the already-mutated PATH; vp_pm_cli::PackageManager::from_system_executable builds a manager whose bin prefix is the managed bin dir, and dispatch_with_resolved_package_manager spawns 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, but vp install satisfies that rule with its own PATH mutation. vp env which npm performs the lookup without the prepend, which is why the two surfaces disagree. Confidence: high — proven by code inspection and observed behavior, including the vp env which npm disagreement 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.
    • Merged commit 0d8beb6cf — "fix(env): preserve system tool PATH precedence (fix(env): preserve system tool PATH precedence #3447)" — asserts in the system_first_path snapshot that Vite+'s PATH manipulation must not shadow the selected system tool; the same intent covers npm.
    • The VpOwned acceptance in find_system_tool is intentional (introduced with feat(env): add VP_ENV_FORBIDDEN to restrict Vite+ to system Node.js and package managers #3233, codified by test_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 own prepend_js_runtime_to_path_env feeding that rule; the interaction is incidental.
    • Open PR fix(cli): move the selected managed Node.js to the front of PATH #3591 explicitly says it left system-first behavior untouched and defers the discussion to this issue. No maintainer statement endorses running the bundled npm in system-first mode.
    • Verdict: bug, confidence high.

    Fix

    Changed files:

    • crates/vp_global_cli/src/commands/mod.rs — prepend_js_runtime_to_path_env resolves 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_command resolves the system package manager (find_system_tool + from_system_executable) before calling prepend_js_runtime_to_path_env, then dispatches that manager by absolute bin prefix.
    • system_first_path fixture — new system-npm scenario in verify.mjs, new case system_npm_first_on_path in snapshots.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 node in a shared directory such as /usr/bin). find_system_tool, vp_command, and vp_pm_cli are unchanged. Alternatives rejected: moving the system npm directory to the front (contradicts #3447's "preserve original PATH precedence"), making find_system_tool skip managed installs (breaks intentional parent-selection semantics), and fixing only cli.rs (prepend_js_runtime_to_path_env's internal branch, also used by vpx/vp exec PATH preparation, had the same ordering hazard).

    Regression test

    system_first_path::system_npm_first_on_path: with vp env on node + vp env off npm, a project pinning npm@10.9.0 and Node.js 22.23.4 (.nvmrc), and a fake system npm that reports 10.9.0 for --version and prints system-npm-ran otherwise, vp install must 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

    • Original reproduction with a rebuilt release binary installed into a fresh isolated VP_HOME: Case 1 → ran: system npm (was bundled npm-cli.js); Case 2 → ran: system npm.
    • Controls preserved: managed npm mode runs the managed 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), and cargo check --workspace --all-features --all-targets --locked → all clean.
    • Not run in this job: full just test and the full pre-merge pnpm 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_handoff and command_self_setup_external_bundled snapshot cases fail on this runner both with and without the change (verified by stashing the fix) — pre-existing environmental failures on this runner (no seeded js_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

  4. added
    - P2: has workaround
    on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

- P2: has workaroundtriage: fix verified

Type

Fields

Priority

None yet

Effort

None yet

Target date

None yet

Start date

None yet

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions