diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/.node-version b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/.node-version new file mode 100644 index 0000000000..ce62013c39 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/.node-version @@ -0,0 +1 @@ +24.22.1 diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/dry-run.npmrc b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/dry-run.npmrc new file mode 100644 index 0000000000..13e3e4991e --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/dry-run.npmrc @@ -0,0 +1 @@ +dry-run=true diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/pkg/cli.js b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/pkg/cli.js new file mode 100644 index 0000000000..f7c4cc908e --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/pkg/cli.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +console.log('relink-cli works'); diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/pkg/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/pkg/package.json new file mode 100644 index 0000000000..f5dc747e8d --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/pkg/package.json @@ -0,0 +1,7 @@ +{ + "name": "npm-global-relink-pkg", + "version": "1.0.0", + "bin": { + "npm-global-relink-cli": "./cli.js" + } +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/project/.npmrc b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/project/.npmrc new file mode 100644 index 0000000000..13e3e4991e --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/project/.npmrc @@ -0,0 +1 @@ +dry-run=true diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/project/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/project/package.json new file mode 100644 index 0000000000..9759d6f3bc --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "npm-global-relink-project", + "private": true +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/snapshots.toml b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/snapshots.toml new file mode 100644 index 0000000000..78a91828f6 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/snapshots.toml @@ -0,0 +1,31 @@ +# Each Vite+-managed Node.js version has its own npm global prefix. The runner +# seeds the runtime directory from a shared cache, so each step sets a +# case-local prefix per Node.js version instead. +[[case]] +name = "npm_global_install_node_version_relink" +vp = "global" +skip-platforms = ["windows"] +steps = [ + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], comment = "Install with Node.js 24 (creates the link)", continue-on-failure = true }, + { argv = ["vpt", "grep-file", "$VP_HOME/bins/npm-global-relink-cli.json", "\"nodeVersion\": \"24.22.1\""], comment = "BinConfig records Node.js 24", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg"], envs = [["VP_NODE_VERSION", "26.12.1"], ["NPM_CONFIG_PREFIX", "${workspace}/node-26"]], comment = "Install again with Node.js 26 (moves the link to the new prefix)", continue-on-failure = true }, + { argv = ["vpt", "grep-file", "$VP_HOME/bins/npm-global-relink-cli.json", "\"nodeVersion\": \"26.12.1\""], comment = "BinConfig records Node.js 26", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "--dry-run", "./pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], comment = "Dry run with Node.js 24, whose prefix still has the package (the link stays on Node.js 26)", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"], ["npm_config_dry_run", "true"]], comment = "Dry run from the npm environment config (the link stays on Node.js 26)", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg", "--no-dry-run", "false"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], comment = "npm reads `--no-dry-run false` as a dry run (the link stays on Node.js 26)", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "--userconfig", "dry-run.npmrc", "./pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], comment = "Dry run from an explicit --userconfig file (the link stays on Node.js 26)", continue-on-failure = true }, + { tty = false, cwd = "ws/packages/child", argv = ["npm", "install", "-g", "--ignore-scripts", "../../../pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/../../../node-24"], ["npm_config_dry_run", "true"]], comment = "Dry run from an npm workspace child (the link stays on Node.js 26)", continue-on-failure = true }, + { argv = ["vpt", "grep-file", "$VP_HOME/bins/npm-global-relink-cli.json", "\"nodeVersion\": \"26.12.1\""], comment = "BinConfig still records Node.js 26", continue-on-failure = true }, + { tty = false, cwd = "project", argv = ["npm", "install", "-g", "--ignore-scripts", "../pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/../node-24"]], comment = "Real install with Node.js 24: `install -g` ignores the project .npmrc that sets dry-run (moves the link)", continue-on-failure = true }, + { argv = ["vpt", "grep-file", "$VP_HOME/bins/npm-global-relink-cli.json", "\"nodeVersion\": \"24.22.1\""], comment = "BinConfig records Node.js 24", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg"], envs = [["VP_NODE_VERSION", "26.12.1"], ["NPM_CONFIG_PREFIX", "${workspace}/node-26"], ["npm_config_dry_run", "undefined"]], comment = "npm reads `npm_config_dry_run=undefined` as a real install with Node.js 26 (moves the link)", continue-on-failure = true }, + { argv = ["vpt", "grep-file", "$VP_HOME/bins/npm-global-relink-cli.json", "\"nodeVersion\": \"26.12.1\""], comment = "BinConfig records Node.js 26", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg", "--dry-run", "false"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], comment = "npm reads `--dry-run false` as a real install with Node.js 24 (moves the link)", continue-on-failure = true }, + { argv = ["vpt", "grep-file", "$VP_HOME/bins/npm-global-relink-cli.json", "\"nodeVersion\": \"24.22.1\""], comment = "BinConfig records Node.js 24", continue-on-failure = true }, + { argv = ["vpt", "rm", "-rf", "node-26"], comment = "Remove the Node.js 26 copy: the command must not depend on it", continue-on-failure = true }, + { argv = ["npm-global-relink-cli"], comment = "Should be callable via the moved link", continue-on-failure = true }, + { tty = false, argv = ["npm", "install", "-g", "--ignore-scripts", "./pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], comment = "Install again with Node.js 24 (the link is current, so no message)" }, +] +after = [ + { tty = false, argv = ["npm", "uninstall", "-g", "--ignore-scripts", "npm-global-relink-pkg"], envs = [["NPM_CONFIG_PREFIX", "${workspace}/node-24"]], continue-on-failure = true }, +] diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/snapshots/npm_global_install_node_version_relink.md b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/snapshots/npm_global_install_node_version_relink.md new file mode 100644 index 0000000000..0a80831145 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/snapshots/npm_global_install_node_version_relink.md @@ -0,0 +1,172 @@ +# npm_global_install_node_version_relink + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm install -g --ignore-scripts ./pkg` + +Install with Node.js 24 (creates the link) + +``` + +added 1 package in +Linked 'npm-global-relink-cli' to /.vite-plus/bin/npm-global-relink-cli +``` + +## `vpt grep-file $VP_HOME/bins/npm-global-relink-cli.json '"nodeVersion": "24.22.1"'` + +BinConfig records Node.js 24 + +``` +/.vite-plus/bins/npm-global-relink-cli.json: found "\"nodeVersion\": \"24.22.1\"" +``` + +## `VP_NODE_VERSION=26.12.1 NPM_CONFIG_PREFIX=${workspace}/node-26 npm install -g --ignore-scripts ./pkg` + +Install again with Node.js 26 (moves the link to the new prefix) + +``` + +added 1 package in +note: Moving 'npm-global-relink-cli' from Node.js to 26.12.1 +Linked 'npm-global-relink-cli' to /.vite-plus/bin/npm-global-relink-cli +``` + +## `vpt grep-file $VP_HOME/bins/npm-global-relink-cli.json '"nodeVersion": "26.12.1"'` + +BinConfig records Node.js 26 + +``` +/.vite-plus/bins/npm-global-relink-cli.json: found "\"nodeVersion\": \"26.12.1\"" +``` + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm install -g --ignore-scripts --dry-run ./pkg` + +Dry run with Node.js 24, whose prefix still has the package (the link stays on Node.js 26) + +``` + +up to date in +``` + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm_config_dry_run=true npm install -g --ignore-scripts ./pkg` + +Dry run from the npm environment config (the link stays on Node.js 26) + +``` + +up to date in +``` + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm install -g --ignore-scripts ./pkg --no-dry-run false` + +npm reads `--no-dry-run false` as a dry run (the link stays on Node.js 26) + +``` + +up to date in +``` + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm install -g --ignore-scripts --userconfig dry-run.npmrc ./pkg` + +Dry run from an explicit --userconfig file (the link stays on Node.js 26) + +``` + +up to date in +``` + +## `cd ws/packages/child && NPM_CONFIG_PREFIX=${workspace}/../../../node-24 npm_config_dry_run=true npm install -g --ignore-scripts ../../../pkg` + +Dry run from an npm workspace child (the link stays on Node.js 26) + +``` + +up to date in +``` + +## `vpt grep-file $VP_HOME/bins/npm-global-relink-cli.json '"nodeVersion": "26.12.1"'` + +BinConfig still records Node.js 26 + +``` +/.vite-plus/bins/npm-global-relink-cli.json: found "\"nodeVersion\": \"26.12.1\"" +``` + +## `cd project && NPM_CONFIG_PREFIX=${workspace}/../node-24 npm install -g --ignore-scripts ../pkg` + +Real install with Node.js 24: `install -g` ignores the project .npmrc that sets dry-run (moves the link) + +``` + +up to date in +note: Moving 'npm-global-relink-cli' from Node.js to 24.22.1 +Linked 'npm-global-relink-cli' to /.vite-plus/bin/npm-global-relink-cli +``` + +## `vpt grep-file $VP_HOME/bins/npm-global-relink-cli.json '"nodeVersion": "24.22.1"'` + +BinConfig records Node.js 24 + +``` +/.vite-plus/bins/npm-global-relink-cli.json: found "\"nodeVersion\": \"24.22.1\"" +``` + +## `VP_NODE_VERSION=26.12.1 NPM_CONFIG_PREFIX=${workspace}/node-26 npm_config_dry_run=undefined npm install -g --ignore-scripts ./pkg` + +npm reads `npm_config_dry_run=undefined` as a real install with Node.js 26 (moves the link) + +``` + +up to date in +note: Moving 'npm-global-relink-cli' from Node.js to 26.12.1 +Linked 'npm-global-relink-cli' to /.vite-plus/bin/npm-global-relink-cli +``` + +## `vpt grep-file $VP_HOME/bins/npm-global-relink-cli.json '"nodeVersion": "26.12.1"'` + +BinConfig records Node.js 26 + +``` +/.vite-plus/bins/npm-global-relink-cli.json: found "\"nodeVersion\": \"26.12.1\"" +``` + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm install -g --ignore-scripts ./pkg --dry-run false` + +npm reads `--dry-run false` as a real install with Node.js 24 (moves the link) + +``` + +up to date in +note: Moving 'npm-global-relink-cli' from Node.js to 24.22.1 +Linked 'npm-global-relink-cli' to /.vite-plus/bin/npm-global-relink-cli +``` + +## `vpt grep-file $VP_HOME/bins/npm-global-relink-cli.json '"nodeVersion": "24.22.1"'` + +BinConfig records Node.js 24 + +``` +/.vite-plus/bins/npm-global-relink-cli.json: found "\"nodeVersion\": \"24.22.1\"" +``` + +## `vpt rm -rf node-26` + +Remove the Node.js 26 copy: the command must not depend on it + +``` +``` + +## `npm-global-relink-cli` + +Should be callable via the moved link + +``` +relink-cli works +``` + +## `NPM_CONFIG_PREFIX=${workspace}/node-24 npm install -g --ignore-scripts ./pkg` + +Install again with Node.js 24 (the link is current, so no message) + +``` + +up to date in +``` diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/ws/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/ws/package.json new file mode 100644 index 0000000000..d6755c91d9 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/ws/package.json @@ -0,0 +1,5 @@ +{ + "name": "npm-global-relink-workspace", + "private": true, + "workspaces": ["packages/*"] +} diff --git a/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/ws/packages/child/package.json b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/ws/packages/child/package.json new file mode 100644 index 0000000000..4362bf04c6 --- /dev/null +++ b/crates/vp_cli_snapshots/tests/cli_snapshots/fixtures/npm_global_install_node_version_relink/ws/packages/child/package.json @@ -0,0 +1,4 @@ +{ + "name": "npm-global-relink-child", + "version": "1.0.0" +} diff --git a/crates/vp_global_cli/src/shim/dispatch.rs b/crates/vp_global_cli/src/shim/dispatch.rs index 2741a544f0..9deac37c9a 100644 --- a/crates/vp_global_cli/src/shim/dispatch.rs +++ b/crates/vp_global_cli/src/shim/dispatch.rs @@ -206,6 +206,49 @@ fn get_npm_global_prefix( node_dir.clone() } +/// Whether npm's effective `dry-run` setting was enabled for an install, or +/// `None` when npm cannot report it. +/// +/// The probe receives the install's own arguments, so npm applies the same +/// global mode, config files (`--userconfig`, global-only npmrc lookup), and +/// flag parsing (`--dry-run false`, `--no-dry-run`, abbreviations) as it did +/// for the install. The subcommand and package specs become extra keys, so +/// npm prints one `key=value` line per key, starting with `dry-run`. +fn npm_dry_run_enabled( + args: &[String], + npm_path: &AbsolutePath, + env: &ToolPathEnv, +) -> Option { + let output = std::process::Command::new(npm_path.as_path()) + .args(["config", "get", "dry-run"]) + .args(args) + .envs(env.clone().into_envs()) + .output() + .ok()?; + if !output.status.success() { + return None; + } + npm_dry_run_from_config_output(std::str::from_utf8(&output.stdout).ok()?) +} + +/// Read the `dry-run=` line that `npm config get dry-run ` prints. +fn npm_dry_run_from_config_output(stdout: &str) -> Option { + stdout.lines().find_map(|line| line.strip_prefix("dry-run=")).map(npm_config_bool) +} + +/// Parse a boolean npm config value the way npm does. `npm config get` prints +/// environment and npmrc values before npm converts them to booleans, such as +/// `1` for `NPM_CONFIG_DRY_RUN=1`. npm first reads `undefined` (case-sensitive) +/// as no value. Its option parser (nopt) then makes numbers true when nonzero, +/// `false` and `null` false, and any other string true. +fn npm_config_bool(value: &str) -> bool { + let value = value.trim(); + if let Ok(number) = value.parse::() { + return number != 0.0; + } + !matches!(value, "" | "false" | "null" | "undefined") +} + /// After npm install -g completes, check if installed binaries are on PATH. /// /// First determines the actual npm global bin directory (which may differ from the @@ -292,16 +335,27 @@ fn check_npm_global_install_result( if config.source == BinSource::Vp { // Managed by vp install -g — warn about the conflict managed_conflicts.push((bin_name, config.package.clone())); - } else if config.source == BinSource::Npm && config.package != package_name { - // Link exists from a different npm package — recreate link for new owner. - // The old symlink points at the previous package's binary; we must - // replace it so it resolves to the new package's binary in npm's bin dir. + } else if config.source == BinSource::Npm { + // Recreate the link when a different npm package now owns the binary, + // or when a reinstall under another Node.js version put the binary + // in a different npm bin dir. Otherwise the link keeps launching the + // previous binary. #[cfg(unix)] let source_path = npm_bin_dir.join(&bin_name); #[cfg(windows)] let source_path = npm_bin_dir.join(vt_str::format!("{bin_name}.cmd")); - if source_path.as_path().exists() { + let same_owner = config.package == package_name; + if source_path.as_path().exists() + && !(same_owner + && npm_bin_link_targets(&bin_dir, &bin_name, &source_path)) + { + if same_owner && config.node_version != node_version { + output::note(&vt_str::format!( + "Moving '{bin_name}' from Node.js {} to {node_version}", + config.node_version + )); + } let _ = std::fs::remove_file(shim_path.as_path()); create_bin_link( &bin_dir, @@ -425,6 +479,27 @@ fn link_missing_npm_bins( } } +/// Whether the existing npm bin link in `bin_dir` launches `source_path`. +fn npm_bin_link_targets( + bin_dir: &AbsolutePath, + bin_name: &str, + source_path: &AbsolutePath, +) -> bool { + #[cfg(unix)] + { + std::fs::read_link(bin_dir.join(bin_name).as_path()) + .is_ok_and(|target| target == source_path.as_path()) + } + + #[cfg(windows)] + { + // `create_bin_link` writes the quoted source path into the `.cmd` wrapper. + let quoted_source = vt_str::format!("\"{}\"", source_path.as_path().display()); + std::fs::read_to_string(bin_dir.join(vt_str::format!("{bin_name}.cmd")).as_path()) + .is_ok_and(|content| content.contains(quoted_source.as_str())) + } +} + /// Create a bin link for a binary and record it via BinConfig. pub(crate) fn create_bin_link( bin_dir: &AbsolutePath, @@ -962,7 +1037,18 @@ pub async fn dispatch(tool: &str, args: &[String], env: ToolPathEnv) -> i32 { if tool == "npm" { if let Some(parsed) = parse_npm_global_install(args) { let exit_code = exec::spawn_tool(&tool_path, args, child_env.clone()); - if exit_code == 0 { + if exit_code != 0 { + return exit_code; + } + // A dry run changes no files in the npm prefix, so the links stay as they are. + // When npm cannot report the setting, do not guess that the install was real. + let Some(dry_run) = npm_dry_run_enabled(args, &tool_path, &child_env) else { + output::note( + "Could not read npm's dry-run setting, so links in ~/.vite-plus/bin/ were not changed.", + ); + return exit_code; + }; + if !dry_run { let node_dir = node_prefix_from_binary(&node_path); let node_version = resolution.as_ref().map_or_else( || read_node_version(&node_path).unwrap_or_else(|| "unknown".into()), @@ -2137,6 +2223,76 @@ mod tests { }); } + #[test] + fn test_npm_dry_run_from_config_output() { + let output = "dry-run=true\ninstall=undefined\npkg=undefined\n"; + assert_eq!(npm_dry_run_from_config_output(output), Some(true)); + // `npm config get` prints environment values unparsed. + assert_eq!(npm_dry_run_from_config_output("dry-run=1\ninstall=undefined\n"), Some(true)); + // A package spec that looks like the key does not hide the real value. + let output = "dry-run=false\ninstall=undefined\ndry-run=true=undefined\n"; + assert_eq!(npm_dry_run_from_config_output(output), Some(false)); + // `--help` and `--version` replace the key lines with other output. + assert_eq!(npm_dry_run_from_config_output("10.9.9\n"), None); + assert_eq!(npm_dry_run_from_config_output(""), None); + } + + #[test] + fn test_npm_config_bool() { + for value in ["true", "1", "yes", "true\n", "UNDEFINED"] { + assert!(npm_config_bool(value), "{value:?} should be true"); + } + for value in ["false", "0", "null", "undefined", "", "false\n"] { + assert!(!npm_config_bool(value), "{value:?} should be false"); + } + } + + #[test] + fn test_npm_reinstall_under_new_node_version_moves_link() { + let temp = TempDir::new().unwrap(); + // One npm prefix per Node.js version, as with Vite+-managed runtimes. + let install = |version: &str| { + let prefix = AbsolutePathBuf::new(temp.path().join("node").join(version)).unwrap(); + let package_dir = prefix.join("lib").join("node_modules").join("test-package"); + std::fs::create_dir_all(&package_dir).unwrap(); + std::fs::write( + package_dir.join("package.json"), + r#"{"name":"test-package","bin":{"test-cli":"cli.js"}}"#, + ) + .unwrap(); + #[cfg(unix)] + let source = prefix.join("bin").join("test-cli"); + #[cfg(windows)] + let source = prefix.join("test-cli.cmd"); + std::fs::create_dir_all(source.parent().unwrap()).unwrap(); + std::fs::write(&source, "").unwrap(); + (prefix, source) + }; + let (_, old_source) = install("22.18.0"); + let (new_prefix, new_source) = install("24.11.0"); + + vp_shared::EnvConfig::with_vars([(env_vars::VP_HOME, temp.path())], |_| { + let bin_dir = config::get_bin_dir().unwrap(); + std::fs::create_dir_all(&bin_dir).unwrap(); + create_bin_link(&bin_dir, "test-cli", &old_source, "test-package", "22.18.0"); + assert!(npm_bin_link_targets(&bin_dir, "test-cli", &old_source)); + + let packages = s(&["test-package"]); + check_npm_global_install_result(&packages, None, &new_prefix, &new_prefix, "24.11.0"); + assert!(npm_bin_link_targets(&bin_dir, "test-cli", &new_source)); + assert!(!npm_bin_link_targets(&bin_dir, "test-cli", &old_source)); + let config = BinConfig::load_sync("test-cli").unwrap().unwrap(); + assert_eq!(config.package, "test-package"); + assert_eq!(config.node_version, "24.11.0"); + + // A repeat install under the same Node.js version keeps the link as is. + let metadata = std::fs::read(BinConfig::path("test-cli").unwrap()).unwrap(); + check_npm_global_install_result(&packages, None, &new_prefix, &new_prefix, "24.11.0"); + assert!(npm_bin_link_targets(&bin_dir, "test-cli", &new_source)); + assert_eq!(std::fs::read(BinConfig::path("test-cli").unwrap()).unwrap(), metadata); + }); + } + #[test] fn test_npm_uninstall_preserves_links_until_package_is_removed() { let temp = TempDir::new().unwrap();