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