Repository navigation
Fix berry restore keeping removed descriptors (#1435) #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Mikola Lysenko (mikolalysenko)
merged 3 commits into
main
from
agent/fix-berry-restore-stale-selectors
Oct 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -757,6 +757,8 @@ async fn restore_berry( | |
| url_pin: bool, | ||
| } | ||
| let mut hits: Vec<Hit> = Vec::new(); | ||
| // What the lock still declares, read once for the URL-keyed hits. | ||
| let mut declared: Option<BTreeSet<String>> = None; | ||
| for (i, block) in blocks.iter().enumerate() { | ||
| let lines = stanza_lines(block); | ||
| let Some(resolution) = berry_field(&lines, "resolution").map(str::to_string) else { | ||
|
|
@@ -825,6 +827,20 @@ async fn restore_berry( | |
| }) | ||
| .cloned() | ||
| .collect(); | ||
| // Only descriptors some entry still declares key the restored | ||
| // entry: `yarn remove` in one workspace member leaves its | ||
| // selector behind while another member keeps the URL entry | ||
| // live, and yarn keys the entry by what is declared (#1435). | ||
| // The selectors are all dropped below either way. A lock that | ||
| // declares none of them (a spelling not read here) keeps every | ||
| // selector's descriptor, as before. | ||
| let declared = declared.get_or_insert_with(|| berry_declared_descriptors(&blocks)); | ||
| if descriptors | ||
| .iter() | ||
| .any(|d| berry_descriptor_declared(declared, d)) | ||
| { | ||
| descriptors.retain(|d| berry_descriptor_declared(declared, d)); | ||
| } | ||
| if descriptors.is_empty() { | ||
| result.refuse( | ||
| &uuid, | ||
|
|
@@ -1051,6 +1067,52 @@ async fn restore_berry( | |
| view.write(rel, doc.render(&moved)); | ||
| } | ||
|
|
||
| /// The descriptors a berry lock's `stanzas` still declare: every | ||
| /// entry's `dependencies:` map, plus the descriptor a `patch:` descriptor | ||
| /// wraps (`name@patch:name@npm%3A1.3.0#鈥 wraps `name@npm:1.3.0`), in | ||
| /// either a dependency map or a key. Yarn 4 spells a registry range | ||
| /// `npm:range`, yarn 2 and 3 a bare `range`; see | ||
| /// [`berry_descriptor_declared`]. | ||
| fn berry_declared_descriptors(stanzas: &[String]) -> BTreeSet<String> { | ||
| use crate::formats::yarn::berry_prune::dependency_descriptors; | ||
| use crate::formats::yarn::patterns::split_berry_key_patterns; | ||
| use crate::formats::yarn::stanzas::stanza_key; | ||
|
|
||
| let mut out = BTreeSet::new(); | ||
| for stanza in stanzas { | ||
| let keys = stanza_key(stanza) | ||
| .map(split_berry_key_patterns) | ||
| .unwrap_or_default(); | ||
| for descriptor in dependency_descriptors(stanza) { | ||
| if let Some(inner) = berry_patch_inner(&descriptor) { | ||
| out.insert(inner); | ||
| } | ||
| out.insert(descriptor); | ||
| } | ||
| out.extend(keys.iter().filter_map(|k| berry_patch_inner(k))); | ||
| } | ||
| out | ||
| } | ||
|
|
||
| /// The descriptor a `name@patch:<inner>#<patch>` descriptor wraps, with | ||
| /// yarn's percent-encoding of the inner `:` undone. | ||
| fn berry_patch_inner(descriptor: &str) -> Option<String> { | ||
| let (_, rest) = descriptor.split_once("@patch:")?; | ||
| let inner = rest.split('#').next().unwrap_or(rest); | ||
| Some(inner.replace("%3A", ":")) | ||
| } | ||
|
|
||
| /// Whether the `name@npm:range` `descriptor` is in `declared` | ||
| /// ([`berry_declared_descriptors`]), in yarn 4's spelling or yarn 2/3's | ||
| /// bare `name@range`. | ||
| fn berry_descriptor_declared(declared: &BTreeSet<String>, descriptor: &str) -> bool { | ||
| use crate::formats::yarn::patterns::split_pattern; | ||
| declared.contains(descriptor) | ||
| || split_pattern(descriptor) | ||
| .and_then(|(name, range)| Some(format!("{name}@{}", range.strip_prefix("npm:")?))) | ||
| .is_some_and(|bare| declared.contains(&bare)) | ||
| } | ||
|
|
||
| /// Retire the leftover hosted `resolutions` selectors of a yarn berry root | ||
| /// `package.json` (#1203): `yarn remove` / `yarn up` deleted the lock entry | ||
| /// a hosted pin keyed by its tarball URL but left its selector, which now | ||
|
|
@@ -2723,6 +2785,55 @@ mod tests { | |
| }; | ||
| use crate::patch::redirect::upstream::client::NpmDist; | ||
|
|
||
| /// #1435: what still declares a descriptor the hosted restore keys | ||
| /// the registry entry by. | ||
| #[test] | ||
| fn berry_descriptor_declared_reads_what_the_lock_still_uses() { | ||
| let declared = |stanzas: &[&str], d: &str| { | ||
| let stanzas: Vec<String> = stanzas.iter().map(|s| s.to_string()).collect(); | ||
| super::berry_descriptor_declared(&super::berry_declared_descriptors(&stanzas), d) | ||
| }; | ||
| let url_entry = "\"left-pad@http://patch.test/left-pad-1.3.0.tgz\":\n version: 1.3.0\n \ | ||
| resolution: \"left-pad@http://patch.test/left-pad-1.3.0.tgz\""; | ||
| let member = |dep: &str| { | ||
| format!( | ||
| "\"b@workspace:packages/b\":\n version: 0.0.0-use.local\n dependencies:\n \ | ||
| {dep}\n linkType: soft" | ||
| ) | ||
| }; | ||
| // Yarn 4 spells the range `npm:range`; yarn 2 and 3 a bare one. | ||
| let yarn4 = member("left-pad: \"npm:^1.3.0\""); | ||
| let yarn3 = member("left-pad: ^1.3.0"); | ||
| assert!(declared(&[url_entry, &yarn4], "left-pad@npm:^1.3.0")); | ||
| assert!(declared(&[url_entry, &yarn3], "left-pad@npm:^1.3.0")); | ||
| // The member a `yarn remove` emptied declares nothing. | ||
| assert!(!declared(&[url_entry, &yarn4], "left-pad@npm:1.3.0")); | ||
| assert!(!declared(&[url_entry, &yarn3], "left-pad@npm:1.3.0")); | ||
| // A `yarn patch` of the descriptor still uses it, in a dependency | ||
| // map or a key. | ||
| let patch = "left-pad@patch:left-pad@npm%3A1.3.0#~/.yarn/patches/x.patch"; | ||
| let patched = format!("\"{patch}\":\n version: 1.3.0"); | ||
| assert!(declared(&[url_entry, &patched], "left-pad@npm:1.3.0")); | ||
| let patch_dep = member(&format!("left-pad: \"{}\"", &patch["left-pad@".len()..])); | ||
| assert!(declared(&[url_entry, &patch_dep], "left-pad@npm:1.3.0")); | ||
| // An alias, a scoped namesake and a longer range declare other | ||
| // descriptors. | ||
| for other in [ | ||
| member("lp: \"npm:[email protected]\""), | ||
| member("\"@acme/left-pad\": \"npm:1.3.0\""), | ||
| member("left-pad: \"npm:1.3.0-rc.1\""), | ||
| "\"@acme/left-pad@patch:@acme/left-pad@npm%3A1.3.0#x.patch\":\n version: 1.3.0" | ||
| .to_string(), | ||
| "\"left-pad@patch:left-pad@npm%3A1.3.0-rc.1#x.patch\":\n version: 1.3.0-rc.1" | ||
| .to_string(), | ||
| ] { | ||
| assert!( | ||
| !declared(&[url_entry, &other], "left-pad@npm:1.3.0"), | ||
| "{other}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn files(npmrc: &[(&str, bool)], bunfig: &[(&str, bool)]) -> super::BunConfigFiles { | ||
| let layer = |list: &[(&str, bool)]| { | ||
| list.iter() | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.