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 _ = tx
961 .send(ResolvedPackage {
962 dep_path: dep_path.clone(),
963 name: linked_name.clone(),
964 version: real_version.clone(),
965 integrity: None,
966 tarball_url: None,
967 // local_source deps aren't aliased —
968 // `file:`/`link:` specifiers go
969 // through the local-source branch,
970 // not the `npm:` rewrite.
971 alias_of: None,
972 local_source: Some(local.clone()),
973 // Local `file:`/`link:` packages never
974 // carry npm-style platform constraints
975 // — they're whatever the user points
976 // at, so the fetch coordinator treats
977 // them as unconstrained (always fetch).
978 os: aube_lockfile::PlatformList::new(),
979 cpu: aube_lockfile::PlatformList::new(),
980 libc: aube_lockfile::PlatformList::new(),
981 deprecated: None,
982 unpacked_size: None,
983 })
984 .await;
985 }
986 // Enqueue transitive deps of the local package
987 // (directories + tarballs only — `link:` deps
988 // are fully the target's responsibility).
989 if !matches!(local, LocalSource::Link(_)) {
990 let mut child_ancestors = task.ancestors.clone();
991 child_ancestors.push((linked_name.clone(), real_version.clone()));
992 for (child_name, child_range) in target_deps {
993 queue.push_back(ResolveTask::transitive(
994 child_name,
995 child_range,
996 DepType::Production,
997 dep_path.clone(),
998 task.importer.clone(),
999 child_ancestors.clone(),
1000 ));
1001 }
1002 }
1003 }
1004 if task.is_root {
1005 note_root_done!();
1006 }
1007 continue;
1008 }
1009
1010 // Handle workspace linkage. Two cases resolve to the
1011 // workspace package rather than the registry:
1012 // 1. Explicit `workspace:` protocol (pnpm/yarn-berry
1013 // style). The range after the prefix is accepted
1014 // unconditionally — the user asserted this should
1015 // link.
1016 // 2. Bare semver range whose name matches a workspace
1017 // package whose version satisfies the range. This
1018 // is the yarn-v1 / npm / bun default: siblings pin
1019 // each other with normal version strings and
1020 // expect the workspace to win over the registry.
1021 // A workspace is typically either unpublished or
1022 // is itself the source of truth for its name, so
1023 // preferring the local copy matches every other
1024 // mainstream pm.
1025 if let Some(ws_version) = workspace_packages.get(&task.name)
1026 && (match task.range.strip_prefix("workspace:") {
1027 // workspace:*, workspace:^, workspace:~
1028 // bind to whatever local workspace version is.
1029 // These are pnpm's "don't pin me, just track
1030 // local" sigils. Match them before range check.
1031 Some("" | "*" | "^" | "~") => true,
1032 // workspace:<range> like workspace:^2.0.0 or
1033 // workspace:1.x. Must still satisfy local
1034 // version. Before this fix, any workspace:
1035 // prefix short-circuited. Consumer could pin
1036 // workspace:^2 against local 1.0.0 and aube
1037 // would silently link the wrong version.
1038 // pnpm errors here with no-matching-version.
1039 Some(rest) => version_satisfies(ws_version, rest),
1040 // Bare semver (no workspace: prefix) path.
1041 // Linker walks up to workspace yarn-v1 style.
1042 // Special case `*` and `""` (bare catch-all)
1043 // to always match the workspace copy, even
1044 // when the ws version is a prerelease like
1045 // `0.0.0-0` which semver strict rules would
1046 // otherwise exclude. Placeholder versions
1047 // are common in fresh changesets-managed
1048 // workspaces and would silently fall through
1049 // to registry resolution otherwise, picking
1050 // up a stale published build instead of the
1051 // local source.
1052 None if task.range.is_empty() || task.range == "*" => true,
1053 None => version_satisfies(ws_version, &task.range),
1054 })
1055 {
1056 let dep_path = dep_path_for(&task.name, ws_version);
1057 if task.is_root
1058 && let Some(deps) = importers.get_mut(&task.importer)
1059 {
1060 deps.push(DirectDep {
1061 name: task.name.clone(),
1062 dep_path: dep_path.clone(),
1063 dep_type: task.dep_type,
1064 specifier: task.original_specifier.clone(),
1065 });
1066 }
1067 if let Some(ref parent_dp) = task.parent
1068 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1069 {
1070 parent_pkg
1071 .dependencies
1072 .insert(task.name.clone(), ws_version.clone());
1073 if task.dep_type == DepType::Optional {
1074 parent_pkg
1075 .optional_dependencies
1076 .insert(task.name.clone(), ws_version.clone());
1077 }
1078 }
1079 if task.is_root {
1080 note_root_done!();
1081 }
1082 continue;
1083 }
1084
1085 // Sibling dedupe. If another task for this same name
1086 // has already settled on a version that satisfies
1087 // this task's range, wire up to that resolution and
1088 // short-circuit. In the old wave code this check
1089 // lived in the post-fetch loop as `existing_match`;
1090 // in the pipelined loop we run it up front so
1091 // dedupable tasks never block on a fetch or a
1092 // lockfile scan.
1093 if let Some(matched_ver) = resolved_versions.get(&task.name).and_then(|versions| {
1094 versions
1095 .iter()
1096 .find(|v| {
1097 version_satisfies(v, &task.range)
1098 && !is_vulnerable(task.registry_name(), v, &self.vulnerable_ranges)
1099 })
1100 .cloned()
1101 }) {
1102 let dep_path = dep_path_for(&task.name, &matched_ver);
1103 if task.is_root
1104 && let Some(deps) = importers.get_mut(&task.importer)
1105 {
1106 deps.push(DirectDep {
1107 name: task.name.clone(),
1108 dep_path: dep_path.clone(),
1109 dep_type: task.dep_type,
1110 specifier: task.original_specifier.clone(),
1111 });
1112 }
1113 if let Some(ref parent_dp) = task.parent
1114 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1115 {
1116 parent_pkg
1117 .dependencies
1118 .insert(task.name.clone(), matched_ver.clone());
1119 if task.dep_type == DepType::Optional {
1120 parent_pkg
1121 .optional_dependencies
1122 .insert(task.name.clone(), matched_ver);
1123 }
1124 }
1125 if task.is_root {
1126 note_root_done!();
1127 }
1128 continue;
1129 }
1130
1131 // Lockfile reuse. Runs unconditionally after sibling
1132 // dedupe fails — the old code gated this behind a
1133 // `cache.contains_key` check, but in the pipelined
1134 // loop the cache is populated incrementally and the
1135 // gate was a false optimization.
1136 {
1137 if let Some(locked_pkg) = existing.and_then(|g| {
1138 g.packages.values().find(|p| {
1139 p.name == task.name
1140 && version_satisfies(&p.version, &task.range)
1141 && !is_vulnerable(
1142 task.registry_name(),
1143 &p.version,
1144 &self.vulnerable_ranges,
1145 )
1146 })
1147 }) {
1148 // Drop optional deps whose platform constraints
1149 // don't match the active host / supported set.
1150 // This is the path that handles frozen/lockfile
1151 // installs on a different machine than the one
1152 // that wrote the lockfile.
1153 if task.dep_type == DepType::Optional
1154 && !is_supported(
1155 &locked_pkg.os,
1156 &locked_pkg.cpu,
1157 &locked_pkg.libc,
1158 &self.supported_architectures,
1159 )
1160 {
1161 tracing::debug!(
1162 "skipping optional dep {}@{}: platform mismatch",
1163 task.name,
1164 locked_pkg.version
1165 );
1166 if task.is_root
1167 && let Some(spec) = task.original_specifier.as_ref()
1168 {
1169 skipped_optional_dependencies
1170 .entry(task.importer.clone())
1171 .or_default()
1172 .insert(task.name.clone(), spec.clone());
1173 }
1174 if task.is_root {
1175 note_root_done!();
1176 }
1177 continue;
1178 }
1179 let version = locked_pkg.version.clone();
1180 let dep_path = dep_path_for(&task.name, &version);
1181
1182 if task.is_root
1183 && let Some(deps) = importers.get_mut(&task.importer)
1184 {
1185 deps.push(DirectDep {
1186 name: task.name.clone(),
1187 dep_path: dep_path.clone(),
1188 dep_type: task.dep_type,
1189 specifier: task.original_specifier.clone(),
1190 });
1191 }
1192 if let Some(ref parent_dp) = task.parent
1193 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1194 {
1195 parent_pkg
1196 .dependencies
1197 .insert(task.name.clone(), version.clone());
1198 if task.dep_type == DepType::Optional {
1199 parent_pkg
1200 .optional_dependencies
1201 .insert(task.name.clone(), version.clone());
1202 }
1203 }
1204 if visited.insert(std::sync::Arc::from(dep_path.as_str())) {
1205 resolved_versions
1206 .entry(task.name.clone())
1207 .or_default()
1208 .push(version.clone());
1209
1210 // Carry any round-tripped publish time
1211 // forward so (a) the cutoff computation at
1212 // the end of wave 0 can see reused directs
1213 // alongside freshly-resolved ones and
1214 // (b) the next lockfile write preserves the
1215 // existing `time:` entry even when this
1216 // install reuses the locked version without
1217 // re-fetching a packument.
1218 if self.should_record_times()
1219 && let Some(g) = existing
1220 && let Some(t) = g.times.get(&dep_path)
1221 {
1222 resolved_times.insert(dep_path.clone(), t.clone());
1223 }
1224
1225 if let Some(ref tx) = self.resolved_tx {
1226 let _ = tx
1227 .send(ResolvedPackage {
1228 dep_path: dep_path.clone(),
1229 name: task.name.clone(),
1230 version: version.clone(),
1231 integrity: locked_pkg.integrity.clone(),
1232 tarball_url: locked_pkg.tarball_url.clone(),
1233 // Carry the alias identity
1234 // through the reuse path — the
1235 // existing `locked_pkg` already
1236 // records it if the lockfile held
1237 // an aliased entry, so the
1238 // streaming fetch still hits the
1239 // real registry name.
1240 alias_of: locked_pkg.alias_of.clone(),
1241 local_source: locked_pkg.local_source.clone(),
1242 os: locked_pkg.os.clone(),
1243 cpu: locked_pkg.cpu.clone(),
1244 libc: locked_pkg.libc.clone(),
1245 // Lockfile reuse skips the packument
1246 // fetch, so we have no deprecation
1247 // message to forward here. The
1248 // `aube deprecations` command re-queries
1249 // packuments live for the
1250 // after-the-fact view.
1251 deprecated: None,
1252 // Same reasoning: lockfile reuse
1253 // doesn't refetch the packument and
1254 // LockedPackage doesn't carry size
1255 // metadata, so the size-estimate
1256 // segment stays absent for these
1257 // packages. The progress UI displays
1258 // a running download total instead
1259 // when the estimate is unavailable.
1260 unpacked_size: None,
1261 })
1262 .await;
1263 }
1264
1265 // Carry declared peer deps forward from the
1266 // existing lockfile so subsequent peer-context
1267 // computation sees them without a re-fetch.
1268 resolved.insert(
1269 dep_path.clone(),
1270 LockedPackage {
1271 name: task.name.clone(),
1272 version: version.clone(),
1273 integrity: locked_pkg.integrity.clone(),
1274 dependencies: BTreeMap::new(),
1275 optional_dependencies: BTreeMap::new(),
1276 peer_dependencies: locked_pkg.peer_dependencies.clone(),
1277 peer_dependencies_meta: locked_pkg
1278 .peer_dependencies_meta
1279 .clone(),
1280 dep_path: dep_path.clone(),
1281 local_source: locked_pkg.local_source.clone(),
1282 os: locked_pkg.os.clone(),
1283 cpu: locked_pkg.cpu.clone(),
1284 libc: locked_pkg.libc.clone(),
1285 bundled_dependencies: locked_pkg.bundled_dependencies.clone(),
1286 optional: locked_pkg.optional,
1287 transitive_peer_dependencies: locked_pkg
1288 .transitive_peer_dependencies
1289 .clone(),
1290 tarball_url: locked_pkg.tarball_url.clone(),
1291 alias_of: locked_pkg.alias_of.clone(),
1292 yarn_checksum: locked_pkg.yarn_checksum.clone(),
1293 engines: locked_pkg.engines.clone(),
1294 bin: locked_pkg.bin.clone(),
1295 declared_dependencies: locked_pkg.declared_dependencies.clone(),
1296 license: locked_pkg.license.clone(),
1297 funding_url: locked_pkg.funding_url.clone(),
1298 extra_meta: locked_pkg.extra_meta.clone(),
1299 },
1300 );
1301
1302 // Enqueue transitive deps from the locked package.
1303 // Strip any peer-context suffix off the version
1304 // before treating it as a semver range — a
1305 // locked `"18.2.0(react@18.2.0)"` tail should
1306 // match against packuments as just `18.2.0`.
1307 // Also strip a leading `name@` if present:
1308 // bun/yarn parsers store transitive deps in
1309 // `name@version` (full dep_path) form, while
1310 // pnpm stores bare versions. Without the
1311 // strip, a yarn/bun-locked `is-odd` would
1312 // emit a transitive task for is-number with
1313 // range `"is-number@6.0.0"`, which doesn't
1314 // parse as semver and fails resolution.
1315 // The lockfile already omitted bundled dep
1316 // edges on write, so iterating
1317 // `locked_pkg.dependencies` naturally skips them.
1318 let mut child_ancestors = task.ancestors.clone();
1319 child_ancestors.push((task.name.clone(), version.clone()));
1320 for (dep_name, dep_version) in &locked_pkg.dependencies {
1321 let prefix = format!("{dep_name}@");
1322 let stripped =
1323 dep_version.strip_prefix(&prefix).unwrap_or(dep_version);
1324 let canonical_version =
1325 stripped.split('(').next().unwrap_or(stripped).to_string();
1326 let dep_type =
1327 if locked_pkg.optional_dependencies.contains_key(dep_name) {
1328 DepType::Optional
1329 } else {
1330 DepType::Production
1331 };
1332 queue.push_back(ResolveTask::transitive(
1333 dep_name.clone(),
1334 canonical_version,
1335 dep_type,
1336 dep_path.clone(),
1337 task.importer.clone(),
1338 child_ancestors.clone(),
1339 ));
1340 }
1341 }
1342 lockfile_reuse_count += 1;
1343 if task.is_root {
1344 note_root_done!();
1345 }
1346 continue;
1347 }
1348 }
1349
1350 // Packument not in cache. Spawn its fetch if one
1351 // isn't already running, then wait for packument
1352 // fetches to land until this task's packument is
1353 // available. Other fetches that happen to complete
1354 // while we're waiting get cached opportunistically,
1355 // which is exactly what lets the pipeline overlap
1356 // network and CPU: by the time a later task is
1357 // popped its packument is usually already sitting
1358 // in the cache because it landed while an earlier
1359 // task was being waited on.
1360 let wait_start = std::time::Instant::now();
1361 // Cache is keyed by the *registry* name — for aliased
1362 // tasks `task.name` is the user-facing alias (e.g.
1363 // `h3-v2`), which would never hit. `registry_name()`
1364 // returns the alias-resolved target (`h3`) on
1365 // aliased tasks and `task.name` otherwise.
1366 let fetch_name = task.registry_name().to_string();
1367 let _diag_task_wait = aube_util::diag::Span::new(
1368 aube_util::diag::Category::Resolver,
1369 "task_wait_packument",
1370 )
1371 .with_meta_fn(|| format!(r#"{{"name":{}}}"#, aube_util::diag::jstr(&fetch_name)));
1372 while !self.cache.contains_key(&fetch_name) {
1373 ensure_fetch!(&fetch_name);
1374 match in_flight.join_next().await {
1375 Some(Ok(Ok((name, packument, from_primer)))) => {
1376 in_flight_names.remove(&name);
1377 if from_primer {
1378 primer_seeded_names.insert(name.clone());
1379 }
1380 self.cache.insert(name, packument);
1381 packument_fetch_count += 1;
1382 }
1383 Some(Ok(Err(e))) => return Err(e),
1384 Some(Err(join_err)) => {
1385 return Err(Error::Registry(
1386 "(join)".to_string(),
1387 join_err.to_string(),
1388 ));
1389 }
1390 None => {
1391 // ensure_fetch! guarantees something is
1392 // in flight if the cache still doesn't
1393 // hold this name, so a None here means
1394 // the spawn failed silently. Surface it.
1395 return Err(Error::Registry(
1396 fetch_name.clone(),
1397 "packument fetch disappeared before completing".to_string(),
1398 ));
1399 }
1400 }
1401 }
1402 packument_fetch_time += wait_start.elapsed();
1403
1404 // TimeBased wave-0 gate. Transitives that reach
1405 // the version-pick step while the cutoff is still
1406 // unknown must wait until the direct deps have
1407 // been picked and the cutoff has been derived;
1408 // otherwise they'd pick against a `None` cutoff
1409 // and miss the filter. In `Highest` mode (the
1410 // default), `cutoff_pending` starts false and this
1411 // is a no-op.
1412 if cutoff_pending && !task.is_root {
1413 deferred_transitives.push(task);
1414 continue;
1415 }
1416
1417 // Version-pick + transitive enqueue. Was a separate
1418 // sub-loop over `processed_batch` in the old wave
1419 // code; here it's inline as the tail of the per-task
1420 // pipeline now that we know the packument is in
1421 // cache. `registry_name()` is the cache key for
1422 // aliased tasks (cache is populated under the real
1423 // registry name), so use the same accessor here.
1424 // Find locked version
1425 let locked_version = existing.and_then(|g| {
1426 g.packages
1427 .values()
1428 .find(|p| p.name == task.name && version_satisfies(&p.version, &task.range))
1429 .map(|p| p.version.as_str())
1430 .filter(|v| {
1431 !is_vulnerable(task.registry_name(), v, &self.vulnerable_ranges)
1432 })
1433 });
1434
1435 // Direct deps in time-based mode pick the lowest
1436 // satisfying version; everything else (transitives,
1437 // and all picks in Highest mode) picks highest.
1438 let pick_lowest = self.resolution_mode == ResolutionMode::TimeBased && task.is_root;
1439 // Apply the cutoff unless this package is on the
1440 // minimumReleaseAge exclude list. The exclude list only
1441 // suppresses the *minimumReleaseAge* leg, not the
1442 // time-based-mode leg — but since we collapse both
1443 // into the same `published_by` string at this point,
1444 // we have to skip the cutoff entirely for excluded
1445 // names. Acceptable: time-based mode and exclude
1446 // lists aren't expected to coexist in the wild.
1447 let cutoff_for_pkg = match self.minimum_release_age.as_ref() {
1448 Some(mra) if mra.exclude.contains(&task.name) => None,
1449 _ => published_by.as_deref(),
1450 };
1451 // Strict semantics in two cases:
1452 // - `minimumReleaseAgeStrict=true` (the user opted in
1453 // to hard failures), or
1454 // - the cutoff comes from `--resolution-mode=time-based`
1455 // alone, with no `minimumReleaseAge` configured. The
1456 // time-based cutoff is intended as a hard wall — if
1457 // no version fits, the *correct* fix is for the user
1458 // to update the lockfile, not for the resolver to
1459 // silently pick a different version.
1460 let strict = match self.minimum_release_age.as_ref() {
1461 Some(m) => m.strict,
1462 None => true,
1463 };
1464 let registry_name = task.registry_name().to_string();
1465 let selected_pick = loop {
1466 let packument = self.cache.get(®istry_name).ok_or_else(|| {
1467 Error::Registry(registry_name.clone(), "packument not in cache".to_string())
1468 })?;
1469 let pick = pick_version(
1470 packument,
1471 &task.range,
1472 locked_version,
1473 pick_lowest,
1474 cutoff_for_pkg,
1475 strict,
1476 );
1477 match pick {
1478 PickResult::Found(meta) => break meta.clone(),
1479 PickResult::AgeGated | PickResult::NoMatch
1480 if primer_seeded_names.remove(®istry_name) =>
1481 {
1482 let fetch_start = std::time::Instant::now();
1483 let live = if needs_time {
1484 match self.packument_full_cache_dir.as_ref() {
1485 Some(dir) => {
1486 self.client
1487 .fetch_packument_with_time_cached(®istry_name, dir)
1488 .await
1489 }
1490 None => self.client.fetch_packument(®istry_name).await,
1491 }
1492 } else {
1493 match self.client.fetch_packument(®istry_name).await {
1494 Ok(live) => {
1495 if let Some(dir) = self.packument_cache_dir.as_ref() {
1496 self.client.replace_packument_cache(
1497 ®istry_name,
1498 dir,
1499 &live,
1500 );
1501 }
1502 Ok(live)
1503 }
1504 Err(err) => Err(err),
1505 }
1506 }
1507 .map_err(|e| Error::Registry(registry_name.clone(), e.to_string()))?;
1508 packument_fetch_time += fetch_start.elapsed();
1509 packument_fetch_count += 1;
1510 self.cache.insert(registry_name.clone(), live);
1511 }
1512 // Only surface `AgeGate` when the cutoff actually
1513 // came from `minimumReleaseAge`. When it came from
1514 // `--resolution-mode=time-based` alone, the user
1515 // never opted into the supply-chain age gate, so
1516 // the failure should report as a plain no-match
1517 // instead of a misleading "older than 0 minutes".
1518 PickResult::AgeGated => match self.minimum_release_age.as_ref() {
1519 Some(mra) => {
1520 return Err(Error::AgeGate(Box::new(error::build_age_gate(
1521 &task,
1522 packument,
1523 mra.minutes,
1524 ))));
1525 }
1526 None => {
1527 return Err(Error::NoMatch(Box::new(error::build_no_match(
1528 &task, packument,
1529 ))));
1530 }
1531 },
1532 PickResult::NoMatch => {
1533 return Err(Error::NoMatch(Box::new(error::build_no_match(
1534 &task, packument,
1535 ))));
1536 }
1537 }
1538 };
1539 let packument = self.cache.get(®istry_name).ok_or_else(|| {
1540 Error::Registry(registry_name.clone(), "packument not in cache".to_string())
1541 })?;
1542 let picked_ref = prefer_non_vulnerable_pick(
1543 task.registry_name(),
1544 packument,
1545 &task.range,
1546 &selected_pick,
1547 pick_lowest,
1548 cutoff_for_pkg,
1549 &self.vulnerable_ranges,
1550 );
1551 // Trust-policy enforcement runs *before* any other
1552 // post-pick processing (mirrors pnpm's placement
1553 // immediately after `pickPackage`). Skip when policy is
1554 // off so the off-by-default case is a single enum
1555 // compare. The check needs the live packument's `time`
1556 // map and all version metadata, both of which are still
1557 // in scope here from L1191.
1558 if self.dependency_policy.trust_policy == crate::TrustPolicy::NoDowngrade {
1559 crate::trust::check_no_downgrade(
1560 packument,
1561 &picked_ref.version,
1562 picked_ref,
1563 &self.dependency_policy.trust_policy_exclude,
1564 self.dependency_policy.trust_policy_ignore_after,
1565 )
1566 .map_err(|e| match e {
1567 crate::trust::TrustCheckError::Downgrade(d) => {
1568 Error::TrustDowngrade(Box::new(d))
1569 }
1570 crate::trust::TrustCheckError::MissingTime(d) => {
1571 Error::TrustCheckMissingTime(Box::new(d))
1572 }
1573 })?;
1574 }
1575
1576 // Clone the picked metadata into an owned value so we can
1577 // both run the `readPackage` hook (which needs a
1578 // disjoint `&mut self` borrow) and, later, mutate the
1579 // resolver's own caches without holding a borrow into
1580 // `self.cache`. Also grab the publish-time entry now,
1581 // for the same reason.
1582 let mut picked_owned = picked_ref.clone();
1583 let picked_publish_time = packument.time.get(&picked_ref.version).cloned();
1584 // Skip the readPackage hook entirely for a `(name, version)`
1585 // pair we've already fully processed via a prior task. The
1586 // mutated dep maps only drive the transitive enqueue below,
1587 // and that block is short-circuited by the `visited` guard
1588 // later in this iteration — so running the hook here would
1589 // just burn an IPC round-trip whose result is discarded.
1590 let prehook_dep_path = dep_path_for(&task.name, &picked_ref.version);
1591 let already_visited = visited.contains(prehook_dep_path.as_str());
1592
1593 if !already_visited {
1594 apply_package_extensions(
1595 &mut picked_owned,
1596 &self.dependency_policy.package_extensions,
1597 );
1598 }
1599
1600 // readPackage hook. Runs at most once per version-picked
1601 // package, before transitive enqueue. We honor edits to
1602 // the four dep maps and warn on (then discard) edits to
1603 // name/version/dist/platform/`hasInstallScript` — pnpm
1604 // tolerates readPackage returning a hollowed-out
1605 // object, so we restore those fields from the original
1606 // packument entry after the call.
1607 if !already_visited && let Some(hook) = self.read_package_hook.as_mut() {
1608 let before_name = picked_owned.name.clone();
1609 let before_version = picked_owned.version.clone();
1610 let before_dist = picked_owned.dist.clone();
1611 let before_os = picked_owned.os.clone();
1612 let before_cpu = picked_owned.cpu.clone();
1613 let before_libc = picked_owned.libc.clone();
1614 let before_bundled = picked_owned.bundled_dependencies.clone();
1615 let before_has_install_script = picked_owned.has_install_script;
1616 let before_deprecated = picked_owned.deprecated.clone();
1617 let input = picked_owned.clone();
1618 let mut after = hook.read_package(input).await.map_err(|e| {
1619 Error::Registry(before_name.clone(), format!("readPackage hook: {e}"))
1620 })?;
1621 if after.name != before_name || after.version != before_version {
1622 tracing::warn!(
1623 code = aube_codes::warnings::WARN_AUBE_HOOK_IDENTITY_REWRITTEN,
1624 "[pnpmfile] readPackage rewrote {}@{} identity to {}@{}; \
1625 aube ignores identity edits",
1626 before_name,
1627 before_version,
1628 after.name,
1629 after.version,
1630 );
1631 }
1632 after.name = before_name;
1633 after.version = before_version;
1634 after.dist = before_dist;
1635 after.os = before_os;
1636 after.cpu = before_cpu;
1637 after.libc = before_libc;
1638 after.bundled_dependencies = before_bundled;
1639 after.has_install_script = before_has_install_script;
1640 after.deprecated = before_deprecated;
1641 picked_owned = after;
1642 }
1643 let version_meta = &picked_owned;
1644
1645 // Optional deps that don't match the host platform get
1646 // silently dropped — pnpm parity. Required deps with a
1647 // bad platform still get installed; the warning matches
1648 // pnpm's `packageIsInstallable` behavior.
1649 let platform_ok = is_supported(
1650 &version_meta.os,
1651 &version_meta.cpu,
1652 &version_meta.libc,
1653 &self.supported_architectures,
1654 );
1655 if !platform_ok {
1656 if task.dep_type == DepType::Optional {
1657 tracing::debug!(
1658 "skipping optional dep {}@{}: unsupported platform (os={:?} cpu={:?} libc={:?})",
1659 task.name,
1660 version_meta.version,
1661 version_meta.os,
1662 version_meta.cpu,
1663 version_meta.libc
1664 );
1665 if task.is_root
1666 && let Some(spec) = task.original_specifier.as_ref()
1667 {
1668 skipped_optional_dependencies
1669 .entry(task.importer.clone())
1670 .or_default()
1671 .insert(task.name.clone(), spec.clone());
1672 }
1673 if task.is_root {
1674 note_root_done!();
1675 }
1676 continue;
1677 }
1678 tracing::warn!(
1679 code = aube_codes::warnings::WARN_AUBE_UNSUPPORTED_PLATFORM_INSTALL,
1680 "required dep {}@{} declares unsupported platform (os={:?} cpu={:?} libc={:?}); installing anyway",
1681 task.name,
1682 version_meta.version,
1683 version_meta.os,
1684 version_meta.cpu,
1685 version_meta.libc
1686 );
1687 }
1688
1689 let version = version_meta.version.clone();
1690 let dep_path = dep_path_for(&task.name, &version);
1691
1692 // Record publish time for the cutoff / `time:` block
1693 // whenever the packument carries one — matches pnpm,
1694 // which populates `publishedAt` opportunistically via
1695 // `meta.time?.[version]` regardless of resolution mode.
1696 // Corgi packuments from npmjs.org omit `time`, so in
1697 // Highest mode this is usually a no-op; Verdaccio
1698 // (v5.15.1+) and full-packument fetches do include it,
1699 // and then we round-trip it into the lockfile just like
1700 // pnpm does.
1701 if self.should_record_times()
1702 && let Some(t) = picked_publish_time.as_ref()
1703 {
1704 resolved_times.insert(dep_path.clone(), t.clone());
1705 }
1706
1707 // Record root dep
1708 if task.is_root
1709 && let Some(deps) = importers.get_mut(&task.importer)
1710 {
1711 deps.push(DirectDep {
1712 name: task.name.clone(),
1713 dep_path: dep_path.clone(),
1714 dep_type: task.dep_type,
1715 specifier: task.original_specifier.clone(),
1716 });
1717 }
1718
1719 // Wire parent
1720 if let Some(ref parent_dp) = task.parent
1721 && let Some(parent_pkg) = resolved.get_mut(parent_dp)
1722 {
1723 parent_pkg
1724 .dependencies
1725 .insert(task.name.clone(), version.clone());
1726 if task.dep_type == DepType::Optional {
1727 parent_pkg
1728 .optional_dependencies
1729 .insert(task.name.clone(), version.clone());
1730 }
1731 }
1732
1733 // Skip if already fully processed this exact version
1734 if visited.contains(dep_path.as_str()) {
1735 if task.is_root {
1736 note_root_done!();
1737 }
1738 continue;
1739 }
1740 visited.insert(std::sync::Arc::from(dep_path.as_str()));
1741
1742 tracing::trace!("resolved {}@{}", task.name, version);
1743
1744 // Forward a deprecation message to the install command,
1745 // subject to `allowedDeprecatedVersions` suppression.
1746 // User-facing rendering is the CLI's job — doing it here
1747 // would fire per resolved version with no way for the
1748 // caller to batch or filter direct-vs-transitive.
1749 let deprecated_msg: Option<Arc<str>> =
1750 version_meta.deprecated.as_deref().and_then(|msg| {
1751 let suppressed = is_deprecation_allowed(
1752 &task.name,
1753 &version,
1754 &self.dependency_policy.allowed_deprecated_versions,
1755 );
1756 (!suppressed).then(|| Arc::<str>::from(msg))
1757 });
1758
1759 // Track this version
1760 resolved_versions
1761 .entry(task.name.clone())
1762 .or_default()
1763 .push(version.clone());
1764
1765 let integrity = version_meta.dist.as_ref().and_then(|d| d.integrity.clone());
1766 // Always stash the registry tarball URL on the locked
1767 // package. pnpm / yarn writers gate emission on
1768 // `lockfile_include_tarball_url` (so the pnpm
1769 // round-trip stays byte-identical for projects that
1770 // opted out); the npm writer emits `resolved:` on
1771 // every package entry unconditionally, which is what
1772 // npm itself writes. Carrying the URL on every
1773 // LockedPackage lets both policies work without a
1774 // second packument fetch at write time.
1775 let tarball_url = version_meta.dist.as_ref().map(|d| d.tarball.clone());
1776
1777 // Stream this resolved package for early tarball fetching.
1778 // `alias_of` mirrors what the LockedPackage below
1779 // will carry — the streaming fetch consumer in
1780 // install.rs uses it to derive the real tarball URL
1781 // for aliased packages where `name` alone (`h3-v2`)
1782 // would 404.
1783 if let Some(ref tx) = self.resolved_tx {
1784 let _ = tx
1785 .send(ResolvedPackage {
1786 dep_path: dep_path.clone(),
1787 name: task.name.clone(),
1788 version: version.clone(),
1789 integrity: integrity.clone(),
1790 tarball_url: tarball_url.clone(),
1791 alias_of: task.real_name.clone(),
1792 local_source: None,
1793 os: version_meta.os.iter().cloned().collect(),
1794 cpu: version_meta.cpu.iter().cloned().collect(),
1795 libc: version_meta.libc.iter().cloned().collect(),
1796 deprecated: deprecated_msg.clone(),
1797 unpacked_size: version_meta.dist.as_ref().and_then(|d| d.unpacked_size),
1798 })
1799 .await;
1800 }
1801
1802 // Capture the declared peer deps now so the post-pass can
1803 // compute each consumer's peer context without re-reading
1804 // the packument.
1805 let peer_deps = version_meta.peer_dependencies.clone();
1806 let peer_meta: BTreeMap<String, aube_lockfile::PeerDepMeta> = version_meta
1807 .peer_dependencies_meta
1808 .iter()
1809 .map(|(k, v)| {
1810 (
1811 k.clone(),
1812 aube_lockfile::PeerDepMeta {
1813 optional: v.optional,
1814 },
1815 )
1816 })
1817 .collect();
1818 // `bundledDependencies` names are shipped inside the
1819 // tarball itself and must not be resolved from the
1820 // registry. If we did enqueue them, we'd fetch a
1821 // (possibly different) version and plant a sibling
1822 // symlink inside `.aube/<parent>@ver/node_modules/`
1823 // that would shadow the bundled copy during Node's
1824 // directory walk. Compute the skip set once here and
1825 // store the names on the LockedPackage so restore
1826 // (from lockfile, skipping this code path) also
1827 // knows to avoid the sibling symlinks — see the
1828 // `.dependencies` write-through downstream.
1829 let bundled_names: FxHashSet<String> = version_meta
1830 .bundled_dependencies
1831 .as_ref()
1832 .map(|b| {
1833 b.names(&version_meta.dependencies)
1834 .into_iter()
1835 .map(String::from)
1836 .collect()
1837 })
1838 .unwrap_or_default();
1839
1840 resolved.insert(
1841 dep_path.clone(),
1842 LockedPackage {
1843 name: task.name.clone(),
1844 version: version.clone(),
1845 integrity,
1846 dependencies: BTreeMap::new(),
1847 optional_dependencies: BTreeMap::new(),
1848 peer_dependencies: peer_deps,
1849 peer_dependencies_meta: peer_meta,
1850 dep_path: dep_path.clone(),
1851 local_source: None,
1852 os: version_meta.os.iter().cloned().collect(),
1853 cpu: version_meta.cpu.iter().cloned().collect(),
1854 libc: version_meta.libc.iter().cloned().collect(),
1855 bundled_dependencies: {
1856 let mut v: Vec<String> = bundled_names.iter().cloned().collect();
1857 v.sort();
1858 v
1859 },
1860 tarball_url,
1861 // `name` is the alias for npm-aliased tasks
1862 // (`"h3-v2": "npm:h3@..."` → name = "h3-v2"),
1863 // so stash the real registry name here. The
1864 // lockfile writer + installer consult
1865 // `alias_of` whenever they need to hit the
1866 // registry, matching how the npm-lockfile
1867 // reader populates this field.
1868 alias_of: task.real_name.clone(),
1869 yarn_checksum: None,
1870 engines: version_meta.engines.clone(),
1871 // Rehydrate a string-form bin (`"bin": "cli.js"`)
1872 // into `{<package_name>: "cli.js"}` — registry
1873 // packuments leave the name off, expecting
1874 // consumers to default it to the package name.
1875 // Doing it here keeps bun's per-entry meta
1876 // byte-identical to bun's own output without
1877 // pushing the fixup into every writer.
1878 bin: {
1879 let mut m = version_meta.bin.clone();
1880 if let Some(path) = m.remove("") {
1881 // String-form `bin` in a packument
1882 // (`"bin": "cli.js"`) is implicitly
1883 // named after the real registry
1884 // package — not the alias. For an
1885 // aliased dep (`"h3-v2": "npm:h3@…"`)
1886 // the bun writer must emit the bin
1887 // under `h3`, not `h3-v2`, or the
1888 // map drifts against bun's own
1889 // output (and the shim install path
1890 // creates the wrong binary name).
1891 let bin_name =
1892 task.real_name.as_deref().unwrap_or(&task.name).to_string();
1893 m.insert(bin_name, path);
1894 }
1895 m
1896 },
1897 // Declared ranges straight from the packument's
1898 // `dependencies` / `optionalDependencies`. Fed
1899 // back out by npm / yarn / bun writers so
1900 // nested package entries keep the original
1901 // specifiers instead of collapsing to pins.
1902 declared_dependencies: {
1903 let mut m = version_meta.dependencies.clone();
1904 for (k, v) in &version_meta.optional_dependencies {
1905 m.insert(k.clone(), v.clone());
1906 }
1907 m
1908 },
1909 license: version_meta.license.clone(),
1910 funding_url: version_meta.funding_url.clone(),
1911 optional: false,
1912 transitive_peer_dependencies: Vec::new(),
1913 extra_meta: BTreeMap::new(),
1914 },
1915 );
1916
1917 // Enqueue transitive deps. Kick off a background
1918 // packument fetch the instant we discover the dep
1919 // name — so by the time the task is popped off the
1920 // queue below, its packument is usually already in
1921 // flight (and often already in cache). This is where
1922 // the pipeline overlaps fetches with CPU work without
1923 // any explicit wave barrier.
1924 //
1925 // Compute the child ancestor chain once — the same
1926 // frame (this package's name + resolved version)
1927 // applies to every dep / optionalDep / peer we enqueue
1928 // below.
1929 let mut child_ancestors = task.ancestors.clone();
1930 child_ancestors.push((task.name.clone(), version.clone()));
1931
1932 for (dep_name, dep_range) in &version_meta.dependencies {
1933 if bundled_names.contains(dep_name) {
1934 continue;
1935 }
1936 if self.dependency_policy.block_exotic_subdeps
1937 && is_non_registry_specifier(dep_range)
1938 {
1939 return Err(Error::Registry(
1940 dep_name.clone(),
1941 format!(
1942 "uses exotic specifier \"{dep_range}\" which is blocked \
1943 by blockExoticSubdeps (declared by {})",
1944 task.name
1945 ),
1946 ));
1947 }
1948 if !existing_names.contains(dep_name.as_str())
1949 && prefetchable!(dep_name.as_str(), dep_range.as_str())
1950 {
1951 ensure_fetch!(dep_name);
1952 }
1953 queue.push_back(ResolveTask::transitive(
1954 dep_name.clone(),
1955 dep_range.clone(),
1956 DepType::Production,
1957 dep_path.clone(),
1958 task.importer.clone(),
1959 child_ancestors.clone(),
1960 ));
1961 }
1962
1963 for (dep_name, dep_range) in &version_meta.optional_dependencies {
1964 if bundled_names.contains(dep_name) {
1965 continue;
1966 }
1967 if self.ignored_optional_dependencies.contains(dep_name) {
1968 continue;
1969 }
1970 if self.dependency_policy.block_exotic_subdeps
1971 && is_non_registry_specifier(dep_range)
1972 {
1973 tracing::warn!(
1974 code = aube_codes::warnings::WARN_AUBE_EXOTIC_SUBDEP_SKIPPED,
1975 "skipping optional dependency {dep_name} of {} — \
1976 exotic specifier \"{dep_range}\" blocked by blockExoticSubdeps",
1977 task.name
1978 );
1979 continue;
1980 }
1981 if !existing_names.contains(dep_name.as_str())
1982 && prefetchable!(dep_name.as_str(), dep_range.as_str())
1983 {
1984 ensure_fetch!(dep_name);
1985 }
1986 queue.push_back(ResolveTask::transitive(
1987 dep_name.clone(),
1988 dep_range.clone(),
1989 DepType::Optional,
1990 dep_path.clone(),
1991 task.importer.clone(),
1992 child_ancestors.clone(),
1993 ));
1994 }
1995
1996 // Peer dependencies: enqueue only required peers that
1997 // are truly missing from the importer/root scope. The
1998 // post-pass below (`apply_peer_contexts`) computes
1999 // which version each consumer sees, via ancestor
2000 // scope, and assigns peer-suffixed dep_paths.
2001 //
2002 // pnpm's `auto-install-peers=true` fills in missing
2003 // required peers, but it does not install optional peer
2004 // alternatives that the user did not ask for, and it
2005 // does not install a second compatible peer when the
2006 // importer already declares that peer name at an
2007 // incompatible version. In the latter case pnpm keeps
2008 // the user's direct dependency and reports an unmet
2009 // peer warning.
2010 //
2011 // When `auto-install-peers=false`, we skip enqueueing
2012 // peers entirely. Users are on the hook for adding
2013 // them to `package.json` themselves. Unmet peers still
2014 // surface as warnings via `detect_unmet_peers` after
2015 // resolve — in fact more so, since nothing gets
2016 // auto-installed.
2017 //
2018 // Skip peers that are already declared as regular or
2019 // optional deps of the same package — those already have a
2020 // task queued via the loops above, and duplicating would
2021 // just burn a queue slot.
2022 if self.auto_install_peers {
2023 for (dep_name, dep_range) in &version_meta.peer_dependencies {
2024 let peer_optional = version_meta
2025 .peer_dependencies_meta
2026 .get(dep_name)
2027 .map(|m| m.optional)
2028 .unwrap_or(false);
2029 // Optional peers are opt-in integrations, not
2030 // auto-install candidates. Users who need one must
2031 // declare it in their own manifest so the normal dep
2032 // loops above resolve it explicitly.
2033 if peer_optional {
2034 continue;
2035 }
2036 let importer_declares_peer = importer_declared_dep_names
2037 .get(&task.importer)
2038 .is_some_and(|names| names.contains(dep_name));
2039 let root_declares_peer = self.resolve_peers_from_workspace_root
2040 && task.importer != "."
2041 && importer_declared_dep_names
2042 .get(".")
2043 .is_some_and(|names| names.contains(dep_name));
2044 let peer_dep_is_ancestor =
2045 task.ancestors.iter().any(|(name, _)| name == dep_name);
2046 if importer_declares_peer || root_declares_peer || peer_dep_is_ancestor {
2047 continue;
2048 }
2049 if version_meta.dependencies.contains_key(dep_name)
2050 || version_meta.optional_dependencies.contains_key(dep_name)
2051 || bundled_names.contains(dep_name)
2052 {
2053 continue;
2054 }
2055 if self.dependency_policy.block_exotic_subdeps
2056 && is_non_registry_specifier(dep_range)
2057 {
2058 tracing::warn!(
2059 code = aube_codes::warnings::WARN_AUBE_EXOTIC_SUBDEP_SKIPPED,
2060 "skipping peer dependency {dep_name} of {} — \
2061 exotic specifier \"{dep_range}\" blocked \
2062 by blockExoticSubdeps",
2063 task.name
2064 );
2065 continue;
2066 }
2067 if !existing_names.contains(dep_name.as_str())
2068 && prefetchable!(dep_name.as_str(), dep_range.as_str())
2069 {
2070 ensure_fetch!(dep_name);
2071 }
2072 queue.push_back(ResolveTask::transitive(
2073 dep_name.clone(),
2074 dep_range.clone(),
2075 DepType::Production,
2076 dep_path.clone(),
2077 task.importer.clone(),
2078 child_ancestors.clone(),
2079 ));
2080 }
2081 }
2082
2083 // Root task just completed its full version-pick
2084 // path. Decrement the pending-directs counter so
2085 // the TimeBased cutoff trigger at the top of the
2086 // outer loop can fire once wave 0 is resolved.
2087 if task.is_root {
2088 note_root_done!();
2089 }
2090 }
2091 }
2092
2093 // Drain any remaining in-flight fetches so their tasks get
2094 // cleanly joined. Normally the main loop has harvested every
2095 // spawned fetch by the time the queue drains, but a few may
2096 // still be pending if the resolver short-circuited via
2097 // sibling dedupe or lockfile reuse after ensure_fetch! had
2098 // already spawned them.
2099 while in_flight.join_next().await.is_some() {}
2100
2101 let resolve_elapsed = resolve_start.elapsed();
2102 tracing::debug!(
2103 "resolver: {:.1?} total, {} packuments fetched ({:.1?} wall), {} reused from lockfile, {} packages resolved",
2104 resolve_elapsed,
2105 packument_fetch_count,
2106 packument_fetch_time,
2107 lockfile_reuse_count,
2108 resolved.len()
2109 );
2110 // Surface the resolver mix to the diag analyzer so the lockfile
2111 // reuse path can be spotted independently of the cold no-lockfile
2112 // path. Counts of: total packages resolved, of which N reused
2113 // from a prior lockfile and M required a network packument fetch.
2114 let resolved_count = resolved.len();
2115 aube_util::diag::instant_lazy(aube_util::diag::Category::Resolver, "decision_mix", || {
2116 format!(
2117 r#"{{"resolved":{},"lockfile_reused":{},"packuments_fetched":{}}}"#,
2118 resolved_count, lockfile_reuse_count, packument_fetch_count
2119 )
2120 });
2121
2122 let resolved_catalogs =
2123 catalog::materialize_catalog_picks(catalog_picks, &resolved_versions);
2124
2125 let canonical = LockfileGraph {
2126 importers,
2127 packages: resolved,
2128 settings: aube_lockfile::LockfileSettings {
2129 auto_install_peers: self.auto_install_peers,
2130 exclude_links_from_lockfile: self.exclude_links_from_lockfile,
2131 // Tarball-URL recording is a lockfile-writer concern; the
2132 // resolver never populates URLs itself. Install flips this
2133 // on after the graph is built when the setting is active.
2134 lockfile_include_tarball_url: false,
2135 },
2136 // Stamp the resolver's overrides into the output graph so the
2137 // lockfile writer can round-trip them and the next install's
2138 // drift check can compare them against the manifest.
2139 overrides: self.overrides.clone(),
2140 ignored_optional_dependencies: self.ignored_optional_dependencies.clone(),
2141 times: resolved_times,
2142 skipped_optional_dependencies,
2143 catalogs: resolved_catalogs,
2144 // Resolver output is format-agnostic; the bun writer layer
2145 // defaults `configVersion` to 1 when emitting a fresh
2146 // lockfile.
2147 bun_config_version: None,
2148 // Fresh resolves don't carry over unknown blocks; the
2149 // install-side merge (`overlay_metadata_from`) copies
2150 // them back from the prior lockfile when round-tripping.
2151 patched_dependencies: BTreeMap::new(),
2152 trusted_dependencies: Vec::new(),
2153 extra_fields: BTreeMap::new(),
2154 workspace_extra_fields: BTreeMap::new(),
2155 };
2156
2157 // Second pass: hoist every auto-installed peer to its importer's
2158 // direct deps so pnpm-style `node_modules/<peer>` top-level
2159 // symlinks get created and the lockfile's `importers.` section
2160 // lists them the way pnpm does with `auto-install-peers=true`.
2161 // Skipped entirely when the setting is off — matches pnpm, which
2162 // leaves the importer's `dependencies` untouched in that mode.
2163 let hoisted = if self.auto_install_peers {
2164 hoist_auto_installed_peers(canonical)
2165 } else {
2166 canonical
2167 };
2168
2169 // Third pass: compute peer-context suffixes for every reachable
2170 // package. See `apply_peer_contexts` for the details.
2171 let peer_options = PeerContextOptions {
2172 dedupe_peer_dependents: self.dedupe_peer_dependents,
2173 dedupe_peers: self.dedupe_peers,
2174 resolve_from_workspace_root: self.resolve_peers_from_workspace_root,
2175 peers_suffix_max_length: self.peers_suffix_max_length,
2176 };
2177 let _diag_peer =
2178 aube_util::diag::Span::new(aube_util::diag::Category::Resolver, "peer_context_apply");
2179 let contextualized = apply_peer_contexts(hoisted, &peer_options)?;
2180 drop(_diag_peer);
2181 tracing::debug!(
2182 "peer-context pass produced {} contextualized packages",
2183 contextualized.packages.len()
2184 );
2185 if let Some((state, sem)) = packument_persist_handle {
2186 sem.persist(&state, "packument:default");
2187 }
2188 Ok(contextualized)
2189 }
2190}
2191
2192fn is_vulnerable(
2193 package_name: &str,
2194 version: &str,
2195 vulnerable_ranges: &BTreeMap<String, Vec<String>>,
2196) -> bool {
2197 let Some(ranges) = vulnerable_ranges.get(package_name) else {
2198 return false;
2199 };
2200 let Ok(version) = node_semver::Version::parse(version) else {
2201 return false;
2202 };
2203 ranges
2204 .iter()
2205 .filter_map(|range| node_semver::Range::parse(range).ok())
2206 .any(|range| version.satisfies(&range))
2207}
2208
2209fn prefer_non_vulnerable_pick<'a>(
2210 package_name: &str,
2211 packument: &'a Packument,
2212 range_str: &str,
2213 fallback: &'a aube_registry::VersionMetadata,
2214 pick_lowest: bool,
2215 cutoff: Option<&str>,
2216 vulnerable_ranges: &BTreeMap<String, Vec<String>>,
2217) -> &'a aube_registry::VersionMetadata {
2218 if !is_vulnerable(package_name, &fallback.version, vulnerable_ranges) {
2219 return fallback;
2220 }
2221 let Ok(range) = node_semver::Range::parse(crate::semver_util::normalize_range(range_str))
2222 else {
2223 return fallback;
2224 };
2225 let passes_cutoff = |ver: &str| -> bool {
2226 let Some(c) = cutoff else { return true };
2227 match packument.time.get(ver) {
2228 Some(t) => t.as_str() <= c,
2229 None => true,
2230 }
2231 };
2232 let mut best: Option<(node_semver::Version, &'a aube_registry::VersionMetadata)> = None;
2233 for (ver_str, meta) in &packument.versions {
2234 let Ok(version) = node_semver::Version::parse(ver_str) else {
2235 continue;
2236 };
2237 if !version.satisfies(&range)
2238 || !passes_cutoff(ver_str)
2239 || is_vulnerable(package_name, ver_str, vulnerable_ranges)
2240 {
2241 continue;
2242 }
2243 let replace = best.as_ref().is_none_or(|(cur, _)| {
2244 if pick_lowest {
2245 version < *cur
2246 } else {
2247 version > *cur
2248 }
2249 });
2250 if replace {
2251 best = Some((version, meta));
2252 }
2253 }
2254 best.map(|(_, meta)| meta).unwrap_or(fallback)
2255}
2256
2257/// Seed the BFS queue with direct deps from every importer manifest.
2258///
2259/// When a package is declared in more than one section
2260/// (`dependencies` + `devDependencies`, etc.) we keep only the
2261/// highest-priority entry — `dependencies` > `devDependencies` >
2262/// `optionalDependencies` — matching pnpm, which silently drops
2263/// the lower-priority duplicates on resolve. Without this the
2264/// same name gets pushed into the importer's `DirectDep` list
2265/// twice (once per section), and the linker's parallel step 2
2266/// races to create the same `node_modules/<name>` symlink from
2267/// two tasks, producing an `EEXIST` on the loser.
2268fn seed_direct_deps(
2269 manifests: &[(String, PackageJson)],
2270 ignored_optional_dependencies: &BTreeSet<String>,
2271 queue: &mut VecDeque<ResolveTask>,
2272 importers: &mut BTreeMap<String, Vec<DirectDep>>,
2273) {
2274 for (importer_path, manifest) in manifests {
2275 importers.insert(importer_path.clone(), Vec::new());
2276
2277 for (name, range) in &manifest.dependencies {
2278 queue.push_back(ResolveTask::root(
2279 name.clone(),
2280 range.clone(),
2281 DepType::Production,
2282 importer_path.clone(),
2283 ));
2284 }
2285 for (name, range) in &manifest.dev_dependencies {
2286 if manifest.dependencies.contains_key(name) {
2287 continue;
2288 }
2289 queue.push_back(ResolveTask::root(
2290 name.clone(),
2291 range.clone(),
2292 DepType::Dev,
2293 importer_path.clone(),
2294 ));
2295 }
2296 for (name, range) in &manifest.optional_dependencies {
2297 if ignored_optional_dependencies.contains(name) {
2298 tracing::debug!(
2299 "ignoring optional dependency {name} (pnpm.ignoredOptionalDependencies)"
2300 );
2301 continue;
2302 }
2303 if manifest.dependencies.contains_key(name)
2304 || manifest.dev_dependencies.contains_key(name)
2305 {
2306 continue;
2307 }
2308 queue.push_back(ResolveTask::root(
2309 name.clone(),
2310 range.clone(),
2311 DepType::Optional,
2312 importer_path.clone(),
2313 ));
2314 }
2315 }
2316}