Skip to main content

aube_resolver/
lib.rs

1mod builder;
2mod catalog;
3mod direct_dep_info;
4mod error;
5mod local_source;
6// `locked_index` is an internal lockfile-reuse index. It is only `pub` under
7// the `bench` feature so the `locked_lookup` bench can reach `LockedIndex`;
8// the default build keeps it `pub(crate)`, off the public surface.
9#[cfg(feature = "bench")]
10pub mod locked_index;
11#[cfg(not(feature = "bench"))]
12pub(crate) mod locked_index;
13pub mod override_rule;
14mod package_ext;
15mod peer_context;
16pub mod platform;
17mod primer;
18mod resolve;
19mod semver_util;
20mod trust;
21mod types;
22
23pub use direct_dep_info::DirectDepInfo;
24pub use error::{AgeGateDetails, CatalogDetails, Error, ExoticSubdepDetails, NoMatchDetails};
25pub use local_source::resolve_exec_script_path;
26pub use package_ext::is_deprecation_allowed;
27pub use peer_context::{
28    PeerContextOptions, UnmetPeer, apply_peer_contexts, detect_unmet_peers,
29    hoist_auto_installed_peers,
30};
31pub use platform::{SupportedArchitectures, is_supported};
32pub use primer::{
33    PruneStats as PrimerPruneStats, popular_package_names, prune_cache as prune_primer_cache,
34};
35pub use semver_util::{PickResult, pick_version_for_add};
36pub use trust::{
37    MissingTimeDetails as MissingTrustTimeDetails, PriorTrustEvidence, TrustCheckError,
38    TrustDowngradeDetails, check_no_downgrade, evidence_for, strongest_prior_evidence,
39};
40pub use trust::{PackageVersionPolicy, TrustEvidence, TrustExcludeParseError, TrustExcludeRules};
41pub use types::{
42    DependencyPolicy, MinimumReleaseAge, PackageExtension, ReadPackageHook, ResolutionMode,
43    ResolvedPackage, TrustPolicy,
44};
45
46pub const YARN_EXEC_WRAPPER: &str = r#"
47const env = JSON.parse(process.env.AUBE_YARN_EXEC_ENV);
48globalThis.execEnv = env;
49for (const name of ['fs', 'path', 'child_process', 'os', 'crypto', 'url', 'util', 'stream', 'buffer']) {
50  globalThis[name] = require(name);
51}
52(async () => {
53  await import(url.pathToFileURL(process.argv[1]).href);
54})().catch((err) => {
55  console.error(err);
56  process.exit(1);
57});
58"#;
59
60use semver_util::version_satisfies;
61
62#[cfg(test)]
63use aube_lockfile::{DirectDep, LocalSource, LockedPackage, LockfileGraph};
64#[cfg(test)]
65use aube_manifest::PackageJson;
66#[cfg(test)]
67use error::{
68    RegistryErrorKind, build_age_gate, build_no_match, classify_registry_error,
69    format_registry_help,
70};
71#[cfg(test)]
72use local_source::{dep_path_for, should_block_exotic_subdep};
73#[cfg(test)]
74use package_ext::{
75    apply_package_extensions, apply_package_extensions_to_deps, package_selector_matches,
76    pick_override_spec,
77};
78#[cfg(test)]
79use peer_context::{
80    apply_dedupe_peers_to_key, contains_canonical_back_ref, dedupe_peer_suffixes,
81    dedupe_peer_variants, effective_peer_suffix, is_hashed_peer_suffix,
82};
83#[cfg(test)]
84use semver_util::{pick_version, strip_alias_prefix};
85#[cfg(test)]
86use types::format_iso8601_utc;
87
88use aube_lockfile::DepType;
89use aube_registry::Packument;
90use aube_registry::client::RegistryClient;
91use std::collections::{BTreeMap, BTreeSet};
92use std::path::PathBuf;
93use std::sync::Arc;
94use tokio::sync::mpsc;
95
96// Re-export shared aube-util collection aliases under the original
97// FxHashMap name to avoid touching every call site.
98pub(crate) use aube_util::collections::FxMap as FxHashMap;
99pub(crate) use aube_util::collections::FxSet as FxHashSet;
100
101/// BFS dependency resolver.
102pub struct Resolver {
103    client: Arc<RegistryClient>,
104    cache: FxHashMap<String, Packument>,
105    /// Optional channel to stream resolved packages as they're discovered.
106    resolved_tx: Option<mpsc::Sender<ResolvedPackage>>,
107    /// Optional disk cache directory for packuments (with ETag revalidation).
108    packument_cache_dir: Option<std::path::PathBuf>,
109    /// Separate disk cache for full (non-corgi) packuments; only used
110    /// when `resolution_mode` is `TimeBased` (which needs the `time:`
111    /// map). Defaults to the sibling `packuments-full-v1/` directory
112    /// next to `packument_cache_dir`.
113    packument_full_cache_dir: Option<std::path::PathBuf>,
114    /// When true (pnpm's default), a package's declared `peerDependencies`
115    /// are enqueued like regular transitives and — if not already
116    /// satisfied by the importer — hoisted to the importer's direct deps.
117    /// When false, peers neither get auto-installed as transitives nor
118    /// hoisted; unmet peers still surface as warnings via
119    /// `detect_unmet_peers`, but the user is on the hook for adding them
120    /// explicitly to `package.json`.
121    auto_install_peers: bool,
122    /// pnpm's `exclude-links-from-lockfile`. Round-tripped through the
123    /// lockfile's `settings:` header; when true, the pnpm writer omits
124    /// `link:` deps from the importer `dependencies:` maps so a
125    /// sibling symlink change doesn't churn the lockfile. Defaults to
126    /// false (pnpm's default). Does not affect resolution itself, only
127    /// the `canonical.settings.exclude_links_from_lockfile` flag the
128    /// writer reads.
129    exclude_links_from_lockfile: bool,
130    /// User-declared override for the host platform triple, used when
131    /// deciding whether an optional dep's `os`/`cpu`/`libc` constraints
132    /// are satisfied. Empty fields fall back to the host.
133    supported_architectures: SupportedArchitectures,
134    /// Raw dependency override map from the manifest (selector key →
135    /// replacement spec). Round-tripped verbatim through the lockfile
136    /// for drift detection; the compiled form in `override_rules` is
137    /// what the resolver hot loop actually consults.
138    overrides: BTreeMap<String, String>,
139    /// Compiled view of `overrides`. Built by `with_overrides`.
140    /// Unparseable selector keys are dropped at compile time so the
141    /// matcher never has to think about them.
142    override_rules: Vec<override_rule::OverrideRule>,
143    /// Names listed in the root manifest's `pnpm.ignoredOptionalDependencies`.
144    /// Any optional dep (root or transitive) whose name is in this set is
145    /// dropped before enqueueing — the resolver never fetches or locks it.
146    /// Mirrors pnpm's `createOptionalDependenciesRemover` read-package hook.
147    ignored_optional_dependencies: BTreeSet<String>,
148    /// pnpm's `resolution-mode` — `Highest` (default) or `TimeBased`.
149    resolution_mode: ResolutionMode,
150    /// Project root used to resolve `file:` / `link:` paths to the
151    /// target directory. Defaults to the current working directory;
152    /// callers set it via `with_project_root`.
153    project_root: PathBuf,
154    /// When true, resolver-time `exec:` generators are blocked the
155    /// same way fetch-time execution is blocked.
156    ignore_scripts: bool,
157    /// pnpm v11's `minimumReleaseAge` triplet. `None` disables the
158    /// supply-chain age gate entirely (matching `minimumReleaseAge: 0`).
159    minimum_release_age: Option<MinimumReleaseAge>,
160    /// Workspace catalog ranges. Outer key is the catalog name
161    /// (`default` for the unnamed `catalog:` field in
162    /// `pnpm-workspace.yaml`); inner key is the package name; value is
163    /// the version range. When the resolver encounters a `catalog:` or
164    /// `catalog:<name>` task range, it rewrites the task in place to
165    /// the matching range *before* the override / npm-alias passes,
166    /// while preserving the original `catalog:...` text in
167    /// `original_specifier` so the lockfile importer keeps the
168    /// reference verbatim.
169    catalogs: BTreeMap<String, BTreeMap<String, String>>,
170    /// Optional `readPackage` hook, invoked once per resolved package
171    /// before its transitive deps are enqueued. See [`ReadPackageHook`].
172    /// Wired up by `aube` when a `.pnpmfile.cjs` is detected and
173    /// `--ignore-pnpmfile` was not set.
174    read_package_hook: Option<Box<dyn ReadPackageHook>>,
175    dependency_policy: DependencyPolicy,
176    /// Advisory ranges to avoid when resolving audit fixes. The map is
177    /// keyed by registry package name and values are npm semver ranges
178    /// from `vulnerable_versions`. When a clean satisfying version
179    /// exists, it wins over locked/sibling reuse and the normal highest
180    /// pick; if not, resolution falls back to the ordinary pick so the
181    /// caller can report the advisory as remaining.
182    vulnerable_ranges: BTreeMap<String, Vec<String>>,
183    /// Hosts for which aube performs shallow git clones, mirroring
184    /// pnpm's `git-shallow-hosts`. When a git dep's URL host is in
185    /// this list, the store attempts `git fetch --depth 1 origin
186    /// <sha>` (falling back to a full fetch if the server refuses);
187    /// otherwise it goes straight to a full fetch. Defaults to an
188    /// empty list — `aube` populates it from the generated
189    /// `aube_settings::resolved::git_shallow_hosts` accessor (which
190    /// carries the pnpm-compat default list baked in from
191    /// `settings.toml`) via [`Self::with_git_shallow_hosts`]. Library
192    /// callers who construct a `Resolver` directly must set it
193    /// explicitly if they want the pnpm list; keeping the list in
194    /// one place (`settings.toml`) avoids drift.
195    git_shallow_hosts: Vec<String>,
196    /// pnpm's `peersSuffixMaxLength`. When the peer-ID suffix body on a
197    /// `dep_path` (the `(name@version)(…)` portion without its outer
198    /// parens) would exceed this many bytes, the post-pass replaces the
199    /// whole suffix with a parenthesized short hash `(<short-hash>)` —
200    /// the first 32 chars of SHA-256 of the body, matching pnpm's
201    /// `createPeerDepGraphHash` lockfile format. Default 1000.
202    peers_suffix_max_length: usize,
203    /// pnpm's `dedupe-peer-dependents`. When true (pnpm's default),
204    /// the peer-context post-pass collapses multiple dep_path variants
205    /// of the same canonical package into a single entry when their
206    /// peer resolutions are pairwise-equivalent. When false, every
207    /// distinct ancestor scope gets its own variant — useful for
208    /// debugging peer-context divergence or mimicking pnpm v6/v7
209    /// behavior.
210    dedupe_peer_dependents: bool,
211    /// pnpm's `dedupe-peers`. When true, peer suffixes in the lockfile
212    /// emit just the resolved version — `(18.2.0)` — instead of the
213    /// full `(react@18.2.0)` form. Shorter dep_paths at the cost of
214    /// peer-name fidelity in the snapshot. Defaults to false.
215    dedupe_peers: bool,
216    /// pnpm's `resolve-peers-from-workspace-root`. When true (pnpm's
217    /// default), an importer's unresolved peer can be satisfied by a
218    /// dependency declared in the root importer's `package.json`, even
219    /// when no ancestor scope carries that dep. Common monorepo knob:
220    /// the workspace root pins shared peers like `react`, and every
221    /// subpackage can peer on it without hoisting the version into
222    /// every sibling.
223    resolve_peers_from_workspace_root: bool,
224    /// pnpm's `registry-supports-time-field`. When true, the resolver
225    /// trusts the abbreviated (corgi) packument to carry the `time:`
226    /// map and keeps using the cheap `fetch_packument_cached` path
227    /// even under time-aware resolution (`TimeBased` or
228    /// `minimumReleaseAge`). Defaults to false — the same assumption
229    /// pnpm and npmjs.org ship with — so the resolver falls back to
230    /// the full-packument fetch to get `time:` reliably. No effect
231    /// when neither time-based resolution nor `minimumReleaseAge` is
232    /// active, since the abbreviated path is already the only one
233    /// running.
234    registry_supports_time_field: bool,
235    /// Use the bundled metadata primer even when the configured
236    /// registry is not npmjs.org. Intended for npm-compatible mirrors
237    /// and controlled benchmarks; tarball URLs are rewritten to the
238    /// active registry before cache seeding so installs still fetch
239    /// package bytes from the configured source.
240    force_metadata_primer: bool,
241    pub(crate) packument_network_concurrency: Option<usize>,
242}
243
244pub(crate) struct ResolveTask {
245    pub(crate) name: String,
246    pub(crate) range: String,
247    dep_type: DepType,
248    is_root: bool,
249    /// The parent dep_path, for wiring up transitive dep references
250    parent: Option<String>,
251    /// Which importer this task belongs to (e.g., "." or "packages/app")
252    pub(crate) importer: String,
253    /// The original specifier from package.json before any rewrites
254    /// (e.g. `"npm:real-pkg@^2.0.0"` for an alias, or `"^4.17.0"` for a normal range).
255    /// Only set for root deps; recorded into the lockfile for drift detection.
256    pub(crate) original_specifier: Option<String>,
257    /// Real registry package name for npm-alias tasks.
258    ///
259    /// When a task arrives with `range` like `"npm:h3@2.0.1-rc.20"`,
260    /// the preprocessing loop strips the prefix and sets this field to
261    /// the real package name (`"h3"`) while *keeping* `name` as the
262    /// user-facing alias (`"h3-v2"`, the key the package.json used).
263    /// Every identity-facing site — dep_path formation, direct-dep
264    /// records, parent `dependencies` wiring, the resolved-versions
265    /// dedupe map — uses `name`, so the alias survives all the way
266    /// to the linker and ends up as `node_modules/<alias>/` with
267    /// `LockedPackage.alias_of = Some(real_name)`. Only registry
268    /// I/O (packument fetch, tarball URL derivation) consults this
269    /// field.
270    ///
271    /// `None` for ordinary (non-aliased) tasks — `name` is already
272    /// the registry name and nothing downstream needs to distinguish.
273    real_name: Option<String>,
274    /// Outermost-first chain of `(name, version)` ancestors above this
275    /// task in the dependency graph, used by `parent>child` override
276    /// selectors. Empty for root/importer deps. Each child-enqueue
277    /// site is responsible for extending its parent's chain with the
278    /// parent's own `(name, version)` frame.
279    ///
280    /// `Arc<[_]>` rather than `Vec` because the chain is immutable once
281    /// built and is shared, unmodified, by every dependency a package
282    /// enqueues: the child chain is materialized into a `Vec` once per
283    /// package, frozen here, and each per-dep enqueue is then a refcount
284    /// bump instead of a full deep clone (clone cost scaled with graph
285    /// edges × depth before this).
286    pub(crate) ancestors: Arc<[(String, String)]>,
287    /// `true` when an override rewrote `range` to a `link:`/`file:`
288    /// path. Override paths are anchored at the project root (where the
289    /// override is declared), not at the consuming workspace package or
290    /// transitive parent — same convention pnpm follows. Without this
291    /// signal the local-source resolver would re-anchor `link:./libs/x`
292    /// against the importer or parent dir and walk to a phantom path.
293    pub(crate) range_from_override: bool,
294}
295
296impl ResolveTask {
297    /// Name to use for registry operations (packument fetch, tarball
298    /// URL). Returns `real_name` for aliased tasks and `name`
299    /// otherwise. Every call site that talks to the registry goes
300    /// through this accessor so alias handling stays localized.
301    fn registry_name(&self) -> &str {
302        self.real_name.as_deref().unwrap_or(&self.name)
303    }
304
305    /// Construct a root-importer task for `(name, range)` under
306    /// `importer`, with the appropriate `dep_type` and no parent/ancestry.
307    /// Every root-dep enqueue site uses this shape; the factory keeps
308    /// the literal in one place so a new field added to `ResolveTask`
309    /// lands consistently across prod/dev/optional loops.
310    fn root(name: String, range: String, dep_type: DepType, importer: String) -> Self {
311        let original = range.clone();
312        Self {
313            name,
314            range,
315            dep_type,
316            is_root: true,
317            parent: None,
318            importer,
319            original_specifier: Some(original),
320            real_name: None,
321            ancestors: Arc::from([]),
322            range_from_override: false,
323        }
324    }
325
326    /// Construct a transitive (non-root) task discovered by walking a
327    /// parent package's dependency map. Carries the parent dep_path
328    /// and inherited ancestor chain for overrides.
329    fn transitive(
330        name: String,
331        range: String,
332        dep_type: DepType,
333        parent: String,
334        importer: String,
335        ancestors: Arc<[(String, String)]>,
336    ) -> Self {
337        Self {
338            name,
339            range,
340            dep_type,
341            is_root: false,
342            parent: Some(parent),
343            importer,
344            original_specifier: None,
345            real_name: None,
346            ancestors,
347            range_from_override: false,
348        }
349    }
350}
351
352#[cfg(test)]
353mod tests;