mbx 1.20.0

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
use super::*;

fn write_tree(root: &Path, files: &[(&str, &[u8])]) {
    for (name, contents) in files {
        let path = root.join(name);
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, contents).unwrap();
    }
}

#[test]
fn identical_trees_at_different_paths_share_one_stable_directory() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let first = directory.path().join("checkout-a/target/build/x/out");
    let second = directory.path().join("checkout-b/target/build/x/out");
    let files: &[(&str, &[u8])] = &[
        ("generated.rs", b"pub const VALUE: u32 = 7;\n"),
        ("nested/header.h", b"#define X 1\n"),
    ];
    write_tree(&first, files);
    write_tree(&second, files);

    let a = stabilize(&first, &root).unwrap().unwrap();
    let b = stabilize(&second, &root).unwrap().unwrap();

    assert_eq!(a, b, "the same bytes name the same directory");
    assert!(a.starts_with(&root));
    assert_eq!(
        std::fs::read(a.join("generated.rs")).unwrap(),
        b"pub const VALUE: u32 = 7;\n"
    );
    assert_eq!(
        std::fs::read(a.join("nested/header.h")).unwrap(),
        b"#define X 1\n"
    );
    assert!(
        std::fs::metadata(a.join("generated.rs"))
            .unwrap()
            .permissions()
            .readonly(),
        "the shared copy is read-only"
    );
    assert!(
        std::fs::read_dir(&root)
            .unwrap()
            .flatten()
            .all(|entry| !entry
                .file_name()
                .to_string_lossy()
                .starts_with(STAGING_PREFIX)),
        "no staging directory is left behind"
    );
}

#[test]
fn different_bytes_name_different_directories() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let first = directory.path().join("a/out");
    let second = directory.path().join("b/out");
    write_tree(
        &first,
        &[("generated.rs", b"pub const WHERE: &str = \"/a\";\n")],
    );
    write_tree(
        &second,
        &[("generated.rs", b"pub const WHERE: &str = \"/b\";\n")],
    );

    let a = stabilize(&first, &root).unwrap().unwrap();
    let b = stabilize(&second, &root).unwrap().unwrap();

    assert_ne!(a, b);
}

#[test]
fn a_renamed_file_names_a_different_directory() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let first = directory.path().join("a/out");
    let second = directory.path().join("b/out");
    write_tree(&first, &[("one.rs", b"x")]);
    write_tree(&second, &[("two.rs", b"x")]);

    assert_ne!(
        stabilize(&first, &root).unwrap(),
        stabilize(&second, &root).unwrap()
    );
}

#[cfg(unix)]
#[test]
fn the_executable_bit_is_part_of_the_name_and_survives_the_copy() {
    use std::os::unix::fs::PermissionsExt as _;
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let plain = directory.path().join("a/out");
    let executable = directory.path().join("b/out");
    write_tree(&plain, &[("tool", b"#!/bin/sh\n")]);
    write_tree(&executable, &[("tool", b"#!/bin/sh\n")]);
    std::fs::set_permissions(
        executable.join("tool"),
        std::fs::Permissions::from_mode(0o755),
    )
    .unwrap();

    let a = stabilize(&plain, &root).unwrap().unwrap();
    let b = stabilize(&executable, &root).unwrap().unwrap();

    assert_ne!(a, b);
    assert_eq!(
        std::fs::metadata(b.join("tool"))
            .unwrap()
            .permissions()
            .mode()
            & 0o777,
        0o555
    );
    assert_eq!(
        std::fs::metadata(a.join("tool"))
            .unwrap()
            .permissions()
            .mode()
            & 0o777,
        0o444
    );
}

#[cfg(unix)]
#[test]
fn a_tree_with_a_symlink_is_left_alone() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let out = directory.path().join("a/out");
    write_tree(&out, &[("generated.rs", b"x")]);
    std::os::unix::fs::symlink("generated.rs", out.join("link.rs")).unwrap();

    assert_eq!(stabilize(&out, &root).unwrap(), None);
    assert!(!root.exists(), "nothing was copied");
}

#[test]
fn the_scan_finds_a_mention_and_skips_what_is_not_source() {
    let directory = tempfile::tempdir().unwrap();
    let src = directory.path().join("src");
    write_tree(&src, &[("lib.rs", b"pub fn value() -> u32 { 1 }\n")]);
    assert!(!sources_mention_out_dir(&src));

    write_tree(
        &src,
        &[(
            "deep/generated.rs",
            b"include!(concat!(env!(\"OUT_DIR\"), \"/x.rs\"));\n",
        )],
    );
    assert!(sources_mention_out_dir(&src));

    // A mention under a target directory or in a non-Rust file is not the
    // crate reading it.
    let other = directory.path().join("other");
    write_tree(
        &other,
        &[
            ("target/debug/build/out/generated.rs", b"OUT_DIR"),
            ("notes.md", b"OUT_DIR"),
            (".hidden/lib.rs", b"OUT_DIR"),
            ("lib.rs", b"nothing"),
        ],
    );
    assert!(!sources_mention_out_dir(&other));
}

#[test]
fn unused_trees_are_collected_by_age_and_used_ones_kept() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let old = directory.path().join("a/out");
    let recent = directory.path().join("b/out");
    write_tree(&old, &[("generated.rs", b"old")]);
    write_tree(&recent, &[("generated.rs", b"recent")]);
    let old = stabilize(&old, &root).unwrap().unwrap();
    let recent = stabilize(&recent, &root).unwrap().unwrap();
    // The compilations that used them have exited.
    release_all_under(&root);
    let old_marker = marker_path(&root, &old.file_name().unwrap().to_string_lossy());
    let long_ago = std::time::SystemTime::now() - Duration::from_secs(3 * 24 * 60 * 60);
    std::fs::File::options()
        .write(true)
        .open(&old_marker)
        .unwrap()
        .set_times(std::fs::FileTimes::new().set_modified(long_ago))
        .unwrap();
    // A staging directory a dead process left is finished off too.
    let staging = root.join(format!("{STAGING_PREFIX}abc-1"));
    std::fs::create_dir(&staging).unwrap();
    std::fs::File::options()
        .read(true)
        .open(&staging)
        .ok()
        .map(|file| file.set_times(std::fs::FileTimes::new().set_modified(long_ago)));

    let dry = collect(&root, None, Some(Duration::from_secs(24 * 60 * 60)), true).unwrap();
    assert_eq!(dry.removed_directories, 1);
    assert!(old.exists(), "a dry run removes nothing");

    let outcome = collect(&root, None, Some(Duration::from_secs(24 * 60 * 60)), false).unwrap();

    assert_eq!(outcome.removed_directories, 1);
    assert_eq!(outcome.removed_bytes, 3);
    assert_eq!(outcome.remaining_directories, 1);
    assert!(!old.exists());
    assert!(!old_marker.exists());
    assert!(recent.exists());
    assert_eq!(stats(&root).unwrap().remaining_directories, 1);
}

#[test]
fn a_use_refreshes_the_marker_at_most_hourly() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let out = directory.path().join("a/out");
    write_tree(&out, &[("generated.rs", b"x")]);
    let stable = stabilize(&out, &root).unwrap().unwrap();
    let marker = marker_path(&root, &stable.file_name().unwrap().to_string_lossy());
    let earlier = std::time::SystemTime::now() - Duration::from_secs(10 * 60);
    std::fs::File::options()
        .write(true)
        .open(&marker)
        .unwrap()
        .set_times(std::fs::FileTimes::new().set_modified(earlier))
        .unwrap();

    stabilize(&out, &root).unwrap();
    let within_the_hour = std::fs::metadata(&marker).unwrap().modified().unwrap();
    assert!(
        within_the_hour <= earlier + Duration::from_secs(1),
        "ten minutes old is fresh enough"
    );

    let long_ago = std::time::SystemTime::now() - Duration::from_secs(2 * 60 * 60);
    std::fs::File::options()
        .write(true)
        .open(&marker)
        .unwrap()
        .set_times(std::fs::FileTimes::new().set_modified(long_ago))
        .unwrap();
    stabilize(&out, &root).unwrap();
    assert!(
        std::fs::metadata(&marker).unwrap().modified().unwrap()
            > long_ago + Duration::from_secs(60),
        "two hours old is restamped"
    );
}

#[test]
fn a_tree_a_compilation_holds_a_lease_on_is_not_collected() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let out = directory.path().join("a/out");
    write_tree(&out, &[("generated.rs", b"x")]);
    let stable = stabilize(&out, &root).unwrap().unwrap();
    let digest = stable.file_name().unwrap().to_string_lossy().into_owned();
    // A lease of this test's own, standing in for the shim of another
    // process: the leases this process took above are shared with every
    // other test in it, and released by whichever of them finishes first.
    let mut compiling =
        fslock::LockFile::open(&leases_dir(&root, &digest).join("other.lease")).unwrap();
    compiling.lock().unwrap();
    let marker = marker_path(&root, &digest);
    let long_ago = std::time::SystemTime::now() - Duration::from_secs(3 * 24 * 60 * 60);
    std::fs::File::options()
        .write(true)
        .open(&marker)
        .unwrap()
        .set_times(std::fs::FileTimes::new().set_modified(long_ago))
        .unwrap();

    let projected = collect(&root, None, Some(Duration::ZERO), true).unwrap();
    assert_eq!(projected.removed_directories, 0, "a dry run keeps it too");
    assert_eq!(projected.remaining_directories, 1);

    let outcome = collect(&root, None, Some(Duration::ZERO), false).unwrap();

    assert_eq!(outcome.removed_directories, 0, "a leased tree is kept");
    assert_eq!(outcome.remaining_directories, 1);
    assert!(stable.exists());

    // The compilations exit: their leases are nobody's, and the tree goes.
    drop(compiling);
    release_all_under(&root);
    let outcome = collect(&root, None, Some(Duration::ZERO), false).unwrap();

    assert_eq!(outcome.removed_directories, 1);
    assert!(!stable.exists());
    assert!(!leases_dir(&root, &digest).exists());
}

#[cfg(unix)]
#[test]
fn the_shared_copy_cannot_be_altered_through_its_directories() {
    use std::os::unix::fs::PermissionsExt as _;
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let out = directory.path().join("a/out");
    write_tree(&out, &[("nested/generated.rs", b"x")]);
    let stable = stabilize(&out, &root).unwrap().unwrap();

    for path in [stable.clone(), stable.join("nested")] {
        assert_eq!(
            std::fs::metadata(&path).unwrap().permissions().mode() & 0o777,
            0o555,
            "{}",
            path.display()
        );
    }
    // Collection still removes it, opening the directories up first.
    release_all_under(&root);
    let marker = marker_path(&root, &stable.file_name().unwrap().to_string_lossy());
    let long_ago = std::time::SystemTime::now() - Duration::from_secs(3 * 24 * 60 * 60);
    std::fs::File::options()
        .write(true)
        .open(&marker)
        .unwrap()
        .set_times(std::fs::FileTimes::new().set_modified(long_ago))
        .unwrap();
    collect(&root, None, Some(Duration::ZERO), false).unwrap();
    assert!(!stable.exists());
}

/// Environment-wide, so it runs in one test with the others' state cleared.
#[test]
fn a_bypassed_compilation_gets_cargos_out_dir_back_and_holds_no_lease() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let real = directory.path().join("target/build/x/out");
    write_tree(&real, &[("generated.rs", b"x")]);
    let stable = stabilize(&real, &root).unwrap().unwrap();
    let digest = stable.file_name().unwrap().to_string_lossy().into_owned();
    *ORIGINAL.lock().unwrap() = Some(real.clone().into_os_string());
    unsafe { std::env::set_var("OUT_DIR", &stable) };
    assert!(
        LEASES
            .lock()
            .unwrap()
            .keys()
            .any(|path| path.starts_with(leases_dir(&root, &digest)))
    );

    restore();

    assert_eq!(
        std::env::var_os("OUT_DIR").as_deref(),
        Some(real.as_os_str())
    );
    assert!(ORIGINAL.lock().unwrap().is_none());
    let _registrar = registrar(&root).unwrap();
    assert!(
        !leased(&root, &digest, true).unwrap(),
        "the lease went with the value"
    );
    unsafe { std::env::remove_var("OUT_DIR") };
}

#[cfg(unix)]
#[test]
fn a_name_that_spells_like_a_path_keeps_cargos_out_dir() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let nested = directory.path().join("a/out");
    let flat = directory.path().join("b/out");
    // `sub/dir` in one tree, a file literally named `sub\dir` in the other:
    // the same spelling with the separator replaced.
    write_tree(&nested, &[("sub/dir", b"")]);
    std::fs::create_dir_all(flat.join("sub")).unwrap();
    std::fs::write(flat.join("sub\\dir"), b"").unwrap();

    assert!(stabilize(&nested, &root).unwrap().is_some());
    assert_eq!(stabilize(&flat, &root).unwrap(), None);
}

#[test]
fn a_budget_evicts_the_least_recently_used_trees_first() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let mut trees = Vec::new();
    for (name, age_secs) in [("old", 300), ("older", 600), ("recent", 0)] {
        let out = directory.path().join(name).join("out");
        write_tree(&out, &[("generated.rs", format!("{name:>10}").as_bytes())]);
        let stable = stabilize(&out, &root).unwrap().unwrap();
        let marker = marker_path(&root, &stable.file_name().unwrap().to_string_lossy());
        let when = std::time::SystemTime::now() - Duration::from_secs(age_secs);
        std::fs::File::options()
            .write(true)
            .open(&marker)
            .unwrap()
            .set_times(std::fs::FileTimes::new().set_modified(when))
            .unwrap();
        trees.push((name, stable));
    }
    release_all_under(&root);

    // Ten bytes each: a budget of fifteen keeps one.
    let outcome = collect(&root, Some(15), None, false).unwrap();

    assert_eq!(outcome.removed_directories, 2);
    assert_eq!(outcome.remaining_bytes, 10);
    for (name, stable) in &trees {
        assert_eq!(stable.exists(), *name == "recent", "{name}");
    }
}

#[test]
fn a_failed_copy_leaves_no_lease_behind() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let out = directory.path().join("a/out");
    write_tree(&out, &[("generated.rs", b"x")]);
    // The root is a file, so nothing can be copied under it.
    std::fs::write(&root, b"").unwrap();

    assert!(stabilize(&out, &root).is_err());
    assert!(
        LEASES.lock().unwrap().is_empty()
            || !LEASES.lock().unwrap().keys().any(|p| p.starts_with(&root))
    );
}

#[test]
fn leases_of_a_tree_that_is_gone_are_swept() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    let out = directory.path().join("a/out");
    write_tree(&out, &[("generated.rs", b"x")]);
    let stable = stabilize(&out, &root).unwrap().unwrap();
    let digest = stable.file_name().unwrap().to_string_lossy().into_owned();
    release_all_under(&root);
    // The tree went without its leases, as when a copy failed after the
    // lease was taken and the process died.
    remove_tree(&stable).unwrap();
    assert!(leases_dir(&root, &digest).exists());

    collect(&root, None, None, false).unwrap();

    assert!(!leases_dir(&root, &digest).exists());
    assert!(!publish_lock_path(&root, &digest).exists());
}

#[test]
fn an_unlistable_root_measures_as_unknown_not_empty() {
    let directory = tempfile::tempdir().unwrap();
    let root = directory.path().join("out-dirs");
    std::fs::write(&root, b"not a directory").unwrap();

    assert_eq!(stats(&root), None);
    assert_eq!(
        stats(&directory.path().join("absent")),
        Some(PruneOutcome::default()),
        "a root that does not exist yet holds nothing"
    );
}