mbx-cache-rustc 0.10.1

mbx internals: conservative rustc action analysis and key construction. No API stability -- use the mbx CLI or mbx-cache-protocol.
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
use super::{
    ActionContext, ActionInput, Argument, BypassReason, MAX_NATIVE_INPUT_BYTES,
    MAX_PREDICTED_INPUTS, PathMapping, RustcInvocation, normalize_components,
};
use mbx_cache_core::{
    CacheDigest, FileDigestCache, FileDigestScope, FileIdentity, RecordedFileDigest,
};
use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

/// A side-effect-minimized rustc invocation that emits only dependency data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DepInfoCommand {
    arguments: Vec<OsString>,
    output: PathBuf,
}

impl DepInfoCommand {
    /// Arguments for the real compiler, excluding the compiler executable.
    pub fn arguments(&self) -> &[OsString] {
        &self.arguments
    }

    /// Exact file the compiler must populate with dep-info.
    pub fn output(&self) -> &Path {
        &self.output
    }
}

/// The source and environment inputs reported by rustc's dep-info output.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RustcDepInfo {
    /// Source paths listed in the first dep-info dependency rule.
    pub files: Vec<PathBuf>,
    /// Environment inputs recorded by rustc `# env-dep:` lines.
    pub environment: BTreeMap<String, Option<String>>,
}

impl RustcDepInfo {
    /// Read and parse a dep-info file, treating missing or non-UTF-8 output as
    /// an explicit cache bypass.
    pub fn read(path: &Path) -> Result<Self, BypassReason> {
        let contents =
            std::fs::read_to_string(path).map_err(|error| BypassReason::DepInfoRead {
                path: path.to_path_buf(),
                message: error.to_string(),
            })?;
        Self::parse(&contents)
    }

    /// Parse rustc's Makefile-style dep-info format.
    ///
    /// This intentionally follows Cargo's parser contract: the first target
    /// rule contains all source dependencies, spaces are escaped with a
    /// trailing backslash on each token fragment, and `# env-dep:` records
    /// contain the environment observed by `env!` and `option_env!`.
    pub fn parse(contents: &str) -> Result<Self, BypassReason> {
        let mut files = BTreeSet::new();
        let mut environment = BTreeMap::new();
        let mut found_dependencies = false;

        for line in contents.lines() {
            if let Some(record) = line.strip_prefix("# env-dep:") {
                let (name, value) = record
                    .split_once('=')
                    .map_or((record, None), |(name, value)| (name, Some(value)));
                let name = unescape_environment(name)?;
                if name.is_empty() {
                    return Err(BypassReason::MalformedDepInfo(
                        "environment input has an empty name".into(),
                    ));
                }
                let value = value.map(unescape_environment).transpose()?;
                if environment
                    .insert(name.clone(), value.clone())
                    .is_some_and(|previous| previous != value)
                {
                    return Err(BypassReason::ConflictingEnvironment(name));
                }
                continue;
            }

            let Some(separator) = line.find(": ") else {
                continue;
            };
            if found_dependencies {
                continue;
            }
            found_dependencies = true;
            let mut fragments = line[separator + 2..].split_whitespace();
            while let Some(fragment) = fragments.next() {
                let mut file = fragment.to_string();
                while file.ends_with('\\') {
                    file.pop();
                    let continuation = fragments.next().ok_or_else(|| {
                        BypassReason::MalformedDepInfo(
                            "dependency path ends with an unterminated escape".into(),
                        )
                    })?;
                    file.push(' ');
                    file.push_str(continuation);
                }
                if file.is_empty() {
                    return Err(BypassReason::MalformedDepInfo(
                        "dependency path is empty".into(),
                    ));
                }
                files.insert(PathBuf::from(file));
            }
        }

        if !found_dependencies {
            return Err(BypassReason::MalformedDepInfo(
                "dependency rule is missing".into(),
            ));
        }
        if files.is_empty() {
            return Err(BypassReason::MalformedDepInfo(
                "dependency rule contains no inputs".into(),
            ));
        }
        Ok(Self {
            files: files.into_iter().collect(),
            environment,
        })
    }
}

/// A complete, content-addressed compiler input manifest.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoveredInputs {
    working_dir: PathBuf,
    /// Content-addressed compiler input files.
    pub inputs: Vec<ActionInput>,
    /// Environment inputs captured from dep-info.
    pub environment: BTreeMap<String, Option<String>>,
}

impl DiscoveredInputs {
    pub(crate) fn from_paths(
        working_dir: &Path,
        paths: BTreeSet<PathBuf>,
        environment: BTreeMap<String, Option<String>>,
        digests: &dyn FileDigestCache,
    ) -> Result<Self, BypassReason> {
        if !working_dir.is_absolute() {
            return Err(BypassReason::RelativeWorkingDirectory(
                working_dir.to_path_buf(),
            ));
        }
        let working_dir = normalize_components(working_dir);
        // Stat everything first: the identities drive one batched ledger
        // lookup, so an upstream rlib the session already hashed -- once, when
        // it was materialized or published -- is not read again by every crate
        // that links it. A file the filesystem reports no modification time
        // for gets no identity and is simply hashed.
        let mut identified = Vec::with_capacity(paths.len());
        for path in paths {
            let metadata = std::fs::metadata(&path).map_err(|error| BypassReason::InputRead {
                path: path.clone(),
                message: error.to_string(),
            })?;
            if !metadata.is_file() {
                return Err(BypassReason::InputRead {
                    path,
                    message: "input is not a regular file".into(),
                });
            }
            let identity = FileIdentity::describe(&path, &metadata);
            identified.push((path, identity));
        }
        let queries = identified
            .iter()
            .filter_map(|(_, identity)| identity.clone())
            .collect::<Vec<_>>();
        let mut recorded = digests.find(FileDigestScope::Content, &queries).into_iter();
        let mut inputs = Vec::with_capacity(identified.len());
        let mut fresh = Vec::new();
        for (path, identity) in identified {
            let remembered = identity
                .as_ref()
                .and_then(|_| recorded.next().flatten())
                .filter(|digest| {
                    identity
                        .as_ref()
                        .is_some_and(|identity| identity.len == digest.size)
                });
            let digest = match remembered {
                Some(digest) => digest,
                None => {
                    let digest = CacheDigest::blake3_file(&path).map_err(|error| {
                        BypassReason::InputRead {
                            path: path.clone(),
                            message: error.to_string(),
                        }
                    })?;
                    if let Some(identity) = identity
                        && identity.len == digest.size
                    {
                        fresh.push(RecordedFileDigest {
                            file: identity,
                            digest: digest.clone(),
                        });
                    }
                    digest
                }
            };
            inputs.push(ActionInput { path, digest });
        }
        if !fresh.is_empty() {
            digests.record(FileDigestScope::Content, fresh);
        }
        Ok(Self {
            working_dir,
            inputs,
            environment,
        })
    }

    /// Reject inputs whose modification time overlaps the compiler invocation.
    ///
    /// Input contents are first hashed after rustc reports their paths. This
    /// timestamp barrier prevents a post-compile write from being mistaken for
    /// the contents that produced the artifact. `verify` closes the remaining
    /// race after hashing.
    pub fn verify_not_modified_since(&self, started_at: SystemTime) -> Result<(), BypassReason> {
        for input in &self.inputs {
            let modified = std::fs::metadata(&input.path)
                .and_then(|metadata| metadata.modified())
                .map_err(|error| BypassReason::InputRead {
                    path: input.path.clone(),
                    message: error.to_string(),
                })?;
            if modified >= started_at {
                return Err(BypassReason::InputModifiedDuringCompilation(
                    input.path.clone(),
                ));
            }
        }
        Ok(())
    }

    /// Rehash every discovered file after compilation and before publication.
    /// This closes the discovery/compile race by degrading changed inputs to a
    /// cache miss rather than storing outputs beneath a stale action key.
    pub fn verify(&self) -> Result<(), BypassReason> {
        for input in &self.inputs {
            let matches = input.digest.matches_file(&input.path).map_err(|error| {
                BypassReason::InputRead {
                    path: input.path.clone(),
                    message: error.to_string(),
                }
            })?;
            if !matches {
                return Err(BypassReason::InputChanged(input.path.clone()));
            }
        }
        Ok(())
    }

    /// Merge the manifest into an action context after verifying that both use
    /// the same compiler working directory.
    pub fn apply_to(self, context: &mut ActionContext) -> Result<(), BypassReason> {
        if normalize_components(&context.working_dir) != self.working_dir {
            return Err(BypassReason::DiscoveryWorkingDirectory);
        }
        for (name, value) in &self.environment {
            if context
                .environment
                .get(name)
                .is_some_and(|previous| previous != value)
            {
                return Err(BypassReason::ConflictingEnvironment(name.clone()));
            }
        }
        context.environment.extend(self.environment);
        context.inputs.extend(self.inputs);
        Ok(())
    }
}

impl RustcInvocation {
    /// Replace the original output flags with a single explicit dep-info file.
    pub fn dep_info_command(&self, output: &Path) -> Result<DepInfoCommand, BypassReason> {
        if !output.is_absolute() {
            return Err(BypassReason::RelativeDepInfoPath(output.to_path_buf()));
        }
        let output_text = output
            .to_str()
            .ok_or_else(|| BypassReason::NonUtf8Path(output.to_path_buf()))?;
        if output_text.contains(',') {
            return Err(BypassReason::UnsafeDepInfoPath(output.to_path_buf()));
        }

        let mut arguments = Vec::new();
        for argument in &self.arguments {
            match argument {
                Argument::Emit(_) => {}
                Argument::Path { flag, .. } if flag == "--out-dir" || flag == "-o" => {}
                argument => arguments.push(render_argument(argument)?),
            }
        }
        arguments.push(format!("--emit=dep-info={output_text}").into());
        arguments.push(self.source.clone().into_os_string());
        Ok(DepInfoCommand {
            arguments,
            output: output.to_path_buf(),
        })
    }

    /// Hash dep-info sources plus every direct compiler input already modeled
    /// by the invocation (`--extern` artifacts and custom target specs).
    pub fn discover_inputs(
        &self,
        dep_info: &RustcDepInfo,
        working_dir: &Path,
    ) -> Result<DiscoveredInputs, BypassReason> {
        self.discover_inputs_with_mappings(
            dep_info,
            working_dir,
            &[],
            &mbx_cache_core::NoFileDigestCache,
        )
    }

    /// Hash dep-info sources plus modeled compiler inputs, allowing native
    /// search directories beneath the working directory or a mapped root.
    ///
    /// `digests` may answer for files the session already read in full;
    /// pass [`mbx_cache_core::NoFileDigestCache`] to hash everything.
    pub fn discover_inputs_with_mappings(
        &self,
        dep_info: &RustcDepInfo,
        working_dir: &Path,
        path_mappings: &[PathMapping],
        digests: &dyn FileDigestCache,
    ) -> Result<DiscoveredInputs, BypassReason> {
        if !working_dir.is_absolute() {
            return Err(BypassReason::RelativeWorkingDirectory(
                working_dir.to_path_buf(),
            ));
        }
        let working_dir = normalize_components(working_dir);
        let mut paths = dep_info
            .files
            .iter()
            .chain(&self.required_inputs)
            .map(|path| {
                let absolute = if path.is_absolute() {
                    path.to_path_buf()
                } else {
                    working_dir.join(path)
                };
                normalize_components(&absolute)
            })
            .collect::<BTreeSet<_>>();
        let admitted_roots = native_input_roots(&working_dir, path_mappings);
        let mut native_bytes = 0_u64;
        for argument in &self.arguments {
            if let Argument::SearchPath { kind, path } = argument
                && kind == "native"
            {
                let directory = if path.is_absolute() {
                    path.clone()
                } else {
                    working_dir.join(path)
                };
                // An inert directory outside every mapped root enters the key
                // by its literal path in the arguments, not by its contents --
                // predictions skip it under the same rule, so both discovery
                // paths agree on the action key.
                if self.native_search_is_inert()
                    && matches!(
                        super::normalize_mapped_path(&directory, &working_dir, path_mappings),
                        Err(BypassReason::UnmappedAbsolutePath(_))
                    )
                {
                    continue;
                }
                collect_native_directory(
                    &directory,
                    &admitted_roots,
                    &mut paths,
                    &mut native_bytes,
                )?;
            }
        }
        DiscoveredInputs::from_paths(&working_dir, paths, dep_info.environment.clone(), digests)
    }
}

/// Return normalized roots whose native search directories can be tracked.
pub(super) fn native_input_roots(
    working_dir: &Path,
    path_mappings: &[PathMapping],
) -> Vec<PathBuf> {
    std::iter::once(working_dir)
        .chain(path_mappings.iter().map(|mapping| mapping.root.as_path()))
        .map(normalize_components)
        .collect()
}

/// Add regular files beneath an admitted native search directory, enforcing
/// the prediction input count and the caller's cumulative native byte budget.
pub(super) fn collect_native_directory(
    directory: &Path,
    admitted_roots: &[PathBuf],
    paths: &mut BTreeSet<PathBuf>,
    native_bytes: &mut u64,
) -> Result<(), BypassReason> {
    let directory = normalize_components(directory);
    if !admitted_roots
        .iter()
        .any(|root| directory.starts_with(root))
    {
        return Err(BypassReason::UnsupportedSearchPath("native".into()));
    }

    let mut pending = vec![directory];
    while let Some(directory) = pending.pop() {
        let entries = match std::fs::read_dir(&directory) {
            Ok(entries) => entries,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
            Err(error) => {
                return Err(BypassReason::InputRead {
                    path: directory,
                    message: error.to_string(),
                });
            }
        };
        for entry in entries {
            let entry = entry.map_err(|error| BypassReason::InputRead {
                path: directory.clone(),
                message: error.to_string(),
            })?;
            let path = entry.path();
            let file_type = entry.file_type().map_err(|error| BypassReason::InputRead {
                path: path.clone(),
                message: error.to_string(),
            })?;
            if file_type.is_dir() {
                pending.push(path);
            } else if file_type.is_file() {
                *native_bytes = native_bytes
                    .checked_add(
                        entry
                            .metadata()
                            .map_err(|error| BypassReason::InputRead {
                                path: path.clone(),
                                message: error.to_string(),
                            })?
                            .len(),
                    )
                    .ok_or_else(|| BypassReason::UnsupportedSearchPath("native".into()))?;
                paths.insert(path);
            } else {
                return Err(BypassReason::UnsupportedSearchPath("native".into()));
            }
            if paths.len() > MAX_PREDICTED_INPUTS || *native_bytes > MAX_NATIVE_INPUT_BYTES {
                return Err(BypassReason::UnsupportedSearchPath("native".into()));
            }
        }
    }
    Ok(())
}

fn render_argument(argument: &Argument) -> Result<OsString, BypassReason> {
    let rendered = match argument {
        Argument::Plain(value) => value.clone(),
        Argument::Path { flag, path } => format!(
            "{flag}={}",
            path.to_str()
                .ok_or_else(|| BypassReason::NonUtf8Path(path.clone()))?
        ),
        Argument::SearchPath { kind, path } => format!(
            "-L{kind}={}",
            path.to_str()
                .ok_or_else(|| BypassReason::NonUtf8Path(path.clone()))?
        ),
        Argument::Extern { name, path } => match path {
            Some(path) => format!(
                "--extern={name}={}",
                path.to_str()
                    .ok_or_else(|| BypassReason::NonUtf8Path(path.clone()))?
            ),
            None => format!("--extern={name}"),
        },
        Argument::Emit(_) => unreachable!("emit arguments are removed before rendering"),
        Argument::RemapPath { from, to } => format!(
            "--remap-path-prefix={}={to}",
            from.to_str()
                .ok_or_else(|| BypassReason::NonUtf8Path(from.clone()))?
        ),
        // Nothing links while emitting dep-info, so the prefix is inert here;
        // it is replayed verbatim to keep the command faithful.
        Argument::OsoPrefix {
            path,
            trailing_slash,
        } => format!(
            "--codegen=link-arg=-Wl,-oso_prefix,{}{}",
            path.to_str()
                .ok_or_else(|| BypassReason::NonUtf8Path(path.clone()))?,
            if *trailing_slash { "/" } else { "" }
        ),
    };
    Ok(rendered.into())
}

fn unescape_environment(value: &str) -> Result<String, BypassReason> {
    let mut output = String::with_capacity(value.len());
    let mut characters = value.chars();
    while let Some(character) = characters.next() {
        if character != '\\' {
            output.push(character);
            continue;
        }
        match characters.next() {
            Some('\\') => output.push('\\'),
            Some('n') => output.push('\n'),
            Some('r') => output.push('\r'),
            Some(character) => {
                return Err(BypassReason::MalformedDepInfo(format!(
                    "unknown environment escape \\{character}"
                )));
            }
            None => {
                return Err(BypassReason::MalformedDepInfo(
                    "environment input ends with an unterminated escape".into(),
                ));
            }
        }
    }
    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::process::Command;

    fn args(values: &[&str]) -> Vec<OsString> {
        values.iter().map(OsString::from).collect()
    }

    #[test]
    fn parses_files_spaces_and_environment_records() {
        let parsed = RustcDepInfo::parse(
            "target/lib.rlib: src/lib.rs src/a\\ file.rs generated.rs\n\
             src/lib.rs:\n\
             # env-dep:SET=value\\nnext\n\
             # env-dep:UNSET\n\
             # env-dep:SLASH=a\\\\b\n",
        )
        .unwrap();
        assert_eq!(
            parsed.files,
            vec![
                PathBuf::from("generated.rs"),
                PathBuf::from("src/a file.rs"),
                PathBuf::from("src/lib.rs"),
            ]
        );
        assert_eq!(parsed.environment["SET"], Some("value\nnext".into()));
        assert_eq!(parsed.environment["UNSET"], None);
        assert_eq!(parsed.environment["SLASH"], Some(r"a\b".into()));
    }

    #[test]
    fn malformed_dep_info_bypasses_caching() {
        for contents in [
            "",
            "target: ",
            "target: src/trailing\\\n",
            "target: src/lib.rs\n# env-dep:NAME=bad\\q\n",
        ] {
            assert!(RustcDepInfo::parse(contents).is_err(), "{contents:?}");
        }
    }

    #[test]
    fn native_directory_byte_limit_is_cumulative() {
        let directory = tempfile::tempdir().unwrap();
        let native = directory.path().join("native");
        std::fs::create_dir_all(&native).unwrap();
        std::fs::write(native.join("input.lib"), b"xx").unwrap();
        let roots = native_input_roots(directory.path(), &[]);
        let mut paths = BTreeSet::new();
        let mut native_bytes = MAX_NATIVE_INPUT_BYTES - 1;

        assert_eq!(
            collect_native_directory(&native, &roots, &mut paths, &mut native_bytes),
            Err(BypassReason::UnsupportedSearchPath("native".into()))
        );
    }

    #[test]
    fn discovery_command_removes_real_outputs() {
        let invocation = RustcInvocation::parse(&args(&[
            "--crate-name=widget",
            "--crate-type=lib",
            "--emit=dep-info,metadata,link",
            "--out-dir=target/debug/deps",
            "-o",
            "target/debug/libwidget.rlib",
            "src/lib.rs",
        ]))
        .unwrap();
        let output = if cfg!(windows) {
            PathBuf::from(r"C:\tmp\mbx cache\inputs.d")
        } else {
            PathBuf::from("/tmp/mbx cache/inputs.d")
        };
        let command = invocation.dep_info_command(&output).unwrap();
        let arguments = command
            .arguments()
            .iter()
            .map(|value| value.to_string_lossy())
            .collect::<Vec<_>>();
        assert_eq!(
            arguments,
            vec![
                "--crate-name=widget",
                "--crate-type=lib",
                &format!("--emit=dep-info={}", output.display()),
                "src/lib.rs",
            ]
        );
    }

    #[test]
    fn discovery_hashes_externs_and_custom_targets() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        let source = root.join("src/lib.rs");
        let external = root.join("target/libdependency.rlib");
        let target = root.join("targets/custom.json");
        std::fs::create_dir_all(source.parent().unwrap()).unwrap();
        std::fs::create_dir_all(external.parent().unwrap()).unwrap();
        std::fs::create_dir_all(target.parent().unwrap()).unwrap();
        std::fs::write(&source, "pub fn library() {}\n").unwrap();
        std::fs::write(&external, "dependency artifact\n").unwrap();
        std::fs::write(&target, "{}\n").unwrap();

        let invocation = RustcInvocation::parse(&[
            "--crate-name=widget".into(),
            "--crate-type=lib".into(),
            "--emit=metadata".into(),
            format!("--extern=dependency={}", external.display()).into(),
            format!("--target={}", target.display()).into(),
            source.clone().into_os_string(),
        ])
        .unwrap();
        let dep_info = RustcDepInfo::parse(&format!("output: {}\n", source.display())).unwrap();
        let discovered = invocation.discover_inputs(&dep_info, root).unwrap();
        assert_eq!(discovered.inputs.len(), 3);

        std::fs::remove_file(&external).unwrap();
        assert!(matches!(
            invocation.discover_inputs(&dep_info, root),
            Err(BypassReason::InputRead { path, .. }) if path == external
        ));
    }

    #[test]
    fn discovery_rejects_inputs_modified_during_compilation() {
        let directory = tempfile::tempdir().unwrap();
        let source = directory.path().join("lib.rs");
        std::fs::write(&source, "pub fn library() {}\n").unwrap();
        let invocation = RustcInvocation::parse(&[
            "--crate-name=widget".into(),
            "--crate-type=lib".into(),
            "--emit=dep-info,metadata".into(),
            source.clone().into_os_string(),
        ])
        .unwrap();
        let dep_info = RustcDepInfo::parse(&format!("output: {}\n", source.display())).unwrap();
        let discovered = invocation
            .discover_inputs(&dep_info, directory.path())
            .unwrap();
        let modified = std::fs::metadata(&source).unwrap().modified().unwrap();

        assert_eq!(
            discovered.verify_not_modified_since(modified),
            Err(BypassReason::InputModifiedDuringCompilation(source))
        );
    }

    /// The MSVC toolset directories `cc`-built dependencies hand to every
    /// downstream compile on Windows: absolute, version-stamped, and outside
    /// every mapped root.
    fn toolchain_native_directory(version: &str) -> PathBuf {
        if cfg!(windows) {
            PathBuf::from(format!(r"C:\Program Files\MSVC\{version}\lib\x64"))
        } else {
            PathBuf::from(format!("/opt/msvc/{version}/lib/x64"))
        }
    }

    fn library_with_native_search(source: &Path, directory: &Path) -> RustcInvocation {
        RustcInvocation::parse(&[
            "--crate-name=widget".into(),
            "--crate-type=lib".into(),
            "--emit=metadata,link".into(),
            format!("-Lnative={}", directory.display()).into(),
            source.to_path_buf().into_os_string(),
        ])
        .unwrap()
    }

    fn library_context(root: &Path, mappings: Vec<PathMapping>) -> ActionContext {
        ActionContext {
            compiler: crate::CompilerIdentity {
                toolchain: "core:rust@test".into(),
                rustc_version: "test".into(),
                host: std::env::consts::ARCH.into(),
            },
            working_dir: root.to_path_buf(),
            path_mappings: mappings,
            environment: BTreeMap::new(),
            portable_environment: BTreeSet::new(),
            inputs: Vec::new(),
        }
    }

    #[test]
    fn unmapped_native_directory_is_keyed_by_path_for_library_emits() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        let source = root.join("lib.rs");
        std::fs::write(&source, "pub fn library() {}\n").unwrap();
        let toolchain = toolchain_native_directory("14.51.36231");
        let invocation = library_with_native_search(&source, &toolchain);
        let mappings = vec![PathMapping::new(root, "workspace")];
        let dep_info = RustcDepInfo::parse(&format!("output: {}\n", source.display())).unwrap();

        // The directory does not even exist: its contents are not inputs.
        let discovered = invocation
            .discover_inputs_with_mappings(
                &dep_info,
                root,
                &mappings,
                &mbx_cache_core::NoFileDigestCache,
            )
            .unwrap();
        assert_eq!(discovered.inputs.len(), 1);
        assert_eq!(discovered.inputs[0].path, source);

        // The literal path is key material, so a toolset update misses.
        let context = library_context(root, mappings.clone());
        let digest = invocation.invocation_digest(&context).unwrap();
        let updated = library_with_native_search(&source, &toolchain_native_directory("14.52.0"));
        assert_ne!(digest, updated.invocation_digest(&context).unwrap());

        // The prediction skips the directory the same way discovery does, so a
        // build that replays it derives the action key dep-info would have.
        let mut recorded = context.clone();
        discovered.clone().apply_to(&mut recorded).unwrap();
        let action = invocation.action(recorded).unwrap();
        let prediction = invocation.prediction(&context, &discovered).unwrap();
        let replayed = prediction
            .discover(
                root,
                &context.path_mappings,
                &mbx_cache_core::NoFileDigestCache,
            )
            .unwrap();
        let mut replay_context = context.clone();
        replayed.apply_to(&mut replay_context).unwrap();
        assert_eq!(
            invocation.action(replay_context).unwrap().digest,
            action.digest
        );
    }

    #[test]
    fn unmapped_native_directory_still_refuses_a_native_link() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        let source = root.join("main.rs");
        std::fs::write(&source, "fn main() {}\n").unwrap();
        let toolchain = toolchain_native_directory("14.51.36231");
        let invocation = RustcInvocation::parse_with(
            &[
                "--crate-name=app".into(),
                "--crate-type=bin".into(),
                "--emit=link".into(),
                format!("-Lnative={}", toolchain.display()).into(),
                source.clone().into_os_string(),
            ],
            crate::ParseOptions::caching_native_links(true),
        )
        .unwrap();
        let mappings = vec![PathMapping::new(root, "workspace")];

        // A linker reads those directories, so their contents stay inputs the
        // key must account for, and an unmapped one stays a bypass.
        let context = library_context(root, mappings.clone());
        assert!(matches!(
            invocation.invocation_digest(&context),
            Err(BypassReason::UnmappedAbsolutePath(_))
        ));
        let dep_info = RustcDepInfo::parse(&format!("output: {}\n", source.display())).unwrap();
        assert_eq!(
            invocation.discover_inputs_with_mappings(
                &dep_info,
                root,
                &mappings,
                &mbx_cache_core::NoFileDigestCache
            ),
            Err(BypassReason::UnsupportedSearchPath("native".into()))
        );
    }

    #[test]
    fn discovery_resolves_parent_components_against_the_working_directory() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path().join("project");
        let shared = directory.path().join("shared.rs");
        std::fs::create_dir(&root).unwrap();
        std::fs::write(&shared, "pub fn shared() {}\n").unwrap();

        let invocation = RustcInvocation::parse(&args(&[
            "--crate-name=widget",
            "--crate-type=lib",
            "--emit=metadata",
            "../shared.rs",
        ]))
        .unwrap();
        let dep_info = RustcDepInfo::parse("output: ../shared.rs\n").unwrap();
        let discovered = invocation.discover_inputs(&dep_info, &root).unwrap();

        assert_eq!(discovered.inputs.len(), 1);
        assert_eq!(discovered.inputs[0].path, shared);
    }

    #[test]
    fn rustc_dep_info_round_trip_discovers_real_inputs() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        std::fs::write(
            root.join("lib.rs"),
            "mod child; const _: &str = include_str!(\"data file.txt\"); \
             const _: &str = env!(\"MBX_DISCOVERY_TEST\"); \
             const _: Option<&str> = option_env!(\"MBX_DISCOVERY_UNSET\");",
        )
        .unwrap();
        std::fs::write(root.join("child.rs"), "pub fn child() {}\n").unwrap();
        std::fs::write(root.join("data file.txt"), "included\n").unwrap();

        let invocation = RustcInvocation::parse(&args(&[
            "--crate-name=mbx_cache_discovery_test",
            "--crate-type=lib",
            "--emit=metadata,link",
            "lib.rs",
        ]))
        .unwrap();
        let dep_info_path = root.join("discovery inputs.d");
        let discovery_command = invocation.dep_info_command(&dep_info_path).unwrap();
        let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
        let output = match Command::new(rustc)
            .args(discovery_command.arguments())
            .current_dir(root)
            .env("MBX_DISCOVERY_TEST", "observed")
            .env_remove("MBX_DISCOVERY_UNSET")
            .output()
        {
            Ok(output) => output,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return,
            Err(error) => panic!("failed to execute rustc: {error}"),
        };
        assert!(
            output.status.success(),
            "{}",
            String::from_utf8_lossy(&output.stderr)
        );
        let parsed = RustcDepInfo::read(&dep_info_path).unwrap();
        let discovered = invocation.discover_inputs(&parsed, root).unwrap();
        assert_eq!(
            discovered.environment["MBX_DISCOVERY_TEST"],
            Some("observed".into())
        );
        assert_eq!(discovered.environment["MBX_DISCOVERY_UNSET"], None);
        assert_eq!(discovered.inputs.len(), 3);
        assert!(
            discovered
                .inputs
                .iter()
                .all(|input| input.digest.algorithm == "blake3")
        );
        let mut context = ActionContext {
            compiler: crate::CompilerIdentity {
                toolchain: "core:rust@test".into(),
                rustc_version: "test".into(),
                host: std::env::consts::ARCH.into(),
            },
            working_dir: root.to_path_buf(),
            path_mappings: vec![crate::PathMapping::new(root, "workspace")],
            environment: BTreeMap::new(),
            portable_environment: BTreeSet::new(),
            inputs: Vec::new(),
        };
        discovered.clone().apply_to(&mut context).unwrap();
        let action = invocation.action(context).unwrap();
        assert!(
            String::from_utf8(action.bytes)
                .unwrap()
                .contains(r#""MBX_DISCOVERY_TEST":"observed""#)
        );
        discovered.verify().unwrap();
        std::fs::write(root.join("child.rs"), "pub fn changed() {}\n").unwrap();
        assert_eq!(
            discovered.verify(),
            Err(BypassReason::InputChanged(root.join("child.rs")))
        );
    }

    /// A ledger that answers with a sentinel and remembers what was recorded.
    struct SentinelLedger {
        known: FileIdentity,
        digest: CacheDigest,
        recorded: std::sync::Mutex<Vec<RecordedFileDigest>>,
    }

    impl FileDigestCache for SentinelLedger {
        fn find(&self, scope: FileDigestScope, files: &[FileIdentity]) -> Vec<Option<CacheDigest>> {
            assert_eq!(scope, FileDigestScope::Content);
            files
                .iter()
                .map(|file| (*file == self.known).then(|| self.digest.clone()))
                .collect()
        }

        fn record(&self, scope: FileDigestScope, entries: Vec<RecordedFileDigest>) {
            assert_eq!(scope, FileDigestScope::Content);
            self.recorded.lock().unwrap().extend(entries);
        }
    }

    #[test]
    fn discovery_reuses_recorded_digests_and_records_fresh_ones() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        let known_path = root.join("libdep.rlib");
        let fresh_path = root.join("lib.rs");
        std::fs::write(&known_path, b"rlib bytes").unwrap();
        std::fs::write(&fresh_path, b"fn lib() {}").unwrap();
        let metadata = std::fs::metadata(&known_path).unwrap();
        // A sentinel digest that hashing could never produce proves the read
        // was skipped: the discovered input carries it verbatim.
        let sentinel = CacheDigest {
            algorithm: "blake3".into(),
            hash: "c".repeat(64),
            size: metadata.len(),
        };
        let ledger = SentinelLedger {
            known: FileIdentity::describe(&known_path, &metadata).unwrap(),
            digest: sentinel.clone(),
            recorded: std::sync::Mutex::new(Vec::new()),
        };

        let discovered = DiscoveredInputs::from_paths(
            root,
            BTreeSet::from([known_path.clone(), fresh_path.clone()]),
            BTreeMap::new(),
            &ledger,
        )
        .unwrap();

        let by_path = |path: &Path| {
            discovered
                .inputs
                .iter()
                .find(|input| input.path == path)
                .unwrap()
                .digest
                .clone()
        };
        assert_eq!(
            by_path(&known_path),
            sentinel,
            "the recorded digest answers"
        );
        assert_eq!(
            by_path(&fresh_path),
            CacheDigest::blake3_file(&fresh_path).unwrap(),
            "an unrecorded file is hashed"
        );
        let recorded = ledger.recorded.lock().unwrap();
        assert_eq!(recorded.len(), 1, "only the fresh hash is recorded");
        assert_eq!(recorded[0].file.path, fresh_path);
        assert_eq!(recorded[0].digest, by_path(&fresh_path));
    }

    /// A rewrite that restores length and modification time must still be
    /// hashed: the platform change token moves with every write, so the
    /// identity the ledger recorded no longer describes the file.
    #[cfg(unix)]
    #[test]
    fn a_disguised_rewrite_is_not_answered_from_the_ledger() {
        let directory = tempfile::tempdir().unwrap();
        let root = directory.path();
        let path = root.join("lib.rs");
        std::fs::write(&path, b"fn lib() -> u8 { 1 }").unwrap();
        let before = std::fs::metadata(&path).unwrap();
        let identity = FileIdentity::describe(&path, &before).unwrap();

        // Same length, modification time put back: only the change token can
        // tell this file has new contents.
        std::fs::write(&path, b"fn lib() -> u8 { 2 }").unwrap();
        let file = std::fs::File::options().write(true).open(&path).unwrap();
        file.set_times(std::fs::FileTimes::new().set_modified(before.modified().unwrap()))
            .unwrap();
        drop(file);
        let after = std::fs::metadata(&path).unwrap();
        let disguised = FileIdentity::describe(&path, &after).unwrap();
        assert_eq!(disguised.len, identity.len);
        assert_eq!(disguised.modified, identity.modified);
        assert_ne!(
            disguised, identity,
            "the change token must expose the rewrite"
        );

        let ledger = SentinelLedger {
            known: identity,
            digest: CacheDigest {
                algorithm: "blake3".into(),
                hash: "e".repeat(64),
                size: after.len(),
            },
            recorded: std::sync::Mutex::new(Vec::new()),
        };
        let discovered = DiscoveredInputs::from_paths(
            root,
            BTreeSet::from([path.clone()]),
            BTreeMap::new(),
            &ledger,
        )
        .unwrap();
        assert_eq!(
            discovered.inputs[0].digest,
            CacheDigest::blake3_file(&path).unwrap(),
            "the disguised rewrite is hashed, not answered from the ledger"
        );
    }
}