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..471912632e 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 = "managed_node_precedes_later_node_path" +vp = "global" +skip-platforms = ["windows"] +prewarm-package-managers = false +steps = [["node", "verify.mjs", "managed-node-later"]] + [[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/managed_node_precedes_later_node_path.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots/managed_node_precedes_later_node_path.md new file mode 100644 index 0000000000..5af0173619 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/system_first_path/snapshots/managed_node_precedes_later_node_path.md @@ -0,0 +1,7 @@ +# managed_node_precedes_later_node_path + +## `node verify.mjs managed-node-later` + +``` +managed-node-later: selected Node and original PATH precedence preserved +``` 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..8b1bf973ca 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 @@ -1,7 +1,7 @@ import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { chmodSync, mkdirSync, writeFileSync, symlinkSync } from 'node:fs'; -import { delimiter, join } from 'node:path'; +import { delimiter, dirname, join } from 'node:path'; const scenario = process.argv[2]; const root = process.cwd(); @@ -51,7 +51,25 @@ function run(args) { } run(['env', 'off', 'node']); run(['env', 'off', 'pm']); -if (scenario === 'system-pm') { +if (scenario === 'managed-node-later') { + // The selected managed Node.js is already on PATH, but after another Node.js. + // Pin the seeded runtime that runs this script, and let Vite+ resolve its location. + writeFileSync('.nvmrc', `${process.versions.node}\n`); + run(['env', 'on', 'node']); + const { node } = JSON.parse(run(['env', 'current', '--json'])); + assert.equal(node.mode, 'managed'); + assert.ok(node.installed, `missing seeded runtime ${node.bin_path}`); + const managedNode = dirname(node.bin_path); + const otherNode = join(root, 'other-node-bin'); + mkdirSync(otherNode); + executable(join(otherNode, 'node'), '#!/bin/sh\necho shadowed-by-other-node\nexit 1\n'); + const result = spawnSync('vp', ['install'], { + env: { ...env, PATH: [otherNode, managedNode, env.PATH].join(delimiter) }, + encoding: 'utf8', + }); + assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); + assert.match(result.stdout, /selected Node and original PATH precedence preserved/); +} else if (scenario === 'system-pm') { assert.match(run(['install']), /selected Node and original PATH precedence preserved/); run(['env', 'on', 'node']); assert.match(run(['install']), /selected Node and original PATH precedence preserved/); diff --git a/crates/vp_global_cli/src/commands/mod.rs b/crates/vp_global_cli/src/commands/mod.rs index c135f19bb4..224a174ce0 100644 --- a/crates/vp_global_cli/src/commands/mod.rs +++ b/crates/vp_global_cli/src/commands/mod.rs @@ -118,12 +118,14 @@ 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); - let node_bin_prefix = if config.node_shim_mode == env::config::ShimMode::SystemFirst + let (node_bin_prefix, system_runtime) = 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() { - bin_dir.to_absolute_path_buf() + (bin_dir.to_absolute_path_buf(), true) } else { + // The else branch selects a managed runtime: a system-first system Node is handled above. // Use project runtime if package.json exists, otherwise use CLI runtime let package_json_path = project_path.join("package.json"); let runtime = if package_json_path.as_path().exists() { @@ -131,13 +133,16 @@ pub async fn prepend_js_runtime_to_path_env(project_path: &AbsolutePath) -> Resu } else { executor.ensure_cli_runtime().await? }; - runtime.get_bin_prefix() + (runtime.get_bin_prefix(), false) }; + // A later PATH entry for the selected runtime does not make it win the + // `node` lookup, so move it to the front. A system directory keeps its + // position because it can also contain unrelated tools. prepend_tools_to_path_env( &node_bin_prefix, &["node"], - PrependOptions { dedupe_anywhere: true }, + PrependOptions { dedupe_anywhere: system_runtime }, )?; 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)