Skip to main content

aube_resolver/
peer_context.rs

1//! Peer-dependency post-processing over an already-resolved graph.
2//!
3//! Two user-visible passes live here:
4//!
5//! * [`hoist_auto_installed_peers`] — promotes declared-but-unmet peers
6//!   up to importer direct deps, matching pnpm's `auto-install-peers=true`
7//!   behavior. Idempotent on graphs that already ship with those hoists
8//!   (npm v7+ output, lockfile-driven installs).
9//! * [`apply_peer_contexts`] — computes pnpm-style `(peer@ver)` suffixes
10//!   on contextualized `dep_path`s. Drives the sibling-symlink wiring in
11//!   `aube-linker` so each subtree that pins different peer versions gets
12//!   its own virtual-store entry.
13//!
14//! [`detect_unmet_peers`] reports what the two passes above couldn't wire
15//! up, so the CLI can surface warnings.
16//!
17//! Call order from `Resolver::resolve`: `hoist_auto_installed_peers`
18//! (fresh resolves only) → `apply_peer_contexts` → `detect_unmet_peers`.
19
20use crate::version_satisfies;
21use aube_lockfile::{DepType, DirectDep, LockedPackage, LockfileGraph};
22use rustc_hash::{FxHashMap, FxHashSet};
23use std::collections::{BTreeMap, BTreeSet};
24
25/// A peer dependency whose declared range doesn't match the version the
26/// tree actually ends up providing. Emitted as a warning by `aube install`.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct UnmetPeer {
29    /// dep_path of the package that declared the peer.
30    pub from_dep_path: String,
31    /// Human-friendly package name (pre-context) for display.
32    pub from_name: String,
33    /// Name of the peer being declared (e.g. `"react"`).
34    pub peer_name: String,
35    /// The declared peer range from the package's packument
36    /// (e.g. `"^16.8.0 || ^17.0.0 || ^18.0.0"`).
37    pub declared: String,
38    /// What the tree actually provides, if anything. `None` means the
39    /// peer is completely missing — rare in practice because the BFS
40    /// auto-install path usually drags *some* version in, but it can
41    /// happen for corner cases.
42    pub found: Option<String>,
43}
44
45/// Scan the resolved graph and return every declared required peer whose
46/// resolved version doesn't satisfy its declared range. Optional peers
47/// (`peerDependenciesMeta.optional = true`) are skipped — pnpm treats
48/// those as "warn suppressed" with `auto-install-peers=true`. The result
49/// is purely informational; aube never fails an install on unmet peers,
50/// matching pnpm.
51///
52/// The "found" version for each package comes from its own
53/// `dependencies` map — the peer-context pass writes the resolved peer
54/// tail there, so we don't have to re-walk ancestors. Any peer suffix on
55/// the stored tail is stripped before the semver check so `18.2.0(foo@1)`
56/// is treated as `18.2.0`.
57pub fn detect_unmet_peers(graph: &LockfileGraph) -> Vec<UnmetPeer> {
58    let mut unmet = Vec::new();
59    for pkg in graph.packages.values() {
60        for (peer_name, declared_range) in &pkg.peer_dependencies {
61            let optional = pkg
62                .peer_dependencies_meta
63                .get(peer_name)
64                .map(|m| m.optional)
65                .unwrap_or(false);
66            if optional {
67                continue;
68            }
69
70            let found_tail = pkg.dependencies.get(peer_name);
71            let found_version = found_tail.map(|t| canonical_tail(t).to_string());
72
73            let satisfied = match &found_version {
74                Some(v) => version_satisfies(v, declared_range),
75                None => false,
76            };
77            if satisfied {
78                continue;
79            }
80
81            unmet.push(UnmetPeer {
82                from_dep_path: pkg.dep_path.clone(),
83                from_name: pkg.name.clone(),
84                peer_name: peer_name.clone(),
85                declared: declared_range.clone(),
86                found: found_version,
87            });
88        }
89    }
90    // Stable order for deterministic test output and readable warnings.
91    unmet.sort_by(|a, b| {
92        (a.from_dep_path.as_str(), a.peer_name.as_str())
93            .cmp(&(b.from_dep_path.as_str(), b.peer_name.as_str()))
94    });
95    unmet
96}
97
98/// Promote unmet peers to importer direct deps.
99///
100/// Walks every resolved package's declared peer deps and hoists any
101/// peer that isn't already a direct dep of the importer up to the
102/// importer's `dependencies` list — what pnpm's
103/// `auto-install-peers=true` produces in its v9 lockfile. If you
104/// depend on a package whose `peerDependencies` declares `react` and
105/// you don't list `react` yourself, pnpm (and now aube) adds it to
106/// your importer's dependencies with the declared peer range as the
107/// specifier, and the linker creates a top-level
108/// `node_modules/react` symlink you can import from your own code.
109///
110/// Public so lockfile-driven installs that need to re-derive peer
111/// wiring (npm/yarn/bun formats, which don't record peer contexts)
112/// can run this before [`apply_peer_contexts`] to match fresh-resolve
113/// behavior. Idempotent in the npm case: npm v7+ already hoists
114/// auto-installed peers into root's `dependencies`, so they arrive
115/// pre-`satisfied` and no additions are emitted.
116///
117/// Algorithm:
118///   1. For each importer, collect the set of names already in its
119///      direct deps. Those are "satisfied" and need no hoist.
120///   2. DFS the reachable graph from the importer, visiting each package
121///      and examining its `peer_dependencies` declarations. For each
122///      declared peer not already satisfied by the importer, find a
123///      resolved version somewhere in the graph and synthesize a
124///      `DirectDep` entry. Mark it as satisfied so a second encounter
125///      doesn't add a duplicate.
126///   3. Stable: we walk in-order and take the first declared peer range
127///      encountered per name as the specifier. Conflicting ranges across
128///      the tree are not reconciled — first one wins. This matches pnpm
129///      for the simple case; the complex case is deferred.
130///
131/// Leaves everything else about the graph untouched — no packages are
132/// added or removed, only importer entries grow.
133pub fn hoist_auto_installed_peers(mut graph: LockfileGraph) -> LockfileGraph {
134    let importer_paths: Vec<String> = graph.importers.keys().cloned().collect();
135    for importer_path in importer_paths {
136        let Some(direct_deps) = graph.importers.get(&importer_path) else {
137            continue;
138        };
139        let mut satisfied: FxHashSet<String> = direct_deps.iter().map(|d| d.name.clone()).collect();
140
141        let mut queue: std::collections::VecDeque<String> =
142            direct_deps.iter().map(|d| d.dep_path.clone()).collect();
143        let mut walked: FxHashSet<String> = FxHashSet::default();
144        // Additions are gathered into a separate vec so we don't mutate
145        // the importer's direct-dep list while still borrowing from it.
146        let mut additions: Vec<DirectDep> = Vec::new();
147
148        while let Some(dep_path) = queue.pop_front() {
149            if !walked.insert(dep_path.clone()) {
150                continue;
151            }
152            let Some(pkg) = graph.packages.get(&dep_path) else {
153                continue;
154            };
155
156            // Collect unmet peer declarations from this package.
157            for (peer_name, peer_range) in &pkg.peer_dependencies {
158                if satisfied.contains(peer_name) {
159                    continue;
160                }
161                // Find any resolved version in the graph for this peer.
162                // Prefer the one the package already wired via its own
163                // dependencies map (the BFS auto-install result), and
164                // fall back to scanning `graph.packages` for a name
165                // match. If nothing matches, we quietly drop the peer —
166                // that's the only path where aube stays stricter than
167                // pnpm today; a future PR will emit an unmet warning.
168                //
169                // Fallback takes the semver-max version rather than
170                // whatever `BTreeMap` iteration order surfaces first —
171                // otherwise two resolved `react` entries like `18.0.0`
172                // and `18.3.1` would pick the lexicographically-earlier
173                // (older) one.
174                let resolved_via_pkg_deps = pkg.dependencies.contains_key(peer_name);
175                let resolved_version = pkg.dependencies.get(peer_name).cloned().or_else(|| {
176                    // Filter to parseable semver versions *before* the
177                    // max_by — returning `Equal` on parse failure makes
178                    // the comparator non-transitive, so an unparseable
179                    // entry sitting between two valid ones would cause
180                    // `max_by` to pick an iteration-order-dependent
181                    // result instead of the true maximum.
182                    graph
183                        .packages
184                        .values()
185                        .filter(|p| p.name == *peer_name)
186                        .filter_map(|p| {
187                            node_semver::Version::parse(&p.version)
188                                .ok()
189                                .map(|v| (v, p.version.clone()))
190                        })
191                        .max_by(|a, b| a.0.cmp(&b.0))
192                        .map(|(_, s)| s)
193                });
194                let Some(version) = resolved_version else {
195                    continue;
196                };
197                let canonical_version = canonical_tail(&version).to_string();
198                let synth_dep_path = format!("{peer_name}@{canonical_version}");
199                if !graph.packages.contains_key(&synth_dep_path) {
200                    // The peer version the package wired didn't match an
201                    // actual package entry — bail out for this peer
202                    // rather than writing a dangling DirectDep.
203                    continue;
204                }
205                satisfied.insert(peer_name.clone());
206                // Peer reached via the fallback path isn't in
207                // `pkg.dependencies`, so the normal "walk pkg's deps"
208                // loop at the bottom of the while block would skip it.
209                // Push it onto the queue directly so its own declared
210                // peers get hoisted too.
211                if !resolved_via_pkg_deps {
212                    queue.push_back(synth_dep_path.clone());
213                }
214                additions.push(DirectDep {
215                    name: peer_name.clone(),
216                    dep_path: synth_dep_path,
217                    // Peers auto-hoisted to the root are in the prod
218                    // graph by convention — matches what pnpm writes.
219                    dep_type: DepType::Production,
220                    specifier: Some(peer_range.clone()),
221                });
222            }
223
224            // Queue the package's own resolved deps for further walking.
225            for (child_name, child_version_tail) in &pkg.dependencies {
226                let canonical = child_version_tail
227                    .split('(')
228                    .next()
229                    .unwrap_or(child_version_tail);
230                queue.push_back(format!("{child_name}@{canonical}"));
231            }
232        }
233
234        if !additions.is_empty() {
235            tracing::debug!(
236                "hoisted {} auto-installed peer(s) into importer {}",
237                additions.len(),
238                importer_path
239            );
240            if let Some(deps) = graph.importers.get_mut(&importer_path) {
241                deps.extend(additions);
242                deps.sort_by(|a, b| a.name.cmp(&b.name));
243            }
244        }
245    }
246    graph
247}
248
249/// Walk the resolved graph top-down from each importer and compute a
250/// peer-dependency context for every package, producing a new graph whose
251/// dep_paths carry pnpm-style `(peer@ver)` suffixes.
252///
253/// The goal is parity with pnpm's v9 lockfile output: the same
254/// `name@version` can appear multiple times — once per distinct set of peer
255/// resolutions — so different subtrees that pin incompatible peers get
256/// isolated virtual-store entries and truly different sibling-symlink
257/// neighborhoods.
258///
259/// Algorithm per visited package P, reached at some point in a DFS from an
260/// importer with `ancestor_scope: name -> dep_path_tail`:
261///
262///  1. For each peer name declared by P, look it up in `ancestor_scope`
263///     (nearest-ancestor-wins, since the scope is rebuilt per recursion).
264///     If missing, fall back to P's own entry in `dependencies` — the BFS
265///     enqueue above auto-installed it as a transitive, which matches
266///     pnpm's `auto-install-peers=true` default.
267///  2. Sort the (peer_name, resolution) pairs and serialize as
268///     `(n1@v1)(n2@v2)…` for the suffix.
269///  3. Produce a contextualized dep_path `name@version{suffix}`. If that
270///     key is already in `out_packages` (or currently on the DFS stack via
271///     `visiting`), short-circuit — we've already emitted this variant.
272///  4. Build a new scope for P's children by merging the ancestor scope
273///     with P's own `dependencies` (rewritten to point at contextualized
274///     children) and the resolved peer map. Recurse.
275///  5. Emit the contextualized LockedPackage.
276///
277/// Cycles: protected by `visiting` — if a package is re-entered via a
278/// dependency cycle, we return the already-computed dep_path without
279/// recursing again. The peer context is fixed at first visit; any cycle
280/// traversal uses whatever context was live at that first visit.
281///
282/// Nested peer suffixes: pnpm writes `(react-dom@18.2.0(react@18.2.0))`
283/// when a declared peer has its own resolved peers. A single top-down
284/// DFS pass can't produce that form, because when a parent P records
285/// a peer version in its children's scope, it only knows the canonical
286/// tail — the peer's OWN suffix is computed later when the peer itself
287/// gets visited. We solve this by running `apply_peer_contexts_once` in
288/// a fixed-point loop: the second iteration's input has Pass 1's
289/// contextualized tails in every `pkg.dependencies` map, so when a
290/// descendant looks a peer up in ancestor scope it sees the full
291/// nested tail and serializes it as such. Most peer chains converge in
292/// 2–3 iterations; we cap at 16 as a safety belt.
293///
294/// Limitations (documented as follow-ups in the README):
295///   - No per-peer range satisfaction — we take whatever the ancestor has,
296///     even if it technically doesn't match P's declared peer range.
297///
298/// Knobs controlling the peer-context pass. Plumbed from four
299/// pnpm-compatible settings (`dedupe-peer-dependents`, `dedupe-peers`,
300/// `resolve-peers-from-workspace-root`, `peers-suffix-max-length`)
301/// through the `Resolver`'s `with_*` setters.
302#[derive(Debug, Clone, Copy)]
303pub struct PeerContextOptions {
304    /// When true, run the cross-subtree peer-variant collapse pass
305    /// after every iteration of the fixed-point loop. Matches pnpm's
306    /// default.
307    pub dedupe_peer_dependents: bool,
308    /// When true, emit suffixes as `(version)` instead of
309    /// `(name@version)`. Affects both the package key, the reference
310    /// tails stored in `dependencies`, and the cycle-break form of
311    /// `contains_canonical_back_ref`.
312    pub dedupe_peers: bool,
313    /// When true, unresolved peers can be satisfied by a dep declared
314    /// at the root importer (`"."`) even if no ancestor scope carries
315    /// the peer. Runs between own-deps and graph-wide scan in the
316    /// peer-context visitor — see `visit_peer_context` in this
317    /// module for the owning implementation (intentionally crate-
318    /// private; the public API here is the option flag itself).
319    pub resolve_from_workspace_root: bool,
320    /// Byte cap on the peer-ID suffix after which the entire suffix
321    /// is hashed to `_<10-char-sha256-hex>`. pnpm's default is 1000.
322    pub peers_suffix_max_length: usize,
323}
324
325impl Default for PeerContextOptions {
326    fn default() -> Self {
327        Self {
328            dedupe_peer_dependents: true,
329            dedupe_peers: false,
330            resolve_from_workspace_root: true,
331            peers_suffix_max_length: 1000,
332        }
333    }
334}
335
336/// Compute peer-context suffixes over an already-resolved graph.
337///
338/// Takes a *canonical* graph — one `LockedPackage` per `(name,
339/// version)` with `peer_dependencies` populated — and produces a
340/// *contextualized* graph whose keys and transitive references carry
341/// `(peer@ver)` suffixes when packages resolve peers differently in
342/// different subtrees. Drives the sibling-symlink wiring in
343/// `aube-linker` for peers, so every fetch/materialize site sees a
344/// per-context identity for any package whose peers disambiguate.
345///
346/// Public so lockfile-driven installs can run the pass over graphs
347/// parsed from npm/yarn/bun lockfiles (which emit canonical form —
348/// no peer suffixes — and would otherwise leave peer-dependent
349/// packages without their peers as `.aube/<pkg>/node_modules/<peer>`
350/// siblings). Fresh resolves call it internally from
351/// `Resolver::resolve`.
352pub fn apply_peer_contexts(
353    canonical: LockfileGraph,
354    options: &PeerContextOptions,
355) -> LockfileGraph {
356    const MAX_ITERATIONS: usize = 16;
357    let mut current = canonical;
358    let mut converged = false;
359    for i in 0..MAX_ITERATIONS {
360        let current_keys: Vec<String> = current.packages.keys().cloned().collect();
361        let after_once = apply_peer_contexts_once(current, options);
362        let next = if options.dedupe_peer_dependents {
363            dedupe_peer_variants(after_once)
364        } else {
365            after_once
366        };
367        if current_keys
368            .iter()
369            .map(String::as_str)
370            .eq(next.packages.keys().map(String::as_str))
371        {
372            tracing::debug!("peer-context pass converged after {i} iteration(s)");
373            current = next;
374            converged = true;
375            break;
376        }
377        current = next;
378    }
379    if !converged {
380        // Hit iteration cap. Means mutually recursive peers or
381        // genuine cycle. Lockfile now has partial nested suffixes.
382        // Linker downstream will wire symlinks against incomplete
383        // graph. Returning this silently ships broken node_modules.
384        // Old code used warn!, warn gets swallowed in CI. Bump to
385        // error! so ops see it. Proper fix is returning a Result
386        // from apply_peer_contexts but that cascades up through
387        // Resolver::resolve signature, do that separately.
388        tracing::error!(
389            "peer-context hit MAX_ITERATIONS={MAX_ITERATIONS} without convergence. \
390             mutually recursive peers likely. lockfile incomplete, linker output will be wrong"
391        );
392    }
393    // `dedupe-peers=true` rewrites the parenthesized peer suffix to
394    // drop the `name@` prefix. Done as a post-pass rather than inline
395    // so cycle detection during the fixed-point loop keeps the full
396    // `name@version` form (otherwise unrelated same-version packages
397    // would false-positive as back-references).
398    if options.dedupe_peers {
399        dedupe_peer_suffixes(current)
400    } else {
401        current
402    }
403}
404
405/// Cross-subtree peer-variant dedupe. When `dedupe-peer-dependents` is
406/// on, packages that landed at different contextualized dep_paths but
407/// resolved every declared peer to the *same* version (ignoring the
408/// nested peer suffix on each peer tail) collapse into a single
409/// canonical variant — chosen as the lexicographically smallest key in
410/// the equivalence class. References in every surviving
411/// `LockedPackage.dependencies` map and every `importers[*]` direct
412/// dep get rewritten through the old→canonical map, and the
413/// non-canonical entries are dropped from `packages`.
414///
415/// Packages whose `peer_dependencies` map is empty — i.e. the canonical
416/// base already has only one variant — are skipped.
417pub(crate) fn dedupe_peer_variants(graph: LockfileGraph) -> LockfileGraph {
418    let canonical_base = |key: &str| -> String { canonical_tail(key).to_string() };
419    // Only the peer-bearing part of the resolved peer tail is
420    // comparable across subtrees — the nested suffix could differ even
421    // for peer-equivalent variants on mid-iterations of the outer
422    // fixed-point loop.
423    let peer_base = |tail: &str| -> String { canonical_tail(tail).to_string() };
424
425    // Group dep_paths by their peer-free base name.
426    let mut groups: BTreeMap<String, Vec<String>> = BTreeMap::new();
427    for key in graph.packages.keys() {
428        groups
429            .entry(canonical_base(key))
430            .or_default()
431            .push(key.clone());
432    }
433
434    let mut rewrite: BTreeMap<String, String> = BTreeMap::new();
435    for (_base, mut keys) in groups {
436        if keys.len() < 2 {
437            continue;
438        }
439        // Deterministic order for canonical selection + stable hashing.
440        keys.sort();
441        // Union-find over equivalence classes. Two variants are
442        // equivalent when each declared peer name resolves to the same
443        // peer base in both (or is missing from both).
444        let mut parent: Vec<usize> = (0..keys.len()).collect();
445        fn find(parent: &mut [usize], i: usize) -> usize {
446            if parent[i] == i {
447                i
448            } else {
449                let r = find(parent, parent[i]);
450                parent[i] = r;
451                r
452            }
453        }
454        for i in 0..keys.len() {
455            for j in (i + 1)..keys.len() {
456                let pa = &graph.packages[&keys[i]];
457                let pb = &graph.packages[&keys[j]];
458                // Same canonical version is required — packages with
459                // different versions but the same name would share no
460                // canonical_base only if the name-without-version
461                // collided, which doesn't happen (version is in the
462                // base). Still, belt-and-suspenders.
463                if pa.version != pb.version {
464                    continue;
465                }
466                let peer_names: BTreeSet<&String> = pa
467                    .peer_dependencies
468                    .keys()
469                    .chain(pb.peer_dependencies.keys())
470                    .collect();
471                let equivalent = peer_names.iter().all(|name| {
472                    match (
473                        pa.dependencies.get(name.as_str()),
474                        pb.dependencies.get(name.as_str()),
475                    ) {
476                        (Some(va), Some(vb)) => peer_base(va) == peer_base(vb),
477                        (None, None) => true,
478                        _ => false,
479                    }
480                });
481                if equivalent {
482                    let ri = find(&mut parent, i);
483                    let rj = find(&mut parent, j);
484                    if ri != rj {
485                        parent[ri] = rj;
486                    }
487                }
488            }
489        }
490        // Build class → canonical (smallest key) mapping. Using
491        // index-based iteration here because `find` takes a mutable
492        // reference into `parent`, so holding an immutable borrow
493        // from `keys.iter()` at the same time would double-borrow.
494        #[allow(clippy::needless_range_loop)]
495        {
496            let mut class_rep: BTreeMap<usize, String> = BTreeMap::new();
497            for i in 0..keys.len() {
498                let root = find(&mut parent, i);
499                class_rep
500                    .entry(root)
501                    .and_modify(|cur| {
502                        if keys[i] < *cur {
503                            *cur = keys[i].clone();
504                        }
505                    })
506                    .or_insert_with(|| keys[i].clone());
507            }
508            for i in 0..keys.len() {
509                let root = find(&mut parent, i);
510                let canonical = class_rep[&root].clone();
511                if keys[i] != canonical {
512                    rewrite.insert(keys[i].clone(), canonical);
513                }
514            }
515        }
516    }
517
518    if rewrite.is_empty() {
519        return graph;
520    }
521
522    // Rewrite package dependency tails and keep only canonicals.
523    let LockfileGraph {
524        importers,
525        packages,
526        settings,
527        overrides,
528        ignored_optional_dependencies,
529        times,
530        skipped_optional_dependencies,
531        catalogs,
532        bun_config_version,
533    } = graph;
534
535    let mut new_packages: BTreeMap<String, LockedPackage> = BTreeMap::new();
536    for (key, mut pkg) in packages {
537        if rewrite.contains_key(&key) {
538            continue;
539        }
540        for (dep_name, dep_tail) in pkg.dependencies.iter_mut() {
541            let dep_key = format!("{dep_name}@{dep_tail}");
542            if let Some(canonical) = rewrite.get(&dep_key) {
543                let new_tail = canonical
544                    .strip_prefix(&format!("{dep_name}@"))
545                    .map(|s| s.to_string())
546                    .unwrap_or_else(|| canonical.clone());
547                *dep_tail = new_tail;
548            }
549        }
550        new_packages.insert(key, pkg);
551    }
552
553    let mut new_importers: BTreeMap<String, Vec<DirectDep>> = BTreeMap::new();
554    for (importer_path, deps) in importers {
555        let mut new_deps = Vec::with_capacity(deps.len());
556        for mut dep in deps {
557            if let Some(canonical) = rewrite.get(&dep.dep_path) {
558                dep.dep_path = canonical.clone();
559            }
560            new_deps.push(dep);
561        }
562        new_importers.insert(importer_path, new_deps);
563    }
564
565    LockfileGraph {
566        importers: new_importers,
567        packages: new_packages,
568        settings,
569        overrides,
570        ignored_optional_dependencies,
571        times,
572        skipped_optional_dependencies,
573        catalogs,
574        bun_config_version,
575    }
576}
577
578/// Single pass of the peer-context computation. See `apply_peer_contexts`
579/// for the wrapping fixed-point loop.
580///
581/// Algorithm per visited package P, reached at some point in a DFS from an
582/// importer with `ancestor_scope: name -> dep_path_tail`:
583///
584///  1. For each peer name declared by P, look it up in `ancestor_scope`
585///     (nearest-ancestor-wins, since the scope is rebuilt per recursion).
586///     If missing, fall back to P's own entry in `dependencies` — the BFS
587///     enqueue auto-installed it as a transitive, matching pnpm's
588///     `auto-install-peers=true` default.
589///  2. Sort the (peer_name, resolution) pairs and serialize as
590///     `(n1@v1)(n2@v2)…` for the suffix.
591///  3. Produce a contextualized dep_path `name@version{suffix}`. If that
592///     key is already in `out_packages` (or currently on the DFS stack via
593///     `visiting`), short-circuit — we've already emitted this variant.
594///  4. Build a new scope for P's children by merging the ancestor scope
595///     with P's own `dependencies` and the resolved peer map. Recurse.
596///  5. Emit the contextualized LockedPackage.
597///
598/// Cycles: protected by `visiting` — if a package is re-entered via a
599/// dependency cycle, we return the already-computed dep_path without
600/// recursing again. The peer context is fixed at first visit; any cycle
601/// traversal uses whatever context was live at that first visit.
602fn apply_peer_contexts_once(
603    canonical: LockfileGraph,
604    options: &PeerContextOptions,
605) -> LockfileGraph {
606    let mut out_packages: BTreeMap<String, LockedPackage> = BTreeMap::new();
607    let mut new_importers: BTreeMap<String, Vec<DirectDep>> = BTreeMap::new();
608
609    // Name-indexed view of the canonical graph, shared across
610    // every `visit_peer_context` call in this pass. Peer-resolution
611    // scan-by-name is the resolver's hottest inner loop. Without
612    // this, each peer runs `O(|graph|)` per package per fixed-point
613    // iter. Prebuilt index drops the scan to O(1) average.
614    let mut name_index: FxHashMap<&str, Vec<&LockedPackage>> = FxHashMap::default();
615    for pkg in canonical.packages.values() {
616        name_index.entry(pkg.name.as_str()).or_default().push(pkg);
617    }
618
619    // Root-importer scope used by `resolve-peers-from-workspace-root`.
620    // Computed once from the canonical input so it reflects the
621    // contextualized state of every root dep on fixed-point iterations
622    // 2+ — same logic as per-importer `importer_scope` below.
623    let root_scope: FxHashMap<String, String> = canonical
624        .importers
625        .get(".")
626        .map(|deps| scope_map_from_deps(deps))
627        .unwrap_or_default();
628
629    for (importer_path, direct_deps) in &canonical.importers {
630        // An importer's own direct deps are in scope for its children's
631        // peer resolution — this is how pnpm's "auto-install at the root"
632        // path gets peer links that point at root-level packages.
633        //
634        // Use the *full contextualized tail* off each DirectDep rather
635        // than the package's plain version. On Pass 1 of the fixed-point
636        // loop the tail is canonical and equal to `p.version`; on Pass 2+
637        // it's already contextualized, and passing the plain version
638        // would make descendants look up keys that don't exist in the
639        // (now-nested) graph.
640        let importer_scope = scope_map_from_deps(direct_deps);
641
642        let mut new_deps = Vec::with_capacity(direct_deps.len());
643        for dep in direct_deps {
644            // `visiting` is the DFS stack guard for this particular descent
645            // — reset per direct dep so we don't incorrectly flag a package
646            // as a cycle when it's reached again from a sibling subtree.
647            // The shared `out_packages` still dedupes across siblings since
648            // the second visit hits the `contains_key` short-circuit below.
649            //
650            // Invariant (see `visit_peer_context` for the detailed handling):
651            // a dep_path returned from the cycle-break branch may not yet
652            // be present in `out_packages` at the moment of return, because
653            // the package is still being assembled up the call stack. The
654            // parent that records the returned tail will complete its own
655            // insertion before the recursion unwinds, so by the time
656            // anything reads the graph, every referenced dep_path exists.
657            let mut visiting: FxHashSet<String> = FxHashSet::default();
658            let new_dep_path = visit_peer_context(
659                &dep.dep_path,
660                &canonical,
661                &name_index,
662                &importer_scope,
663                &root_scope,
664                &mut out_packages,
665                &mut visiting,
666                options,
667            )
668            .unwrap_or_else(|| dep.dep_path.clone());
669            new_deps.push(DirectDep {
670                name: dep.name.clone(),
671                dep_path: new_dep_path,
672                dep_type: dep.dep_type,
673                specifier: dep.specifier.clone(),
674            });
675        }
676        new_importers.insert(importer_path.clone(), new_deps);
677    }
678
679    // Any canonical package that was never reached by the DFS (orphaned
680    // from every importer) is dropped — that matches the filter_deps
681    // semantics and avoids emitting dead entries into the lockfile.
682
683    LockfileGraph {
684        importers: new_importers,
685        packages: out_packages,
686        // The post-pass is pure — settings + overrides carry through
687        // from the input graph untouched.
688        settings: canonical.settings,
689        overrides: canonical.overrides,
690        ignored_optional_dependencies: canonical.ignored_optional_dependencies,
691        times: canonical.times,
692        skipped_optional_dependencies: canonical.skipped_optional_dependencies,
693        catalogs: canonical.catalogs,
694        bun_config_version: canonical.bun_config_version,
695    }
696}
697
698/// DFS helper for `apply_peer_contexts`. Returns the peer-contextualized
699/// dep_path of the visited package, or `None` if the canonical package is
700/// missing (shouldn't happen in practice but we degrade gracefully).
701/// Does `value` contain a peer-suffix reference to `canonical` as a
702/// proper name@version boundary (i.e. preceded by `(` and followed by
703/// `(` / `)` / end-of-string)? Used by the peer-context pass to detect
704/// when a nested tail loops back to the current package so it can
705/// short-circuit the chain instead of growing the suffix forever.
706/// If `s` ends with `_<10 lowercase hex>` (the marker written by
707/// `hash_peer_suffix`), strip it and return the prefix. Otherwise
708/// return `s` unchanged.
709///
710/// Safe against false positives: `s` here is always a post-split
711/// `name@version` base, and semver forbids `_` inside a version, so
712/// an underscore 10 chars from the end of `name@version` can only be
713/// our marker.
714/// Everything before the first `(` — i.e. the canonical `name@version`
715/// part of a dep-path with the peer-context suffix stripped. Returns
716/// the original string when no `(` is present. Borrowed; callers that
717/// need owned bump with `.to_string()`.
718fn canonical_tail(s: &str) -> &str {
719    s.split('(').next().unwrap_or(s)
720}
721
722/// Build a `name → contextualized tail` map from a direct-dep slice.
723/// The tail is the dep_path with the `{name}@` prefix stripped, which
724/// on pass 1 is equal to `pkg.version` and on pass 2+ carries the
725/// nested peer-context suffix. Used both for the root scope and for
726/// each importer's own scope inside `apply_peer_contexts_once`.
727fn scope_map_from_deps(deps: &[DirectDep]) -> FxHashMap<String, String> {
728    deps.iter()
729        .map(|d| {
730            let tail = d
731                .dep_path
732                .strip_prefix(&format!("{}@", d.name))
733                .map(|s| s.to_string())
734                .unwrap_or_else(|| d.dep_path.clone());
735            (d.name.clone(), tail)
736        })
737        .collect()
738}
739
740fn strip_hashed_peer_suffix(s: &str) -> &str {
741    const MARKER_LEN: usize = 11; // `_` + 10 hex chars
742    if s.len() < MARKER_LEN {
743        return s;
744    }
745    let tail = &s[s.len() - MARKER_LEN..];
746    if !tail.starts_with('_') {
747        return s;
748    }
749    if tail[1..]
750        .chars()
751        .all(|c| c.is_ascii_digit() || ('a'..='f').contains(&c))
752    {
753        &s[..s.len() - MARKER_LEN]
754    } else {
755        s
756    }
757}
758
759/// Hash a peer-ID suffix with SHA-256 and return `_<10-char-hex>`.
760/// Used by the peer-context pass when the raw suffix length exceeds
761/// `peersSuffixMaxLength`. Matches pnpm's format so lockfile dep_path
762/// keys stay portable.
763pub(crate) fn hash_peer_suffix(suffix: &str) -> String {
764    use sha2::{Digest, Sha256};
765    let digest = Sha256::digest(suffix.as_bytes());
766    let mut out = String::with_capacity(11);
767    out.push('_');
768    for byte in digest.iter().take(5) {
769        use std::fmt::Write;
770        let _ = write!(out, "{byte:02x}");
771    }
772    out
773}
774
775pub(crate) fn contains_canonical_back_ref(value: &str, canonical: &str) -> bool {
776    let bytes = value.as_bytes();
777    let target = canonical.as_bytes();
778    if target.is_empty() || target.len() > bytes.len() {
779        return false;
780    }
781    let mut i = 0;
782    while i + target.len() <= bytes.len() {
783        if &bytes[i..i + target.len()] == target {
784            let before = if i == 0 { b'\0' } else { bytes[i - 1] };
785            let after = bytes.get(i + target.len()).copied().unwrap_or(b'\0');
786            let before_ok = before == b'(';
787            let after_ok = after == b'(' || after == b')' || after == b'\0';
788            if before_ok && after_ok {
789                return true;
790            }
791        }
792        i += 1;
793    }
794    false
795}
796
797/// Dedupe-peers post-pass: strip the `name@` prefix from every
798/// parenthesized peer segment in every dep_path key and reference,
799/// turning `react-dom@18.2.0(react@18.2.0)` into
800/// `react-dom@18.2.0(18.2.0)`. Nested segments get the same treatment
801/// so `a@1(b@2(c@3))` becomes `a@1(2(3))`.
802///
803/// Running this as a final post-pass (instead of inline during suffix
804/// assembly in `visit_peer_context`) keeps cycle detection correct:
805/// the detection path works against the full `name@version` form
806/// throughout the fixed-point loop, and only the serialized output
807/// gets the shorter form. A version-only inline approach would
808/// false-positive on unrelated packages that coincidentally share a
809/// version with the current package's canonical base.
810///
811/// Pure: no-op when `dedupe_peers` is off (caller gates the call);
812/// otherwise rewrites every package key, every `LockedPackage.dep_path`
813/// and `LockedPackage.dependencies` value, and every `importers[*]`
814/// DirectDep `dep_path` through the same `apply_dedupe_peers_to_tail`
815/// helper. Package bodies (integrity, metadata, etc.) are cloned
816/// verbatim.
817pub(crate) fn dedupe_peer_suffixes(graph: LockfileGraph) -> LockfileGraph {
818    // Pass 1: compute the intended deduped key for each package and
819    // tally how many distinct full-form keys map to it. Stripping
820    // `name@` from suffix segments is lossy — two variants whose peer
821    // *names* differ but whose peer *versions* coincide would collapse
822    // onto the same deduped key (e.g. `consumer@1.0.0(foo@1.0.0)` and
823    // `consumer@1.0.0(bar@1.0.0)` both → `consumer@1.0.0(1.0.0)`).
824    // `dedupe_peer_variants` already merged the peer-equivalent
825    // duplicates, so any remaining collision here represents genuinely
826    // distinct variants — losing one would silently drop its
827    // dependency wiring. We detect those collisions and keep both
828    // sides in full form.
829    let mut target_counts: BTreeMap<String, usize> = BTreeMap::new();
830    let mut intended: BTreeMap<String, String> = BTreeMap::new();
831    for key in graph.packages.keys() {
832        let new_key = apply_dedupe_peers_to_key(key);
833        *target_counts.entry(new_key.clone()).or_insert(0) += 1;
834        intended.insert(key.clone(), new_key);
835    }
836    let rewrite: BTreeMap<String, String> = intended
837        .into_iter()
838        .map(|(old, new)| {
839            if target_counts.get(&new).copied().unwrap_or(0) > 1 {
840                tracing::warn!(
841                    "dedupe-peers: collision on {new} — keeping {old} in full form to avoid \
842                     dropping a distinct peer-variant"
843                );
844                (old.clone(), old)
845            } else {
846                (old, new)
847            }
848        })
849        .collect();
850
851    // Rewrite a `(child_name, tail)` reference by reconstructing the
852    // target's full-form key, looking up its effective rewrite, and
853    // stripping `child_name@` off the result to recover the tail.
854    // Tails always follow their target package's rewrite decision,
855    // so references stay consistent when a collision forces a target
856    // back to full form.
857    let rewrite_tail = |child_name: &str, tail: &str| -> String {
858        let old_key = format!("{child_name}@{tail}");
859        match rewrite.get(&old_key) {
860            Some(new_key) => new_key
861                .strip_prefix(&format!("{child_name}@"))
862                .map(|s| s.to_string())
863                .unwrap_or_else(|| tail.to_string()),
864            None => apply_dedupe_peers_to_tail(tail),
865        }
866    };
867
868    let mut new_packages: BTreeMap<String, LockedPackage> = BTreeMap::new();
869    for (old_key, pkg) in graph.packages {
870        let new_key = rewrite
871            .get(&old_key)
872            .cloned()
873            .unwrap_or_else(|| old_key.clone());
874        let new_dependencies: BTreeMap<String, String> = pkg
875            .dependencies
876            .into_iter()
877            .map(|(n, v)| {
878                let new_v = rewrite_tail(&n, &v);
879                (n, new_v)
880            })
881            .collect();
882        let new_optional_dependencies: BTreeMap<String, String> = pkg
883            .optional_dependencies
884            .into_iter()
885            .map(|(n, v)| {
886                let new_v = rewrite_tail(&n, &v);
887                (n, new_v)
888            })
889            .collect();
890        new_packages.insert(
891            new_key.clone(),
892            LockedPackage {
893                name: pkg.name,
894                version: pkg.version,
895                integrity: pkg.integrity,
896                dependencies: new_dependencies,
897                optional_dependencies: new_optional_dependencies,
898                peer_dependencies: pkg.peer_dependencies,
899                peer_dependencies_meta: pkg.peer_dependencies_meta,
900                dep_path: new_key,
901                local_source: pkg.local_source,
902                os: pkg.os,
903                cpu: pkg.cpu,
904                libc: pkg.libc,
905                bundled_dependencies: pkg.bundled_dependencies,
906                tarball_url: pkg.tarball_url,
907                alias_of: pkg.alias_of,
908                yarn_checksum: pkg.yarn_checksum,
909                engines: pkg.engines,
910                bin: pkg.bin,
911                declared_dependencies: pkg.declared_dependencies,
912                license: pkg.license,
913                funding_url: pkg.funding_url,
914            },
915        );
916    }
917
918    let new_importers: BTreeMap<String, Vec<DirectDep>> = graph
919        .importers
920        .into_iter()
921        .map(|(path, deps)| {
922            let rewritten = deps
923                .into_iter()
924                .map(|d| {
925                    let new_dep_path = rewrite
926                        .get(&d.dep_path)
927                        .cloned()
928                        .unwrap_or_else(|| apply_dedupe_peers_to_key(&d.dep_path));
929                    DirectDep {
930                        name: d.name,
931                        dep_path: new_dep_path,
932                        dep_type: d.dep_type,
933                        specifier: d.specifier,
934                    }
935                })
936                .collect();
937            (path, rewritten)
938        })
939        .collect();
940
941    LockfileGraph {
942        importers: new_importers,
943        packages: new_packages,
944        settings: graph.settings,
945        overrides: graph.overrides,
946        ignored_optional_dependencies: graph.ignored_optional_dependencies,
947        times: graph.times,
948        skipped_optional_dependencies: graph.skipped_optional_dependencies,
949        catalogs: graph.catalogs,
950        bun_config_version: graph.bun_config_version,
951    }
952}
953
954/// Strip `name@` from inside every parenthesized segment of a full
955/// dep_path key (e.g. `react-dom@18.2.0(react@18.2.0)` →
956/// `react-dom@18.2.0(18.2.0)`). The first `name@version` outside any
957/// parens is preserved verbatim — that's the canonical head of the
958/// dep_path and `dedupe-peers` only affects the peer suffix.
959pub(crate) fn apply_dedupe_peers_to_key(key: &str) -> String {
960    let mut parts = key.split('(');
961    let Some(first) = parts.next() else {
962        return key.to_string();
963    };
964    let mut out = String::with_capacity(key.len());
965    out.push_str(first);
966    for part in parts {
967        out.push('(');
968        // In a well-formed key, `part` looks like `name@version)` /
969        // `name@version` / `version)` / ... We strip everything up to
970        // and including the LAST `@` (scoped packages like
971        // `@types/react@18.2.0` contain two `@`s; the separator is the
972        // rightmost one). We only strip if that `@` comes before the
973        // first `)` or `(` (i.e. the segment actually starts with
974        // `name@`, not the outer parens closing with no name inside).
975        if let Some(at_idx) = part.rfind('@') {
976            let close_idx = part.find([')', '(']).unwrap_or(usize::MAX);
977            if at_idx < close_idx {
978                out.push_str(&part[at_idx + 1..]);
979                continue;
980            }
981        }
982        out.push_str(part);
983    }
984    out
985}
986
987/// Same as [`apply_dedupe_peers_to_key`] but for dep-tail values
988/// stored in `LockedPackage.dependencies` (e.g. `18.2.0(react@18.2.0)`
989/// → `18.2.0(18.2.0)`). Tails differ from keys only by lacking the
990/// leading `name@` prefix — both use the same parens-based suffix
991/// shape, so the algorithm is identical.
992fn apply_dedupe_peers_to_tail(tail: &str) -> String {
993    apply_dedupe_peers_to_key(tail)
994}
995
996#[allow(clippy::too_many_arguments)]
997fn visit_peer_context<'g>(
998    input_dep_path: &str,
999    graph: &'g LockfileGraph,
1000    name_index: &FxHashMap<&'g str, Vec<&'g LockedPackage>>,
1001    ancestor_scope: &FxHashMap<String, String>,
1002    root_scope: &FxHashMap<String, String>,
1003    out_packages: &mut BTreeMap<String, LockedPackage>,
1004    visiting: &mut FxHashSet<String>,
1005    options: &PeerContextOptions,
1006) -> Option<String> {
1007    let pkg = graph.packages.get(input_dep_path)?;
1008
1009    // The input key may already carry a peer suffix (fixed-point loop
1010    // Pass 2+). Drop it before we build a new one — otherwise we'd
1011    // append the new suffix on top of the old and grow unboundedly
1012    // across iterations (classic mutual-peer-cycle blow-up).
1013    //
1014    // Two suffix forms can be present from a prior pass:
1015    //   1. `(name@version)(…)` — the normal nested peer suffix. Stripped
1016    //      by splitting on the first `(`.
1017    //   2. `_<10-char-sha256-hex>` — the hashed form produced when the
1018    //      normal suffix exceeded `peersSuffixMaxLength`. Must also be
1019    //      stripped; otherwise each pass re-hashes the already-hashed
1020    //      key and appends another marker (exposed by the
1021    //      `peer_suffix_is_hashed_when_exceeding_cap` unit test).
1022    let canonical_base = canonical_tail(input_dep_path);
1023    let canonical_base = strip_hashed_peer_suffix(canonical_base).to_string();
1024
1025    // Compute peer context: walk declared peers, resolve from ancestors
1026    // (nearest wins — the scope is rebuilt as we recurse) or from the
1027    // package's own dependency map as the auto-install fallback. Both
1028    // sides may produce nested tails on the second and later iterations
1029    // of the fixed-point loop.
1030    // Resolution source priority for each declared peer:
1031    //   1. Ancestor scope — if the ancestor's version actually
1032    //      satisfies the declared peer range. Different subtrees can
1033    //      pin different versions of the same peer name (classic
1034    //      `lib-a peers on react@^17`, `lib-b peers on react@^18`),
1035    //      and silently reusing the ancestor's version regardless of
1036    //      the declared range would force both libs onto the same
1037    //      version — exactly the behavior we want to fix here.
1038    //   2. The current package's own `pkg.dependencies` entry — the
1039    //      BFS peer-walk enqueued this peer with the declared range,
1040    //      so whatever got picked there is guaranteed to satisfy.
1041    //   3. A graph-wide scan as a last resort: any package whose name
1042    //      matches and whose version satisfies the declared range.
1043    //      This keeps nested-context callers from losing their peer
1044    //      resolution when neither ancestor nor own-deps has it.
1045    //   4. If no satisfying version exists, fall back to the nearest
1046    //      incompatible ancestor/root/pkg dependency. pnpm still wires
1047    //      that user-declared version into the peer context and then
1048    //      reports the semver mismatch; omitting it would produce a
1049    //      weaker "missing peer" warning and an unsuffixed snapshot.
1050    //
1051    // If nothing in the graph satisfies, the peer is left out of the
1052    // context entirely — `detect_unmet_peers` will surface it as a
1053    // warning after the pass.
1054    let mut peer_context: Vec<(String, String)> = Vec::new();
1055    for (peer_name, declared_range) in &pkg.peer_dependencies {
1056        let satisfies_declared = |v: &str| -> bool {
1057            // The tail may carry a nested peer suffix on fixed-point
1058            // iterations 2+; strip it before checking the semver.
1059            let canonical = canonical_tail(v);
1060            version_satisfies(canonical, declared_range)
1061        };
1062
1063        let from_ancestor = ancestor_scope
1064            .get(peer_name)
1065            .filter(|v| satisfies_declared(v))
1066            .cloned();
1067        let from_ancestor_incompatible = ancestor_scope.get(peer_name).cloned();
1068
1069        let from_pkg_deps = pkg
1070            .dependencies
1071            .get(peer_name)
1072            .filter(|v| satisfies_declared(v))
1073            .cloned();
1074        let from_pkg_deps_incompatible = pkg.dependencies.get(peer_name).cloned();
1075
1076        // `resolve-peers-from-workspace-root`: fall back to the root
1077        // importer's direct deps before the graph-wide scan. Common in
1078        // monorepos where the workspace root pins shared peers (e.g.
1079        // `react`) that leaf packages peer on without declaring them
1080        // in their own subtree. Skipped when the setting is off —
1081        // matches pnpm's `resolve-peers-from-workspace-root=false`.
1082        let from_root = if options.resolve_from_workspace_root {
1083            root_scope
1084                .get(peer_name)
1085                .filter(|v| satisfies_declared(v))
1086                .cloned()
1087        } else {
1088            None
1089        };
1090        let from_root_incompatible = if options.resolve_from_workspace_root {
1091            root_scope.get(peer_name).cloned()
1092        } else {
1093            None
1094        };
1095
1096        // Return the full dep_path TAIL (the part after `name@`), not
1097        // just `p.version`. On fixed-point iteration 2+, the input
1098        // graph's keys are contextualized — e.g. `react-dom` lives at
1099        // `react-dom@18.2.0(react@18.2.0)`. Downstream code
1100        // reconstructs the child lookup key with
1101        // `format!("{child_name}@{tail}")` and needs the tail to
1102        // match whatever the graph has keyed it under, otherwise the
1103        // lookup returns None and the peer gets silently dropped
1104        // from `new_dependencies`. The semver check is against the
1105        // package's canonical `version` field, not the tail, because
1106        // the tail may carry a peer suffix that isn't valid semver.
1107        let from_graph_scan = || {
1108            name_index
1109                .get(peer_name.as_str())
1110                .into_iter()
1111                .flat_map(|bucket| bucket.iter().copied())
1112                .filter(|p| version_satisfies(&p.version, declared_range))
1113                .filter_map(|p| {
1114                    let tail = p
1115                        .dep_path
1116                        .strip_prefix(&format!("{}@", p.name))
1117                        .map(|s| s.to_string())
1118                        .unwrap_or_else(|| p.version.clone());
1119                    node_semver::Version::parse(&p.version)
1120                        .ok()
1121                        .map(|ver| (ver, tail))
1122                })
1123                .max_by(|a, b| a.0.cmp(&b.0))
1124                .map(|(_, tail)| tail)
1125        };
1126
1127        if let Some(version) = from_ancestor
1128            .or(from_pkg_deps)
1129            .or(from_root)
1130            .or_else(from_graph_scan)
1131            .or(from_ancestor_incompatible)
1132            .or(from_pkg_deps_incompatible)
1133            .or(from_root_incompatible)
1134        {
1135            peer_context.push((peer_name.clone(), version));
1136        }
1137    }
1138    peer_context.sort_by(|a, b| a.0.cmp(&b.0));
1139
1140    // For the SUFFIX we build a cycle-broken copy: any peer value that
1141    // nests a reference back to the current package's canonical base
1142    // gets stripped to its plain version. Without this, mutual peer
1143    // cycles (a peers on b, b peers on a) grow the suffix one level
1144    // per iteration of the fixed-point loop and never converge.
1145    //
1146    // The non-cycle paths are untouched, so a regular nested chain
1147    // like `(react-dom@18.2.0(react@18.2.0))` still serializes fully.
1148    // We deliberately keep the full nested tails in `peer_context` for
1149    // downstream scope propagation and child lookups — suffix cycle-
1150    // breaking is cosmetic and should not change what packages exist
1151    // or which snapshot entries reference each other.
1152    //
1153    // Cycle detection is always done against the full `name@version`
1154    // canonical base — even when `dedupe-peers=true` is on, because
1155    // the version-only form is ambiguous (two unrelated packages at
1156    // the same version would false-positive). `dedupe-peers` is
1157    // applied as a post-pass over the final graph in
1158    // `dedupe_peer_suffixes` after cycle detection is done.
1159    let suffix: String = peer_context
1160        .iter()
1161        .map(|(n, v)| {
1162            let cycles_back = contains_canonical_back_ref(v, &canonical_base);
1163            let display_v = if cycles_back {
1164                canonical_tail(v).to_string()
1165            } else {
1166                v.clone()
1167            };
1168            format!("({n}@{display_v})")
1169        })
1170        .collect();
1171    // pnpm's `peersSuffixMaxLength`: when the built suffix exceeds the
1172    // cap, replace the entire suffix with `_<10-char-sha256-hex>` so the
1173    // lockfile key stays bounded. Matches pnpm's lockfile format, so
1174    // lockfiles shared between aube and pnpm stay comparable.
1175    let effective_suffix = if suffix.len() > options.peers_suffix_max_length {
1176        hash_peer_suffix(&suffix)
1177    } else {
1178        suffix
1179    };
1180    let contextualized = format!("{canonical_base}{effective_suffix}");
1181
1182    if out_packages.contains_key(&contextualized) || visiting.contains(&contextualized) {
1183        return Some(contextualized);
1184    }
1185    visiting.insert(contextualized.clone());
1186
1187    // Build the scope for P's children. This is ancestor_scope, overlaid
1188    // with P's own dependencies and its resolved peer map. Children see
1189    // their grandparents too — this mirrors pnpm's all-the-way-up peer
1190    // walk.
1191    //
1192    // We deliberately do NOT strip any existing peer-context suffix
1193    // off the tails we put into the scope. On the first pass the
1194    // values are plain (BFS output has no suffixes), so preserving
1195    // them is a no-op; on subsequent passes (see the fixed-point loop
1196    // in `apply_peer_contexts`) the input graph already carries
1197    // contextualized tails, and keeping them in scope is exactly how
1198    // nested peer suffixes propagate down to consumers — a package
1199    // that peers on `react-dom` and reaches it through a parent whose
1200    // `react-dom` entry is already `18.2.0(react@18.2.0)` will see
1201    // that nested tail in its own scope, and its own suffix will
1202    // serialize as `(react-dom@18.2.0(react@18.2.0))`. That's the
1203    // nested form pnpm writes.
1204    let mut child_scope = ancestor_scope.clone();
1205    for (name, version) in &pkg.dependencies {
1206        child_scope.insert(name.clone(), version.clone());
1207    }
1208    for (name, version) in &peer_context {
1209        child_scope.insert(name.clone(), version.clone());
1210    }
1211
1212    // Recurse into each child, rewriting its dependency map entry to
1213    // point at the contextualized dep_path's tail. A child whose visit
1214    // fails (orphaned / missing) keeps its own tail.
1215    //
1216    // For declared peer names, the peer context (filled from the
1217    // ancestor scope) is authoritative — we override whatever the BFS
1218    // peer walk auto-installed. Otherwise the snapshot suffix and the
1219    // actual wired `dependencies[peer]` could disagree, which made the
1220    // sibling symlink target inconsistent with the peer-context claim.
1221    // When the ancestor's version doesn't satisfy the declared range,
1222    // `detect_unmet_peers` will flag it as a warning after the pass.
1223    let peer_context_versions: FxHashMap<String, String> = peer_context.iter().cloned().collect();
1224
1225    let mut new_dependencies: BTreeMap<String, String> = BTreeMap::new();
1226    let mut visited_dep_names: FxHashSet<String> = FxHashSet::default();
1227
1228    for (child_name, child_version_tail) in &pkg.dependencies {
1229        // If this child is a declared peer, its tail comes from the
1230        // peer context (which may be nested). Otherwise we use the
1231        // tail we already have — also possibly nested on a 2nd pass.
1232        let lookup_tail = match peer_context_versions.get(child_name) {
1233            Some(v) => v.clone(),
1234            None => child_version_tail.clone(),
1235        };
1236        let child_canonical_dep_path = format!("{child_name}@{lookup_tail}");
1237        let child_new = visit_peer_context(
1238            &child_canonical_dep_path,
1239            graph,
1240            name_index,
1241            &child_scope,
1242            root_scope,
1243            out_packages,
1244            visiting,
1245            options,
1246        );
1247        let new_tail = match child_new {
1248            Some(new_dep_path) => new_dep_path
1249                .strip_prefix(&format!("{child_name}@"))
1250                .map(|s| s.to_string())
1251                .unwrap_or_else(|| lookup_tail.clone()),
1252            None => lookup_tail.clone(),
1253        };
1254        new_dependencies.insert(child_name.clone(), new_tail);
1255        visited_dep_names.insert(child_name.clone());
1256    }
1257
1258    // Peers that were satisfied purely from the ancestor scope may not
1259    // have been in `pkg.dependencies` at all (no auto-install needed).
1260    // Wire them as deps now so the linker creates the sibling symlink
1261    // and the lockfile snapshot records them.
1262    for (peer_name, peer_version) in &peer_context {
1263        if visited_dep_names.contains(peer_name) {
1264            continue;
1265        }
1266        let child_canonical_dep_path = format!("{peer_name}@{peer_version}");
1267        let child_new = visit_peer_context(
1268            &child_canonical_dep_path,
1269            graph,
1270            name_index,
1271            &child_scope,
1272            root_scope,
1273            out_packages,
1274            visiting,
1275            options,
1276        );
1277        if let Some(new_dep_path) = child_new {
1278            let new_tail = new_dep_path
1279                .strip_prefix(&format!("{peer_name}@"))
1280                .map(|s| s.to_string())
1281                .unwrap_or_else(|| peer_version.clone());
1282            new_dependencies.insert(peer_name.clone(), new_tail);
1283        }
1284    }
1285
1286    visiting.remove(&contextualized);
1287    let new_optional_dependencies: BTreeMap<String, String> = pkg
1288        .optional_dependencies
1289        .keys()
1290        .filter_map(|name| {
1291            new_dependencies
1292                .get(name)
1293                .map(|tail| (name.clone(), tail.clone()))
1294        })
1295        .collect();
1296
1297    out_packages.insert(
1298        contextualized.clone(),
1299        LockedPackage {
1300            name: pkg.name.clone(),
1301            version: pkg.version.clone(),
1302            integrity: pkg.integrity.clone(),
1303            dependencies: new_dependencies,
1304            optional_dependencies: new_optional_dependencies,
1305            peer_dependencies: pkg.peer_dependencies.clone(),
1306            peer_dependencies_meta: pkg.peer_dependencies_meta.clone(),
1307            dep_path: contextualized.clone(),
1308            local_source: pkg.local_source.clone(),
1309            os: pkg.os.clone(),
1310            cpu: pkg.cpu.clone(),
1311            libc: pkg.libc.clone(),
1312            bundled_dependencies: pkg.bundled_dependencies.clone(),
1313            tarball_url: pkg.tarball_url.clone(),
1314            alias_of: pkg.alias_of.clone(),
1315            yarn_checksum: pkg.yarn_checksum.clone(),
1316            engines: pkg.engines.clone(),
1317            bin: pkg.bin.clone(),
1318            declared_dependencies: pkg.declared_dependencies.clone(),
1319            license: pkg.license.clone(),
1320            funding_url: pkg.funding_url.clone(),
1321        },
1322    );
1323    Some(contextualized)
1324}