aube_resolver/builder.rs
1use crate::{
2 DependencyPolicy, MinimumReleaseAge, ReadPackageHook, ResolutionMode, ResolvedPackage,
3 Resolver, SupportedArchitectures, override_rule,
4};
5use aube_registry::client::RegistryClient;
6use rustc_hash::FxHashMap;
7use std::collections::{BTreeMap, BTreeSet};
8use std::path::PathBuf;
9use std::sync::Arc;
10use tokio::sync::mpsc;
11
12impl Resolver {
13 pub fn new(client: Arc<RegistryClient>) -> Self {
14 Self {
15 client,
16 cache: FxHashMap::default(),
17 resolved_tx: None,
18 packument_cache_dir: None,
19 packument_full_cache_dir: None,
20 auto_install_peers: true,
21 exclude_links_from_lockfile: false,
22 supported_architectures: SupportedArchitectures::default(),
23 overrides: BTreeMap::new(),
24 override_rules: Vec::new(),
25 ignored_optional_dependencies: BTreeSet::new(),
26 resolution_mode: ResolutionMode::Highest,
27 project_root: PathBuf::from("."),
28 minimum_release_age: None,
29 catalogs: BTreeMap::new(),
30 read_package_hook: None,
31 dependency_policy: DependencyPolicy::default(),
32 vulnerable_ranges: BTreeMap::new(),
33 git_shallow_hosts: Vec::new(),
34 peers_suffix_max_length: 1000,
35 dedupe_peer_dependents: true,
36 dedupe_peers: false,
37 resolve_peers_from_workspace_root: true,
38 registry_supports_time_field: false,
39 packument_network_concurrency: None,
40 }
41 }
42
43 /// Create a resolver that streams resolved packages through a channel.
44 /// Returns `(resolver, receiver)`. The receiver yields packages as they're
45 /// discovered, allowing tarball fetches to start during resolution.
46 pub fn with_stream(
47 client: Arc<RegistryClient>,
48 ) -> (Self, mpsc::UnboundedReceiver<ResolvedPackage>) {
49 let (tx, rx) = mpsc::unbounded_channel();
50 (
51 Self {
52 client,
53 cache: FxHashMap::default(),
54 resolved_tx: Some(tx),
55 packument_cache_dir: None,
56 packument_full_cache_dir: None,
57 auto_install_peers: true,
58 exclude_links_from_lockfile: false,
59 supported_architectures: SupportedArchitectures::default(),
60 overrides: BTreeMap::new(),
61 override_rules: Vec::new(),
62 ignored_optional_dependencies: BTreeSet::new(),
63 resolution_mode: ResolutionMode::Highest,
64 project_root: PathBuf::from("."),
65 minimum_release_age: None,
66 catalogs: BTreeMap::new(),
67 read_package_hook: None,
68 dependency_policy: DependencyPolicy::default(),
69 vulnerable_ranges: BTreeMap::new(),
70 git_shallow_hosts: Vec::new(),
71 peers_suffix_max_length: 1000,
72 dedupe_peer_dependents: true,
73 dedupe_peers: false,
74 resolve_peers_from_workspace_root: true,
75 registry_supports_time_field: false,
76 packument_network_concurrency: None,
77 },
78 rx,
79 )
80 }
81
82 pub fn with_packument_network_concurrency(mut self, n: Option<usize>) -> Self {
83 self.packument_network_concurrency = n.filter(|&n| n > 0);
84 self
85 }
86
87 /// Enable disk-backed packument caching with ETag/Last-Modified revalidation.
88 pub fn with_packument_cache(mut self, cache_dir: std::path::PathBuf) -> Self {
89 self.packument_cache_dir = Some(cache_dir);
90 self
91 }
92
93 /// Disk cache for full (non-corgi) packuments, used in
94 /// `ResolutionMode::TimeBased` so we can read the `time:` map.
95 pub fn with_packument_full_cache(mut self, cache_dir: std::path::PathBuf) -> Self {
96 self.packument_full_cache_dir = Some(cache_dir);
97 self
98 }
99
100 /// Set the resolution mode. Defaults to `Highest` (pnpm's classic
101 /// behavior). `TimeBased` switches direct deps to lowest-satisfying
102 /// and constrains transitives by a publish-date cutoff.
103 pub fn with_resolution_mode(mut self, mode: ResolutionMode) -> Self {
104 self.resolution_mode = mode;
105 self
106 }
107
108 /// Configure pnpm v11's `minimumReleaseAge` family of settings.
109 /// Pass `None` (or a config with `minutes == 0`) to disable.
110 pub fn with_minimum_release_age(mut self, mra: Option<MinimumReleaseAge>) -> Self {
111 self.minimum_release_age = mra.filter(|m| m.minutes > 0);
112 self
113 }
114
115 /// Whether the resolver should round-trip registry `time:` entries
116 /// into the output graph. pnpm only writes `time:` to its lockfile
117 /// when one of `resolution-mode=time-based` / `minimumReleaseAge`
118 /// is active — otherwise the field is dead weight and, worse, shows
119 /// up as churn in a pnpm ↔ aube diff. Gate the insertion at the
120 /// two `resolved_times.insert` call sites on this predicate so
121 /// Highest-mode installs never populate the map.
122 pub(crate) fn should_record_times(&self) -> bool {
123 self.resolution_mode == ResolutionMode::TimeBased
124 || self.minimum_release_age.is_some()
125 || self.dependency_policy.trust_policy == crate::TrustPolicy::NoDowngrade
126 }
127
128 /// Override the default `auto-install-peers=true` behavior. pnpm reads
129 /// this from `.npmrc` or `pnpm-workspace.yaml`; aube's install command
130 /// plumbs the resolved value through here before running resolution.
131 pub fn with_auto_install_peers(mut self, auto_install_peers: bool) -> Self {
132 self.auto_install_peers = auto_install_peers;
133 self
134 }
135
136 /// Configure pnpm's `peersSuffixMaxLength`. When the peer suffix on a
137 /// `dep_path` would exceed this many bytes, the post-pass replaces it
138 /// with `_<10-char-sha256-hex>`. Default 1000 (pnpm's default).
139 pub fn with_peers_suffix_max_length(mut self, max_length: usize) -> Self {
140 self.peers_suffix_max_length = max_length;
141 self
142 }
143
144 /// Override the default `dedupe-peer-dependents=true` behavior. When
145 /// false, the peer-context pass keeps every distinct ancestor-scope
146 /// variant of a package instead of collapsing peer-equivalent ones
147 /// into a single dep_path. Plumbed from `.npmrc` /
148 /// `pnpm-workspace.yaml` via the install command.
149 pub fn with_dedupe_peer_dependents(mut self, value: bool) -> Self {
150 self.dedupe_peer_dependents = value;
151 self
152 }
153
154 /// Override the default `dedupe-peers=false` behavior. When true,
155 /// peer suffixes in the lockfile drop the peer name and emit only
156 /// the resolved version — `(18.2.0)` instead of `(react@18.2.0)`.
157 /// Plumbed from `.npmrc` / `pnpm-workspace.yaml` via the install
158 /// command.
159 pub fn with_dedupe_peers(mut self, value: bool) -> Self {
160 self.dedupe_peers = value;
161 self
162 }
163
164 /// Override the default `resolve-peers-from-workspace-root=true`
165 /// behavior. When false, peer resolution stops at the importer's
166 /// own scope + BFS-auto-installed transitives instead of consulting
167 /// the workspace root's direct deps as a fallback tier. Plumbed
168 /// from `.npmrc` / `pnpm-workspace.yaml` via the install command.
169 pub fn with_resolve_peers_from_workspace_root(mut self, value: bool) -> Self {
170 self.resolve_peers_from_workspace_root = value;
171 self
172 }
173
174 /// Configure pnpm's `registry-supports-time-field`. When true,
175 /// the resolver keeps using the abbreviated (corgi) packument
176 /// path even when `time:` is needed, saving one full-packument
177 /// fetch per distinct package. Safe for registries that embed
178 /// `time` in their abbreviated responses (Verdaccio 5.15.1+, JSR,
179 /// most in-house mirrors); leave at the default `false` for
180 /// npmjs.org.
181 pub fn with_registry_supports_time_field(mut self, value: bool) -> Self {
182 self.registry_supports_time_field = value;
183 self
184 }
185
186 /// Configure pnpm's `exclude-links-from-lockfile` setting. Only
187 /// affects lockfile serialization — the resolver still builds the
188 /// same graph either way, but the value is stamped into
189 /// `LockfileGraph::settings` so the pnpm writer can filter `link:`
190 /// importer entries on write.
191 pub fn with_exclude_links_from_lockfile(mut self, value: bool) -> Self {
192 self.exclude_links_from_lockfile = value;
193 self
194 }
195
196 /// Override the host platform triple used when filtering optional
197 /// dependencies. See [`platform::SupportedArchitectures`].
198 pub fn with_supported_architectures(mut self, value: SupportedArchitectures) -> Self {
199 self.supported_architectures = value;
200 self
201 }
202
203 /// Provide dependency overrides. The map's keys are selector
204 /// strings — bare name, `parent>child`, `foo@<2`, `**/foo`, or any
205 /// combination thereof — and values are version specifiers (or
206 /// `npm:` aliases). Keys are compiled into `override_rule`
207 /// structures; unparseable keys are dropped. Whenever the resolver
208 /// encounters a task matching a rule (by name + ancestor chain +
209 /// optional version constraints), the requested range is replaced
210 /// with the rule's replacement before any packument fetch or
211 /// version pick. Workspace + manifest sources are merged by the
212 /// caller.
213 pub fn with_overrides(mut self, overrides: BTreeMap<String, String>) -> Self {
214 self.override_rules = override_rule::compile(&overrides);
215 self.overrides = overrides;
216 self
217 }
218
219 /// Provide workspace catalog ranges. Outer key is the catalog name
220 /// (`default` for the unnamed `catalog:` field in
221 /// `pnpm-workspace.yaml`); inner key is the package name. The
222 /// resolver rewrites `catalog:` and `catalog:<name>` task ranges
223 /// against this map before the override / npm-alias passes, and
224 /// records the picks in the output graph's `catalogs` field.
225 pub fn with_catalogs(mut self, catalogs: BTreeMap<String, BTreeMap<String, String>>) -> Self {
226 self.catalogs = catalogs;
227 self
228 }
229
230 /// Set the project root used to resolve `file:` / `link:` paths.
231 /// `file:./vendor/foo` resolves against this directory, and a
232 /// matching directory / tarball is read to drive resolution of the
233 /// local package's transitive deps.
234 pub fn with_project_root(mut self, project_root: PathBuf) -> Self {
235 self.project_root = project_root;
236 self
237 }
238
239 /// Names to strip from every `optionalDependencies` map before
240 /// enqueueing (pnpm's `pnpm.ignoredOptionalDependencies`). Applied
241 /// to both root and transitive optional deps. Empty by default.
242 pub fn with_ignored_optional_dependencies(mut self, ignored: BTreeSet<String>) -> Self {
243 self.ignored_optional_dependencies = ignored;
244 self
245 }
246
247 /// Install a `readPackage` hook. The resolver calls it once per
248 /// version-picked packument before enqueueing transitives; see
249 /// [`ReadPackageHook`] for what mutations are honored.
250 pub fn with_read_package_hook(mut self, hook: Box<dyn ReadPackageHook>) -> Self {
251 self.read_package_hook = Some(hook);
252 self
253 }
254
255 /// Configure dependency resolution policy settings such as
256 /// `packageExtensions`, `allowedDeprecatedVersions`, `trustPolicy*`,
257 /// and `blockExoticSubdeps`.
258 pub fn with_dependency_policy(mut self, policy: DependencyPolicy) -> Self {
259 self.dependency_policy = policy;
260 self
261 }
262
263 /// Prefer non-vulnerable versions for the supplied audit ranges.
264 /// Used by `audit --fix=update` to reuse the normal resolver while
265 /// steering only vulnerable packages away from affected versions.
266 pub fn with_vulnerable_ranges(mut self, ranges: BTreeMap<String, Vec<String>>) -> Self {
267 self.vulnerable_ranges = ranges;
268 self
269 }
270
271 /// Set the `git-shallow-hosts` list used when cloning git deps.
272 /// When a git URL's host matches an entry here (exact match,
273 /// same as pnpm), aube attempts a shallow fetch by SHA; other
274 /// hosts get a plain `git fetch origin`. An empty list forces
275 /// every git dep through the full-fetch path.
276 pub fn with_git_shallow_hosts(mut self, hosts: Vec<String>) -> Self {
277 self.git_shallow_hosts = hosts;
278 self
279 }
280}