aube_resolver/resolve.rs
1use crate::local_source::{
2 dep_path_for, is_non_registry_specifier, read_local_manifest, rebase_local, resolve_git_source,
3 resolve_remote_tarball, should_block_exotic_subdep,
4};
5use crate::package_ext::{apply_package_extensions, pick_override_spec};
6use crate::semver_util::{PickResult, pick_version, version_satisfies};
7use crate::{
8 Error, ExoticSubdepDetails, PeerContextOptions, ResolutionMode, ResolveTask, ResolvedPackage,
9 Resolver, apply_peer_contexts, catalog, error, hoist_auto_installed_peers,
10 is_deprecation_allowed, is_supported,
11};
12use crate::{FxHashMap, FxHashSet};
13use aube_lockfile::{DepType, DirectDep, LocalSource, LockedPackage, LockfileGraph};
14use aube_manifest::PackageJson;
15use aube_registry::Packument;
16use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
17use std::sync::Arc;
18
19impl Resolver {
20 /// Resolve all dependencies from a package.json.
21 ///
22 /// Uses batch-parallel BFS: each "wave" drains the queue, identifies
23 /// uncached package names, fetches their packuments concurrently, then
24 /// processes the entire batch before starting the next wave.
25 pub async fn resolve(
26 &mut self,
27 manifest: &PackageJson,
28 existing: Option<&LockfileGraph>,
29 ) -> Result<LockfileGraph, Error> {
30 self.resolve_workspace(
31 &[(".".to_string(), manifest.clone())],
32 existing,
33 &HashMap::new(),
34 )
35 .await
36 }
37
38 /// Resolve all dependencies for a workspace (multiple importers).
39 ///
40 /// `manifests` is a list of (importer_path, PackageJson) — e.g. (".", root), ("packages/app", app).
41 /// `workspace_packages` maps package name → version. Used both for
42 /// explicit `workspace:` protocol resolution and for yarn/npm/bun
43 /// style linkage where a bare semver range on a workspace-package
44 /// name resolves to the local copy when its version satisfies the
45 /// range.
46 pub async fn resolve_workspace(
47 &mut self,
48 manifests: &[(String, PackageJson)],
49 existing: Option<&LockfileGraph>,
50 workspace_packages: &HashMap<String, String>,
51 ) -> Result<LockfileGraph, Error> {
52 let resolve_start = std::time::Instant::now();
53 let mut packument_fetch_count = 0u32;
54 let mut packument_fetch_time = std::time::Duration::ZERO;
55 let mut lockfile_reuse_count = 0u32;
56 let mut resolved: BTreeMap<String, LockedPackage> = BTreeMap::new();
57 // 1024 covers typical monorepo. 5000-dep graphs take one grow.
58 let mut resolved_versions: FxHashMap<String, Vec<String>> =
59 FxHashMap::with_capacity_and_hasher(1024, Default::default());
60 let mut importers: BTreeMap<String, Vec<DirectDep>> = BTreeMap::new();
61 let mut queue: VecDeque<ResolveTask> = VecDeque::with_capacity(512);
62 let mut visited: FxHashSet<std::sync::Arc<str>> =
63 FxHashSet::with_capacity_and_hasher(2048, Default::default());
64 // Round-tripped to the lockfile's top-level `time:` block so
65 // subsequent installs can reuse them for the cutoff computation.
66 // Populated opportunistically from whatever packuments we fetch:
67 // empty when the metadata omits `time` (corgi from npmjs.org in
68 // default mode), filled when it doesn't (Verdaccio, or the
69 // full-packument path taken for time-based resolution and
70 // `minimumReleaseAge`). This matches pnpm's `publishedAt` wiring.
71 let mut resolved_times: BTreeMap<String, String> = BTreeMap::new();
72 // Per-importer record of optionals the resolver intentionally
73 // dropped on this run — either filtered by os/cpu/libc or
74 // named in `pnpm.ignoredOptionalDependencies`. Round-tripped
75 // through the lockfile so drift detection on subsequent
76 // installs can distinguish "previously skipped" from "newly
77 // added by the user".
78 let mut skipped_optional_dependencies: BTreeMap<String, BTreeMap<String, String>> =
79 BTreeMap::new();
80 // Catalog picks gathered as the BFS rewrites `catalog:` task
81 // ranges. Outer key: catalog name. Inner: package name → spec.
82 // Resolved versions are filled in post-resolution by walking
83 // `resolved_versions` for the spec, since the picked version is
84 // an output the BFS doesn't know until version_satisfies fires.
85 let mut catalog_picks: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();
86 let importer_declared_dep_names: BTreeMap<String, BTreeSet<String>> = manifests
87 .iter()
88 .map(|(importer_path, manifest)| {
89 let names = manifest
90 .dependencies
91 .keys()
92 .chain(manifest.dev_dependencies.keys())
93 .chain(manifest.optional_dependencies.keys())
94 .cloned()
95 .collect();
96 (importer_path.clone(), names)
97 })
98 .collect();
99 // ISO-8601 UTC cutoff string. npm's registry `time` map uses
100 // `Z`-suffixed UTC timestamps throughout, which sort
101 // lexicographically — so a raw `String` doubles as a
102 // comparable instant without pulling in a date library.
103 //
104 // Two independent features feed this cutoff:
105 // - `minimum_release_age` (pnpm v11 default, supply-chain
106 // mitigation): seeded *before* wave 0 so even direct deps
107 // are filtered. The exclude list and strict-mode behavior
108 // are scoped per-package by `pick_version` below.
109 // - `resolution-mode=time-based`: derived from the max
110 // publish time across direct deps once wave 0 finishes,
111 // then constrains transitives only.
112 // When both are configured, the resolver carries both cutoffs
113 // and the picker takes the more restrictive (earlier) one.
114 let mut published_by: Option<String> =
115 self.minimum_release_age.as_ref().and_then(|m| m.cutoff());
116 if let Some(c) = published_by.as_deref() {
117 tracing::debug!("minimumReleaseAge cutoff: {}", c);
118 }
119
120 seed_direct_deps(
121 manifests,
122 &self.ignored_optional_dependencies,
123 &mut queue,
124 &mut importers,
125 );
126
127 // Pipelined resolver state. The resolver is strictly serial in
128 // its *processing* order (tasks are popped and version-picked
129 // in seed/BFS order, which is what keeps the output lockfile
130 // byte-deterministic across runs) but fetches run freely in
131 // the background via `in_flight`. When a popped task's
132 // packument isn't in the cache, the main loop waits inline on
133 // `in_flight.join_next()` — harvesting whatever other fetches
134 // happen to land in the meantime — until this task's
135 // packument is available. Because `ensure_fetch!` is called
136 // speculatively at every enqueue site, by the time a task is
137 // popped its packument is usually already cached, so the
138 // wait is short.
139 /*
140 * Adaptive packument concurrency. Loaded from the cross run
141 * persistent store when available so the limiter resumes
142 * the converged operating point of the previous run instead
143 * of cold ramping. Falls back to seed 256 (h2 stream cap)
144 * on a fresh install. The CUSUM gated AIMD controller in
145 * `aube_util::adaptive` shrinks on real back pressure
146 * (HTTP 429 / 503 / timeout) and on sustained latency
147 * regime rise. Floor 4 keeps progress under continuous
148 * throttling.
149 */
150 // User-configured `networkConcurrency` (or `env_concurrency`)
151 // is honored as the seed: it's the operating cap they
152 // explicitly chose for their environment (constrained CI
153 // runner, private registry rate-limit, fat residential
154 // pipe). Adaptive shrink/grow still kicks in around it.
155 // Floor stays at 4 so even an over-aggressive user value
156 // can't deadlock progress on continuous throttling.
157 let packument_seed = self.packument_network_concurrency.unwrap_or(256).max(4);
158 let packument_max = packument_seed.max(256);
159 let persistent = aube_util::adaptive::global_persistent_state();
160 let shared_semaphore = match persistent.as_ref() {
161 Some(state) => aube_util::adaptive::AdaptiveLimit::from_persistent(
162 state,
163 "packument:default",
164 packument_seed,
165 4,
166 packument_max,
167 ),
168 None => aube_util::adaptive::AdaptiveLimit::new(packument_seed, 4, packument_max),
169 };
170 let packument_persist_handle = persistent
171 .as_ref()
172 .map(|p| (Arc::clone(p), Arc::clone(&shared_semaphore)));
173 // Time-based mode and `minimumReleaseAge` both need the
174 // packument's `time:` map. The abbreviated (corgi) response
175 // omits `time` by default, so we normally fall back to the
176 // full packument. `registry-supports-time-field=true` flips
177 // that: the user is asserting the configured registry ships
178 // `time` in corgi too (Verdaccio 5.15.1+, JSR, etc.), so the
179 // cheaper abbreviated path stays on the hot path and we save
180 // one full-packument fetch per distinct package.
181 let needs_time = (self.resolution_mode == ResolutionMode::TimeBased
182 || self.minimum_release_age.is_some()
183 || self.dependency_policy.trust_policy == crate::TrustPolicy::NoDowngrade)
184 && !self.registry_supports_time_field;
185 // When time data is required, fetch the full packument directly.
186 // The previous corgi-first shortcut saved bytes for old packages
187 // but cost an extra round trip for active packages whose top-level
188 // `modified` timestamp was newer than the cutoff. Clean installs of
189 // modern dependency graphs are dominated by those active packages.
190
191 // In-flight packument fetches. The spawned task returns the
192 // `(name, packument, from_primer)` tuple so `join_next` gives
193 // us back the identity of whichever fetch landed next without
194 // a side table lookup. `from_primer` matters because the
195 // bundled primer intentionally keeps only a capped slice of
196 // high-traffic package histories; a range miss against that
197 // slice must fall through to the live registry before we
198 // report `ERR_AUBE_NO_MATCHING_VERSION`.
199 #[allow(clippy::type_complexity)]
200 let mut in_flight: tokio::task::JoinSet<Result<(String, Packument, bool), Error>> =
201 tokio::task::JoinSet::new();
202 // Names whose fetch has been spawned but not yet harvested.
203 // Dedupes spawn calls when multiple tasks discover the same
204 // transitive before any of them has been processed.
205 let mut in_flight_names: FxHashSet<String> = FxHashSet::default();
206 let mut primer_seeded_names: FxHashSet<String> = FxHashSet::default();
207 // TimeBased wave-0 gate: the publish-time cutoff is derived
208 // from the direct deps' resolved versions, so transitives
209 // that reach the version-pick step before all directs have
210 // completed must wait. Populated only when
211 // `cutoff_pending == true` (TimeBased mode); `Highest` mode
212 // leaves these at their defaults and the gate is a no-op.
213 let mut direct_deps_pending: usize = queue.len();
214 let mut cutoff_pending = self.resolution_mode == ResolutionMode::TimeBased;
215 let mut deferred_transitives: Vec<ResolveTask> = Vec::new();
216
217 // Set of names present in the existing lockfile. Used as a
218 // prefetch gate: names the lockfile already covers will hit
219 // the lockfile-reuse path and don't need their packuments
220 // fetched, so prefetching them is wasted tokio-spawn
221 // overhead. Load-bearing for `aube add` and
222 // frozen-lockfile-install scenarios where most tasks go
223 // through lockfile-reuse.
224 //
225 // This is strictly a *prefetch* gate, not a correctness
226 // gate: a task that fails sibling dedupe AND lockfile reuse
227 // (because its range doesn't match any of the lockfile's
228 // versions for that name) still needs a fresh fetch, and
229 // the wait-for-fetch loop below calls `ensure_fetch!`
230 // without consulting `existing_names`.
231 // Borrow names from `existing` instead of cloning. The set
232 // lives only inside `Resolver::resolve` and the prior
233 // lockfile graph outlives it. Skips 5000 String allocations
234 // on a 5000-pkg lockfile at resolve-entry.
235 let existing_names: FxHashSet<&str> = existing
236 .map(|g| g.packages.values().map(|p| p.name.as_str()).collect())
237 .unwrap_or_default();
238
239 // Spawn a packument fetch into `in_flight` if one isn't
240 // already running for `name` and the packument isn't
241 // already cached. Gated *only* on in-flight + cache —
242 // callers that want to skip prefetching names already
243 // covered by the lockfile check `existing_names` explicitly
244 // before invoking the macro.
245 macro_rules! ensure_fetch {
246 ($name:expr) => {{
247 let name: &str = $name;
248 if !in_flight_names.contains(name) && !self.cache.contains_key(name) {
249 let name_owned = name.to_string();
250 in_flight_names.insert(name_owned.clone());
251 let client = self.client.clone();
252 let cache_dir = self.packument_cache_dir.clone();
253 let full_cache_dir = self.packument_full_cache_dir.clone();
254 let minimum_release_age_excludes_name = self
255 .minimum_release_age
256 .as_ref()
257 .is_some_and(|mra| mra.exclude.contains(name));
258 let primer_covers_cutoff = minimum_release_age_excludes_name
259 || published_by
260 .as_deref()
261 .is_none_or(crate::primer::covers_cutoff);
262 let use_metadata_primer = (self.force_metadata_primer
263 || client.uses_default_npm_registry_for(&name_owned))
264 && primer_covers_cutoff;
265 let force_metadata_primer = self.force_metadata_primer;
266 let sem = shared_semaphore.clone();
267 in_flight.spawn(async move {
268 let _diag_span = aube_util::diag::Span::new(
269 aube_util::diag::Category::Resolver,
270 "packument_fetch",
271 )
272 .with_meta_fn(|| {
273 format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&name_owned))
274 });
275 let _diag_inflight = aube_util::diag::inflight(aube_util::diag::Slot::Pack);
276 let permit_wait = std::time::Instant::now();
277 let permit = sem.acquire().await;
278 let permit_wait_ms = permit_wait.elapsed();
279 if permit_wait_ms.as_millis() > 1 {
280 aube_util::diag::event_lazy(
281 aube_util::diag::Category::Resolver,
282 "packument_permit_wait",
283 permit_wait_ms,
284 || format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&name_owned)),
285 );
286 }
287 aube_util::diag::attribute_wait(
288 aube_util::diag::Slot::Pack,
289 &name_owned,
290 permit_wait_ms,
291 );
292 let _holder_guard = aube_util::diag::register_holder(
293 aube_util::diag::Slot::Pack,
294 &name_owned,
295 );
296 let mut cached = if needs_time {
297 match full_cache_dir.as_ref() {
298 Some(dir) => client.cached_full_packument_lookup(&name_owned, dir),
299 None => Default::default(),
300 }
301 } else if let Some(ref dir) = cache_dir {
302 client.cached_packument_lookup(&name_owned, dir)
303 } else {
304 Default::default()
305 };
306 if let Some(packument) = cached.packument.take() {
307 aube_util::diag::instant_lazy(
308 aube_util::diag::Category::Resolver,
309 "packument_disk_hit",
310 || format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&name_owned)),
311 );
312 permit.record_cancelled();
313 return Ok::<_, Error>((name_owned, packument, false));
314 }
315 if use_metadata_primer
316 && !cached.stale
317 && let Some(seed) = crate::primer::get(&name_owned)
318 {
319 let mut packument = seed.packument();
320 if force_metadata_primer {
321 for version in packument.versions.values_mut() {
322 let tarball =
323 client.tarball_url(&version.name, &version.version);
324 version.dist = version.dist.take().map(|mut dist| {
325 dist.tarball = tarball;
326 dist
327 });
328 }
329 }
330 if needs_time {
331 if let Some(dir) = full_cache_dir.as_ref() {
332 client.seed_full_packument_cache(
333 &name_owned,
334 dir,
335 &packument,
336 seed.etag.as_deref(),
337 seed.last_modified.as_deref(),
338 false,
339 );
340 }
341 } else if let Some(dir) = cache_dir.as_ref() {
342 client.seed_packument_cache(
343 &name_owned,
344 dir,
345 &packument,
346 seed.etag.as_deref(),
347 seed.last_modified.as_deref(),
348 false,
349 );
350 }
351 aube_util::diag::instant_lazy(
352 aube_util::diag::Category::Resolver,
353 "packument_primer_hit",
354 || format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&name_owned)),
355 );
356 permit.record_cancelled();
357 return Ok::<_, Error>((name_owned, packument, true));
358 }
359 let fetch_outcome = if needs_time {
360 match full_cache_dir.as_ref() {
361 Some(dir) => {
362 client
363 .fetch_packument_with_time_cached_after_lookup(
364 &name_owned,
365 dir,
366 cached,
367 )
368 .await
369 }
370 None => client.fetch_packument(&name_owned).await,
371 }
372 } else if let Some(ref dir) = cache_dir {
373 client
374 .fetch_packument_cached_after_lookup(&name_owned, dir, cached)
375 .await
376 } else {
377 client.fetch_packument(&name_owned).await
378 };
379 let packument = match fetch_outcome {
380 Ok(p) => {
381 permit.record_success();
382 p
383 }
384 Err(e) => {
385 if e.is_throttle() {
386 permit.record_throttle();
387 } else {
388 permit.record_cancelled();
389 }
390 return Err(Error::Registry(name_owned.clone(), e.to_string()));
391 }
392 };
393 aube_util::diag::instant_lazy(
394 aube_util::diag::Category::Resolver,
395 "packument_network_hit",
396 || format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&name_owned)),
397 );
398 Ok::<_, Error>((name_owned, packument, false))
399 });
400 }
401 }};
402 }
403
404 // Decrement the pending-directs counter when a root task
405 // reaches a terminal state. Used by the TimeBased cutoff
406 // trigger at the top of the outer loop.
407 macro_rules! note_root_done {
408 () => {
409 if direct_deps_pending > 0 {
410 direct_deps_pending -= 1;
411 }
412 };
413 }
414
415 // `(name, range)` is safe to speculatively prefetch against
416 // the registry when:
417 //
418 // - The range isn't a protocol we rewrite in preprocessing
419 // (`workspace:` / `catalog:` / `npm:` alias) — for those
420 // we don't know the real package name yet, so fetching
421 // the raw task name is either useless (preprocessing
422 // won't go through the registry at all) or wrong (we'd
423 // fetch the alias key instead of the real package).
424 // - The range isn't a `file:` / `link:` / `git:` /
425 // remote-tarball spec (covered by
426 // `is_non_registry_specifier`).
427 // - The name isn't in the overrides map — an override can
428 // rewrite the range into any of the above, and we can't
429 // cheaply tell whether it will, so be conservative.
430 //
431 // Called both from the upfront prefetch loop over seeded
432 // root deps *and* from the three transitive-enqueue sites
433 // inside the version-pick body, where the same class of
434 // unsafe specs can arrive via a published package's
435 // `dependencies` / `optionalDependencies` / `peerDependencies`
436 // maps (real-world case: a package whose dependency entry
437 // is an npm alias).
438 macro_rules! prefetchable {
439 ($name:expr, $range:expr) => {{
440 let r: &str = $range;
441 let n: &str = $name;
442 // A bare semver range that matches a workspace package
443 // will resolve to the workspace without ever reading
444 // the packument, so prefetching would just be a
445 // speculative 404 on e.g. an unpublished monorepo
446 // package.
447 let workspace_hit = workspace_packages
448 .get(n)
449 .is_some_and(|ws_v| version_satisfies(ws_v, r));
450 !aube_util::pkg::is_workspace_spec(r)
451 && !aube_util::pkg::is_catalog_spec(r)
452 && !aube_util::pkg::is_npm_spec(r)
453 && !aube_util::pkg::is_jsr_spec(r)
454 && !is_non_registry_specifier(r)
455 && !self.overrides.contains_key(n)
456 && !workspace_hit
457 }};
458 }
459
460 // Fire prefetches for every seeded root dep up front, so
461 // their packuments are already in flight by the time the
462 // first task is popped. Skip lockfile-covered names —
463 // they'll hit the lockfile-reuse path and never need their
464 // packuments — and anything `prefetchable!` rejects.
465 for task in queue.iter() {
466 if !prefetchable!(task.name.as_str(), task.range.as_str()) {
467 continue;
468 }
469 if existing_names.contains(task.name.as_str()) {
470 continue;
471 }
472 ensure_fetch!(&task.name);
473 }
474
475 'outer: loop {
476 // TimeBased cutoff trigger. Fires the first time
477 // `direct_deps_pending` hits zero with the cutoff still
478 // pending — at which point every direct dep has been
479 // version-picked (or terminated in preprocessing),
480 // `resolved_times` holds their publish times, and we can
481 // derive the max to seed `published_by` for the
482 // transitives we deferred.
483 if cutoff_pending && direct_deps_pending == 0 {
484 let direct_dep_paths: FxHashSet<&String> = importers
485 .values()
486 .flat_map(|deps| deps.iter().map(|d| &d.dep_path))
487 .collect();
488 let mut max_time: Option<&String> = None;
489 for (dep_path, t) in resolved_times.iter() {
490 if !direct_dep_paths.contains(dep_path) {
491 continue;
492 }
493 if max_time.map(|m| t > m).unwrap_or(true) {
494 max_time = Some(t);
495 }
496 }
497 if let Some(existing_graph) = existing {
498 for (dep_path, t) in &existing_graph.times {
499 if !direct_dep_paths.contains(dep_path) {
500 continue;
501 }
502 if max_time.map(|m| t > m).unwrap_or(true) {
503 max_time = Some(t);
504 }
505 }
506 }
507 if let Some(m) = max_time {
508 tracing::debug!("time-based resolution cutoff: {}", m);
509 published_by = Some(match published_by.take() {
510 Some(existing) if existing.as_str() < m.as_str() => existing,
511 _ => m.clone(),
512 });
513 }
514 cutoff_pending = false;
515 queue.extend(deferred_transitives.drain(..));
516 }
517
518 let Some(mut task) = queue.pop_front() else {
519 if !deferred_transitives.is_empty() {
520 return Err(Error::Registry(
521 "(resolver)".to_string(),
522 format!(
523 "{} transitives still deferred when resolve completed",
524 deferred_transitives.len()
525 ),
526 ));
527 }
528 break 'outer;
529 };
530
531 // Body of the former per-task preprocessing loop.
532 // The old wave-based code split this into a
533 // preprocessing pass and a post-fetch version-pick
534 // pass with a fetch barrier between them. Here both
535 // passes run inline for a single task: preprocess →
536 // sibling dedupe → lockfile reuse → wait on this
537 // task's packument → version-pick → enqueue
538 // transitives. The bare block keeps the original
539 // indentation so the diff stays readable against the
540 // prior shape; `continue` inside it still continues
541 // the 'outer loop because a bare block is not itself
542 // a loop.
543 {
544 // Apply bare-name overrides + npm-alias rewrites in a
545 // small fixed-point loop. Two interleavings need to
546 // work simultaneously:
547 // 1. The override *value* is itself a `npm:` alias
548 // (e.g. `"foo": "npm:bar@^2"`). The first override
549 // pass rewrites `task.range`; the alias pass then
550 // rewrites `task.name` to `bar`.
551 // 2. The user's *declared dep* is an `npm:` alias
552 // (e.g. `"foo": "npm:bar@^1"`) and the override
553 // targets the real package (`"overrides":
554 // {"bar": "2.0.0"}`). The first override pass
555 // misses (`task.name` is still `foo`), the alias
556 // pass rewrites `task.name = "bar"`, and the
557 // second override pass catches it.
558 // A two-iteration cap is enough — after one alias
559 // rewrite the name is canonical, and an override that
560 // points at a third package is itself constrained by
561 // the same rule, so there's no infinite chain.
562 //
563 // We deliberately don't touch `original_specifier`,
564 // since the lockfile/importer record should still
565 // reflect what the user wrote in package.json —
566 // overrides are a graph-shaping rule, not a rewrite of
567 // the user's declared deps.
568 // Catalog protocol: rewrite `catalog:` and
569 // `catalog:<name>` to the workspace catalog's actual
570 // range *before* the override loop, so overrides can
571 // still target a catalog dep by bare name. The original
572 // `catalog:...` text stays in `original_specifier` so
573 // the lockfile importer keeps the catalog reference and
574 // drift detection works.
575 if let Some((catalog_name, real_range)) =
576 self.resolve_catalog_spec(&task.name, &task.range)?
577 {
578 tracing::trace!("catalog: {} {} -> {}", task.name, task.range, real_range);
579 catalog_picks
580 .entry(catalog_name)
581 .or_default()
582 .insert(task.name.clone(), real_range.clone());
583 task.range = real_range;
584 }
585
586 for _ in 0..2 {
587 let mut changed = false;
588 if let Some(override_spec) = pick_override_spec(
589 &self.override_rules,
590 &task.name,
591 &task.range,
592 &task.ancestors,
593 ) {
594 // pnpm's removal marker: an override value of
595 // `"-"` drops the dep edge entirely. Skip before
596 // catalog/alias rewrites so `-` never reaches
597 // the registry resolver. The dropped edge never
598 // gets written to the parent's `.dependencies`
599 // map (that write happens downstream) and, for
600 // direct deps, never gets pushed into the
601 // importer's direct-dep list.
602 if override_spec == "-" {
603 tracing::trace!("override: {}@{} -> dropped", task.name, task.range,);
604 if task.is_root {
605 note_root_done!();
606 }
607 continue 'outer;
608 }
609 // An override may itself point at a catalog
610 // entry (e.g. `"overrides": {"foo": "catalog:"}`).
611 // The catalog pre-pass above already ran against
612 // the original range, so resolve the indirection
613 // here before assigning — otherwise `catalog:`
614 // leaks through to the registry resolver.
615 // Stash the catalog pick in a local so we only
616 // record it if the override actually moves
617 // `task.range`.
618 let (effective_spec, pending_pick) =
619 match self.resolve_catalog_spec(&task.name, &override_spec)? {
620 Some((catalog_name, real_range)) => {
621 (real_range.clone(), Some((catalog_name, real_range)))
622 }
623 None => (override_spec, None),
624 };
625 if task.range != effective_spec {
626 if let Some((catalog_name, real_range)) = pending_pick {
627 catalog_picks
628 .entry(catalog_name)
629 .or_default()
630 .insert(task.name.clone(), real_range);
631 }
632 tracing::trace!(
633 "override: {}@{} -> {}",
634 task.name,
635 task.range,
636 effective_spec
637 );
638 // Overrides are declared at the project root,
639 // so a substituted `link:./libs/x` /
640 // `file:./vendor/y` path is project-root-
641 // relative — never importer- or parent-
642 // relative. Mark the task so the local-source
643 // branch anchors the path correctly even when
644 // the consumer is a workspace pkg or a nested
645 // local parent.
646 if is_non_registry_specifier(&effective_spec) {
647 task.range_from_override = true;
648 }
649 task.range = effective_spec;
650 // If the override replaced the spec with a
651 // bare range (not itself an `npm:` / `jsr:`
652 // alias), it's targeting `task.name` —
653 // implicitly undoing any prior alias
654 // rewrite. Without this, an override that
655 // fires after a catalog-aliased entry
656 // (e.g. catalog `js-yaml:
657 // npm:@zkochan/js-yaml@0.0.11`, override
658 // `js-yaml@<3.14.2: ^3.14.2`) would keep
659 // `task.real_name = @zkochan/js-yaml` and
660 // try to fetch `^3.14.2` from a packument
661 // that only carries `0.0.x`. If the
662 // override's value is itself an alias, the
663 // alias pass below picks up the new target
664 // on the next loop iteration.
665 if task.real_name.is_some()
666 && !task.range.starts_with("npm:")
667 && !task.range.starts_with("jsr:")
668 {
669 task.real_name = None;
670 }
671 changed = true;
672 }
673 }
674 if let Some(rest) = task.range.strip_prefix("npm:")
675 && let Some(at_idx) = rest.rfind('@')
676 {
677 let real_name = rest[..at_idx].to_string();
678 let real_range = rest[at_idx + 1..].to_string();
679 // Keep `task.name` as the user-facing alias
680 // (the key the package.json used) and stash
681 // the registry name on `real_name` so every
682 // identity-facing site — dep_path formation,
683 // direct-dep records, parent wiring — sees
684 // the alias, while only packument/tarball
685 // fetch sites (via `task.registry_name()`)
686 // hit the real package. Overwriting
687 // `task.name` here would collapse
688 // `node_modules/h3-v2/` to `node_modules/h3/`
689 // and any `require("h3-v2")` would break.
690 if task.real_name.as_deref() != Some(real_name.as_str())
691 || real_range != task.range
692 {
693 tracing::trace!(
694 "npm alias: {} -> {}@{}",
695 task.name,
696 real_name,
697 real_range
698 );
699 task.real_name = Some(real_name);
700 task.range = real_range;
701 changed = true;
702 }
703 }
704 // `jsr:<range>` and `jsr:<@scope/name>[@<range>]` both
705 // land here. JSR's npm-compat endpoint serves every
706 // package under `@jsr/<scope>__<name>`, but the
707 // user-facing dependency name stays the JSR name (or
708 // explicit alias) from package.json. Keep `task.name`
709 // unchanged for dep_path/importer/link identity and
710 // stash the npm-compat name in `real_name`, matching
711 // the npm-alias path above. Only registry IO should
712 // see `@jsr/...`.
713 if let Some(rest) = task.range.strip_prefix("jsr:") {
714 let (jsr_name_raw, jsr_range) = if let Some(body) = rest.strip_prefix('@') {
715 match body.rfind('@') {
716 Some(rel_at) => {
717 // Indices are relative to `body`; add 1 for
718 // the `@` we just stripped so we can slice
719 // against the original `rest`.
720 let at_idx = rel_at + 1;
721 (rest[..at_idx].to_string(), rest[at_idx + 1..].to_string())
722 }
723 None => (rest.to_string(), "latest".to_string()),
724 }
725 } else {
726 // Bare range form — the manifest key carries the
727 // JSR name (e.g. `"@std/collections": "jsr:^1"`).
728 (task.name.clone(), rest.to_string())
729 };
730 match aube_registry::jsr::jsr_to_npm_name(&jsr_name_raw) {
731 Some(npm_name) => {
732 if task.real_name.as_deref() != Some(npm_name.as_str())
733 || jsr_range != task.range
734 {
735 tracing::trace!(
736 "jsr: {} -> {}@{}",
737 task.name,
738 npm_name,
739 jsr_range,
740 );
741 task.real_name = Some(npm_name);
742 task.range = jsr_range;
743 changed = true;
744 }
745 }
746 None => {
747 return Err(Error::Registry(
748 task.name.clone(),
749 format!(
750 "invalid jsr: spec `{}` — expected `jsr:@scope/name[@range]`",
751 task.range,
752 ),
753 ));
754 }
755 }
756 }
757 if !changed {
758 break;
759 }
760 }
761
762 // Handle file: / link: / git: protocols — the dep points
763 // at a path on disk or a remote git repo rather than a
764 // registry package. Root deps anchor on the importer's
765 // directory; transitive `link:`/`file:` deps anchor on
766 // the parent package's source root, but only when the
767 // parent itself was a `file:`/`link:` source (a workspace
768 // sibling or a directly-linked local dir). Registry-
769 // hosted parents have no on-disk source to resolve a
770 // relative path against, so transitive `link:`/`file:`
771 // from them stays an error.
772 if is_non_registry_specifier(&task.range) {
773 // Root-declared `pnpm.overrides` opts the user into
774 // the rewritten `link:`/`file:` target by name, so
775 // they bypass the exotic-subdep block — otherwise
776 // an override aimed at a transitive of a registry
777 // package would always lose to the default-on
778 // guard.
779 if !task.range_from_override
780 && should_block_exotic_subdep(
781 &task,
782 &resolved,
783 self.dependency_policy.block_exotic_subdeps,
784 )
785 {
786 return Err(Error::BlockedExoticSubdep(Box::new(ExoticSubdepDetails {
787 name: task.name.clone(),
788 spec: task.range.clone(),
789 parent: task
790 .parent
791 .clone()
792 .unwrap_or_else(|| "<unknown>".to_string()),
793 ancestors: task.ancestors.clone(),
794 importer: task.importer.clone(),
795 })));
796 }
797 // Pull the parent's on-disk source root, when the
798 // parent is a Directory/Link source. The BFS always
799 // inserts a parent into `resolved` before enqueuing
800 // its children, so for transitive tasks the parent
801 // record is reliably present here.
802 let parent_source_root: Option<std::path::PathBuf> = (!task.is_root)
803 .then(|| {
804 task.parent
805 .as_ref()
806 .and_then(|dp| resolved.get(dp))
807 .and_then(|pkg| pkg.local_source.as_ref())
808 .and_then(|src| match src {
809 LocalSource::Directory(p) | LocalSource::Link(p) => {
810 Some(self.project_root.join(p))
811 }
812 _ => None,
813 })
814 })
815 .flatten();
816 // Override-substituted link:/file: paths are
817 // project-root-relative regardless of where the
818 // consumer lives — pin them at the root before any
819 // importer/parent fallback wins.
820 let importer_root = if task.range_from_override {
821 self.project_root.clone()
822 } else {
823 parent_source_root.clone().unwrap_or_else(|| {
824 if task.importer == "." {
825 self.project_root.clone()
826 } else {
827 self.project_root.join(&task.importer)
828 }
829 })
830 };
831 let Some(raw_local) = LocalSource::parse(&task.range, &importer_root) else {
832 return Err(Error::Registry(
833 task.name.clone(),
834 format!("unparseable local specifier: {}", task.range),
835 ));
836 };
837 // Git and remote-tarball specifiers don't reference
838 // a path, so they pass through regardless of parent
839 // shape. `link:`/`file:` transitives only resolve
840 // when we either (a) located a parent source root
841 // or (b) inherited the path from a project-root-
842 // anchored override.
843 if !task.is_root
844 && parent_source_root.is_none()
845 && !task.range_from_override
846 && matches!(
847 raw_local,
848 LocalSource::Directory(_)
849 | LocalSource::Tarball(_)
850 | LocalSource::Link(_)
851 )
852 {
853 return Err(Error::Registry(
854 task.name.clone(),
855 format!(
856 "transitive local specifier {} cannot be resolved without the parent package source root",
857 task.range
858 ),
859 ));
860 }
861 let (local, real_version, target_deps) = if let LocalSource::Git(ref g) =
862 raw_local
863 {
864 let shallow = aube_store::git_host_in_list(&g.url, &self.git_shallow_hosts);
865 let (resolved_local, version, deps) =
866 resolve_git_source(&task.name, g, shallow, Some(self.client.as_ref()))
867 .await
868 .map_err(|e| {
869 Error::Registry(
870 task.name.clone(),
871 format!("git resolve {}: {e}", task.range),
872 )
873 })?;
874 (resolved_local, version, deps)
875 } else if let LocalSource::RemoteTarball(ref t) = raw_local {
876 let (resolved_local, version, deps) =
877 resolve_remote_tarball(&task.name, t, self.client.as_ref())
878 .await
879 .map_err(|e| {
880 Error::Registry(
881 task.name.clone(),
882 format!("remote tarball {}: {e}", task.range),
883 )
884 })?;
885 (resolved_local, version, deps)
886 } else {
887 // Rewrite the path to be relative to the
888 // project root so every downstream consumer
889 // can resolve it with a single
890 // `project_root.join(rel)`.
891 let local = rebase_local(&raw_local, &importer_root, &self.project_root);
892 let (_target_name, version, deps) =
893 read_local_manifest(&raw_local, &importer_root).unwrap_or_else(|_| {
894 (task.name.clone(), "0.0.0".to_string(), BTreeMap::new())
895 });
896 (local, version, deps)
897 };
898 let dep_path = local.dep_path(&task.name);
899 let linked_name = task.name.clone();
900
901 if task.is_root
902 && let Some(deps) = importers.get_mut(&task.importer)
903 {
904 deps.push(DirectDep {
905 name: task.name.clone(),
906 dep_path: dep_path.clone(),
907 dep_type: task.dep_type,
908 specifier: task.original_specifier.clone(),
909 });
910 }
911
912 // Wire parent -> this exotic transitive. Without
913 // this, the parent snapshot's `dependencies` map
914 // omits the git/url/file subdep entirely, so the
915 // linker never creates the sibling symlink inside
916 // the parent's node_modules and the package fails
917 // to resolve at runtime. The value is the dep_path
918 // tail (e.g. `git+<hash>`) so the linker can
919 // reconstruct the full dep_path by concatenating
920 // `{name}@{value}` — matching the key format used
921 // when inserting the resolved package below.
922 if let Some(ref parent_dp) = task.parent
923 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
924 {
925 // `local.dep_path(name)` always returns
926 // `{name}@{tail}`; if that invariant ever
927 // breaks we'd silently store a malformed dep
928 // value that the pnpm writer would emit as-is.
929 let name_prefix = format!("{}@", task.name);
930 debug_assert!(
931 dep_path.starts_with(&name_prefix),
932 "local.dep_path returned {dep_path:?} without expected prefix {name_prefix:?}"
933 );
934 let dep_tail = dep_path
935 .strip_prefix(&name_prefix)
936 .unwrap_or(&dep_path)
937 .to_string();
938 parent_pkg
939 .dependencies
940 .insert(task.name.clone(), dep_tail.clone());
941 if task.dep_type == DepType::Optional {
942 parent_pkg
943 .optional_dependencies
944 .insert(task.name.clone(), dep_tail);
945 }
946 }
947
948 if visited.insert(std::sync::Arc::from(dep_path.as_str())) {
949 resolved.insert(
950 dep_path.clone(),
951 LockedPackage {
952 name: linked_name.clone(),
953 version: real_version.clone(),
954 dep_path: dep_path.clone(),
955 local_source: Some(local.clone()),
956 ..Default::default()
957 },
958 );
959 if let Some(ref tx) = self.resolved_tx {
960 let pending =
961 queue.len() + in_flight.len() + deferred_transitives.len();
962 let _ = tx
963 .send(ResolvedPackage {
964 dep_path: dep_path.clone(),
965 name: linked_name.clone(),
966 version: real_version.clone(),
967 integrity: None,
968 tarball_url: None,
969 // local_source deps aren't aliased —
970 // `file:`/`link:` specifiers go
971 // through the local-source branch,
972 // not the `npm:` rewrite.
973 alias_of: None,
974 local_source: Some(local.clone()),
975 // Local `file:`/`link:` packages never
976 // carry npm-style platform constraints
977 // — they're whatever the user points
978 // at, so the fetch coordinator treats
979 // them as unconstrained (always fetch).
980 os: aube_lockfile::PlatformList::new(),
981 cpu: aube_lockfile::PlatformList::new(),
982 libc: aube_lockfile::PlatformList::new(),
983 deprecated: None,
984 unpacked_size: None,
985 pending,
986 })
987 .await;
988 }
989 // Enqueue transitive deps of the local package
990 // (directories + tarballs only — `link:` deps
991 // are fully the target's responsibility).
992 if !matches!(local, LocalSource::Link(_)) {
993 let mut child_ancestors = task.ancestors.clone();
994 child_ancestors.push((linked_name.clone(), real_version.clone()));
995 for (child_name, child_range) in target_deps {
996 queue.push_back(ResolveTask::transitive(
997 child_name,
998 child_range,
999 DepType::Production,
1000 dep_path.clone(),
1001 task.importer.clone(),
1002 child_ancestors.clone(),
1003 ));
1004 }
1005 }
1006 }
1007 if task.is_root {
1008 note_root_done!();
1009 }
1010 continue;
1011 }
1012
1013 // Handle workspace linkage. Two cases resolve to the
1014 // workspace package rather than the registry:
1015 // 1. Explicit `workspace:` protocol (pnpm/yarn-berry
1016 // style). The range after the prefix is accepted
1017 // unconditionally — the user asserted this should
1018 // link.
1019 // 2. Bare semver range whose name matches a workspace
1020 // package whose version satisfies the range. This
1021 // is the yarn-v1 / npm / bun default: siblings pin
1022 // each other with normal version strings and
1023 // expect the workspace to win over the registry.
1024 // A workspace is typically either unpublished or
1025 // is itself the source of truth for its name, so
1026 // preferring the local copy matches every other
1027 // mainstream pm.
1028 if let Some(ws_version) = workspace_packages.get(&task.name)
1029 && (match task.range.strip_prefix("workspace:") {
1030 // workspace:*, workspace:^, workspace:~
1031 // bind to whatever local workspace version is.
1032 // These are pnpm's "don't pin me, just track
1033 // local" sigils. Match them before range check.
1034 Some("" | "*" | "^" | "~") => true,
1035 // workspace:<range> like workspace:^2.0.0 or
1036 // workspace:1.x. Must still satisfy local
1037 // version. Before this fix, any workspace:
1038 // prefix short-circuited. Consumer could pin
1039 // workspace:^2 against local 1.0.0 and aube
1040 // would silently link the wrong version.
1041 // pnpm errors here with no-matching-version.
1042 Some(rest) => version_satisfies(ws_version, rest),
1043 // Bare semver (no workspace: prefix) path.
1044 // Linker walks up to workspace yarn-v1 style.
1045 // Special case `*` and `""` (bare catch-all)
1046 // to always match the workspace copy, even
1047 // when the ws version is a prerelease like
1048 // `0.0.0-0` which semver strict rules would
1049 // otherwise exclude. Placeholder versions
1050 // are common in fresh changesets-managed
1051 // workspaces and would silently fall through
1052 // to registry resolution otherwise, picking
1053 // up a stale published build instead of the
1054 // local source.
1055 None if task.range.is_empty() || task.range == "*" => true,
1056 None => version_satisfies(ws_version, &task.range),
1057 })
1058 {
1059 let dep_path = dep_path_for(&task.name, ws_version);
1060 if task.is_root
1061 && let Some(deps) = importers.get_mut(&task.importer)
1062 {
1063 deps.push(DirectDep {
1064 name: task.name.clone(),
1065 dep_path: dep_path.clone(),
1066 dep_type: task.dep_type,
1067 specifier: task.original_specifier.clone(),
1068 });
1069 }
1070 if let Some(ref parent_dp) = task.parent
1071 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1072 {
1073 parent_pkg
1074 .dependencies
1075 .insert(task.name.clone(), ws_version.clone());
1076 if task.dep_type == DepType::Optional {
1077 parent_pkg
1078 .optional_dependencies
1079 .insert(task.name.clone(), ws_version.clone());
1080 }
1081 }
1082 if task.is_root {
1083 note_root_done!();
1084 }
1085 continue;
1086 }
1087
1088 // Sibling dedupe. If another task for this same name
1089 // has already settled on a version that satisfies
1090 // this task's range, wire up to that resolution and
1091 // short-circuit. In the old wave code this check
1092 // lived in the post-fetch loop as `existing_match`;
1093 // in the pipelined loop we run it up front so
1094 // dedupable tasks never block on a fetch or a
1095 // lockfile scan.
1096 if let Some(matched_ver) = resolved_versions.get(&task.name).and_then(|versions| {
1097 versions
1098 .iter()
1099 .find(|v| {
1100 version_satisfies(v, &task.range)
1101 && !is_vulnerable(task.registry_name(), v, &self.vulnerable_ranges)
1102 })
1103 .cloned()
1104 }) {
1105 let dep_path = dep_path_for(&task.name, &matched_ver);
1106 if task.is_root
1107 && let Some(deps) = importers.get_mut(&task.importer)
1108 {
1109 deps.push(DirectDep {
1110 name: task.name.clone(),
1111 dep_path: dep_path.clone(),
1112 dep_type: task.dep_type,
1113 specifier: task.original_specifier.clone(),
1114 });
1115 }
1116 if let Some(ref parent_dp) = task.parent
1117 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1118 {
1119 parent_pkg
1120 .dependencies
1121 .insert(task.name.clone(), matched_ver.clone());
1122 if task.dep_type == DepType::Optional {
1123 parent_pkg
1124 .optional_dependencies
1125 .insert(task.name.clone(), matched_ver);
1126 }
1127 }
1128 if task.is_root {
1129 note_root_done!();
1130 }
1131 continue;
1132 }
1133
1134 // Lockfile reuse. Runs unconditionally after sibling
1135 // dedupe fails — the old code gated this behind a
1136 // `cache.contains_key` check, but in the pipelined
1137 // loop the cache is populated incrementally and the
1138 // gate was a false optimization.
1139 {
1140 if let Some(locked_pkg) = existing.and_then(|g| {
1141 g.packages.values().find(|p| {
1142 p.name == task.name
1143 && version_satisfies(&p.version, &task.range)
1144 && !is_vulnerable(
1145 task.registry_name(),
1146 &p.version,
1147 &self.vulnerable_ranges,
1148 )
1149 })
1150 }) {
1151 // Drop optional deps whose platform constraints
1152 // don't match the active host / supported set.
1153 // This is the path that handles frozen/lockfile
1154 // installs on a different machine than the one
1155 // that wrote the lockfile.
1156 if task.dep_type == DepType::Optional
1157 && !is_supported(
1158 &locked_pkg.os,
1159 &locked_pkg.cpu,
1160 &locked_pkg.libc,
1161 &self.supported_architectures,
1162 )
1163 {
1164 tracing::debug!(
1165 "skipping optional dep {}@{}: platform mismatch",
1166 task.name,
1167 locked_pkg.version
1168 );
1169 if task.is_root
1170 && let Some(spec) = task.original_specifier.as_ref()
1171 {
1172 skipped_optional_dependencies
1173 .entry(task.importer.clone())
1174 .or_default()
1175 .insert(task.name.clone(), spec.clone());
1176 }
1177 if task.is_root {
1178 note_root_done!();
1179 }
1180 continue;
1181 }
1182 let version = locked_pkg.version.clone();
1183 let dep_path = dep_path_for(&task.name, &version);
1184
1185 if task.is_root
1186 && let Some(deps) = importers.get_mut(&task.importer)
1187 {
1188 deps.push(DirectDep {
1189 name: task.name.clone(),
1190 dep_path: dep_path.clone(),
1191 dep_type: task.dep_type,
1192 specifier: task.original_specifier.clone(),
1193 });
1194 }
1195 if let Some(ref parent_dp) = task.parent
1196 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1197 {
1198 parent_pkg
1199 .dependencies
1200 .insert(task.name.clone(), version.clone());
1201 if task.dep_type == DepType::Optional {
1202 parent_pkg
1203 .optional_dependencies
1204 .insert(task.name.clone(), version.clone());
1205 }
1206 }
1207 if visited.insert(std::sync::Arc::from(dep_path.as_str())) {
1208 resolved_versions
1209 .entry(task.name.clone())
1210 .or_default()
1211 .push(version.clone());
1212
1213 // Carry any round-tripped publish time
1214 // forward so (a) the cutoff computation at
1215 // the end of wave 0 can see reused directs
1216 // alongside freshly-resolved ones and
1217 // (b) the next lockfile write preserves the
1218 // existing `time:` entry even when this
1219 // install reuses the locked version without
1220 // re-fetching a packument.
1221 if self.should_record_times()
1222 && let Some(g) = existing
1223 && let Some(t) = g.times.get(&dep_path)
1224 {
1225 resolved_times.insert(dep_path.clone(), t.clone());
1226 }
1227
1228 if let Some(ref tx) = self.resolved_tx {
1229 let pending =
1230 queue.len() + in_flight.len() + deferred_transitives.len();
1231 let _ = tx
1232 .send(ResolvedPackage {
1233 dep_path: dep_path.clone(),
1234 name: task.name.clone(),
1235 version: version.clone(),
1236 integrity: locked_pkg.integrity.clone(),
1237 tarball_url: locked_pkg.tarball_url.clone(),
1238 // Carry the alias identity
1239 // through the reuse path — the
1240 // existing `locked_pkg` already
1241 // records it if the lockfile held
1242 // an aliased entry, so the
1243 // streaming fetch still hits the
1244 // real registry name.
1245 alias_of: locked_pkg.alias_of.clone(),
1246 local_source: locked_pkg.local_source.clone(),
1247 os: locked_pkg.os.clone(),
1248 cpu: locked_pkg.cpu.clone(),
1249 libc: locked_pkg.libc.clone(),
1250 // Lockfile reuse skips the packument
1251 // fetch, so we have no deprecation
1252 // message to forward here. The
1253 // `aube deprecations` command re-queries
1254 // packuments live for the
1255 // after-the-fact view.
1256 deprecated: None,
1257 // Same reasoning: lockfile reuse
1258 // doesn't refetch the packument and
1259 // LockedPackage doesn't carry size
1260 // metadata, so the size-estimate
1261 // segment stays absent for these
1262 // packages. The progress UI displays
1263 // a running download total instead
1264 // when the estimate is unavailable.
1265 unpacked_size: None,
1266 pending,
1267 })
1268 .await;
1269 }
1270
1271 // Carry declared peer deps forward from the
1272 // existing lockfile so subsequent peer-context
1273 // computation sees them without a re-fetch.
1274 resolved.insert(
1275 dep_path.clone(),
1276 LockedPackage {
1277 name: task.name.clone(),
1278 version: version.clone(),
1279 integrity: locked_pkg.integrity.clone(),
1280 dependencies: BTreeMap::new(),
1281 optional_dependencies: BTreeMap::new(),
1282 peer_dependencies: locked_pkg.peer_dependencies.clone(),
1283 peer_dependencies_meta: locked_pkg
1284 .peer_dependencies_meta
1285 .clone(),
1286 dep_path: dep_path.clone(),
1287 local_source: locked_pkg.local_source.clone(),
1288 os: locked_pkg.os.clone(),
1289 cpu: locked_pkg.cpu.clone(),
1290 libc: locked_pkg.libc.clone(),
1291 bundled_dependencies: locked_pkg.bundled_dependencies.clone(),
1292 optional: locked_pkg.optional,
1293 transitive_peer_dependencies: locked_pkg
1294 .transitive_peer_dependencies
1295 .clone(),
1296 tarball_url: locked_pkg.tarball_url.clone(),
1297 alias_of: locked_pkg.alias_of.clone(),
1298 yarn_checksum: locked_pkg.yarn_checksum.clone(),
1299 engines: locked_pkg.engines.clone(),
1300 bin: locked_pkg.bin.clone(),
1301 declared_dependencies: locked_pkg.declared_dependencies.clone(),
1302 license: locked_pkg.license.clone(),
1303 funding_url: locked_pkg.funding_url.clone(),
1304 extra_meta: locked_pkg.extra_meta.clone(),
1305 },
1306 );
1307
1308 // Enqueue transitive deps from the locked package.
1309 // Strip any peer-context suffix off the version
1310 // before treating it as a semver range — a
1311 // locked `"18.2.0(react@18.2.0)"` tail should
1312 // match against packuments as just `18.2.0`.
1313 // Also strip a leading `name@` if present:
1314 // bun/yarn parsers store transitive deps in
1315 // `name@version` (full dep_path) form, while
1316 // pnpm stores bare versions. Without the
1317 // strip, a yarn/bun-locked `is-odd` would
1318 // emit a transitive task for is-number with
1319 // range `"is-number@6.0.0"`, which doesn't
1320 // parse as semver and fails resolution.
1321 // The lockfile already omitted bundled dep
1322 // edges on write, so iterating
1323 // `locked_pkg.dependencies` naturally skips them.
1324 let mut child_ancestors = task.ancestors.clone();
1325 child_ancestors.push((task.name.clone(), version.clone()));
1326 for (dep_name, dep_version) in &locked_pkg.dependencies {
1327 let prefix = format!("{dep_name}@");
1328 let stripped =
1329 dep_version.strip_prefix(&prefix).unwrap_or(dep_version);
1330 let canonical_version =
1331 stripped.split('(').next().unwrap_or(stripped).to_string();
1332 let dep_type =
1333 if locked_pkg.optional_dependencies.contains_key(dep_name) {
1334 DepType::Optional
1335 } else {
1336 DepType::Production
1337 };
1338 queue.push_back(ResolveTask::transitive(
1339 dep_name.clone(),
1340 canonical_version,
1341 dep_type,
1342 dep_path.clone(),
1343 task.importer.clone(),
1344 child_ancestors.clone(),
1345 ));
1346 }
1347 }
1348 lockfile_reuse_count += 1;
1349 if task.is_root {
1350 note_root_done!();
1351 }
1352 continue;
1353 }
1354 }
1355
1356 // Packument not in cache. Spawn its fetch if one
1357 // isn't already running, then wait for packument
1358 // fetches to land until this task's packument is
1359 // available. Other fetches that happen to complete
1360 // while we're waiting get cached opportunistically,
1361 // which is exactly what lets the pipeline overlap
1362 // network and CPU: by the time a later task is
1363 // popped its packument is usually already sitting
1364 // in the cache because it landed while an earlier
1365 // task was being waited on.
1366 let wait_start = std::time::Instant::now();
1367 // Cache is keyed by the *registry* name — for aliased
1368 // tasks `task.name` is the user-facing alias (e.g.
1369 // `h3-v2`), which would never hit. `registry_name()`
1370 // returns the alias-resolved target (`h3`) on
1371 // aliased tasks and `task.name` otherwise.
1372 let fetch_name = task.registry_name().to_string();
1373 let _diag_task_wait = aube_util::diag::Span::new(
1374 aube_util::diag::Category::Resolver,
1375 "task_wait_packument",
1376 )
1377 .with_meta_fn(|| format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&fetch_name)));
1378 while !self.cache.contains_key(&fetch_name) {
1379 ensure_fetch!(&fetch_name);
1380 match in_flight.join_next().await {
1381 Some(Ok(Ok((name, packument, from_primer)))) => {
1382 in_flight_names.remove(&name);
1383 if from_primer {
1384 primer_seeded_names.insert(name.clone());
1385 }
1386 self.cache.insert(name, packument);
1387 packument_fetch_count += 1;
1388 }
1389 Some(Ok(Err(e))) => return Err(e),
1390 Some(Err(join_err)) => {
1391 return Err(Error::Registry(
1392 "(join)".to_string(),
1393 join_err.to_string(),
1394 ));
1395 }
1396 None => {
1397 // ensure_fetch! guarantees something is
1398 // in flight if the cache still doesn't
1399 // hold this name, so a None here means
1400 // the spawn failed silently. Surface it.
1401 return Err(Error::Registry(
1402 fetch_name.clone(),
1403 "packument fetch disappeared before completing".to_string(),
1404 ));
1405 }
1406 }
1407 }
1408 packument_fetch_time += wait_start.elapsed();
1409
1410 // TimeBased wave-0 gate. Transitives that reach
1411 // the version-pick step while the cutoff is still
1412 // unknown must wait until the direct deps have
1413 // been picked and the cutoff has been derived;
1414 // otherwise they'd pick against a `None` cutoff
1415 // and miss the filter. In `Highest` mode (the
1416 // default), `cutoff_pending` starts false and this
1417 // is a no-op.
1418 if cutoff_pending && !task.is_root {
1419 deferred_transitives.push(task);
1420 continue;
1421 }
1422
1423 // Version-pick + transitive enqueue. Was a separate
1424 // sub-loop over `processed_batch` in the old wave
1425 // code; here it's inline as the tail of the per-task
1426 // pipeline now that we know the packument is in
1427 // cache. `registry_name()` is the cache key for
1428 // aliased tasks (cache is populated under the real
1429 // registry name), so use the same accessor here.
1430 // Find locked version
1431 let locked_version = existing.and_then(|g| {
1432 g.packages
1433 .values()
1434 .find(|p| p.name == task.name && version_satisfies(&p.version, &task.range))
1435 .map(|p| p.version.as_str())
1436 .filter(|v| {
1437 !is_vulnerable(task.registry_name(), v, &self.vulnerable_ranges)
1438 })
1439 });
1440
1441 // Direct deps in time-based mode pick the lowest
1442 // satisfying version; everything else (transitives,
1443 // and all picks in Highest mode) picks highest.
1444 let pick_lowest = self.resolution_mode == ResolutionMode::TimeBased && task.is_root;
1445 // Apply the cutoff unless this package is on the
1446 // minimumReleaseAge exclude list. The exclude list only
1447 // suppresses the *minimumReleaseAge* leg, not the
1448 // time-based-mode leg — but since we collapse both
1449 // into the same `published_by` string at this point,
1450 // we have to skip the cutoff entirely for excluded
1451 // names. Acceptable: time-based mode and exclude
1452 // lists aren't expected to coexist in the wild.
1453 let cutoff_for_pkg = match self.minimum_release_age.as_ref() {
1454 Some(mra) if mra.exclude.contains(&task.name) => None,
1455 _ => published_by.as_deref(),
1456 };
1457 // Strict semantics in two cases:
1458 // - `minimumReleaseAgeStrict=true` (the user opted in
1459 // to hard failures), or
1460 // - the cutoff comes from `--resolution-mode=time-based`
1461 // alone, with no `minimumReleaseAge` configured. The
1462 // time-based cutoff is intended as a hard wall — if
1463 // no version fits, the *correct* fix is for the user
1464 // to update the lockfile, not for the resolver to
1465 // silently pick a different version.
1466 let strict = match self.minimum_release_age.as_ref() {
1467 Some(m) => m.strict,
1468 None => true,
1469 };
1470 let registry_name = task.registry_name().to_string();
1471 let selected_pick = loop {
1472 let packument = self.cache.get(®istry_name).ok_or_else(|| {
1473 Error::Registry(registry_name.clone(), "packument not in cache".to_string())
1474 })?;
1475 let pick = pick_version(
1476 packument,
1477 &task.range,
1478 locked_version,
1479 pick_lowest,
1480 cutoff_for_pkg,
1481 strict,
1482 );
1483 match pick {
1484 PickResult::Found(meta) => break meta.clone(),
1485 PickResult::AgeGated | PickResult::NoMatch
1486 if primer_seeded_names.remove(®istry_name) =>
1487 {
1488 let fetch_start = std::time::Instant::now();
1489 let live = if needs_time {
1490 match self.packument_full_cache_dir.as_ref() {
1491 Some(dir) => {
1492 self.client
1493 .fetch_packument_with_time_cached(®istry_name, dir)
1494 .await
1495 }
1496 None => self.client.fetch_packument(®istry_name).await,
1497 }
1498 } else {
1499 match self.client.fetch_packument(®istry_name).await {
1500 Ok(live) => {
1501 if let Some(dir) = self.packument_cache_dir.as_ref() {
1502 self.client.replace_packument_cache(
1503 ®istry_name,
1504 dir,
1505 &live,
1506 );
1507 }
1508 Ok(live)
1509 }
1510 Err(err) => Err(err),
1511 }
1512 }
1513 .map_err(|e| Error::Registry(registry_name.clone(), e.to_string()))?;
1514 packument_fetch_time += fetch_start.elapsed();
1515 packument_fetch_count += 1;
1516 self.cache.insert(registry_name.clone(), live);
1517 }
1518 // Only surface `AgeGate` when the cutoff actually
1519 // came from `minimumReleaseAge`. When it came from
1520 // `--resolution-mode=time-based` alone, the user
1521 // never opted into the supply-chain age gate, so
1522 // the failure should report as a plain no-match
1523 // instead of a misleading "older than 0 minutes".
1524 PickResult::AgeGated => match self.minimum_release_age.as_ref() {
1525 Some(mra) => {
1526 return Err(Error::AgeGate(Box::new(error::build_age_gate(
1527 &task,
1528 packument,
1529 mra.minutes,
1530 ))));
1531 }
1532 None => {
1533 return Err(Error::NoMatch(Box::new(error::build_no_match(
1534 &task, packument,
1535 ))));
1536 }
1537 },
1538 PickResult::NoMatch => {
1539 return Err(Error::NoMatch(Box::new(error::build_no_match(
1540 &task, packument,
1541 ))));
1542 }
1543 }
1544 };
1545 let packument = self.cache.get(®istry_name).ok_or_else(|| {
1546 Error::Registry(registry_name.clone(), "packument not in cache".to_string())
1547 })?;
1548 let picked_ref = prefer_non_vulnerable_pick(
1549 task.registry_name(),
1550 packument,
1551 &task.range,
1552 &selected_pick,
1553 pick_lowest,
1554 cutoff_for_pkg,
1555 &self.vulnerable_ranges,
1556 );
1557 // Trust-policy enforcement runs *before* any other
1558 // post-pick processing (mirrors pnpm's placement
1559 // immediately after `pickPackage`). Skip when policy is
1560 // off so the off-by-default case is a single enum
1561 // compare. The check needs the live packument's `time`
1562 // map and all version metadata, both of which are still
1563 // in scope here from L1191.
1564 if self.dependency_policy.trust_policy == crate::TrustPolicy::NoDowngrade {
1565 crate::trust::check_no_downgrade(
1566 packument,
1567 &picked_ref.version,
1568 picked_ref,
1569 &self.dependency_policy.trust_policy_exclude,
1570 self.dependency_policy.trust_policy_ignore_after,
1571 )
1572 .map_err(|e| match e {
1573 crate::trust::TrustCheckError::Downgrade(d) => {
1574 Error::TrustDowngrade(Box::new(d))
1575 }
1576 crate::trust::TrustCheckError::MissingTime(d) => {
1577 Error::TrustCheckMissingTime(Box::new(d))
1578 }
1579 })?;
1580 }
1581
1582 // Clone the picked metadata into an owned value so we can
1583 // both run the `readPackage` hook (which needs a
1584 // disjoint `&mut self` borrow) and, later, mutate the
1585 // resolver's own caches without holding a borrow into
1586 // `self.cache`. Also grab the publish-time entry now,
1587 // for the same reason.
1588 let mut picked_owned = picked_ref.clone();
1589 let picked_publish_time = packument.time.get(&picked_ref.version).cloned();
1590 // Skip the readPackage hook entirely for a `(name, version)`
1591 // pair we've already fully processed via a prior task. The
1592 // mutated dep maps only drive the transitive enqueue below,
1593 // and that block is short-circuited by the `visited` guard
1594 // later in this iteration — so running the hook here would
1595 // just burn an IPC round-trip whose result is discarded.
1596 let prehook_dep_path = dep_path_for(&task.name, &picked_ref.version);
1597 let already_visited = visited.contains(prehook_dep_path.as_str());
1598
1599 if !already_visited {
1600 apply_package_extensions(
1601 &mut picked_owned,
1602 &self.dependency_policy.package_extensions,
1603 );
1604 }
1605
1606 // readPackage hook. Runs at most once per version-picked
1607 // package, before transitive enqueue. We honor edits to
1608 // the four dep maps and warn on (then discard) edits to
1609 // name/version/dist/platform/`hasInstallScript` — pnpm
1610 // tolerates readPackage returning a hollowed-out
1611 // object, so we restore those fields from the original
1612 // packument entry after the call.
1613 if !already_visited && let Some(hook) = self.read_package_hook.as_mut() {
1614 let before_name = picked_owned.name.clone();
1615 let before_version = picked_owned.version.clone();
1616 let before_dist = picked_owned.dist.clone();
1617 let before_os = picked_owned.os.clone();
1618 let before_cpu = picked_owned.cpu.clone();
1619 let before_libc = picked_owned.libc.clone();
1620 let before_bundled = picked_owned.bundled_dependencies.clone();
1621 let before_has_install_script = picked_owned.has_install_script;
1622 let before_deprecated = picked_owned.deprecated.clone();
1623 let input = picked_owned.clone();
1624 let mut after = hook.read_package(input).await.map_err(|e| {
1625 Error::Registry(before_name.clone(), format!("readPackage hook: {e}"))
1626 })?;
1627 if after.name != before_name || after.version != before_version {
1628 tracing::warn!(
1629 code = aube_codes::warnings::WARN_AUBE_HOOK_IDENTITY_REWRITTEN,
1630 "[pnpmfile] readPackage rewrote {}@{} identity to {}@{}; \
1631 aube ignores identity edits",
1632 before_name,
1633 before_version,
1634 after.name,
1635 after.version,
1636 );
1637 }
1638 after.name = before_name;
1639 after.version = before_version;
1640 after.dist = before_dist;
1641 after.os = before_os;
1642 after.cpu = before_cpu;
1643 after.libc = before_libc;
1644 after.bundled_dependencies = before_bundled;
1645 after.has_install_script = before_has_install_script;
1646 after.deprecated = before_deprecated;
1647 picked_owned = after;
1648 }
1649 let version_meta = &picked_owned;
1650
1651 // Optional deps that don't match the host platform get
1652 // silently dropped — pnpm parity. Required deps with a
1653 // bad platform still get installed; the warning matches
1654 // pnpm's `packageIsInstallable` behavior.
1655 let platform_ok = is_supported(
1656 &version_meta.os,
1657 &version_meta.cpu,
1658 &version_meta.libc,
1659 &self.supported_architectures,
1660 );
1661 if !platform_ok {
1662 if task.dep_type == DepType::Optional {
1663 tracing::debug!(
1664 "skipping optional dep {}@{}: unsupported platform (os={:?} cpu={:?} libc={:?})",
1665 task.name,
1666 version_meta.version,
1667 version_meta.os,
1668 version_meta.cpu,
1669 version_meta.libc
1670 );
1671 if task.is_root
1672 && let Some(spec) = task.original_specifier.as_ref()
1673 {
1674 skipped_optional_dependencies
1675 .entry(task.importer.clone())
1676 .or_default()
1677 .insert(task.name.clone(), spec.clone());
1678 }
1679 if task.is_root {
1680 note_root_done!();
1681 }
1682 continue;
1683 }
1684 tracing::warn!(
1685 code = aube_codes::warnings::WARN_AUBE_UNSUPPORTED_PLATFORM_INSTALL,
1686 "required dep {}@{} declares unsupported platform (os={:?} cpu={:?} libc={:?}); installing anyway",
1687 task.name,
1688 version_meta.version,
1689 version_meta.os,
1690 version_meta.cpu,
1691 version_meta.libc
1692 );
1693 }
1694
1695 let version = version_meta.version.clone();
1696 let dep_path = dep_path_for(&task.name, &version);
1697
1698 // Record publish time for the cutoff / `time:` block
1699 // whenever the packument carries one — matches pnpm,
1700 // which populates `publishedAt` opportunistically via
1701 // `meta.time?.[version]` regardless of resolution mode.
1702 // Corgi packuments from npmjs.org omit `time`, so in
1703 // Highest mode this is usually a no-op; Verdaccio
1704 // (v5.15.1+) and full-packument fetches do include it,
1705 // and then we round-trip it into the lockfile just like
1706 // pnpm does.
1707 if self.should_record_times()
1708 && let Some(t) = picked_publish_time.as_ref()
1709 {
1710 resolved_times.insert(dep_path.clone(), t.clone());
1711 }
1712
1713 // Record root dep
1714 if task.is_root
1715 && let Some(deps) = importers.get_mut(&task.importer)
1716 {
1717 deps.push(DirectDep {
1718 name: task.name.clone(),
1719 dep_path: dep_path.clone(),
1720 dep_type: task.dep_type,
1721 specifier: task.original_specifier.clone(),
1722 });
1723 }
1724
1725 // Wire parent
1726 if let Some(ref parent_dp) = task.parent
1727 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1728 {
1729 parent_pkg
1730 .dependencies
1731 .insert(task.name.clone(), version.clone());
1732 if task.dep_type == DepType::Optional {
1733 parent_pkg
1734 .optional_dependencies
1735 .insert(task.name.clone(), version.clone());
1736 }
1737 }
1738
1739 // Skip if already fully processed this exact version
1740 if visited.contains(dep_path.as_str()) {
1741 if task.is_root {
1742 note_root_done!();
1743 }
1744 continue;
1745 }
1746 visited.insert(std::sync::Arc::from(dep_path.as_str()));
1747
1748 tracing::trace!("resolved {}@{}", task.name, version);
1749
1750 // Forward a deprecation message to the install command,
1751 // subject to `allowedDeprecatedVersions` suppression.
1752 // User-facing rendering is the CLI's job — doing it here
1753 // would fire per resolved version with no way for the
1754 // caller to batch or filter direct-vs-transitive.
1755 let deprecated_msg: Option<Arc<str>> =
1756 version_meta.deprecated.as_deref().and_then(|msg| {
1757 let suppressed = is_deprecation_allowed(
1758 &task.name,
1759 &version,
1760 &self.dependency_policy.allowed_deprecated_versions,
1761 );
1762 (!suppressed).then(|| Arc::<str>::from(msg))
1763 });
1764
1765 // Track this version
1766 resolved_versions
1767 .entry(task.name.clone())
1768 .or_default()
1769 .push(version.clone());
1770
1771 let integrity = version_meta.dist.as_ref().and_then(|d| d.integrity.clone());
1772 // Always stash the registry tarball URL on the locked
1773 // package. pnpm / yarn writers gate emission on
1774 // `lockfile_include_tarball_url` (so the pnpm
1775 // round-trip stays byte-identical for projects that
1776 // opted out); the npm writer emits `resolved:` on
1777 // every package entry unconditionally, which is what
1778 // npm itself writes. Carrying the URL on every
1779 // LockedPackage lets both policies work without a
1780 // second packument fetch at write time.
1781 let tarball_url = version_meta.dist.as_ref().map(|d| d.tarball.clone());
1782
1783 // Stream this resolved package for early tarball fetching.
1784 // `alias_of` mirrors what the LockedPackage below
1785 // will carry — the streaming fetch consumer in
1786 // install.rs uses it to derive the real tarball URL
1787 // for aliased packages where `name` alone (`h3-v2`)
1788 // would 404.
1789 if let Some(ref tx) = self.resolved_tx {
1790 let pending = queue.len() + in_flight.len() + deferred_transitives.len();
1791 let _ = tx
1792 .send(ResolvedPackage {
1793 dep_path: dep_path.clone(),
1794 name: task.name.clone(),
1795 version: version.clone(),
1796 integrity: integrity.clone(),
1797 tarball_url: tarball_url.clone(),
1798 alias_of: task.real_name.clone(),
1799 local_source: None,
1800 os: version_meta.os.iter().cloned().collect(),
1801 cpu: version_meta.cpu.iter().cloned().collect(),
1802 libc: version_meta.libc.iter().cloned().collect(),
1803 deprecated: deprecated_msg.clone(),
1804 unpacked_size: version_meta.dist.as_ref().and_then(|d| d.unpacked_size),
1805 pending,
1806 })
1807 .await;
1808 }
1809
1810 // Capture the declared peer deps now so the post-pass can
1811 // compute each consumer's peer context without re-reading
1812 // the packument.
1813 let peer_deps = version_meta.peer_dependencies.clone();
1814 let peer_meta: BTreeMap<String, aube_lockfile::PeerDepMeta> = version_meta
1815 .peer_dependencies_meta
1816 .iter()
1817 .map(|(k, v)| {
1818 (
1819 k.clone(),
1820 aube_lockfile::PeerDepMeta {
1821 optional: v.optional,
1822 },
1823 )
1824 })
1825 .collect();
1826 // `bundledDependencies` names are shipped inside the
1827 // tarball itself and must not be resolved from the
1828 // registry. If we did enqueue them, we'd fetch a
1829 // (possibly different) version and plant a sibling
1830 // symlink inside `.aube/<parent>@ver/node_modules/`
1831 // that would shadow the bundled copy during Node's
1832 // directory walk. Compute the skip set once here and
1833 // store the names on the LockedPackage so restore
1834 // (from lockfile, skipping this code path) also
1835 // knows to avoid the sibling symlinks — see the
1836 // `.dependencies` write-through downstream.
1837 let bundled_names: FxHashSet<String> = version_meta
1838 .bundled_dependencies
1839 .as_ref()
1840 .map(|b| {
1841 b.names(&version_meta.dependencies)
1842 .into_iter()
1843 .map(String::from)
1844 .collect()
1845 })
1846 .unwrap_or_default();
1847
1848 resolved.insert(
1849 dep_path.clone(),
1850 LockedPackage {
1851 name: task.name.clone(),
1852 version: version.clone(),
1853 integrity,
1854 dependencies: BTreeMap::new(),
1855 optional_dependencies: BTreeMap::new(),
1856 peer_dependencies: peer_deps,
1857 peer_dependencies_meta: peer_meta,
1858 dep_path: dep_path.clone(),
1859 local_source: None,
1860 os: version_meta.os.iter().cloned().collect(),
1861 cpu: version_meta.cpu.iter().cloned().collect(),
1862 libc: version_meta.libc.iter().cloned().collect(),
1863 bundled_dependencies: {
1864 let mut v: Vec<String> = bundled_names.iter().cloned().collect();
1865 v.sort();
1866 v
1867 },
1868 tarball_url,
1869 // `name` is the alias for npm-aliased tasks
1870 // (`"h3-v2": "npm:h3@..."` → name = "h3-v2"),
1871 // so stash the real registry name here. The
1872 // lockfile writer + installer consult
1873 // `alias_of` whenever they need to hit the
1874 // registry, matching how the npm-lockfile
1875 // reader populates this field.
1876 alias_of: task.real_name.clone(),
1877 yarn_checksum: None,
1878 engines: version_meta.engines.clone(),
1879 // Rehydrate a string-form bin (`"bin": "cli.js"`)
1880 // into `{<package_name>: "cli.js"}` — registry
1881 // packuments leave the name off, expecting
1882 // consumers to default it to the package name.
1883 // Doing it here keeps bun's per-entry meta
1884 // byte-identical to bun's own output without
1885 // pushing the fixup into every writer.
1886 bin: {
1887 let mut m = version_meta.bin.clone();
1888 if let Some(path) = m.remove("") {
1889 // String-form `bin` in a packument
1890 // (`"bin": "cli.js"`) is implicitly
1891 // named after the real registry
1892 // package — not the alias. For an
1893 // aliased dep (`"h3-v2": "npm:h3@…"`)
1894 // the bun writer must emit the bin
1895 // under `h3`, not `h3-v2`, or the
1896 // map drifts against bun's own
1897 // output (and the shim install path
1898 // creates the wrong binary name).
1899 let bin_name =
1900 task.real_name.as_deref().unwrap_or(&task.name).to_string();
1901 m.insert(bin_name, path);
1902 }
1903 m
1904 },
1905 // Declared ranges straight from the packument's
1906 // `dependencies` / `optionalDependencies`. Fed
1907 // back out by npm / yarn / bun writers so
1908 // nested package entries keep the original
1909 // specifiers instead of collapsing to pins.
1910 declared_dependencies: {
1911 let mut m = version_meta.dependencies.clone();
1912 for (k, v) in &version_meta.optional_dependencies {
1913 m.insert(k.clone(), v.clone());
1914 }
1915 m
1916 },
1917 license: version_meta.license.clone(),
1918 funding_url: version_meta.funding_url.clone(),
1919 optional: false,
1920 transitive_peer_dependencies: Vec::new(),
1921 extra_meta: BTreeMap::new(),
1922 },
1923 );
1924
1925 // Enqueue transitive deps. Kick off a background
1926 // packument fetch the instant we discover the dep
1927 // name — so by the time the task is popped off the
1928 // queue below, its packument is usually already in
1929 // flight (and often already in cache). This is where
1930 // the pipeline overlaps fetches with CPU work without
1931 // any explicit wave barrier.
1932 //
1933 // Compute the child ancestor chain once — the same
1934 // frame (this package's name + resolved version)
1935 // applies to every dep / optionalDep / peer we enqueue
1936 // below.
1937 let mut child_ancestors = task.ancestors.clone();
1938 child_ancestors.push((task.name.clone(), version.clone()));
1939
1940 for (dep_name, dep_range) in &version_meta.dependencies {
1941 if bundled_names.contains(dep_name) {
1942 continue;
1943 }
1944 if self.dependency_policy.block_exotic_subdeps
1945 && is_non_registry_specifier(dep_range)
1946 {
1947 return Err(Error::Registry(
1948 dep_name.clone(),
1949 format!(
1950 "uses exotic specifier \"{dep_range}\" which is blocked \
1951 by blockExoticSubdeps (declared by {})",
1952 task.name
1953 ),
1954 ));
1955 }
1956 if !existing_names.contains(dep_name.as_str())
1957 && prefetchable!(dep_name.as_str(), dep_range.as_str())
1958 {
1959 ensure_fetch!(dep_name);
1960 }
1961 queue.push_back(ResolveTask::transitive(
1962 dep_name.clone(),
1963 dep_range.clone(),
1964 DepType::Production,
1965 dep_path.clone(),
1966 task.importer.clone(),
1967 child_ancestors.clone(),
1968 ));
1969 }
1970
1971 for (dep_name, dep_range) in &version_meta.optional_dependencies {
1972 if bundled_names.contains(dep_name) {
1973 continue;
1974 }
1975 if self.ignored_optional_dependencies.contains(dep_name) {
1976 continue;
1977 }
1978 if self.dependency_policy.block_exotic_subdeps
1979 && is_non_registry_specifier(dep_range)
1980 {
1981 tracing::warn!(
1982 code = aube_codes::warnings::WARN_AUBE_EXOTIC_SUBDEP_SKIPPED,
1983 "skipping optional dependency {dep_name} of {} — \
1984 exotic specifier \"{dep_range}\" blocked by blockExoticSubdeps",
1985 task.name
1986 );
1987 continue;
1988 }
1989 if !existing_names.contains(dep_name.as_str())
1990 && prefetchable!(dep_name.as_str(), dep_range.as_str())
1991 {
1992 ensure_fetch!(dep_name);
1993 }
1994 queue.push_back(ResolveTask::transitive(
1995 dep_name.clone(),
1996 dep_range.clone(),
1997 DepType::Optional,
1998 dep_path.clone(),
1999 task.importer.clone(),
2000 child_ancestors.clone(),
2001 ));
2002 }
2003
2004 // Peer dependencies: enqueue only required peers that
2005 // are truly missing from the importer/root scope. The
2006 // post-pass below (`apply_peer_contexts`) computes
2007 // which version each consumer sees, via ancestor
2008 // scope, and assigns peer-suffixed dep_paths.
2009 //
2010 // pnpm's `auto-install-peers=true` fills in missing
2011 // required peers, but it does not install optional peer
2012 // alternatives that the user did not ask for, and it
2013 // does not install a second compatible peer when the
2014 // importer already declares that peer name at an
2015 // incompatible version. In the latter case pnpm keeps
2016 // the user's direct dependency and reports an unmet
2017 // peer warning.
2018 //
2019 // When `auto-install-peers=false`, we skip enqueueing
2020 // peers entirely. Users are on the hook for adding
2021 // them to `package.json` themselves. Unmet peers still
2022 // surface as warnings via `detect_unmet_peers` after
2023 // resolve — in fact more so, since nothing gets
2024 // auto-installed.
2025 //
2026 // Skip peers that are already declared as regular or
2027 // optional deps of the same package — those already have a
2028 // task queued via the loops above, and duplicating would
2029 // just burn a queue slot.
2030 if self.auto_install_peers {
2031 for (dep_name, dep_range) in &version_meta.peer_dependencies {
2032 let peer_optional = version_meta
2033 .peer_dependencies_meta
2034 .get(dep_name)
2035 .map(|m| m.optional)
2036 .unwrap_or(false);
2037 // Optional peers are opt-in integrations, not
2038 // auto-install candidates. Users who need one must
2039 // declare it in their own manifest so the normal dep
2040 // loops above resolve it explicitly.
2041 if peer_optional {
2042 continue;
2043 }
2044 let importer_declares_peer = importer_declared_dep_names
2045 .get(&task.importer)
2046 .is_some_and(|names| names.contains(dep_name));
2047 let root_declares_peer = self.resolve_peers_from_workspace_root
2048 && task.importer != "."
2049 && importer_declared_dep_names
2050 .get(".")
2051 .is_some_and(|names| names.contains(dep_name));
2052 let peer_dep_is_ancestor =
2053 task.ancestors.iter().any(|(name, _)| name == dep_name);
2054 if importer_declares_peer || root_declares_peer || peer_dep_is_ancestor {
2055 continue;
2056 }
2057 if version_meta.dependencies.contains_key(dep_name)
2058 || version_meta.optional_dependencies.contains_key(dep_name)
2059 || bundled_names.contains(dep_name)
2060 {
2061 continue;
2062 }
2063 if self.dependency_policy.block_exotic_subdeps
2064 && is_non_registry_specifier(dep_range)
2065 {
2066 tracing::warn!(
2067 code = aube_codes::warnings::WARN_AUBE_EXOTIC_SUBDEP_SKIPPED,
2068 "skipping peer dependency {dep_name} of {} — \
2069 exotic specifier \"{dep_range}\" blocked \
2070 by blockExoticSubdeps",
2071 task.name
2072 );
2073 continue;
2074 }
2075 if !existing_names.contains(dep_name.as_str())
2076 && prefetchable!(dep_name.as_str(), dep_range.as_str())
2077 {
2078 ensure_fetch!(dep_name);
2079 }
2080 queue.push_back(ResolveTask::transitive(
2081 dep_name.clone(),
2082 dep_range.clone(),
2083 DepType::Production,
2084 dep_path.clone(),
2085 task.importer.clone(),
2086 child_ancestors.clone(),
2087 ));
2088 }
2089 }
2090
2091 // Root task just completed its full version-pick
2092 // path. Decrement the pending-directs counter so
2093 // the TimeBased cutoff trigger at the top of the
2094 // outer loop can fire once wave 0 is resolved.
2095 if task.is_root {
2096 note_root_done!();
2097 }
2098 }
2099 }
2100
2101 // Drain any remaining in-flight fetches so their tasks get
2102 // cleanly joined. Normally the main loop has harvested every
2103 // spawned fetch by the time the queue drains, but a few may
2104 // still be pending if the resolver short-circuited via
2105 // sibling dedupe or lockfile reuse after ensure_fetch! had
2106 // already spawned them.
2107 while in_flight.join_next().await.is_some() {}
2108
2109 let resolve_elapsed = resolve_start.elapsed();
2110 tracing::debug!(
2111 "resolver: {:.1?} total, {} packuments fetched ({:.1?} wall), {} reused from lockfile, {} packages resolved",
2112 resolve_elapsed,
2113 packument_fetch_count,
2114 packument_fetch_time,
2115 lockfile_reuse_count,
2116 resolved.len()
2117 );
2118 // Surface the resolver mix to the diag analyzer so the lockfile
2119 // reuse path can be spotted independently of the cold no-lockfile
2120 // path. Counts of: total packages resolved, of which N reused
2121 // from a prior lockfile and M required a network packument fetch.
2122 let resolved_count = resolved.len();
2123 aube_util::diag::instant_lazy(aube_util::diag::Category::Resolver, "decision_mix", || {
2124 format!(
2125 r#"{{"resolved":{},"lockfile_reused":{},"packuments_fetched":{}}}"#,
2126 resolved_count, lockfile_reuse_count, packument_fetch_count
2127 )
2128 });
2129
2130 let resolved_catalogs =
2131 catalog::materialize_catalog_picks(catalog_picks, &resolved_versions);
2132
2133 let canonical = LockfileGraph {
2134 importers,
2135 packages: resolved,
2136 settings: aube_lockfile::LockfileSettings {
2137 auto_install_peers: self.auto_install_peers,
2138 exclude_links_from_lockfile: self.exclude_links_from_lockfile,
2139 // Tarball-URL recording is a lockfile-writer concern; the
2140 // resolver never populates URLs itself. Install flips this
2141 // on after the graph is built when the setting is active.
2142 lockfile_include_tarball_url: false,
2143 },
2144 // Stamp the resolver's overrides into the output graph so the
2145 // lockfile writer can round-trip them and the next install's
2146 // drift check can compare them against the manifest.
2147 overrides: self.overrides.clone(),
2148 ignored_optional_dependencies: self.ignored_optional_dependencies.clone(),
2149 times: resolved_times,
2150 skipped_optional_dependencies,
2151 catalogs: resolved_catalogs,
2152 // Resolver output is format-agnostic; the bun writer layer
2153 // defaults `configVersion` to 1 when emitting a fresh
2154 // lockfile.
2155 bun_config_version: None,
2156 // Fresh resolves don't carry over unknown blocks; the
2157 // install-side merge (`overlay_metadata_from`) copies
2158 // them back from the prior lockfile when round-tripping.
2159 patched_dependencies: BTreeMap::new(),
2160 trusted_dependencies: Vec::new(),
2161 extra_fields: BTreeMap::new(),
2162 workspace_extra_fields: BTreeMap::new(),
2163 };
2164
2165 // Second pass: hoist every auto-installed peer to its importer's
2166 // direct deps so pnpm-style `node_modules/<peer>` top-level
2167 // symlinks get created and the lockfile's `importers.` section
2168 // lists them the way pnpm does with `auto-install-peers=true`.
2169 // Skipped entirely when the setting is off — matches pnpm, which
2170 // leaves the importer's `dependencies` untouched in that mode.
2171 let hoisted = if self.auto_install_peers {
2172 hoist_auto_installed_peers(canonical)
2173 } else {
2174 canonical
2175 };
2176
2177 // Third pass: compute peer-context suffixes for every reachable
2178 // package. See `apply_peer_contexts` for the details.
2179 let peer_options = PeerContextOptions {
2180 dedupe_peer_dependents: self.dedupe_peer_dependents,
2181 dedupe_peers: self.dedupe_peers,
2182 resolve_from_workspace_root: self.resolve_peers_from_workspace_root,
2183 peers_suffix_max_length: self.peers_suffix_max_length,
2184 };
2185 let _diag_peer =
2186 aube_util::diag::Span::new(aube_util::diag::Category::Resolver, "peer_context_apply");
2187 let contextualized = apply_peer_contexts(hoisted, &peer_options)?;
2188 drop(_diag_peer);
2189 tracing::debug!(
2190 "peer-context pass produced {} contextualized packages",
2191 contextualized.packages.len()
2192 );
2193 if let Some((state, sem)) = packument_persist_handle {
2194 sem.persist(&state, "packument:default");
2195 }
2196 Ok(contextualized)
2197 }
2198}
2199
2200fn is_vulnerable(
2201 package_name: &str,
2202 version: &str,
2203 vulnerable_ranges: &BTreeMap<String, Vec<String>>,
2204) -> bool {
2205 let Some(ranges) = vulnerable_ranges.get(package_name) else {
2206 return false;
2207 };
2208 let Ok(version) = node_semver::Version::parse(version) else {
2209 return false;
2210 };
2211 ranges
2212 .iter()
2213 .filter_map(|range| node_semver::Range::parse(range).ok())
2214 .any(|range| version.satisfies(&range))
2215}
2216
2217fn prefer_non_vulnerable_pick<'a>(
2218 package_name: &str,
2219 packument: &'a Packument,
2220 range_str: &str,
2221 fallback: &'a aube_registry::VersionMetadata,
2222 pick_lowest: bool,
2223 cutoff: Option<&str>,
2224 vulnerable_ranges: &BTreeMap<String, Vec<String>>,
2225) -> &'a aube_registry::VersionMetadata {
2226 if !is_vulnerable(package_name, &fallback.version, vulnerable_ranges) {
2227 return fallback;
2228 }
2229 let Ok(range) = node_semver::Range::parse(crate::semver_util::normalize_range(range_str))
2230 else {
2231 return fallback;
2232 };
2233 let passes_cutoff = |ver: &str| -> bool {
2234 let Some(c) = cutoff else { return true };
2235 match packument.time.get(ver) {
2236 Some(t) => t.as_str() <= c,
2237 None => true,
2238 }
2239 };
2240 let mut best: Option<(node_semver::Version, &'a aube_registry::VersionMetadata)> = None;
2241 for (ver_str, meta) in &packument.versions {
2242 let Ok(version) = node_semver::Version::parse(ver_str) else {
2243 continue;
2244 };
2245 if !version.satisfies(&range)
2246 || !passes_cutoff(ver_str)
2247 || is_vulnerable(package_name, ver_str, vulnerable_ranges)
2248 {
2249 continue;
2250 }
2251 let replace = best.as_ref().is_none_or(|(cur, _)| {
2252 if pick_lowest {
2253 version < *cur
2254 } else {
2255 version > *cur
2256 }
2257 });
2258 if replace {
2259 best = Some((version, meta));
2260 }
2261 }
2262 best.map(|(_, meta)| meta).unwrap_or(fallback)
2263}
2264
2265/// Seed the BFS queue with direct deps from every importer manifest.
2266///
2267/// When a package is declared in more than one section
2268/// (`dependencies` + `devDependencies`, etc.) we keep only the
2269/// highest-priority entry — `dependencies` > `devDependencies` >
2270/// `optionalDependencies` — matching pnpm, which silently drops
2271/// the lower-priority duplicates on resolve. Without this the
2272/// same name gets pushed into the importer's `DirectDep` list
2273/// twice (once per section), and the linker's parallel step 2
2274/// races to create the same `node_modules/<name>` symlink from
2275/// two tasks, producing an `EEXIST` on the loser.
2276fn seed_direct_deps(
2277 manifests: &[(String, PackageJson)],
2278 ignored_optional_dependencies: &BTreeSet<String>,
2279 queue: &mut VecDeque<ResolveTask>,
2280 importers: &mut BTreeMap<String, Vec<DirectDep>>,
2281) {
2282 for (importer_path, manifest) in manifests {
2283 importers.insert(importer_path.clone(), Vec::new());
2284
2285 for (name, range) in &manifest.dependencies {
2286 queue.push_back(ResolveTask::root(
2287 name.clone(),
2288 range.clone(),
2289 DepType::Production,
2290 importer_path.clone(),
2291 ));
2292 }
2293 for (name, range) in &manifest.dev_dependencies {
2294 if manifest.dependencies.contains_key(name) {
2295 continue;
2296 }
2297 queue.push_back(ResolveTask::root(
2298 name.clone(),
2299 range.clone(),
2300 DepType::Dev,
2301 importer_path.clone(),
2302 ));
2303 }
2304 for (name, range) in &manifest.optional_dependencies {
2305 if ignored_optional_dependencies.contains(name) {
2306 tracing::debug!(
2307 "ignoring optional dependency {name} (pnpm.ignoredOptionalDependencies)"
2308 );
2309 continue;
2310 }
2311 if manifest.dependencies.contains_key(name)
2312 || manifest.dev_dependencies.contains_key(name)
2313 {
2314 continue;
2315 }
2316 queue.push_back(ResolveTask::root(
2317 name.clone(),
2318 range.clone(),
2319 DepType::Optional,
2320 importer_path.clone(),
2321 ));
2322 }
2323 }
2324}