mbx 0.5.2

A build cache for Rust projects
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
use super::*;

fn test_config(cache_dir: &Path) -> Config {
    Config {
        cache_dir: cache_dir.to_path_buf(),
        stats_report: None,
        verify: false,
        incremental: false,
        share_out_dir: false,
        events: false,
        cc: false,
        remote: Default::default(),
        http: Default::default(),
        gc: Default::default(),
        target: crate::config::TargetSettings {
            views: false,
            root: cache_dir.join("targets"),
        },
    }
}

#[tokio::test]
async fn session_environment_directs_cargo_at_the_shim() {
    let cache = tempfile::tempdir().unwrap();
    let session_dir = tempfile::tempdir().unwrap();
    let session = CacheSession::start(session_dir.path(), &test_config(cache.path()))
        .await
        .unwrap();

    let workspace = tempfile::tempdir().unwrap();
    let mut values = BTreeMap::from([("RUSTC_WRAPPER".into(), "existing".into())]);
    let run = session
        .begin(
            workspace.path(),
            &workspace.path().join("target"),
            &["build".to_string()],
            &mut values,
        )
        .await;

    assert!(run.is_some());
    assert!(values.contains_key(SOCKET_ENV));
    assert!(values.contains_key(STAGING_ENV));
    assert_eq!(values.get(BUILD_ENV).unwrap().len(), 64);
    // The shim carries an .exe suffix on Windows, so compare stems.
    let wrapper = Path::new(values.get("RUSTC_WRAPPER").unwrap());
    assert_eq!(wrapper.file_stem().unwrap(), RUSTC_SHIM_STEM);
    assert_eq!(values.get(PREVIOUS_RUSTC_WRAPPER_ENV).unwrap(), "existing");
    assert_eq!(values.get("CARGO_INCREMENTAL").unwrap(), "0");
    assert_eq!(values.get(VERIFY_ENV).unwrap(), "0");

    session.finish().await.unwrap();
}

#[tokio::test]
async fn incremental_builds_leave_cargo_incremental_alone() {
    let cache = tempfile::tempdir().unwrap();
    let session_dir = tempfile::tempdir().unwrap();
    let mut config = test_config(cache.path());
    config.incremental = true;
    let session = CacheSession::start(session_dir.path(), &config)
        .await
        .unwrap();

    let workspace = tempfile::tempdir().unwrap();
    let mut values = BTreeMap::new();
    session
        .begin(
            workspace.path(),
            &workspace.path().join("target"),
            &["build".to_string()],
            &mut values,
        )
        .await;

    // Absent, not "1": cargo's own per-profile default is what we want, and
    // forcing the value on would turn incremental on for release too.
    assert!(!values.contains_key("CARGO_INCREMENTAL"));

    session.finish().await.unwrap();
}

#[tokio::test]
async fn verify_mode_is_passed_to_the_shim() {
    let cache = tempfile::tempdir().unwrap();
    let session_dir = tempfile::tempdir().unwrap();
    let mut config = test_config(cache.path());
    config.verify = true;
    let session = CacheSession::start(session_dir.path(), &config)
        .await
        .unwrap();

    let workspace = tempfile::tempdir().unwrap();
    let mut values = BTreeMap::new();
    session
        .begin(
            workspace.path(),
            &workspace.path().join("target"),
            &["build".to_string()],
            &mut values,
        )
        .await;

    assert_eq!(values.get(VERIFY_ENV).unwrap(), "1");

    session.finish().await.unwrap();
}

#[test]
fn identity_follows_the_dependency_graph_not_the_path() {
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    std::fs::write(first.path().join("Cargo.lock"), "version = 4\n").unwrap();
    std::fs::write(second.path().join("Cargo.lock"), "version = 4\n").unwrap();
    let command = ["build".to_string()];

    assert_eq!(
        build_identity(first.path(), &command),
        build_identity(second.path(), &command),
        "separate worktrees of one project must share a manifest"
    );

    std::fs::write(second.path().join("Cargo.lock"), "version = 3\n").unwrap();
    assert_ne!(
        build_identity(first.path(), &command),
        build_identity(second.path(), &command),
    );
    assert_ne!(
        build_identity(first.path(), &command),
        build_identity(first.path(), &["test".to_string()]),
    );
}

#[test]
fn identity_falls_back_to_the_directory_name() {
    let directory = tempfile::tempdir().unwrap();
    let command = ["build".to_string()];
    assert_eq!(
        build_identity(directory.path(), &command).len(),
        64,
        "a project without a lockfile still gets an identity"
    );
}

#[test]
fn path_shim_names_select_their_language() {
    assert_eq!(path_shim_language("cc"), Some(CcLanguage::C));
    assert_eq!(path_shim_language("gcc"), Some(CcLanguage::C));
    assert_eq!(path_shim_language("clang"), Some(CcLanguage::C));
    assert_eq!(path_shim_language("c++"), Some(CcLanguage::Cxx));
    assert_eq!(path_shim_language("g++"), Some(CcLanguage::Cxx));
    assert_eq!(path_shim_language("clang++"), Some(CcLanguage::Cxx));
    // A versioned driver was chosen deliberately and is never intercepted.
    assert_eq!(path_shim_language("gcc-13"), None);
    assert_eq!(path_shim_language("mbx"), None);
}

#[test]
fn exec_identity_is_shared_across_checkouts_with_one_lockfile() {
    let first = tempfile::tempdir().unwrap();
    let second = tempfile::tempdir().unwrap();
    std::fs::write(first.path().join("Cargo.lock"), "version = 4\n").unwrap();
    std::fs::write(second.path().join("Cargo.lock"), "version = 4\n").unwrap();
    let command = ["make".to_string()];

    assert_eq!(
        exec_identity(first.path(), &command),
        exec_identity(second.path(), &command),
        "worktrees of one project must share a manifest for predictions to travel"
    );
    assert_ne!(
        exec_identity(first.path(), &command),
        exec_identity(first.path(), &["make".to_string(), "-j8".to_string()]),
    );
}

#[test]
fn exec_identity_falls_back_to_the_directory_name() {
    let directory = tempfile::tempdir().unwrap();
    let command = ["make".to_string()];
    assert_eq!(
        exec_identity(directory.path(), &command).len(),
        64,
        "a project with no lockfile and no git origin still gets an identity"
    );
}

#[cfg(unix)]
#[test]
fn a_hard_linked_shim_is_recognized_as_the_same_binary() {
    // Both files are created here rather than linked from the running test
    // binary: a hard link needs one filesystem, and a temporary directory is
    // on a different one from the build directory often enough that CI proves
    // it. `install_shim_named` falls back to a copy in that case, which would
    // test the fallback rather than the recognition below.
    let directory = tempfile::tempdir().unwrap();
    let binary = directory.path().join("mbx");
    std::fs::write(&binary, b"#!/bin/sh\n").unwrap();
    let shim_dir = directory.path().join("cc-path");
    std::fs::create_dir(&shim_dir).unwrap();

    // The case a path comparison cannot see: a shim directory an outer
    // session left on PATH, holding a link to the same binary under a
    // compiler's name.
    let linked = shim_dir.join("cc");
    std::fs::hard_link(&binary, &linked).unwrap();
    assert!(is_same_binary(&linked, Some(&binary)));

    // A copy is a different file, and saying so is correct: the shim resolves
    // its own name away by path first, so recognition here only has to cover
    // the links that share an inode.
    let copied = shim_dir.join("c++");
    std::fs::copy(&binary, &copied).unwrap();
    assert!(!is_same_binary(&copied, Some(&binary)));
}

#[test]
fn handshake_rejects_version_skew() {
    let response = serde_json::to_string(&AgentResponse::Hello {
        protocol: AGENT_PROTOCOL_VERSION,
        agent_version: "another-version".into(),
    })
    .unwrap();
    assert!(validate_handshake_response(&response).is_err());
}

/// Build the statistics a test needs without naming every counter.
///
/// `AgentStats` is `#[non_exhaustive]` so that the agent can keep adding
/// counters, which also means no struct literal can be written from here.
fn agent_stats(fill: impl FnOnce(&mut AgentStats)) -> AgentStats {
    let mut stats = AgentStats::default();
    fill(&mut stats);
    stats
}

#[test]
fn qualification_results_are_not_reported_as_misses() {
    let stats = agent_stats(|stats| {
        stats.lookups = 5;
        stats.hits = 2;
        stats.verifications = 2;
    });
    assert_eq!(cache_misses(&stats), 1);
}

#[test]
fn compiler_only_sessions_are_reportable() {
    let stats = agent_stats(|stats| {
        stats.compiler =
            BTreeMap::from([("bypass".into(), mbx_cache_core::CompilerStats::new(1, 42))]);
    });

    assert!(should_display_stats(&stats));
}

#[test]
fn a_session_that_only_failed_its_remote_is_reportable() {
    // Nothing was looked up, stored or compiled, so every other signal this
    // gate reads is zero -- and a remote cache that answered nothing but
    // failures is the one thing the build most needs to be told about.
    let stats = agent_stats(|stats| stats.remote_failures = 3);

    assert!(should_display_stats(&stats));
}

#[test]
fn writes_versioned_stats_report() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("nested").join("stats.json");
    let stats = agent_stats(|stats| {
        stats.session_duration_ns = 42;
        stats.lookups = 5;
        stats.hits = 2;
        stats.verifications = 1;
        stats.prefetched_actions = 3;
        stats.downloaded_bytes = 1024;
        stats.restored_output_files = 7;
        stats.restored_output_bytes = 2048;
        stats.reflinked_output_files = 5;
        stats.reflinked_output_bytes = 1536;
        stats.copied_output_files = 2;
        stats.copied_output_bytes = 512;
        stats.avoided_compiler_duration_ns = 2_000;
        stats.compiler =
            BTreeMap::from([("miss".into(), mbx_cache_core::CompilerStats::new(3, 4_000))]);
        stats.slow_compilations = BTreeMap::from([("slow_crate".into(), 3_000)]);
        stats.remote_blob_requests = 4;
        stats.remote_blob_pack_requests = 2;
        stats.remote_blob_pack_blobs = 100;
        stats.materialization_duration_ns = 9;
    });

    write_stats_report(&path, &stats).unwrap();
    let report: serde_json::Value = serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap();

    assert_eq!(report["version"], 2);
    assert_eq!(report["session_duration_ns"], 42);
    assert_eq!(report["hits"], 2);
    assert_eq!(report["misses"], 2);
    assert_eq!(report["compiler_invocations_avoided"], 2);
    assert_eq!(report["estimated_compiler_duration_avoided_ns"], 2_000);
    assert_eq!(report["compiler"]["miss"]["invocations"], 3);
    assert_eq!(report["compiler"]["miss"]["duration_ns"], 4_000);
    assert_eq!(report["slow_compilations"][0]["crate_name"], "slow_crate");
    assert_eq!(report["slow_compilations"][0]["duration_ns"], 3_000);
    assert_eq!(report["prefetched_actions"], 3);
    assert_eq!(report["downloaded_bytes"], 1024);
    assert_eq!(report["restored_output_files"], 7);
    assert_eq!(report["restored_output_bytes"], 2048);
    assert_eq!(report["reflinked_output_files"], 5);
    assert_eq!(report["reflinked_output_bytes"], 1536);
    assert_eq!(report["copied_output_files"], 2);
    assert_eq!(report["copied_output_bytes"], 512);
    assert_eq!(report["remote_blob_requests"], 4);
    assert_eq!(report["remote_blob_pack_requests"], 2);
    assert_eq!(report["remote_blob_pack_blobs"], 100);
    assert_eq!(report["materialization_duration_ns"], 9);
}

#[test]
fn finds_crate_names_in_transparent_invocations() {
    assert_eq!(
        crate_name_argument(&["--crate-name".into(), "fixture".into()]),
        Some("fixture".into())
    );
    assert_eq!(
        crate_name_argument(&["--crate-name=attached".into()]),
        Some("attached".into())
    );
    assert_eq!(crate_name_argument(&["--version".into()]), None);
}

/// The session shim must be a symlink, not a hard link.
///
/// Cargo execs it within milliseconds of its creation, and a hard link that new
/// is not reliably runnable on macOS -- see [`install_shim`]. Asserted on the
/// kind of link rather than by racing the kernel, which no test can do
/// dependably.
#[cfg(unix)]
#[test]
fn the_session_shim_tracks_the_binary_it_was_installed_from() {
    let directory = tempfile::tempdir().unwrap();
    let executable = std::env::current_exe().unwrap();
    let shim = install_shim(&executable, directory.path(), ShimLink::Tracking).unwrap();

    let metadata = std::fs::symlink_metadata(&shim).unwrap();
    assert!(
        metadata.file_type().is_symlink(),
        "the session shim should be a symlink, found {metadata:?}"
    );
    assert_eq!(std::fs::read_link(&shim).unwrap(), executable);
}

/// The `mbx setup` wrapper keeps the bytes it was installed from.
///
/// A symlink there would break the moment the binary it names is deleted, and
/// nothing execs it soon enough for the race to matter.
#[cfg(unix)]
#[test]
fn the_installed_wrapper_pins_the_bytes_it_was_made_from() {
    let directory = tempfile::tempdir().unwrap();
    let executable = std::env::current_exe().unwrap();
    let shim = install_shim(&executable, directory.path(), ShimLink::Pinned).unwrap();

    assert!(
        !std::fs::symlink_metadata(&shim)
            .unwrap()
            .file_type()
            .is_symlink()
    );
    // Length rather than contents: the point is that the shim is a file of its
    // own and not a name that can dangle, and reading the binary twice to prove
    // it costs a hundred megabytes.
    assert_eq!(
        std::fs::metadata(&shim).unwrap().len(),
        std::fs::metadata(&executable).unwrap().len()
    );
}

/// No shim is ever installed as a link to nothing.
///
/// A symlink can name a target that does not exist, so the tracking path has to
/// decline one; the hard link it falls back to then fails where the mistake was
/// made rather than when cargo execs the wrapper.
#[cfg(unix)]
#[test]
fn a_shim_is_never_installed_as_a_link_to_nothing() {
    let directory = tempfile::tempdir().unwrap();
    for missing in [directory.path().join("gone"), PathBuf::from("relative/mbx")] {
        assert!(
            install_shim(&missing, directory.path(), ShimLink::Tracking).is_err(),
            "{} should not install a shim",
            missing.display()
        );
    }
}

/// A relative target is resolved before it is linked, not after.
///
/// A symlink resolves its target from the shim's own directory, and the session
/// shim's directory is a temporary one that shares nothing with the caller's, so
/// the relative target a hard link would have read from the working directory
/// has to be resolved against that directory here. `Cargo.toml` stands in for
/// the binary: under test is how the path is read, not what it points at.
#[cfg(unix)]
#[test]
fn a_relative_target_is_resolved_before_it_is_linked() {
    let directory = tempfile::tempdir().unwrap();
    let shim = directory.path().join(RUSTC_SHIM_STEM);
    assert!(symlink_shim(Path::new("Cargo.toml"), &shim));

    let target = std::fs::read_link(&shim).unwrap();
    assert!(
        target.is_absolute(),
        "{} should be absolute",
        target.display()
    );
    // Resolves through the link, which it could not if the target had been left
    // relative: the shim's own directory holds no `Cargo.toml`.
    assert_eq!(
        std::fs::canonicalize(&shim).unwrap(),
        std::fs::canonicalize("Cargo.toml").unwrap()
    );
}

/// An image with a C compiler and no C++ one is ordinary, and it must not cost
/// a C-only sys-crate its caching.
#[test]
fn a_missing_cpp_compiler_still_leaves_c_compilations_cached() {
    let shims = CcShims {
        cc: Some((
            PathBuf::from("/session/mbx-cc"),
            PathBuf::from("/usr/bin/cc"),
        )),
        cxx: None,
    };
    let mut environment = BTreeMap::new();
    shims.apply_to(&mut environment);

    assert_eq!(
        environment.get("HOST_CC").map(String::as_str),
        Some("/session/mbx-cc")
    );
    assert_eq!(
        environment.get("MBX_REAL_CC").map(String::as_str),
        Some("/usr/bin/cc")
    );
    // Nothing is claimed for the language that has no compiler, so the `cc`
    // crate keeps whatever it would have chosen for C++.
    assert!(!environment.contains_key("HOST_CXX"));
    assert!(!environment.contains_key("MBX_REAL_CXX"));
}

/// Both present is the ordinary case, and both get pointed at their shim.
#[test]
fn both_compilers_present_are_both_redirected() {
    let shims = CcShims {
        cc: Some((
            PathBuf::from("/session/mbx-cc"),
            PathBuf::from("/usr/bin/cc"),
        )),
        cxx: Some((
            PathBuf::from("/session/mbx-cxx"),
            PathBuf::from("/usr/bin/c++"),
        )),
    };
    let mut environment = BTreeMap::new();
    shims.apply_to(&mut environment);
    for name in ["HOST_CC", "HOST_CXX", "MBX_REAL_CC", "MBX_REAL_CXX"] {
        assert!(environment.contains_key(name), "{name} should be set");
    }
}