Skip to main content

aube_resolver/
lib.rs

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