diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots.toml index 3f2ae082b8..78d16b838e 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots.toml +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots.toml @@ -5,6 +5,13 @@ skip-platforms = ["windows"] prewarm-package-managers = false steps = [["node", "verify.mjs", "system-pm"]] +[[case]] +name = "system_npm_first_on_path" +vp = "global" +skip-platforms = ["windows"] +prewarm-package-managers = false +steps = [["node", "verify.mjs", "system-npm"]] + [[case]] name = "managed_shim_preserves_system_node_path" vp = "global" diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots/system_npm_first_on_path.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots/system_npm_first_on_path.md new file mode 100644 index 0000000000..1ff4e29c92 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots/system_npm_first_on_path.md @@ -0,0 +1,7 @@ +# system_npm_first_on_path + +## `node verify.mjs system-npm` + +``` +system-npm: the system npm first on PATH ran for vp install +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/verify.mjs b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/verify.mjs index a1598951c1..106b5a6c92 100644 --- a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/verify.mjs +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/verify.mjs @@ -59,6 +59,32 @@ if (scenario === 'system-pm') { run(['env', 'exec', '--node', process.versions.node, 'pnpm', 'install']), /selected Node and original PATH precedence preserved/, ); +} else if (scenario === 'system-npm') { + // npm is bundled with the managed Node.js, so prepending its bin directory + // must not hide the system npm that is first on PATH. + writeFileSync( + 'package.json', + JSON.stringify({ name: 'system-first-path', private: true, packageManager: 'npm@10.9.0' }), + ); + writeFileSync('.nvmrc', '22.23.4\n'); + const npmBin = join(root, 'system-npm-bin'); + mkdirSync(npmBin, { recursive: true }); + executable( + join(npmBin, 'npm'), + '#!/bin/sh\nif [ "${1-}" = "--version" ]; then echo 10.9.0; else echo system-npm-ran; fi\n', + ); + const managedNodeBin = join(process.env.VP_HOME, 'js_runtime/node/22.23.4/bin'); + const installOnPath = (path) => { + const result = spawnSync('vp', ['install'], { env: { ...env, PATH: path }, encoding: 'utf8' }); + assert.equal(result.status, 0, `install\n${result.stdout}\n${result.stderr}`); + assert.match(result.stdout, /system-npm-ran/); + }; + run(['env', 'on', 'node']); + run(['env', 'off', 'npm']); + // The managed Node.js bin directory is not on PATH. + installOnPath([npmBin, process.env.PATH].join(delimiter)); + // The system npm stays ahead even when the managed Node.js bin directory is on PATH. + installOnPath([npmBin, managedNodeBin, process.env.PATH].join(delimiter)); } else { const managed = join(process.env.VP_HOME, 'package_manager/pnpm/10.18.0/pnpm/bin'); mkdirSync(managed, { recursive: true }); @@ -80,4 +106,8 @@ if (scenario === 'system-pm') { ); } } -console.log(`${scenario}: selected Node and original PATH precedence preserved`); +console.log( + scenario === 'system-npm' + ? 'system-npm: the system npm first on PATH ran for vp install' + : `${scenario}: selected Node and original PATH precedence preserved`, +); diff --git a/crates/vp_global_cli/src/cli.rs b/crates/vp_global_cli/src/cli.rs index 8a3c671e0f..bcef1dd393 100644 --- a/crates/vp_global_cli/src/cli.rs +++ b/crates/vp_global_cli/src/cli.rs @@ -727,9 +727,13 @@ async fn run_package_manager_command( None => {} } - commands::prepend_js_runtime_to_path_env(&cwd).await?; - let selected = commands::env::package_manager::resolve_current_spec(&cwd).await?; - let result = if let Some(selected) = selected.as_ref() + // Resolve a system-first package manager before + // `prepend_js_runtime_to_path_env` prepends the managed Node.js bin + // directory: that directory also contains npm, so a lookup on the + // modified PATH would dispatch Vite+'s bundled npm where system-first + // mode expects the system package manager that is first on PATH. + let system_manager = if let Some(selected) = + commands::env::package_manager::resolve_current_spec(&cwd).await? && commands::env::config::load_config() .await? .package_manager_shim_mode_for(selected.package_manager_type) @@ -743,6 +747,12 @@ async fn run_package_manager_command( ) .await { + Some(manager) + } else { + None + }; + commands::prepend_js_runtime_to_path_env(&cwd).await?; + let result = if let Some(manager) = system_manager { vp_pm_cli::dispatch_with_resolved_package_manager(&cwd, command, manager).await? } else { let selected = commands::env::package_manager::resolve_current(&cwd).await?; diff --git a/crates/vp_global_cli/src/commands/mod.rs b/crates/vp_global_cli/src/commands/mod.rs index c135f19bb4..d6e10e5885 100644 --- a/crates/vp_global_cli/src/commands/mod.rs +++ b/crates/vp_global_cli/src/commands/mod.rs @@ -118,6 +118,24 @@ pub async fn prepend_js_runtime_to_path_env(project_path: &AbsolutePath) -> Resu let config = env::config::load_config().await?; let mut executor = JsExecutor::new(None); + // Resolve a system-first package manager before prepending the managed + // Node.js bin directory: that directory also contains npm, so a lookup on + // the modified PATH would reuse Vite+'s own selection instead of the + // system package manager that is first on PATH. + let system_package_manager = if let Some(package_manager) = + env::package_manager::resolve_current_spec(project_path).await? + && config.package_manager_shim_mode_for(package_manager.package_manager_type) + == env::config::ShimMode::SystemFirst + && let Some(system_path) = crate::shim::dispatch::find_system_tool( + &package_manager.package_manager_type.to_string(), + ) + && let Some(bin_dir) = system_path.parent() + { + Some((package_manager.package_manager_type.to_string(), bin_dir.to_absolute_path_buf())) + } else { + None + }; + let node_bin_prefix = if config.node_shim_mode == env::config::ShimMode::SystemFirst && let Some(system_node) = crate::shim::dispatch::find_system_tool("node") && let Some(bin_dir) = system_node.parent() @@ -139,18 +157,9 @@ pub async fn prepend_js_runtime_to_path_env(project_path: &AbsolutePath) -> Resu &["node"], PrependOptions { dedupe_anywhere: true }, )?; - if let Some(package_manager) = env::package_manager::resolve_current_spec(project_path).await? { - if config.package_manager_shim_mode_for(package_manager.package_manager_type) - == env::config::ShimMode::SystemFirst - && let Some(system_path) = crate::shim::dispatch::find_system_tool( - &package_manager.package_manager_type.to_string(), - ) - && let Some(bin_dir) = system_path.parent() - { - let tool = package_manager.package_manager_type.to_string(); - prepend_tools_to_path_env(bin_dir, &[&tool], PrependOptions { dedupe_anywhere: true })?; - return Ok(()); - } + if let Some((tool, bin_dir)) = system_package_manager { + prepend_tools_to_path_env(&bin_dir, &[&tool], PrependOptions { dedupe_anywhere: true })?; + return Ok(()); } if let Some(package_manager) = env::package_manager::resolve_current(project_path).await? { let (install_dir, _, _) = vp_pm_cli::download_package_manager_for_cwd(