mitts 0.3.17

Rust-first build and package tooling with a shared artifact cache
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Mitts Build — Rust-first build and package tooling with shared artifact cache
 * Copyright (c) 2026 Alexander Panasenko
 *
 * Contact: alex@prod.codes
 * Author: https://prod.codes/about/
 * Project: https://github.com/alex09x/mitts
 * SPDX-License-Identifier: MIT OR Apache-2.0
 */

use super::features_helpers::*;
pub use super::features_helpers::{
    format_package_id, normalize_unit_id_without_source, parse_package_id_source,
};
use crate::adapter::rust::manifest::{CargoToml, TomlDependency};
use crate::adapter::rust::types::{RustAdapterError, RustBuildOptions};
use std::collections::{HashMap, HashSet};

/// Context for resolving compile-time package features.
pub struct PackageFeatureContext<'a> {
    pub name: &'a str,
    pub version: &'a str,
    pub source: Option<&'a str>,
    pub is_workspace: bool,
    pub is_root: bool,
    pub toml: &'a CargoToml,
    pub dependencies: &'a [String],
}

impl<'a> PackageFeatureContext<'a> {
    pub fn id(&self) -> String {
        format_package_id(self.name, self.version, self.source)
    }

    pub fn is_proc_macro(&self) -> bool {
        self.toml.lib.as_ref().is_some_and(|l| {
            l.proc_macro == Some(true) || l.crate_type.iter().any(|k| k == "proc-macro")
        })
    }
}

#[derive(Debug, Clone, Default)]
pub struct ResolvedFeatures {
    pub target: HashMap<String, Vec<String>>,
    pub host: HashMap<String, Vec<String>>,
    pub active_target_packages: HashSet<String>,
    pub active_host_packages: HashSet<String>,
}

impl std::ops::Deref for ResolvedFeatures {
    type Target = HashMap<String, Vec<String>>;
    fn deref(&self) -> &Self::Target {
        &self.target
    }
}

/// Resolves effective compile-time features for all selected packages.
/// Unifies explicit features from dependents, default features, and workspace flags,
/// decoupling host (build-dependencies/proc-macros) and target feature sets (Resolver 2).
pub fn resolve_package_features(
    packages: &[PackageFeatureContext],
    opt: &RustBuildOptions,
) -> Result<ResolvedFeatures, RustAdapterError> {
    resolve_package_features_with_resolver(packages, opt, None)
}

pub fn resolve_package_features_with_resolver(
    packages: &[PackageFeatureContext],
    opt: &RustBuildOptions,
    ws_resolver: Option<&str>,
) -> Result<ResolvedFeatures, RustAdapterError> {
    let mut index = FeatureGraphIndex::build(packages, opt);
    let mut target_requested: HashMap<String, HashSet<String>> = HashMap::new();
    let mut host_requested: HashMap<String, HashSet<String>> = HashMap::new();
    let mut active_target_packages: HashSet<String> = HashSet::new();
    let mut active_host_packages: HashSet<String> = HashSet::new();

    // 1. Initial feature requests from root packages and CLI options
    for (idx, ctx) in packages.iter().enumerate() {
        let id = &index.pkg_ids[idx];
        let is_pm = ctx.is_proc_macro();
        let is_all_ws = opt.all_features && ctx.is_workspace && opt.package.is_none();
        if ctx.is_root || is_all_ws {
            let (active_set, req_map) = if is_pm {
                (&mut active_host_packages, &mut host_requested)
            } else {
                (&mut active_target_packages, &mut target_requested)
            };
            active_set.insert(id.clone());
            let req_set = req_map.entry(id.clone()).or_default();
            if opt.all_features {
                req_set.extend(ctx.toml.features.keys().cloned());
            } else if ctx.is_root {
                req_set.extend(opt.features.iter().cloned());
            }
            if ctx.is_root && !opt.no_default_features && ctx.toml.features.contains_key("default")
            {
                req_set.insert("default".to_string());
            }
        }
    }

    // 2. Fixed-point propagation loop: activate dependencies and expand features
    let is_test = opt.mode == crate::adapter::BuildMode::Test
        || opt.all_tests
        || !opt.test_targets.is_empty();
    let mut changed = true;
    while changed {
        changed = false;
        for is_host_ctx in [false, true] {
            for (idx, ctx) in packages.iter().enumerate() {
                let active_set = if is_host_ctx {
                    &active_host_packages
                } else {
                    &active_target_packages
                };
                if !active_set.contains(&index.pkg_ids[idx]) {
                    continue;
                }
                propagate_package(
                    idx,
                    ctx,
                    &mut index,
                    is_test,
                    is_host_ctx,
                    &mut active_target_packages,
                    &mut active_host_packages,
                    &mut target_requested,
                    &mut host_requested,
                    &mut changed,
                )?;
            }
        }
    }

    // Check workspace resolver: Resolver 1 unifies host and target features.
    let is_resolver_1 = ws_resolver.map_or_else(
        || {
            packages
                .iter()
                .find(|p| p.toml.workspace.is_some() || p.is_root)
                .is_some_and(|p| p.toml.effective_resolver() == "1")
        },
        |r| r == "1",
    );

    if is_resolver_1 {
        let host_snapshot = host_requested.clone();
        for (pkg_id, feats) in &target_requested {
            host_requested
                .entry(pkg_id.clone())
                .or_default()
                .extend(feats.iter().cloned());
        }
        for (pkg_id, feats) in &host_snapshot {
            target_requested
                .entry(pkg_id.clone())
                .or_default()
                .extend(feats.iter().cloned());
        }
        // The unified features can switch optional dependencies on in the other
        // context (a host library's `serde` feature needs `serde` built for the host):
        // propagate again until nothing changes.
        let mut changed = true;
        while changed {
            changed = false;
            for is_host_ctx in [false, true] {
                for (idx, ctx) in packages.iter().enumerate() {
                    let active_set = if is_host_ctx {
                        &active_host_packages
                    } else {
                        &active_target_packages
                    };
                    if !active_set.contains(&index.pkg_ids[idx]) {
                        continue;
                    }
                    propagate_package(
                        idx,
                        ctx,
                        &mut index,
                        is_test,
                        is_host_ctx,
                        &mut active_target_packages,
                        &mut active_host_packages,
                        &mut target_requested,
                        &mut host_requested,
                        &mut changed,
                    )?;
                }
            }
            if changed {
                // keep both contexts unified while new features appear
                let host_now = host_requested.clone();
                for (pkg_id, feats) in &target_requested {
                    host_requested
                        .entry(pkg_id.clone())
                        .or_default()
                        .extend(feats.iter().cloned());
                }
                for (pkg_id, feats) in &host_now {
                    target_requested
                        .entry(pkg_id.clone())
                        .or_default()
                        .extend(feats.iter().cloned());
                }
            }
        }
    }

    // 3. Build finalized feature maps
    let target = finalize_feature_map(
        packages,
        &index.pkg_ids,
        target_requested,
        &active_target_packages,
    );
    let host = finalize_feature_map(
        packages,
        &index.pkg_ids,
        host_requested,
        &active_host_packages,
    );

    Ok(ResolvedFeatures {
        target,
        host,
        active_target_packages,
        active_host_packages,
    })
}

#[allow(clippy::too_many_arguments)]
fn propagate_package<'a>(
    pkg_idx: usize,
    ctx: &'a PackageFeatureContext<'a>,
    index: &mut FeatureGraphIndex<'a>,
    is_test: bool,
    is_host_ctx: bool,
    active_target: &mut HashSet<String>,
    active_host: &mut HashSet<String>,
    target_req: &mut HashMap<String, HashSet<String>>,
    host_req: &mut HashMap<String, HashSet<String>>,
    changed: &mut bool,
) -> Result<(), RustAdapterError> {
    let id = index.pkg_ids[pkg_idx].clone();
    let mut runtime_deps: Vec<(&String, &TomlDependency)> = ctx.toml.dependencies.iter().collect();
    let mut build_deps: Vec<(&String, &TomlDependency)> =
        ctx.toml.build_dependencies.iter().collect();
    let is_pm = ctx.is_proc_macro();
    let include_dev_deps =
        is_test && ctx.is_root && (if is_pm { is_host_ctx } else { !is_host_ctx });
    if include_dev_deps {
        runtime_deps.extend(&ctx.toml.dev_dependencies);
    }

    for (spec, t) in &ctx.toml.target {
        if !index.is_target_incompat(spec, is_host_ctx)? {
            runtime_deps.extend(&t.dependencies);
            if include_dev_deps {
                runtime_deps.extend(&t.dev_dependencies);
            }
        }
        if !index.is_target_incompat(spec, true)? {
            build_deps.extend(&t.build_dependencies);
        }
    }

    // (A) Activate non-optional dependencies
    for (dep_key, dep) in runtime_deps {
        let is_opt = matches!(dep, TomlDependency::Detailed(d) if d.optional.unwrap_or(false));
        if !is_opt {
            let to_host =
                is_host_ctx || index.is_dep_proc_macro_with_hint(pkg_idx, dep_key, Some(dep))?;
            let (act, req) = if to_host {
                (&mut *active_host, &mut *host_req)
            } else {
                (&mut *active_target, &mut *target_req)
            };
            index.activate_dep_instance(pkg_idx, dep_key, dep, act, req, changed)?;
        }
    }

    for (dep_key, dep) in build_deps {
        let is_opt = matches!(dep, TomlDependency::Detailed(d) if d.optional.unwrap_or(false));
        if !is_opt {
            index.activate_dep_instance(pkg_idx, dep_key, dep, active_host, host_req, changed)?;
        }
    }

    // (B) Expand active features
    let current_feats = if is_host_ctx {
        &*host_req
    } else {
        &*target_req
    }
    .get(&id)
    .cloned()
    .unwrap_or_default();

    for feat in &current_feats {
        let deps = index.collect_manifest_dependencies(ctx.toml, feat)?;
        if is_optional_dep(&deps) {
            let is_pm_dep =
                index.is_dep_proc_macro_with_hint(pkg_idx, feat, deps.first().copied())?;
            let (to_target, to_host) = optional_dep_sides(ctx.toml, feat, is_host_ctx, is_pm_dep);
            if to_target {
                index.activate_manifest_dep(
                    pkg_idx,
                    feat,
                    &deps,
                    active_target,
                    target_req,
                    changed,
                )?;
            }
            if to_host {
                index.activate_manifest_dep(pkg_idx, feat, &deps, active_host, host_req, changed)?;
            }
        }

        if let Some(sub_feats) = ctx.toml.features.get(feat) {
            for sub in sub_feats {
                if let Some((dep, dep_feat)) = sub.split_once('/') {
                    let is_weak = dep.ends_with('?');
                    let dep_unweak = dep.strip_suffix('?').unwrap_or(dep);
                    let actual_dep = dep_unweak.strip_prefix("dep:").unwrap_or(dep_unweak);
                    let real_dep: String = index.aliases_by_idx[pkg_idx]
                        .alias_to_real
                        .get(actual_dep)
                        .cloned()
                        .unwrap_or_else(|| actual_dep.to_string());
                    let mut dep_decls =
                        index.collect_manifest_dependencies(ctx.toml, actual_dep)?;
                    if dep_decls.is_empty() {
                        dep_decls = index.collect_manifest_dependencies(ctx.toml, &real_dep)?;
                    }
                    if !is_weak
                        && (ctx.toml.features.contains_key(actual_dep)
                            || is_optional_dep(&dep_decls))
                    {
                        let this_req = if is_host_ctx {
                            &mut *host_req
                        } else {
                            &mut *target_req
                        };
                        if this_req
                            .entry(id.clone())
                            .or_default()
                            .insert(actual_dep.to_string())
                        {
                            *changed = true;
                        }
                    }
                    if dep_decls.is_empty()
                        && ctx.toml.target.values().any(|t| {
                            t.dependencies.contains_key(actual_dep)
                                || t.build_dependencies.contains_key(actual_dep)
                        })
                    {
                        continue;
                    }
                    let dep_hint = dep_decls.first().copied();
                    let is_pm_dep =
                        index.is_dep_proc_macro_with_hint(pkg_idx, &real_dep, dep_hint)?;
                    let (t1, h1) = optional_dep_sides(ctx.toml, actual_dep, is_host_ctx, is_pm_dep);
                    let (t2, h2) = optional_dep_sides(ctx.toml, &real_dep, is_host_ctx, is_pm_dep);
                    let (to_target, to_host) = (t1 || t2, h1 || h2);

                    if is_weak {
                        let dep_prefix = format!("dep:{}", actual_dep);
                        let real_prefix = format!("dep:{}", real_dep);
                        let cur_req = if is_host_ctx {
                            &*host_req
                        } else {
                            &*target_req
                        };
                        let is_dep_active = !dep_decls.is_empty()
                            && (!is_optional_dep(&dep_decls)
                                || cur_req.get(&id).is_some_and(|s| {
                                    s.contains(actual_dep)
                                        || s.contains(&dep_prefix)
                                        || s.contains(&real_dep)
                                        || s.contains(&real_prefix)
                                }));
                        if !is_dep_active {
                            continue;
                        }
                    } else if !dep_decls.is_empty() {
                        if to_target {
                            index.activate_manifest_dep(
                                pkg_idx,
                                actual_dep,
                                &dep_decls,
                                active_target,
                                target_req,
                                changed,
                            )?;
                        }
                        if to_host {
                            index.activate_manifest_dep(
                                pkg_idx, actual_dep, &dep_decls, active_host, host_req, changed,
                            )?;
                        }
                    }

                    if let Some(target_id) =
                        index.resolve_dep_id_with_hint(pkg_idx, &real_dep, dep_hint)?
                    {
                        if to_target
                            && target_req
                                .entry(target_id.clone())
                                .or_default()
                                .insert(dep_feat.to_string())
                        {
                            *changed = true;
                        }
                        if to_host
                            && host_req
                                .entry(target_id)
                                .or_default()
                                .insert(dep_feat.to_string())
                        {
                            *changed = true;
                        }
                    }
                } else {
                    let this_req = if is_host_ctx {
                        &mut *host_req
                    } else {
                        &mut *target_req
                    };
                    if this_req.entry(id.clone()).or_default().insert(sub.clone()) {
                        *changed = true;
                    }
                    let (dep_name, explicit_dep) = match sub.strip_prefix("dep:") {
                        Some(d) => {
                            let norm = format!("dep:{}", d.replace('-', "_"));
                            if norm != *sub {
                                this_req.entry(id.clone()).or_default().insert(norm);
                            }
                            (d, true)
                        }
                        None => (sub.as_str(), false),
                    };
                    let deps = index.collect_manifest_dependencies(ctx.toml, dep_name)?;
                    if explicit_dep || is_optional_dep(&deps) {
                        let hint = deps.first().copied();
                        let is_pm_dep =
                            index.is_dep_proc_macro_with_hint(pkg_idx, dep_name, hint)?;
                        let (to_target, to_host) =
                            optional_dep_sides(ctx.toml, dep_name, is_host_ctx, is_pm_dep);
                        if to_target {
                            index.activate_manifest_dep(
                                pkg_idx,
                                dep_name,
                                &deps,
                                active_target,
                                target_req,
                                changed,
                            )?;
                        }
                        if to_host {
                            index.activate_manifest_dep(
                                pkg_idx, dep_name, &deps, active_host, host_req, changed,
                            )?;
                        }
                    }
                }
            }
        }
    }
    Ok(())
}