whisker-dev-server 0.2.1

Host-side dev server for `whisker run`. File watch + cargo build + WebSocket push of subsecond patches. Pulled in by whisker-cli; no presence in release builds.
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
//! Fat-build runner + captured-args loader.
//!
//! The other half of the rustc/linker hijack started in I4g-4a.
//! `whisker-rustc-shim` writes a JSON file per rustc invocation into a
//! cache dir; this module:
//!
//! 1. Spawns the *fat build* — a normal cargo build with
//!    `RUSTC_WORKSPACE_WRAPPER=whisker-rustc-shim` set, so the cache
//!    fills up.
//! 2. Loads those JSON files back into a `HashMap<String,
//!    CapturedRustcInvocation>` keyed by crate name, picking the
//!    most recent timestamp when a crate was rebuilt mid-session.
//! 3. (Future, I4g-5) hands the captured args to a thin-rebuild
//!    driver that only recompiles the changed crate and re-links.
//!
//! `CapturedRustcInvocation` is currently *defined* here, not in
//! whisker-cli, so that the shim binary doesn't need to pull in the
//! whole dev-server dep tree (tokio / axum / notify / object). The
//! shim has its own copy of the struct shape; serde keeps the wire
//! format compatible. A future cleanup will extract a tiny
//! `whisker-hotpatch-types` crate and dedupe both sides — see TODO.

use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::Target;

/// Mirrors `whisker_cli::rustc_shim::CapturedRustcInvocation` exactly.
/// Kept duplicated (rather than imported) so the shim binary stays
/// dep-light. JSON wire format is what binds them — both sides go
/// through serde, so a field rename in one without the other will
/// trip the deserialize step at run time and emit a clear error.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct CapturedRustcInvocation {
    pub crate_name: String,
    pub args: Vec<String>,
    pub timestamp_micros: u128,
}

/// Mirrors `whisker_cli::linker_shim::CapturedLinkerInvocation` exactly.
/// Same duplication rationale as [`CapturedRustcInvocation`].
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct CapturedLinkerInvocation {
    pub output: Option<String>,
    pub args: Vec<String>,
    pub timestamp_micros: u128,
}

/// Optional linker-shim wiring for [`run_fat_build`]. Provide all
/// three when you want the linker invocation captured (Tier 1 needs
/// it); leave this `None` for plain Tier 2 / Tier 0 fat builds.
#[derive(Debug, Clone)]
pub struct LinkerCaptureConfig<'a> {
    /// Path to `whisker-linker-shim`. The shim is the value rustc sees
    /// for `-C linker=<…>`.
    pub shim_path: &'a Path,
    /// Where the shim writes its JSON cache files.
    pub cache_dir: &'a Path,
    /// What the shim forwards to. Typically `xcrun -f clang` on
    /// macOS or PATH-resolved `cc` on Linux. Required because the
    /// shim doesn't know the real linker on its own.
    pub real_linker: &'a Path,
}

/// Spawn a `cargo` build for the given target with `RUSTC_WORKSPACE_WRAPPER`
/// pointed at `shim_path` and `WHISKER_RUSTC_CACHE_DIR` pointed at `cache_dir`.
/// Inherits stdout/stderr so cargo's progress is visible. After the build
/// completes successfully, [`load_captured_args`] can read the cache.
///
/// When `linker_capture` is `Some(_)`, the build *also* installs the
/// linker shim by setting `RUSTFLAGS=-Clinker=<shim>` plus the
/// shim's two env vars. The two captures (rustc-args and linker-args)
/// then both fill up during the same fat build, and Tier 1's
/// thin_rebuild_obj can replay them together.
///
/// `target` is currently a hint only; the cargo command we run is the
/// host build (`cargo build -p <pkg>`). I4g-5 will switch to the
/// platform-specific `whisker-build` invocations once thin rebuild
/// is wired up.
pub fn run_fat_build(
    workspace_root: &Path,
    package: &str,
    _target: Target,
    shim_path: &Path,
    cache_dir: &Path,
    linker_capture: Option<&LinkerCaptureConfig<'_>>,
) -> Result<()> {
    std::fs::create_dir_all(cache_dir)
        .with_context(|| format!("create cache dir {}", cache_dir.display()))?;
    let mut cmd = Command::new("cargo");
    cmd.args(["build", "-p", package])
        .current_dir(workspace_root)
        .env("RUSTC_WORKSPACE_WRAPPER", shim_path)
        .env("WHISKER_RUSTC_CACHE_DIR", cache_dir);

    if let Some(lc) = linker_capture {
        std::fs::create_dir_all(lc.cache_dir)
            .with_context(|| format!("create linker cache dir {}", lc.cache_dir.display()))?;
        // Prepend our -Clinker to any existing RUSTFLAGS rather than
        // clobbering them — the user may have set their own through
        // .cargo/config.toml or env.
        let mut rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();
        if !rustflags.is_empty() {
            rustflags.push(' ');
        }
        rustflags.push_str(&format!("-Clinker={}", lc.shim_path.display()));
        cmd.env("RUSTFLAGS", rustflags)
            .env("WHISKER_LINKER_CACHE_DIR", lc.cache_dir)
            .env("WHISKER_REAL_LINKER", lc.real_linker);
    }

    let status = cmd.status().context("spawn cargo for fat build")?;
    if !status.success() {
        anyhow::bail!("fat build failed: cargo exited {status}");
    }
    Ok(())
}

/// Walk `cache_dir`, deserialise every `*.json` produced by
/// `whisker-rustc-shim`, and collapse duplicates per crate by keeping
/// the most-recent timestamp. Empty / unparseable files are skipped
/// with a warning rather than aborting the whole load — a partial
/// fat build shouldn't take the dev loop down.
///
/// `target_triple_filter` restricts the load to entries whose
/// captured `--target` matches. Multi-arch fat builds (e.g. iOS Simulator
/// on Apple Silicon, which xcodebuild compiles for both
/// `aarch64-apple-ios-sim` and `x86_64-apple-ios`) write one JSON per
/// (crate, triple) pair; without the filter the newest-timestamp wins
/// regardless of triple, leaving the patcher likely to pick the wrong
/// arch and emit an `.o` that fails to link into the runtime dylib
/// (`found architecture 'x86_64', required architecture 'arm64'`).
/// Pass `None` to disable filtering (host / single-arch paths).
pub fn load_captured_args(
    cache_dir: &Path,
    target_triple_filter: Option<&str>,
) -> Result<HashMap<String, CapturedRustcInvocation>> {
    let mut by_crate: HashMap<String, CapturedRustcInvocation> = HashMap::new();
    if !cache_dir.is_dir() {
        return Ok(by_crate); // empty cache is fine, just nothing to do
    }
    for entry in
        std::fs::read_dir(cache_dir).with_context(|| format!("read_dir {}", cache_dir.display()))?
    {
        let entry = entry?;
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("json") {
            continue;
        }
        let body = match std::fs::read_to_string(&path) {
            Ok(b) => b,
            Err(e) => {
                whisker_build::ui::warn(format!("skip {}: {e}", path.display()));
                continue;
            }
        };
        let inv: CapturedRustcInvocation = match serde_json::from_str(&body) {
            Ok(i) => i,
            Err(e) => {
                whisker_build::ui::warn(format!("skip {}: malformed json: {e}", path.display()));
                continue;
            }
        };
        if let Some(want) = target_triple_filter {
            if invocation_target_triple(&inv) != Some(want) {
                continue;
            }
        }
        keep_newest(&mut by_crate, inv);
    }
    Ok(by_crate)
}

/// Extract `--target <triple>` from a captured rustc args list, if
/// present. Returns `None` for host-build invocations (which omit
/// `--target` and let cargo's default kick in).
fn invocation_target_triple(inv: &CapturedRustcInvocation) -> Option<&str> {
    let mut iter = inv.args.iter();
    while let Some(a) = iter.next() {
        if a == "--target" {
            return iter.next().map(String::as_str);
        }
        if let Some(rest) = a.strip_prefix("--target=") {
            return Some(rest);
        }
    }
    None
}

/// Pure helper for the load loop's "keep most-recent per crate"
/// decision. Pulled out so unit tests don't have to write JSON to
/// disk to exercise the merge.
pub fn keep_newest(
    map: &mut HashMap<String, CapturedRustcInvocation>,
    inv: CapturedRustcInvocation,
) {
    match map.get(&inv.crate_name) {
        Some(prev) if prev.timestamp_micros >= inv.timestamp_micros => {
            // already have a newer or equal-timestamp entry; ignore.
        }
        _ => {
            map.insert(inv.crate_name.clone(), inv);
        }
    }
}

/// Walk a `whisker-linker-shim` cache dir and collapse duplicates per
/// output filename, keeping the most-recent timestamp. Same shape
/// as [`load_captured_args`] but for the linker side. Empty /
/// unparseable files get a warning, not an abort.
///
/// Keying by the **basename of the output path** (`libdemo.dylib` →
/// `libdemo.dylib`) lets the patcher look up the linker call that
/// produced a specific crate's library without having to re-derive
/// the cargo's hash-suffixed path. Output-less captures are keyed
/// under `_unknown`.
pub fn load_captured_linker_args(
    cache_dir: &Path,
) -> Result<HashMap<String, CapturedLinkerInvocation>> {
    let mut by_output: HashMap<String, CapturedLinkerInvocation> = HashMap::new();
    if !cache_dir.is_dir() {
        return Ok(by_output);
    }
    for entry in
        std::fs::read_dir(cache_dir).with_context(|| format!("read_dir {}", cache_dir.display()))?
    {
        let entry = entry?;
        let path = entry.path();
        if path.extension().and_then(|e| e.to_str()) != Some("json") {
            continue;
        }
        let body = match std::fs::read_to_string(&path) {
            Ok(b) => b,
            Err(e) => {
                whisker_build::ui::warn(format!("skip {}: {e}", path.display()));
                continue;
            }
        };
        let inv: CapturedLinkerInvocation = match serde_json::from_str(&body) {
            Ok(i) => i,
            Err(e) => {
                whisker_build::ui::warn(format!("skip {}: malformed json: {e}", path.display()));
                continue;
            }
        };
        keep_newest_linker(&mut by_output, inv);
    }
    Ok(by_output)
}

/// Pure helper for [`load_captured_linker_args`] — same "keep
/// most-recent per key" pattern as [`keep_newest`], but the key is
/// the output filename rather than a crate name.
pub fn keep_newest_linker(
    map: &mut HashMap<String, CapturedLinkerInvocation>,
    inv: CapturedLinkerInvocation,
) {
    let key = inv
        .output
        .as_deref()
        .and_then(|s| Path::new(s).file_name())
        .and_then(|n| n.to_str())
        .unwrap_or("_unknown")
        .to_string();
    match map.get(&key) {
        Some(prev) if prev.timestamp_micros >= inv.timestamp_micros => {}
        _ => {
            map.insert(key, inv);
        }
    }
}

/// Convenience: best-effort default cache dir under the workspace's
/// `target/.whisker/rustc-args/`. Created on demand.
pub fn default_cache_dir(workspace_root: &Path) -> PathBuf {
    workspace_root.join("target/.whisker/rustc-args")
}

/// Counterpart of [`default_cache_dir`] for the linker side:
/// `target/.whisker/linker-args/`. Created on demand.
pub fn default_linker_cache_dir(workspace_root: &Path) -> PathBuf {
    workspace_root.join("target/.whisker/linker-args")
}

/// Resolve the system linker driver we want to forward to from the
/// shim. Same logic the integration test uses, lifted into one
/// place so production and tests agree:
///
///   - `CC` env var wins.
///   - macOS: `xcrun -f clang` (active toolchain).
///   - Otherwise: PATH-resolved `cc`.
///
/// Returns the resolved path. Caller should `.canonicalize()` if
/// they want absolute, but the shim only needs an executable name
/// the OS can find.
pub fn resolve_host_linker() -> PathBuf {
    if let Some(cc) = std::env::var_os("CC") {
        return PathBuf::from(cc);
    }
    if cfg!(target_os = "macos") {
        if let Ok(out) = Command::new("xcrun").args(["-f", "clang"]).output() {
            if out.status.success() {
                let path = String::from_utf8_lossy(&out.stdout).trim().to_string();
                if !path.is_empty() {
                    return PathBuf::from(path);
                }
            }
        }
        return PathBuf::from("clang");
    }
    PathBuf::from("cc")
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    fn s(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| s.to_string()).collect()
    }

    fn unique_tempdir() -> PathBuf {
        static SEQ: AtomicU64 = AtomicU64::new(0);
        let n = SEQ.fetch_add(1, Ordering::Relaxed);
        let pid = std::process::id();
        let p = std::env::temp_dir().join(format!("whisker-wrapper-test-{pid}-{n}"));
        let _ = std::fs::remove_dir_all(&p);
        std::fs::create_dir_all(&p).unwrap();
        p
    }

    fn write_invocation(dir: &Path, inv: &CapturedRustcInvocation) {
        let name = format!(
            "{}-{}.json",
            inv.crate_name.replace(['-', '/'], "_"),
            inv.timestamp_micros,
        );
        let body = serde_json::to_string_pretty(inv).unwrap();
        std::fs::write(dir.join(name), body).unwrap();
    }

    // ----- load_captured_args ------------------------------------------

    #[test]
    fn load_captured_args_returns_empty_for_missing_cache_dir() {
        let map = load_captured_args(Path::new("/nope/does/not/exist"), None).unwrap();
        assert!(map.is_empty());
    }

    #[test]
    fn load_captured_args_filters_by_target_triple_when_specified() {
        // Multi-arch fat build (iOS Simulator on Apple Silicon): one
        // invocation per triple lands in the cache, both with the same
        // crate name. Without filtering, the newest-timestamp wins
        // regardless of triple — which is wrong when the dev-server's
        // runtime triple is the older one, and the patcher's resulting
        // `.o` fails to link into the runtime dylib (`found architecture
        // 'x86_64', required architecture 'arm64'`). Verify the filter
        // picks the matching triple even when its timestamp is older.
        let dir = unique_tempdir();
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "podcast".into(),
                args: s(&[
                    "--crate-name",
                    "podcast",
                    "--target",
                    "aarch64-apple-ios-sim",
                ]),
                timestamp_micros: 100,
            },
        );
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "podcast".into(),
                args: s(&["--crate-name", "podcast", "--target", "x86_64-apple-ios"]),
                timestamp_micros: 200,
            },
        );

        let map = load_captured_args(&dir, Some("aarch64-apple-ios-sim")).unwrap();
        assert_eq!(map.len(), 1);
        assert_eq!(map["podcast"].timestamp_micros, 100);

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_captured_args_returns_one_entry_per_crate_for_distinct_crates() {
        let dir = unique_tempdir();
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "foo".into(),
                args: s(&["--crate-name", "foo", "src/lib.rs"]),
                timestamp_micros: 100,
            },
        );
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "bar".into(),
                args: s(&["--crate-name", "bar", "src/lib.rs"]),
                timestamp_micros: 200,
            },
        );

        let map = load_captured_args(&dir, None).unwrap();
        assert_eq!(map.len(), 2);
        assert_eq!(map["foo"].args, s(&["--crate-name", "foo", "src/lib.rs"]));
        assert_eq!(map["bar"].args, s(&["--crate-name", "bar", "src/lib.rs"]));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_captured_args_keeps_the_most_recent_invocation_per_crate() {
        let dir = unique_tempdir();
        // Older invocation: shorter args.
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "foo".into(),
                args: s(&["--old-args"]),
                timestamp_micros: 100,
            },
        );
        // Newer invocation: longer args.
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "foo".into(),
                args: s(&["--newer-args", "--more"]),
                timestamp_micros: 200,
            },
        );

        let map = load_captured_args(&dir, None).unwrap();
        assert_eq!(map.len(), 1);
        assert_eq!(map["foo"].timestamp_micros, 200);
        assert_eq!(map["foo"].args, s(&["--newer-args", "--more"]));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_captured_args_skips_non_json_files() {
        let dir = unique_tempdir();
        std::fs::write(dir.join("README.md"), "not json").unwrap();
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "foo".into(),
                args: vec![],
                timestamp_micros: 1,
            },
        );

        let map = load_captured_args(&dir, None).unwrap();
        assert_eq!(map.len(), 1);
        assert!(map.contains_key("foo"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_captured_args_skips_malformed_json_with_a_warning() {
        let dir = unique_tempdir();
        std::fs::write(dir.join("garbage.json"), "{ not valid json").unwrap();
        write_invocation(
            &dir,
            &CapturedRustcInvocation {
                crate_name: "good".into(),
                args: vec![],
                timestamp_micros: 1,
            },
        );

        let map = load_captured_args(&dir, None).unwrap();
        assert_eq!(map.len(), 1);
        assert!(map.contains_key("good"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    // ----- keep_newest --------------------------------------------------

    #[test]
    fn keep_newest_inserts_into_empty_map() {
        let mut m = HashMap::new();
        keep_newest(
            &mut m,
            CapturedRustcInvocation {
                crate_name: "x".into(),
                args: vec![],
                timestamp_micros: 1,
            },
        );
        assert_eq!(m.len(), 1);
    }

    #[test]
    fn keep_newest_replaces_when_timestamp_strictly_newer() {
        let mut m = HashMap::new();
        m.insert(
            "x".into(),
            CapturedRustcInvocation {
                crate_name: "x".into(),
                args: s(&["old"]),
                timestamp_micros: 5,
            },
        );
        keep_newest(
            &mut m,
            CapturedRustcInvocation {
                crate_name: "x".into(),
                args: s(&["new"]),
                timestamp_micros: 10,
            },
        );
        assert_eq!(m["x"].args, s(&["new"]));
    }

    #[test]
    fn keep_newest_does_not_replace_with_equal_or_older_timestamp() {
        let mut m = HashMap::new();
        m.insert(
            "x".into(),
            CapturedRustcInvocation {
                crate_name: "x".into(),
                args: s(&["incumbent"]),
                timestamp_micros: 10,
            },
        );
        keep_newest(
            &mut m,
            CapturedRustcInvocation {
                crate_name: "x".into(),
                args: s(&["equal"]),
                timestamp_micros: 10,
            },
        );
        keep_newest(
            &mut m,
            CapturedRustcInvocation {
                crate_name: "x".into(),
                args: s(&["older"]),
                timestamp_micros: 1,
            },
        );
        assert_eq!(m["x"].args, s(&["incumbent"]));
    }

    // ----- default_cache_dir -------------------------------------------

    #[test]
    fn default_cache_dir_lives_under_target_dot_whisker() {
        let p = default_cache_dir(Path::new("/tmp/ws"));
        assert!(p.ends_with("target/.whisker/rustc-args"));
    }

    // ----- run_fat_build (integration: runs a real cargo) ---------------
    //
    // Smoke-test only: spawn `cargo --version` instead of a real
    // build to keep the test fast. The real round-trip
    // (build → JSON files appear → load_captured_args returns them)
    // is exercised in I4g-5's integration test against a fixture
    // crate.

    #[test]
    fn run_fat_build_creates_the_cache_dir_even_if_build_fails() {
        // Point the wrapper at /bin/true so cargo doesn't actually
        // compile anything; we just want to know `run_fat_build`
        // creates the cache dir and surfaces a non-zero exit as Err.
        let dir = unique_tempdir();
        let cache = dir.join("nested/cache");
        // Bogus workspace_root means cargo build will fail; that's
        // the path we want to assert on.
        let bad_workspace = unique_tempdir();
        let res = run_fat_build(
            &bad_workspace,
            "no-such-package",
            Target::Android,
            Path::new("/bin/true"),
            &cache,
            None,
        );
        assert!(res.is_err(), "build of nonexistent pkg should error");
        assert!(cache.is_dir(), "cache dir should be created up front");

        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::remove_dir_all(&bad_workspace);
    }

    #[test]
    fn run_fat_build_creates_linker_cache_dir_when_capture_requested() {
        // Same shape as above but with linker_capture set: both
        // dirs should be created up front, even though the cargo
        // call ultimately fails.
        let dir = unique_tempdir();
        let rustc_cache = dir.join("rustc");
        let linker_cache = dir.join("linker");
        let bad_workspace = unique_tempdir();
        let lc = LinkerCaptureConfig {
            shim_path: Path::new("/bin/true"),
            cache_dir: &linker_cache,
            real_linker: Path::new("/usr/bin/true"),
        };
        let res = run_fat_build(
            &bad_workspace,
            "no-such-package",
            Target::Android,
            Path::new("/bin/true"),
            &rustc_cache,
            Some(&lc),
        );
        assert!(res.is_err());
        assert!(rustc_cache.is_dir());
        assert!(linker_cache.is_dir());

        let _ = std::fs::remove_dir_all(&dir);
        let _ = std::fs::remove_dir_all(&bad_workspace);
    }

    // ----- load_captured_linker_args -----------------------------------

    fn write_linker_inv(dir: &Path, inv: &CapturedLinkerInvocation) {
        let stem = inv
            .output
            .as_deref()
            .and_then(|s| Path::new(s).file_name())
            .and_then(|n| n.to_str())
            .unwrap_or("_unknown")
            .replace(['/', '\\'], "_");
        let name = format!("{stem}-{}.json", inv.timestamp_micros);
        let body = serde_json::to_string_pretty(inv).unwrap();
        std::fs::write(dir.join(name), body).unwrap();
    }

    #[test]
    fn load_captured_linker_args_returns_empty_for_missing_dir() {
        let map = load_captured_linker_args(Path::new("/nope/does/not/exist")).unwrap();
        assert!(map.is_empty());
    }

    #[test]
    fn load_captured_linker_args_keys_by_output_basename() {
        let dir = unique_tempdir();
        write_linker_inv(
            &dir,
            &CapturedLinkerInvocation {
                output: Some("/cargo/target/debug/deps/libfoo.dylib".into()),
                args: s(&["-shared", "-o", "/cargo/target/debug/deps/libfoo.dylib"]),
                timestamp_micros: 100,
            },
        );
        write_linker_inv(
            &dir,
            &CapturedLinkerInvocation {
                output: Some("/cargo/target/debug/deps/libbar.dylib".into()),
                args: s(&["-shared", "-o", "/cargo/target/debug/deps/libbar.dylib"]),
                timestamp_micros: 200,
            },
        );

        let map = load_captured_linker_args(&dir).unwrap();
        assert_eq!(map.len(), 2);
        assert!(map.contains_key("libfoo.dylib"));
        assert!(map.contains_key("libbar.dylib"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_captured_linker_args_keeps_most_recent_per_output() {
        let dir = unique_tempdir();
        write_linker_inv(
            &dir,
            &CapturedLinkerInvocation {
                output: Some("/path/libfoo.dylib".into()),
                args: s(&["old"]),
                timestamp_micros: 100,
            },
        );
        write_linker_inv(
            &dir,
            &CapturedLinkerInvocation {
                output: Some("/path/libfoo.dylib".into()),
                args: s(&["new"]),
                timestamp_micros: 200,
            },
        );

        let map = load_captured_linker_args(&dir).unwrap();
        assert_eq!(map.len(), 1);
        assert_eq!(map["libfoo.dylib"].timestamp_micros, 200);
        assert_eq!(map["libfoo.dylib"].args, s(&["new"]));

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn load_captured_linker_args_skips_malformed_json() {
        let dir = unique_tempdir();
        std::fs::write(dir.join("garbage.json"), "{ not json").unwrap();
        write_linker_inv(
            &dir,
            &CapturedLinkerInvocation {
                output: Some("/path/lib.dylib".into()),
                args: vec![],
                timestamp_micros: 1,
            },
        );

        let map = load_captured_linker_args(&dir).unwrap();
        assert_eq!(map.len(), 1);
        assert!(map.contains_key("lib.dylib"));

        let _ = std::fs::remove_dir_all(&dir);
    }

    // ----- keep_newest_linker -------------------------------------------

    #[test]
    fn keep_newest_linker_inserts_into_empty() {
        let mut m = HashMap::new();
        keep_newest_linker(
            &mut m,
            CapturedLinkerInvocation {
                output: Some("/path/lib.so".into()),
                args: vec![],
                timestamp_micros: 1,
            },
        );
        assert_eq!(m.len(), 1);
        assert!(m.contains_key("lib.so"));
    }

    #[test]
    fn keep_newest_linker_does_not_replace_with_older() {
        let mut m = HashMap::new();
        keep_newest_linker(
            &mut m,
            CapturedLinkerInvocation {
                output: Some("/path/lib.so".into()),
                args: s(&["incumbent"]),
                timestamp_micros: 10,
            },
        );
        keep_newest_linker(
            &mut m,
            CapturedLinkerInvocation {
                output: Some("/path/lib.so".into()),
                args: s(&["older"]),
                timestamp_micros: 5,
            },
        );
        assert_eq!(m["lib.so"].args, s(&["incumbent"]));
    }

    #[test]
    fn keep_newest_linker_keys_anonymous_invocations_under_unknown() {
        let mut m = HashMap::new();
        keep_newest_linker(
            &mut m,
            CapturedLinkerInvocation {
                output: None,
                args: vec![],
                timestamp_micros: 1,
            },
        );
        assert!(m.contains_key("_unknown"));
    }

    // ----- default_linker_cache_dir + resolve_host_linker --------------

    #[test]
    fn default_linker_cache_dir_lives_under_target_dot_whisker() {
        let p = default_linker_cache_dir(Path::new("/tmp/ws"));
        assert!(p.ends_with("target/.whisker/linker-args"));
    }

    #[test]
    fn resolve_host_linker_returns_something_executable_or_a_path() {
        let p = resolve_host_linker();
        // We don't `which` here — just sanity that it isn't empty.
        assert!(!p.as_os_str().is_empty());
    }
}