mitts 0.3.0

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
/*
 * 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::compiler::RustcOutput;
use super::dag_cache::ExecutionContext;
use super::types::PlannedCompilationUnit;
use crate::core::action_cache::{ActionCacheEntry, CachedOutput};
use crate::core::hash::Sha256;
use anyhow::Result;
use std::fs;
use std::path::{Path, PathBuf};

pub(crate) fn unit_dest_binary(
    unit: &PlannedCompilationUnit,
    target_dir: &Path,
    profile: &str,
) -> PathBuf {
    if unit.kind == "test" || unit.kind == "custom-test" {
        target_dir
            .join(profile)
            .join("deps")
            .join(format!("{}-{}", unit.package_name, unit.target_name))
    } else {
        target_dir.join(profile).join(&unit.target_name)
    }
}

pub(crate) struct PublishArgs<'a> {
    pub unit: &'a PlannedCompilationUnit,
    pub action_key: &'a str,
    pub ctx: &'a ExecutionContext<'a>,
    pub staging_dir: &'a Path,
    pub dest_binary: Option<&'a Path>,
    pub output: &'a RustcOutput,
    pub out_dir_purity: Option<&'a super::build_script_purity::BuildScriptPurity>,
    pub out_dir: Option<&'a Path>,
}

pub(crate) fn publish_unit_artifacts(args: PublishArgs<'_>) -> Result<Vec<PathBuf>> {
    let unit = args.unit;
    let action_key = args.action_key;
    let ctx = args.ctx;
    let clean_crate = unit.target_name.replace('-', "_");
    let mut cached_outputs = Vec::new();
    let mut action_outputs = Vec::new();

    let is_mutable = super::unit_inputs::is_mutable_unit(unit);
    let ws_root = if is_mutable {
        Some(super::compiler::find_workspace_root(&unit.manifest_path))
    } else {
        None
    };

    // 1. Parse dep-info (.d) in staging_dir for extra inputs outside package_dir (P0, P1, P2)
    let extra_inputs_res = extract_unit_extra_inputs(
        unit,
        args.staging_dir,
        ws_root.as_deref(),
        args.out_dir_purity,
        args.out_dir,
    );
    let (extra_inputs, can_publish_cache) = match extra_inputs_res {
        Ok(extra) => (extra, !action_key.contains(":unhashable:")),
        Err(err) => {
            eprintln!(
                "[mitts::warn] Unit '{}' tracked input hashing failed: {}. Discarding cache publication (fail-closed).",
                unit.unit_id(),
                err
            );
            (Vec::new(), false)
        }
    };
    record_extra_inputs_in_ctx(ctx, ws_root.as_deref(), &extra_inputs);

    // 2. Compute result_key; shared artifact names follow it, not the coarse action key
    let result_key = if can_publish_cache {
        compute_composite_result_key(action_key, &extra_inputs)
    } else {
        taint_unhashable_key(action_key)
    };

    if let Some(rk_map) = ctx.resolved_keys {
        rk_map
            .lock()
            .unwrap()
            .insert(unit.unit_id(), result_key.clone());
    }

    let artifact_key = artifact_disambiguator(&result_key, Some(args.staging_dir));

    // 3. Store executable binary in CAS from staging first (N3), then materialize in target/
    if let Some(dest) = args.dest_binary {
        if let Some(ref bp) = args.output.primary_output {
            if bp.is_file() {
                let stored_bin = ctx.cas.store_file(bp).ok();
                if let Some(parent) = dest.parent() {
                    let _ = fs::create_dir_all(parent);
                }
                if bp != dest && fs::rename(bp, dest).is_err() {
                    let _ = crate::core::link_intra_workspace(
                        bp,
                        dest,
                        &[args.staging_dir, ctx.target_dir],
                        true,
                    );
                }
                if let Some((bin_hash, bin_size)) = stored_bin {
                    let rel = dest
                        .strip_prefix(ctx.target_dir)
                        .map(PathBuf::from)
                        .unwrap_or_else(|_| {
                            PathBuf::from(if ctx.opt.release { "release" } else { "debug" })
                                .join(&unit.target_name)
                        });
                    let rel_key = rel.to_string_lossy().to_string();
                    if let Some(ti) = ctx.target_index {
                        ti.lock().unwrap().record(rel_key, bin_hash.clone(), dest);
                    }
                    cached_outputs.push(CachedOutput {
                        relative_path: rel,
                        cas_hash: bin_hash,
                        file_size: bin_size,
                        executable: true,
                    });
                }

                let alt_path = dest.parent().unwrap_or(dest).join(&clean_crate);
                if alt_path != *dest {
                    let _ =
                        crate::core::link_intra_workspace(dest, &alt_path, &[ctx.target_dir], true);
                }
                action_outputs.push(dest.to_path_buf());
            }
        }
    } else {
        // 4. Publish primary library output (rlib/dylib/so)
        if let Some(lib_path) = args
            .output
            .primary_output
            .as_ref()
            .or(args.output.metadata_output.as_ref())
        {
            let (hash, size) = ctx.cas.store_file(lib_path)?;
            let ext = lib_path
                .extension()
                .and_then(|e| e.to_str())
                .unwrap_or("rlib");
            let disambig = if ext == "rmeta" {
                format!("{artifact_key}-m")
            } else {
                artifact_key.clone()
            };
            let named = ctx.cas.named_artifact_path_with_disambiguator(
                &unit.target_name,
                &hash,
                &disambig,
                ext,
            )?;

            let file_name = lib_path.file_name().unwrap_or_default();
            cached_outputs.push(CachedOutput {
                relative_path: PathBuf::from(file_name),
                cas_hash: hash.clone(),
                file_size: size,
                executable: false,
            });
            ctx.completed_artifacts
                .lock()
                .unwrap()
                .insert(unit.unit_id(), named.clone());
            action_outputs.push(named);

            if !ctx.opt.native || unit.is_workspace {
                let profile_name = if ctx.opt.release { "release" } else { "debug" };
                let deps_dir = ctx.target_dir.join(profile_name).join("deps");
                let _ = fs::create_dir_all(&deps_dir);
                let dep_copy = deps_dir.join(file_name);
                if lib_path != &dep_copy {
                    let _ = crate::core::link_intra_workspace(
                        lib_path,
                        &dep_copy,
                        &[args.staging_dir, ctx.target_dir],
                        false,
                    );
                }
                if let Some(ti) = ctx.target_index {
                    let rel_key = format!("{}/deps/{}", profile_name, file_name.to_string_lossy());
                    ti.lock().unwrap().record(rel_key, hash.clone(), &dep_copy);
                }
                let unhashed = deps_dir.join(format!("lib{}.{}", clean_crate, ext));
                if unhashed != dep_copy {
                    let _ = crate::core::link_intra_workspace(
                        &dep_copy,
                        &unhashed,
                        &[ctx.target_dir],
                        false,
                    );
                }
            }
        }

        // 5. Publish metadata output (rmeta) if distinct from primary_output
        if let Some(mp) = args
            .output
            .metadata_output
            .as_ref()
            .filter(|mp| args.output.primary_output.as_ref() != Some(*mp))
        {
            let (meta_hash, meta_size) = ctx.cas.store_file(mp)?;
            let _ = ctx.cas.named_artifact_path_with_disambiguator(
                &unit.target_name,
                &meta_hash,
                &artifact_key,
                "rmeta",
            );
            let file_name = mp.file_name().unwrap_or_default();
            cached_outputs.push(CachedOutput {
                relative_path: PathBuf::from(file_name),
                cas_hash: meta_hash.clone(),
                file_size: meta_size,
                executable: false,
            });
            if !ctx.opt.native || unit.is_workspace {
                let profile_name = if ctx.opt.release { "release" } else { "debug" };
                let deps_dir = ctx.target_dir.join(profile_name).join("deps");
                let _ = fs::create_dir_all(&deps_dir);
                let dep_copy = deps_dir.join(file_name);
                if mp != &dep_copy {
                    let _ = crate::core::link_intra_workspace(
                        mp,
                        &dep_copy,
                        &[args.staging_dir, ctx.target_dir],
                        false,
                    );
                }
                if let Some(ti) = ctx.target_index {
                    let rel_key = format!("{}/deps/{}", profile_name, file_name.to_string_lossy());
                    ti.lock()
                        .unwrap()
                        .record(rel_key, meta_hash.clone(), &dep_copy);
                }
                let unhashed = deps_dir.join(format!("lib{}.rmeta", clean_crate));
                if unhashed != dep_copy {
                    let _ = crate::core::link_intra_workspace(
                        &dep_copy,
                        &unhashed,
                        &[ctx.target_dir],
                        false,
                    );
                }
            }
        }
    }

    // 6. Output byte scanning for workspace root leaks (P2)
    let mut contains_ws_root_path = false;

    if let Some(ref root) = ws_root {
        let ws_needle = root.to_string_lossy().as_bytes().to_vec();
        if !ws_needle.is_empty() {
            let scan_file = |p: &Path| -> bool {
                fs::read(p)
                    .map(|b| {
                        b.windows(ws_needle.len())
                            .any(|w| w == ws_needle.as_slice())
                    })
                    .unwrap_or(false)
            };
            if let Some(bp) = args.dest_binary {
                if scan_file(bp) {
                    contains_ws_root_path = true;
                }
            }
            if let Some(ref lp) = args.output.primary_output {
                if scan_file(lp) {
                    contains_ws_root_path = true;
                }
            }
            if let Some(ref mp) = args.output.metadata_output {
                if scan_file(mp) {
                    contains_ws_root_path = true;
                }
            }
        }
    }

    let effective_action_key = if contains_ws_root_path {
        let root = ws_root.as_ref().unwrap();
        let ws_hash = Sha256::digest(root.to_string_lossy().as_bytes());
        format!("{}:ws_leak:{}", result_key, ws_hash)
    } else {
        result_key.clone()
    };

    // 7. Publish Action Cache entry
    if can_publish_cache && ctx.is_cache_enabled && !cached_outputs.is_empty() {
        let now_secs = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let entry = ActionCacheEntry {
            action_key: effective_action_key,
            action_id: unit.unit_id(),
            created_at_secs: now_secs,
            outputs: cached_outputs,
            stdout: String::new(),
            stderr: args.output.stderr.clone(),
            exit_code: 0,
        };
        let has_linkable = |outs: &[CachedOutput]| {
            outs.iter().any(|o| {
                matches!(
                    o.relative_path.extension().and_then(|e| e.to_str()),
                    Some("rlib" | "dylib" | "so")
                )
            })
        };
        let should_put = has_linkable(&entry.outputs)
            || !ctx
                .ac
                .get(&entry.action_key)
                .ok()
                .flatten()
                .is_some_and(|existing| has_linkable(&existing.outputs));
        if should_put {
            let _ = ctx.ac.put(&entry);
        }

        // 8. Record in ActionManifest for mutable units
        if is_mutable {
            let _ = ctx
                .ac
                .record_manifest_entry(action_key, extra_inputs, &result_key);
        }
    }

    Ok(action_outputs)
}

/// Records dep-info extra inputs (`include_str!`, `#[path]`, `env-dep:`) into the ExecutionContext for FastBuildLock.
pub fn record_extra_inputs_in_ctx(
    ctx: &ExecutionContext,
    ws_root: Option<&Path>,
    extra_inputs: &[(String, String)],
) {
    for (path_str, hash) in extra_inputs {
        if let Some(var_name) = path_str.strip_prefix("env:") {
            if var_name == "OUT_DIR"
                || var_name == "CARGO_MANIFEST_DIR"
                || var_name == "CARGO_CRATE_NAME"
                || var_name == "CARGO_BIN_NAME"
                || var_name.starts_with("CARGO_PKG_")
            {
                continue;
            }
            if let Some(env_map) = ctx.tracked_env_vars {
                env_map
                    .lock()
                    .unwrap()
                    .insert(var_name.to_string(), std::env::var(var_name).ok());
            }
        } else if !path_str.starts_with("out:") {
            let file_path = if let Some(rel) = path_str.strip_prefix("ws:") {
                if let Some(root) = ws_root {
                    root.join(rel)
                } else {
                    PathBuf::from(rel)
                }
            } else {
                PathBuf::from(path_str)
            };
            if let Some(files_map) = ctx.tracked_extra_files {
                files_map.lock().unwrap().insert(file_path, hash.clone());
            }
        }
    }
}

/// Disambiguator for a unit's file names in the shared `{cas}/artifacts` directory.
///
/// Names follow the result key, not the coarse action key: units with one coarse key
/// can read different modules, and a shared name must never be rebound to other bytes
/// while a concurrent build reads it. A tainted key names a single compilation, so its
/// nonce is replaced with the staging dir that both the rmeta and rlib publish share.
pub(crate) fn artifact_disambiguator(result_key: &str, staging_dir: Option<&Path>) -> String {
    let key = match (result_key.split_once(":unhashable:"), staging_dir) {
        (Some((base, _)), Some(dir)) => format!("{base}:unhashable:{}", dir.display()),
        _ => result_key.to_string(),
    };
    if key.bytes().all(|b| b.is_ascii_hexdigit()) {
        key
    } else {
        Sha256::digest(key.as_bytes())
    }
}

/// Appends a unique non-reusable `:unhashable:<nonce>` suffix to taint a key and its dependents.
pub fn taint_unhashable_key(key: &str) -> String {
    static TAINT_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
    let seq = TAINT_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let nonce = super::dag_scheduler::FastPrng::from_env().next_u64() ^ seq;
    format!("{}:unhashable:{:016x}", key, nonce)
}

pub use super::unit_inputs::{
    clean_path, compute_composite_result_key, extract_unit_extra_inputs, resolve_unit_result_key,
};