mbx 1.24.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
use std::path::Path;
use std::process::Command;

mod support;

fn project(path: &Path, source: &str) {
    std::fs::create_dir_all(path.join("src")).unwrap();
    std::fs::write(
        path.join("Cargo.toml"),
        "[package]\nname='scope-fixture'\nversion='0.0.0'\nedition='2021'\n",
    )
    .unwrap();
    std::fs::write(path.join("src/main.rs"), source).unwrap();
}

fn mbx(root: &Path) -> Command {
    let mut command = Command::new(env!("CARGO_BIN_EXE_mbx"));
    command.env_clear();
    // rustc's MSVC discovery needs the Visual Studio/SDK environment as well
    // as PATH. Without it Git's unrelated link.exe can win resolution.
    #[cfg(windows)]
    for (name, value) in std::env::vars_os() {
        let key = name.to_string_lossy().to_ascii_uppercase();
        if !key.starts_with("MBX_")
            && !matches!(
                key.as_str(),
                "RUSTC_WRAPPER"
                    | "RUSTC_WORKSPACE_WRAPPER"
                    | "RUSTDOC"
                    | "CARGO_TARGET_DIR"
                    | "CARGO_INCREMENTAL"
                    | "CARGO"
            )
        {
            command.env(name, value);
        }
    }
    for name in [
        "PATH",
        "HOME",
        "RUSTUP_HOME",
        "CARGO_HOME",
        "RUSTUP_TOOLCHAIN",
        "SystemRoot",
        "TEMP",
        "TMP",
        "TMPDIR",
    ] {
        if let Some(value) = std::env::var_os(name) {
            let key = if cfg!(windows) && name == "PATH" {
                "Path"
            } else {
                name
            };
            command.env(key, value);
        }
    }
    support::isolate_host(&mut command);
    command
        .current_dir(root)
        .env("MBX_CACHE_DIR", root.join("cache"))
        .env("MBX_LINKER", "system")
        .env("CI", "1")
        .env("MBX_SUMMARY", "off");
    command
}

/// Separate fixtures keep a successful direct build from masking a nested
/// build that writes its output into the parent's target directory.
#[test]
fn nested_test_builds_match_plain_cargo_target_directories() {
    for setting in [
        None,
        Some("relative-target"),
        Some("absolute"),
        Some("flag"),
    ] {
        for managed in [false, true] {
            let root = tempfile::tempdir().unwrap();
            let root_path = root.path().canonicalize().unwrap();
            project(
                &root_path,
                r#"
fn main() {}
#[test]
fn nested_build() {
    use std::{path::PathBuf, process::Command};
    let guest = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("guest");
    let target = std::env::var_os("CARGO_TARGET_DIR");
    let expected = std::env::var_os("EXPECTED_CALLER_TARGET");
    let expected_dir = expected.clone().map(PathBuf::from).unwrap_or_else(|| PathBuf::from("target"));
    let artifact = guest.join(expected_dir).join("debug/libnested_guest.rlib");
    // Force the child to produce the artifact even on a warm parent build.
    if artifact.exists() { std::fs::remove_file(&artifact).unwrap(); }
    for cargo in [std::ffi::OsString::from("cargo"), std::env::var_os("CARGO").unwrap()] {
        let output = Command::new(cargo).args(["build", "--offline", "--manifest-path"])
            .arg(guest.join("Cargo.toml")).current_dir(&guest).output().unwrap();
        assert!(output.status.success(), "{output:?}");
        assert!(artifact.is_file(), "missing {}: {output:?}", artifact.display());
        std::fs::remove_file(&artifact).unwrap();
    }
    assert_eq!(target, expected);
}
"#,
            );
            let guest = root_path.join("guest");
            std::fs::create_dir_all(guest.join("src")).unwrap();
            std::fs::write(
                guest.join("Cargo.toml"),
                "[package]\nname='nested-guest'\nversion='0.0.0'\nedition='2021'\n[workspace]\n",
            )
            .unwrap();
            std::fs::write(guest.join("src/lib.rs"), "pub fn guest() {}\n").unwrap();
            let caller_target = setting.filter(|value| *value != "flag").map(|value| {
                if value == "absolute" {
                    root_path.join("absolute-target")
                } else {
                    value.into()
                }
            });
            for pass in 0..3 {
                if pass == 2 {
                    let target = root_path.join(if setting == Some("flag") {
                        Path::new("flag-target")
                    } else {
                        caller_target.as_deref().unwrap_or(Path::new("target"))
                    });
                    // Wipe compiled outputs without leaving a dangling Windows
                    // junction at the managed target directory.
                    std::fs::remove_dir_all(target.join("debug")).unwrap();
                }
                let mut command = mbx(&root_path);
                // The same shim/environment is used for both cases; disabling
                // mbx gives Cargo ownership of the entire plain control build.
                command
                    .env("MBX_CARGO_SHIM_MODE", "1")
                    .env("MBX_DISABLE", if managed { "0" } else { "1" })
                    .env("MBX_TARGET_ROOT", root_path.join("managed-targets"))
                    .env("MBX_STATS_REPORT", root_path.join("stats.json"))
                    .args(["test", "--offline"]);
                if setting == Some("flag") {
                    command.args(["--target-dir", "flag-target"]);
                }
                command.args(["--", "--nocapture"]);
                if let Some(target) = &caller_target {
                    command
                        .env("CARGO_TARGET_DIR", target)
                        .env("EXPECTED_CALLER_TARGET", target);
                }
                let output = command.output().unwrap();
                assert!(
                    output.status.success(),
                    "managed={managed}, target={caller_target:?}, pass={pass}:\n{}\n{}",
                    String::from_utf8_lossy(&output.stdout),
                    String::from_utf8_lossy(&output.stderr)
                );
                if managed && pass == 2 {
                    let report: serde_json::Value = serde_json::from_slice(
                        &std::fs::read(root_path.join("stats.json")).unwrap(),
                    )
                    .unwrap();
                    assert!(report["hits"].as_u64().unwrap() > 0, "{report}");
                }
            }
        }
    }
}

/// Check fresh and warm builds report checkout-local executable paths, and
/// `cargo run` restores the caller's target setting before starting the program.
#[test]
fn build_artifact_paths_and_run_keep_caller_target_settings() {
    for explicit in [false, true] {
        let root = tempfile::Builder::new()
            .prefix("mbx target ")
            .tempdir()
            .unwrap();
        // Match Cargo's workspace spelling: resolve /var on macOS, but avoid
        // introducing the verbatim \\?\ prefix Cargo omits on Windows.
        let root = if cfg!(windows) {
            root.path().to_path_buf()
        } else {
            root.path().canonicalize().unwrap()
        };
        project(
            &root,
            r#"
fn main() {
    assert_eq!(std::env::var_os("CARGO_TARGET_DIR"), std::env::var_os("EXPECTED_TARGET"));
    assert!(std::env::var_os("MBX_SOCKET").is_none());
}
"#,
        );
        let target = if explicit { "caller target" } else { "target" };
        let command = || {
            let mut command = mbx(&root);
            if explicit {
                command
                    .env("CARGO_TARGET_DIR", target)
                    .env("EXPECTED_TARGET", target);
            }
            command
        };
        for _ in 0..2 {
            let output = command()
                .args(["build", "--offline", "--message-format=json"])
                .output()
                .unwrap();
            assert!(
                output.status.success(),
                "{}",
                String::from_utf8_lossy(&output.stderr)
            );
            let executable = String::from_utf8_lossy(&output.stdout)
                .lines()
                .filter_map(|line| serde_json::from_str::<serde_json::Value>(line).ok())
                .find_map(|value| value["executable"].as_str().map(std::path::PathBuf::from))
                .unwrap();
            assert!(
                executable.starts_with(root.join(target)),
                "{}",
                executable.display()
            );
            assert!(executable.is_file());
            let output = command().args(["run", "--offline"]).output().unwrap();
            assert!(
                output.status.success(),
                "{}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
    }
}

#[test]
fn cargo_run_restores_settings_before_starting_application() {
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        r#"
fn main() {
    for key in ["MBX_SOCKET", "MBX_BUILD", "MBX_SESSION_RESTORE", "MBX_SESSION_LEASE", "MBX_LAUNCH_CAPTURE", "MBX_WORKSPACE_ROOT", "MBX_TARGET_DIR", "RUSTDOC", "RUSTC_WRAPPER", "CARGO_TARGET_DIR"] {
        assert!(std::env::var_os(key).is_none(), "{key} leaked");
    }
    assert_eq!(std::env::var("CARGO_INCREMENTAL").unwrap(), "1");
    assert_eq!(std::env::var("MBX_VERIFY").unwrap(), "0");
    let path: Vec<_> = std::env::split_paths(&std::env::var_os("PATH").unwrap()).collect();
    let expected: Vec<_> = std::env::split_paths(&std::env::var_os("EXPECTED_PATH").unwrap()).collect();
    assert!(path.ends_with(&expected));
    assert!(!std::env::var("CARGO").unwrap().is_empty());
    assert_eq!(std::env::args().nth(1).unwrap(), "argument with spaces");
    println!("application started");
    std::process::exit(23);
}
"#,
    );
    let output = mbx(root.path())
        .env("CARGO_INCREMENTAL", "1")
        .env("MBX_VERIFY", "0")
        .env("EXPECTED_PATH", std::env::var_os("PATH").unwrap())
        .env("MBX_CARGO_SHIM_MODE", "1")
        .env("RUSTFLAGS", "-C prefer-dynamic")
        .args([
            "--color",
            "never",
            "run",
            "--offline",
            "--",
            "argument with spaces",
        ])
        .output()
        .unwrap();
    assert_eq!(
        output.status.code(),
        Some(23),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("application started"));
}

#[cfg(unix)]
#[test]
fn cargo_run_preserves_configured_runner_and_wrapper() {
    use std::os::unix::fs::PermissionsExt;
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        r#"
fn main() {
    assert!(std::env::var_os("MBX_SOCKET").is_none());
    assert!(std::env::var("RUSTC_WRAPPER").unwrap().ends_with("caller-wrapper"));
    assert_eq!(std::env::var("RUNNER_ARGUMENT").unwrap(), "runner arg");
}
"#,
    );
    let wrapper = root.path().join("caller-wrapper");
    std::fs::write(&wrapper, "#!/bin/sh\nexec \"$@\"\n").unwrap();
    std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o755)).unwrap();
    let runner = root.path().join("caller-runner");
    std::fs::write(
        &runner,
        "#!/bin/sh\nexport RUNNER_ARGUMENT=\"$1\"\nshift\nexec \"$@\"\n",
    )
    .unwrap();
    std::fs::set_permissions(&runner, std::fs::Permissions::from_mode(0o755)).unwrap();
    std::fs::create_dir(root.path().join(".cargo")).unwrap();
    std::fs::write(
        root.path().join(".cargo/config.toml"),
        "[target.'cfg(unix)']\nrunner = ['./caller-runner', 'runner arg']\n",
    )
    .unwrap();
    let output = mbx(root.path())
        .env("RUSTC_WRAPPER", wrapper)
        .args(["run", "--offline"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[cfg(unix)]
#[test]
fn application_daemon_outlives_session_and_captures_an_independent_build() {
    let root = tempfile::tempdir().unwrap();
    let other = root.path().join("other");
    project(&other, "fn main() {}\n");
    project(
        root.path(),
        r#"
use std::process::{Command, Stdio};
fn main() {
    let args: Vec<_> = std::env::args().collect();
    if args.get(1).map(String::as_str) != Some("daemon") {
        assert!(std::os::unix::net::UnixStream::connect(env!("OUTER_SOCKET")).is_err(), "session still alive at application launch");
        Command::new(std::env::current_exe().unwrap()).arg("daemon").args(&args[1..])
            .stdin(Stdio::null()).stdout(Stdio::null()).stderr(Stdio::null()).spawn().unwrap();
        return;
    }
    for _ in 0..200 {
        if std::path::Path::new(&args[4]).exists() { break; }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    assert!(std::path::Path::new(&args[4]).exists());
    assert!(std::env::var_os("MBX_SOCKET").is_none());
    assert!(std::env::var_os("RUSTC_WRAPPER").is_none());
    let output = Command::new(&args[2]).args(["check", "--offline"])
        .current_dir(&args[3]).env("MBX_LOG", "debug").output().unwrap();
    assert!(output.status.success(), "{:?}", output);
    assert!(!output.stderr.is_empty());
    std::fs::write(&args[5], "independent build captured").unwrap();
}
"#,
    );
    std::fs::write(root.path().join("build.rs"),
        "fn main() { println!(\"cargo:rustc-env=OUTER_SOCKET={}\", std::env::var(\"MBX_SOCKET\").unwrap()); }\n").unwrap();
    let gate = root.path().join("launcher-exited");
    let result = root.path().join("daemon-result");
    let output = mbx(root.path())
        .args(["run", "--offline", "--", env!("CARGO_BIN_EXE_mbx")])
        .arg(&other)
        .arg(&gate)
        .arg(&result)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    std::fs::write(&gate, "go").unwrap();
    let mut contents = String::new();
    for _ in 0..200 {
        contents = std::fs::read_to_string(&result).unwrap_or_default();
        if contents == "independent build captured" {
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(50));
    }
    assert_eq!(contents, "independent build captured");
    assert!(!String::from_utf8_lossy(&output.stderr).contains("Checking scope-fixture"));
}

#[test]
fn stale_session_recovers_original_environment_before_building() {
    use std::ffi::OsString;
    let root = tempfile::tempdir().unwrap();
    project(root.path(), "fn main() {}\n");
    std::fs::write(
        root.path().join("build.rs"),
        r#"
fn main() {
    assert!(!std::env::var("MBX_SOCKET").unwrap().contains("missing-session"));
    assert!(std::env::var("MBX_WORKSPACE_ROOT").unwrap().contains(".tmp"));
}
"#,
    )
    .unwrap();
    let restore: Vec<(OsString, Option<OsString>)> = [
        "MBX_SOCKET",
        "MBX_SESSION_LEASE",
        "MBX_SESSION_RESTORE",
        "RUSTC_WRAPPER",
        "MBX_WORKSPACE_ROOT",
    ]
    .into_iter()
    .map(|key| (key.into(), None))
    .collect();
    let output = mbx(root.path())
        .env("MBX_SOCKET", "missing-session")
        .env("MBX_SESSION_LEASE", root.path().join("missing-owner"))
        .env(
            "MBX_SESSION_RESTORE",
            serde_json::to_string(&restore).unwrap(),
        )
        .env("RUSTC_WRAPPER", "missing-wrapper")
        .env("MBX_WORKSPACE_ROOT", "wrong-workspace")
        .args(["check", "--offline"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[cfg(unix)]
#[test]
fn command_line_runner_override_uses_original_environment() {
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        "fn main() { assert!(std::env::var_os(\"MBX_SOCKET\").is_none()); }\n",
    );
    let output = mbx(root.path())
        .args([
            "run",
            "--offline",
            "--config",
            "target.'cfg(unix)'.runner = ['env', 'CALLER_RUNNER=1']",
        ])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[cfg(unix)]
#[test]
fn nested_build_orchestrators_keep_the_live_session() {
    use std::os::unix::fs::PermissionsExt;
    for orchestrator in ["watch", "nextest"] {
        let root = tempfile::tempdir().unwrap();
        project(root.path(), "fn main() {}\n");
        std::fs::write(
            root.path().join("build.rs"),
            r#"
fn main() {
    assert_eq!(std::env::var("MBX_SOCKET").unwrap(), std::env::var("EXPECTED_SOCKET").unwrap());
    assert_eq!(std::env::var("MBX_BUILD").unwrap(), std::env::var("EXPECTED_BUILD").unwrap());
}
"#,
        )
        .unwrap();
        let program = root.path().join(format!("cargo-{orchestrator}"));
        std::fs::write(&program, "#!/bin/sh\nexport EXPECTED_SOCKET=\"$MBX_SOCKET\" EXPECTED_BUILD=\"$MBX_BUILD\"\nMBX_CARGO_SHIM_MODE=1 exec \"$TEST_MBX\" check --offline\n").unwrap();
        std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap();
        let path = std::env::join_paths(
            std::iter::once(root.path().to_path_buf())
                .chain(std::env::split_paths(&std::env::var_os("PATH").unwrap())),
        )
        .unwrap();
        let output = mbx(root.path())
            .env("PATH", path)
            .env("TEST_MBX", env!("CARGO_BIN_EXE_mbx"))
            .arg(orchestrator)
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "{orchestrator}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

/// nextest's `-j` is its test-thread count, and an alias that names nextest
/// must be read as nextest: only `--build-jobs` limits the build's permits.
#[cfg(unix)]
#[test]
fn nextest_test_threads_do_not_limit_build_permits_through_an_alias() {
    use std::os::unix::fs::PermissionsExt;
    let root = tempfile::tempdir().unwrap();
    project(root.path(), "fn main() {}\n");
    std::fs::create_dir_all(root.path().join(".cargo")).unwrap();
    std::fs::write(
        root.path().join(".cargo/config.toml"),
        "[alias]\nnt = \"nextest run\"\n",
    )
    .unwrap();
    let bin = root.path().join("bin");
    std::fs::create_dir_all(&bin).unwrap();
    let program = bin.join("cargo-nextest");
    std::fs::write(
        &program,
        "#!/bin/sh\nprintf '%s' \"$MBX_SCHED_BUILD_SLOTS\" > \"$SLOTS_FILE\"\n",
    )
    .unwrap();
    std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap();
    let path = std::env::join_paths(
        std::iter::once(bin).chain(std::env::split_paths(&std::env::var_os("PATH").unwrap())),
    )
    .unwrap();
    let slots = root.path().join("slots");
    for (typed, expected) in [
        (&["nextest", "run", "-j", "1"][..], "8"),
        (&["nt", "-j", "1"], "8"),
        (&["nt", "--test-threads=1"], "8"),
        (&["nt", "--build-jobs", "2"], "2"),
    ] {
        let _ = std::fs::remove_file(&slots);
        let output = mbx(root.path())
            .env("PATH", &path)
            .env("SLOTS_FILE", &slots)
            .env("MBX_SCHEDULER", "1")
            .env("MBX_SCHEDULER_CPUS", "8")
            .args(typed)
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "{typed:?}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert_eq!(
            std::fs::read_to_string(&slots).unwrap(),
            expected,
            "{typed:?}"
        );
    }
}

#[cfg(unix)]
#[test]
fn launch_preserves_non_unicode_environment_and_temporary_paths_with_spaces() {
    use std::os::unix::ffi::OsStringExt;
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        r#"
use std::os::unix::ffi::OsStrExt;
fn main() { assert_eq!(std::env::var_os("OPAQUE").unwrap().as_bytes(), b"\xff"); }
"#,
    );
    let temporary = tempfile::Builder::new()
        .prefix("mbx space ")
        .tempdir_in("/tmp")
        .unwrap();
    let output = mbx(root.path())
        .env("TMPDIR", temporary.path())
        .env("MBX_CC", "0")
        .env("OPAQUE", std::ffi::OsString::from_vec(vec![255]))
        .args(["run", "--offline"])
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
}

#[cfg(unix)]
#[test]
fn scheduled_test_binaries_hold_a_permit_and_keep_the_configured_runner() {
    use std::os::unix::fs::PermissionsExt;
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        r#"
fn main() {}
#[test]
fn scheduled() {
    // Long enough to be measured, and idle: the suite keeps one core busy
    // at most, which the next default-width run is weighed by.
    std::thread::sleep(std::time::Duration::from_millis(1100));
    assert_eq!(std::env::var("RUNNER_ARGUMENT").unwrap(), "runner arg");
    let leases = std::path::Path::new(&std::env::var_os("MBX_CACHE_DIR").unwrap())
        .join("scheduler/leases");
    let held: Vec<String> = std::fs::read_dir(&leases)
        .map(|entries| {
            entries
                .map(|entry| std::fs::read_to_string(entry.unwrap().path()).unwrap())
                .collect()
        })
        .unwrap_or_default();
    match std::env::var("EXPECTED_WEIGHT").ok() {
        Some(weight) => {
            assert_eq!(held.len(), 1, "only this test holds a permit: {held:?}");
            assert!(held[0].contains(&format!("\"weight\":{weight},")), "{held:?}");
            // Builds this test starts are charged to its permit, and find
            // the caller's runners rather than the shim.
            assert_eq!(std::env::var("MBX_SCHEDULER").unwrap(), "0");
            assert_eq!(std::env::var("MBX_SCHED_DIR").unwrap(), "");
            assert!(std::env::var_os("MBX_TEST_RUNNERS").is_none());
            for (name, value) in std::env::vars() {
                assert!(!value.contains("mbx-test-runner"), "{name}={value}");
            }
        }
        None => {
            assert!(held.is_empty(), "{held:?}");
            assert!(std::env::var_os("MBX_SCHEDULER").is_none());
        }
    }
}
"#,
    );
    // The library's own, empty test binary finishes at once and is recorded
    // as one core, so it must not share the ledger name of the binary under
    // test.
    std::fs::write(
        root.path().join("Cargo.toml"),
        "[package]\nname='scope-fixture'\nversion='0.0.0'\nedition='2021'\n\n[lib]\nname='scope_lib'\n",
    )
    .unwrap();
    // Rustdoc runs doctests through the same runner; they hold no permit.
    std::fs::write(
        root.path().join("src/lib.rs"),
        r#"
/// ```
/// let leases = std::path::Path::new(&std::env::var_os("MBX_CACHE_DIR").unwrap())
///     .join("scheduler/leases");
/// let held = std::fs::read_dir(&leases).map(|entries| entries.count()).unwrap_or(0);
/// assert_eq!(held, 0, "a doctest holds no permit");
/// ```
pub fn documented() {}
"#,
    )
    .unwrap();
    let runner = root.path().join("caller-runner");
    std::fs::write(
        &runner,
        "#!/bin/sh\nexport RUNNER_ARGUMENT=\"$1\"\nshift\nexec \"$@\"\n",
    )
    .unwrap();
    std::fs::set_permissions(&runner, std::fs::Permissions::from_mode(0o755)).unwrap();
    std::fs::create_dir(root.path().join(".cargo")).unwrap();
    std::fs::write(
        root.path().join(".cargo/config.toml"),
        "[target.'cfg(unix)']\nrunner = ['./caller-runner', 'runner arg']\n",
    )
    .unwrap();
    for (tests, harness, expected) in [
        (Some("1"), &["--test-threads=3"][..], Some("3")),
        // Half of the 8 permits until this binary has been measured,
        (Some("1"), &[][..], Some("4")),
        (None, &[][..], None),
        // then the one core the idle suite was measured using.
        (Some("1"), &[][..], Some("1")),
    ] {
        let mut command = mbx(root.path());
        command
            .env("MBX_SCHEDULER_CPUS", "8")
            .env("MBX_SCHEDULER_MEMORY", "none")
            .args(["test", "--offline", "--"])
            .args(harness);
        if let Some(tests) = tests {
            command.env("MBX_SCHEDULER_TESTS", tests);
        }
        if let Some(expected) = expected {
            command.env("EXPECTED_WEIGHT", expected);
        }
        let output = command.output().unwrap();
        assert!(
            output.status.success(),
            "{tests:?} {harness:?}: {}{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }
    // Cargo hands the test's package to the runner, and the measured history
    // is kept under it rather than under the bare binary name.
    let ledger = std::fs::read_to_string(root.path().join("cache/scheduler/memory.json")).unwrap();
    assert!(
        ledger.contains("scope-fixture/scope_fixture [test]\":1"),
        "{ledger}"
    );
}

#[cfg(unix)]
#[test]
fn a_test_binary_that_finishes_in_under_a_second_is_weighed_one_core() {
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        r#"
fn main() {}
#[test]
fn quick() {
    let leases = std::path::Path::new(&std::env::var_os("MBX_CACHE_DIR").unwrap())
        .join("scheduler/leases");
    let held: Vec<String> = std::fs::read_dir(&leases)
        .unwrap()
        .map(|entry| std::fs::read_to_string(entry.unwrap().path()).unwrap())
        .collect();
    let weight = std::env::var("EXPECTED_WEIGHT").unwrap();
    assert_eq!(held.len(), 1, "{held:?}");
    assert!(held[0].contains(&format!("\"weight\":{weight},")), "{held:?}");
}
"#,
    );
    std::fs::write(
        root.path().join("Cargo.toml"),
        "[package]\nname='scope-fixture'\nversion='0.0.0'\nedition='2021'\n\n[lib]\nname='scope_lib'\n",
    )
    .unwrap();
    std::fs::write(root.path().join("src/lib.rs"), "").unwrap();
    // Unmeasured, the suite asks for half of the 8 permits; one run that
    // ends well under a second records it as a single core.
    for expected in ["4", "1"] {
        let output = mbx(root.path())
            .env("MBX_SCHEDULER_CPUS", "8")
            .env("MBX_SCHEDULER_MEMORY", "none")
            .env("MBX_SCHEDULER_TESTS", "1")
            .env("EXPECTED_WEIGHT", expected)
            .args(["test", "--offline"])
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "expected weight {expected}: {}{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

#[test]
fn cargo_run_reads_an_empty_rustc_wrapper_as_none() {
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        "fn main() { println!(\"application started\"); }",
    );
    std::fs::create_dir_all(root.path().join(".cargo")).unwrap();
    // A `cfg()` section makes the runner lookup ask rustc for the target's cfg,
    // and an empty wrapper is how a workspace cancels one it inherits.
    std::fs::write(
        root.path().join(".cargo/config.toml"),
        r#"[target.'cfg(all(windows, target_env = "msvc"))']
rustflags = ["-C", "target-feature=+crt-static"]

[target.'cfg(target_os = "macos")']
rustflags = ["-C", "split-debuginfo=unpacked"]

[build]
rustc-wrapper = ""
"#,
    )
    .unwrap();

    let output = mbx(root.path())
        .args(["run", "--offline"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "run failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(String::from_utf8_lossy(&output.stdout).contains("application started"));
}

#[cfg(unix)]
#[test]
fn scheduled_test_binaries_hold_a_permit_with_an_empty_rustc_wrapper() {
    let root = tempfile::tempdir().unwrap();
    project(
        root.path(),
        r#"
fn main() {}
#[test]
fn scheduled() {
    let leases = std::path::Path::new(&std::env::var_os("MBX_CACHE_DIR").unwrap())
        .join("scheduler/leases");
    let held = std::fs::read_dir(&leases).map(|entries| entries.count()).unwrap_or(0);
    assert_eq!(held, 1, "this test should hold a permit");
}
"#,
    );
    std::fs::create_dir(root.path().join(".cargo")).unwrap();
    // The runner lookup that schedules tests evaluates `cfg()` sections by
    // asking rustc, and an emptied wrapper used to make that lookup fail. It
    // fails quietly, so a passing `cargo test` alone would not show it: the
    // tests just ran without a permit.
    std::fs::write(
        root.path().join(".cargo/config.toml"),
        r#"[target.'cfg(all(windows, target_env = "msvc"))']
rustflags = ["-C", "target-feature=+crt-static"]

[build]
rustc-wrapper = ""
"#,
    )
    .unwrap();

    let output = mbx(root.path())
        .env("MBX_SCHEDULER_CPUS", "8")
        .env("MBX_SCHEDULER_MEMORY", "none")
        .env("MBX_SCHEDULER_TESTS", "1")
        .args(["test", "--offline"])
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "{}{}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
}