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