From 8d52fcfdcc3a674e641bcc01d666c193e32bc4db Mon Sep 17 00:00:00 2001 From: Liang Date: Sun, 15 Nov 2026 20:22:16 +0800 Subject: [PATCH] fix(env): cache package-manager dist-tags for authenticated default registry --- crates/vp_pm_cli/src/config.rs | 7 ++++++ crates/vp_pm_cli/src/package_manager.rs | 30 +++++++++++++++++++++++-- docs/guide/env.md | 2 +- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/vp_pm_cli/src/config.rs b/crates/vp_pm_cli/src/config.rs index 5a22578ffc..2038258f23 100644 --- a/crates/vp_pm_cli/src/config.rs +++ b/crates/vp_pm_cli/src/config.rs @@ -219,6 +219,13 @@ impl NpmConfig { } } +pub(crate) fn is_default_registry(url: &str) -> bool { + let (Ok(url), Ok(default)) = (Url::parse(url), Url::parse(DEFAULT_NPM_REGISTRY)) else { + return false; + }; + url.origin() == default.origin() +} + fn env_value(name: &str) -> Option { let lower = vt_str::format!("npm_config_{name}"); let upper = vt_str::format!("NPM_CONFIG_{}", name.cow_to_ascii_uppercase()); diff --git a/crates/vp_pm_cli/src/package_manager.rs b/crates/vp_pm_cli/src/package_manager.rs index 92e11cd52d..34fb5c11aa 100644 --- a/crates/vp_pm_cli/src/package_manager.rs +++ b/crates/vp_pm_cli/src/package_manager.rs @@ -21,7 +21,7 @@ use vt_str::Str; use vt_workspace::{WorkspaceFile, WorkspaceRoot, find_workspace_root}; use crate::{ - config::NpmConfig, + config::{NpmConfig, is_default_registry}, env_forbidden::ensure_managed_package_manager_allowed, request::{HttpClient, download_and_extract_tgz_with_hash_and_config, is_not_found}, shim, @@ -1308,7 +1308,9 @@ async fn resolve_dist_tag( // A persisted response authenticated as one user must not be reused by // another user of the same registry URL. Avoid storing credential-derived // cache keys and query authenticated registries directly instead. - if urls.iter().any(|url| npm_config.has_auth_for_origin(url)) { + // Package managers are public on the default registry, so its responses do + // not depend on the credential and keep the cache and offline fallback. + if urls.iter().any(|url| !is_default_registry(url) && npm_config.has_auth_for_origin(url)) { return fetch_dist_tag_version(package_manager_type, tag, &urls, npm_config).await; } @@ -3451,6 +3453,30 @@ mod tests { .await; } + #[tokio::test(flavor = "current_thread")] + async fn authenticated_default_registry_latest_versions_use_the_cache() { + // A fresh cache entry answers without a request, so this never reaches the real registry. + let config = NpmConfig { + values: HashMap::from([( + "//registry.npmjs.org/:_authtoken".to_string(), + "TOKEN".to_string(), + )]), + }; + let vp_home = create_temp_dir(); + EnvConfig::with_vars_async([(env_vars::VP_HOME, vp_home.path().as_os_str())], |_| async { + let cache_path = dist_tag_cache_path( + PackageManagerType::Pnpm, + &["https://registry.npmjs.org/pnpm/latest".into()], + ); + write_dist_tag_cache(&cache_path, "10.1.0").unwrap(); + assert_eq!( + resolve_dist_tag(PackageManagerType::Pnpm, "latest", &config).await.unwrap(), + "10.1.0" + ); + }) + .await; + } + #[cfg(windows)] #[test] fn test_find_cached_package_manager_version_skips_missing_windows_shims() { diff --git a/docs/guide/env.md b/docs/guide/env.md index 6ea65f8a74..0bad1cf602 100644 --- a/docs/guide/env.md +++ b/docs/guide/env.md @@ -99,7 +99,7 @@ A project selection applies only to its matching shims. For example, pnpm contro Package-manager specs follow npm's order: an exact version, then a semver range, then a dist-tag. `default` is an alias of `latest`, except for npm, where it selects the npm on `PATH`. ::: details Dist-tag caching -When a package-manager dist-tag such as `latest` or `latest-10` is resolved, including a named shim falling back to the latest release, the resolved version is cached for one hour. An expired cache remains available when the registry cannot be reached. +When a package-manager dist-tag such as `latest` or `latest-10` is resolved, including a named shim falling back to the latest release, the resolved version is cached for one hour. An expired cache remains available when the registry cannot be reached. Registries other than `https://registry.npmjs.org` that have credentials in `.npmrc` are queried on every resolution, because their responses can differ between users. ::: `pn` runs the same managed binary as `pnpm`, and `pnx` runs the same managed binary as `pnpx` (`pnpm dlx`). These aliases also work with managed pnpm versions older than v11.