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