Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/vp_pm_cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let lower = vt_str::format!("npm_config_{name}");
let upper = vt_str::format!("NPM_CONFIG_{}", name.cow_to_ascii_uppercase());
Expand Down
30 changes: 28 additions & 2 deletions crates/vp_pm_cli/src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading