Skip to content

Commit bbc9ccf

Browse files
committed
Tidy tarball check and cover pnpm evidence
Skip the default-registry lookup when the document came from the project's registry, read the dist once in the berry restore, and add a unit test that npmjs tarballs recorded behind a mirror are no longer read as proof that lockfile-include-tarball-url is on. Refs #1465 Assisted-by: Claude Code:claude-opus-5-5
1 parent 529fea6 commit bbc9ccf

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

  • crates/socket-patch-core/src/patch/redirect/upstream

‎crates/socket-patch-core/src/patch/redirect/upstream/npm.rs‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -456,16 +456,9 @@ fn registry_derives_tarball(
456456
use crate::vendor::registry_fetch::{
457457
npm_registry_base, npm_tarball_is_conventional, DEFAULT_NPM_REGISTRY,
458458
};
459-
let project = project_registry.unwrap_or(DEFAULT_NPM_REGISTRY);
460-
let default = npm_registry_base();
461-
let bases = if from_project {
462-
vec![project]
463-
} else {
464-
vec![default.as_str(), project]
465-
};
466-
bases
467-
.iter()
468-
.any(|base| npm_tarball_is_conventional(base, name, version, tarball))
459+
let conventional = |base: &str| npm_tarball_is_conventional(base, name, version, tarball);
460+
conventional(project_registry.unwrap_or(DEFAULT_NPM_REGISTRY))
461+
|| (!from_project && conventional(&npm_registry_base()))
469462
}
470463

471464
/// The `npm:` locator yarn berry writes for `name@version` resolved from the
@@ -912,14 +905,14 @@ async fn restore_berry(
912905
let Some(found) = dists.get(&(name.clone(), version.clone())) else {
913906
continue;
914907
};
908+
let dist = &found.dist;
915909
let locator = berry_registry_locator(
916910
registries.get(&name).and_then(Option::as_deref),
917911
found.from_project,
918912
&name,
919913
&version,
920-
&found.dist.tarball,
914+
&dist.tarball,
921915
);
922-
let dist = &found.dist;
923916
let resolution = format!(" resolution: \"{locator}\"");
924917
let mut lines = stanza_lines(&blocks[idx]);
925918
if let Some(pinned) = with_body_field(&lines, "resolution", &resolution) {
@@ -1496,14 +1489,11 @@ fn pnpm_lock_tarball_evidence(
14961489
// No document was read: the lock's URL came from the
14971490
// registry pnpm names, the default one when it names none.
14981491
let registry = registry(name);
1499-
let own = registry.as_deref().and_then(non_default_registry);
1500-
derived |= registry_derives_tarball(
1501-
registry.as_deref(),
1502-
own.is_some(),
1503-
name,
1504-
version,
1505-
tarball,
1506-
);
1492+
let own = registry
1493+
.as_deref()
1494+
.is_some_and(|r| non_default_registry(r).is_some());
1495+
derived |=
1496+
registry_derives_tarball(registry.as_deref(), own, name, version, tarball);
15071497
}
15081498
}
15091499
}
@@ -2763,9 +2753,10 @@ mod tests {
27632753
use super::{
27642754
berry_lookup_registry, berry_registry_locator, bun_env_registry, bun_lookup_registry,
27652755
bun_registry_slot, bun_tarball_url, expand_url_env, modules_yaml_pnpm_major,
2766-
non_default_registry, package_json_pnpm_major, pnpm_include_tarball, pnpm_lookup_registry,
2767-
pnpm_tarball_guess_warning, registry_derives_tarball, rush_json_pnpm_major, rush_lock_root,
2768-
split_userinfo, yaml_top_level_value, yarn_expand_env, PnpmTarballGuess, ProjectDist,
2756+
non_default_registry, package_json_pnpm_major, pnpm_include_tarball,
2757+
pnpm_lock_tarball_evidence, pnpm_lookup_registry, pnpm_tarball_guess_warning,
2758+
registry_derives_tarball, rush_json_pnpm_major, rush_lock_root, split_userinfo,
2759+
yaml_top_level_value, yarn_expand_env, PnpmTarballGuess, ProjectDist,
27692760
};
27702761
use crate::patch::redirect::upstream::client::NpmDist;
27712762

@@ -4135,6 +4126,23 @@ mod tests {
41354126
));
41364127
}
41374128

4129+
/// #1465: behind a mirror that passes npmjs's documents through, pnpm
4130+
/// records the npmjs URLs as `tarball:` because it cannot derive them
4131+
/// from the mirror, so they say nothing about
4132+
/// lockfile-include-tarball-url. Without the mirror they prove it on.
4133+
#[test]
4134+
fn npmjs_tarballs_behind_a_mirror_are_no_tarball_setting_evidence() {
4135+
let lock = "lockfileVersion: '9.0'\n\npackages:\n\n a@1.0.0:\n \
4136+
resolution: {integrity: sha512-AAAA, tarball: \
4137+
https://registry.npmjs.org/a/-/a-1.0.0.tgz}\n";
4138+
let mirror = |_: &str| Some("https://r.example/npm".to_string());
4139+
assert_eq!(pnpm_lock_tarball_evidence(lock, mirror, |_| false), None);
4140+
assert_eq!(
4141+
pnpm_lock_tarball_evidence(lock, |_| None, |_| false),
4142+
Some(true)
4143+
);
4144+
}
4145+
41384146
#[test]
41394147
fn berry_locator_binds_only_an_underived_tarball() {
41404148
assert_eq!(

0 commit comments

Comments
 (0)