kache 0.16.0

Zero-copy, content-addressed build cache for Rust, C/C++ and more, with S3 and shared-filesystem remotes.
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
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
use anyhow::{Context, Result, bail};
use std::borrow::Cow;
use std::path::{Path, PathBuf};

use crate::compiler::rustc::RustcCompiler;

/// rustc flags that affect only diagnostics, lint levels, or queries — never
/// the emitted artifact bytes. Their separated value (`--flag value`) is
/// skipped during parsing so neither the flag nor its value reaches the
/// `residual_args` catch-all and over-keys the result (kunobi-ninja/kache#324).
///
/// Deny-level lint gates (`-D`, `--deny`, `-F`, `--forbid`) are deliberately
/// NOT listed here even though they cannot change a successful compile's
/// object bytes: they change whether the compilation *fails*, and a cache hit
/// replays success — see [`OUTCOME_AFFECTING_VALUE_FLAGS`].
const IGNORED_VALUE_FLAGS: &[&str] = &[
    "--error-format",
    "--json",
    "--color",
    "--diagnostic-width",
    "--check-cfg",
    "--print",
    "--explain",
    "-W",
    "--warn",
    "-A",
    "--allow",
];

/// Attached (`--flag=value` / `-Xvalue`) forms of the diagnostics / lint flags
/// above, plus the single-letter lint prefixes (`-Wunused`, `-Dwarnings`). Used
/// to drop the attached spellings from the cache key alongside their separated
/// counterparts in [`IGNORED_VALUE_FLAGS`].
///
/// The `-D` / `-F` prefixes are intentionally absent: those spellings are
/// outcome-affecting and captured by [`OUTCOME_LINT_ATTACHED_PREFIXES`] before
/// this list is consulted.
const IGNORED_ATTACHED_PREFIXES: &[&str] = &[
    "--error-format=",
    "--json=",
    "--color=",
    "--diagnostic-width=",
    "--check-cfg=",
    "--print=",
    "--explain=",
    "--warn=",
    "--allow=",
    "-W",
    "-A",
];

/// rustc flags that can flip the compile's *outcome* from failure to success
/// without changing the object bytes of the successful compile. A cache hit
/// replays success, so two invocations differing only here MUST NOT share a
/// key — otherwise one stored under "warnings allowed" would serve green to a
/// build that `-D warnings` should have failed (review finding #2). Each is
/// captured with its value into [`RustcArgs::outcome_lint_flags`] and folded
/// into the cache key.
const OUTCOME_AFFECTING_VALUE_FLAGS: &[&str] = &[
    "-D",           // deny: warnings of the named lint become hard errors
    "--deny",       // long form of -D
    "-F",           // forbid: like deny, cannot be re-allowed downstream
    "--forbid",     // long form of -F
    "--force-warn", // forces warn level; overrides attribute-level deny/allow
    "--cap-lints", // caps every lint level; changes effective levels (cargo passes --cap-lints allow)
];

/// Attached forms (`--deny=warnings`, `-Dwarnings`, …) of
/// [`OUTCOME_AFFECTING_VALUE_FLAGS`]. Bare `-D` / `-F` prefixes match any
/// attached value (`-Dwarnings`, `-Funused`) because rustc defines no other
/// flag beginning with those spellings; the long forms use explicit `=`.
const OUTCOME_LINT_ATTACHED_PREFIXES: &[&str] = &[
    "--deny=",
    "--forbid=",
    "--force-warn=",
    "--cap-lints=",
    "-D",
    "-F",
];

/// Boolean diagnostics / query flags (no value) that must not reach the key.
const IGNORED_BOOL_FLAGS: &[&str] = &["-v", "--verbose", "-V", "--version", "-h", "--help"];

/// Cargo/rustc artifact basename stem: `{crate_name}{extra_filename}`.
pub fn format_crate_output_stem(crate_name: &str, extra_filename: &str) -> String {
    format!("{crate_name}{extra_filename}")
}

/// Compilation-unit identity for diagnostics: cargo's `-C extra-filename` hash,
/// without its leading dash (kunobi-ninja/kache#627).
///
/// `crate_name` is not a unit identity — two versions of a package, a host and
/// a target build of the same crate, and different feature sets all collapse
/// onto one name. Cargo's `extra-filename` is exactly the disambiguator that
/// keeps those units' artifacts from colliding in one `deps/` directory, so
/// within a build tree it identifies the unit precisely.
///
/// Deliberately NOT `-C metadata`: cargo sets the two to different hashes
/// (`-C metadata=04bad873faff484a -C extra-filename=-843f02d6a46ebef1` in one
/// observed invocation), and it is `extra-filename` that appears in the
/// artifact filename a consumer sees on its `--extern` path. Matching the two
/// sides needs the one that is visible from both.
///
/// This is recorded on events, never folded into a cache key: keying on it
/// would tie the key to cargo's unit hashing and break cross-machine sharing.
pub fn unit_id_from_extra_filename(extra_filename: &str) -> Option<String> {
    let id = extra_filename.strip_prefix('-').unwrap_or(extra_filename);
    (!id.is_empty()).then(|| id.to_string())
}

/// The producing unit's identity, recovered from an `--extern` artifact path.
///
/// Cargo names dependency artifacts `lib{crate_name}-{hash}.rlib` (`.rmeta`,
/// `.dylib`, `.so`, `.dll`), where `-{hash}` is the producer's
/// `-C extra-filename`. Taking the tail after the LAST dash is safe because a
/// rustc crate name cannot contain one.
///
/// Returns `None` for anything not carrying that suffix — a sysroot crate, a
/// hand-rolled rustc invocation, an artifact built without `extra-filename` —
/// so callers fall back to name matching rather than inventing an identity.
pub fn unit_id_from_artifact_path(path: &Path) -> Option<String> {
    let stem = path.file_stem()?.to_str()?;
    let (_, suffix) = stem.rsplit_once('-')?;
    // Cargo's hash is lowercase hex. Requiring that shape keeps a crate whose
    // *file* name happens to carry a dash (`libfoo-bar.rlib`, built outside
    // cargo) from being read as a unit id.
    let hex = suffix.len() >= 8 && suffix.bytes().all(|b| b.is_ascii_hexdigit());
    hex.then(|| suffix.to_string())
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
enum RustcArgfileState {
    #[default]
    None,
    Expanded,
    Unsupported,
}

#[derive(Default)]
struct RustcArgfileExpander {
    shell_argfiles: bool,
    next_is_unstable_option: bool,
    expanded: Vec<String>,
}

impl RustcArgfileExpander {
    fn is_shell_argfiles_option(option: &str) -> bool {
        option == "shell-argfiles" || option.starts_with("shell-argfiles=")
    }

    fn expand_arg(&mut self, arg: &str) -> Result<()> {
        let Some(path) = arg.strip_prefix('@') else {
            self.push(arg.to_string());
            return Ok(());
        };

        if self.shell_argfiles && path.starts_with("shell:") {
            bail!("rustc shell-style response files are not yet supported");
        }

        let contents = std::fs::read_to_string(path)
            .with_context(|| format!("reading rustc response file `{path}`"))?;
        // Match rustc exactly: each line is one argument, whitespace and blank
        // lines are preserved, and lines originating in a response file are
        // pushed verbatim rather than recursively expanded.
        for line in contents.lines() {
            self.push(line.to_string());
        }
        Ok(())
    }

    fn push(&mut self, arg: String) {
        // rustc inspects -Z options while expanding because
        // `-Zshell-argfiles` changes the meaning of a later top-level
        // `@shell:path`. We track that state only to fail closed on the
        // unsupported shell-style subset; ordinary response files still work.
        if self.next_is_unstable_option {
            if Self::is_shell_argfiles_option(&arg) {
                self.shell_argfiles = true;
            }
            self.next_is_unstable_option = false;
        } else if let Some(option) = arg.strip_prefix("-Z") {
            if option.is_empty() {
                self.next_is_unstable_option = true;
            } else if Self::is_shell_argfiles_option(option) {
                self.shell_argfiles = true;
            }
        }
        self.expanded.push(arg);
    }
}

/// Expand rustc's standard UTF-8, one-argument-per-line response files.
///
/// Returns `None` without allocating when the invocation contains no response
/// files. Expansion is atomic: any read/UTF-8/shell-style/lossless-transport
/// failure makes the whole invocation unsupported so the wrapper can pass the
/// original argv to rustc for its authoritative diagnostic.
fn expand_rustc_argfiles(args: &[String]) -> Result<Option<Vec<String>>> {
    if !args.iter().any(|arg| arg.starts_with('@')) {
        return Ok(None);
    }

    let mut expander = RustcArgfileExpander::default();
    for arg in args {
        expander.expand_arg(arg)?;
    }

    // Kache snapshots the effective argv into its own standard response file
    // before invoking rustc. Newlines and carriage returns cannot be encoded
    // losslessly in that format, so leave these rare invocations uncached.
    if expander
        .expanded
        .iter()
        .any(|arg| arg.contains('\n') || arg.contains('\r'))
    {
        bail!("rustc response file expands to an argument containing a line break");
    }

    Ok(Some(expander.expanded))
}

/// Parsed rustc invocation arguments relevant to caching.
#[derive(Debug, Clone, Default)]
pub struct RustcArgs {
    /// Path to the rustc binary (first arg from cargo when using RUSTC_WRAPPER)
    pub rustc: PathBuf,
    /// Crate name (--crate-name)
    pub crate_name: Option<String>,
    /// Crate type (--crate-type): lib, rlib, proc-macro, bin, dylib, cdylib, etc.
    pub crate_types: Vec<String>,
    /// Output path (-o)
    pub output: Option<PathBuf>,
    /// Output directory (--out-dir)
    pub out_dir: Option<PathBuf>,
    /// Emit types (--emit): dep-info, metadata, link, etc.
    pub emit: Vec<String>,
    /// Explicit output path from `--emit=dep-info=<path>`, when present.
    /// Cargo normally leaves this implicit; direct-rustc layouts are retained
    /// so cache admission can force a safe passthrough.
    pub dep_info_output: Option<PathBuf>,
    /// Source file (positional argument, typically the .rs file)
    pub source_file: Option<PathBuf>,
    /// Extern dependencies (--extern name=path)
    pub externs: Vec<ExternDep>,
    /// Target triple (--target)
    pub target: Option<String>,
    /// Edition (--edition)
    pub edition: Option<String>,
    /// Codegen options (-C key=value)
    pub codegen_opts: Vec<(String, Option<String>)>,
    /// Feature cfg flags (--cfg 'feature="name"')
    pub features: Vec<String>,
    /// All cfg flags (--cfg)
    pub cfgs: Vec<String>,
    /// Extra output file path (--extra-filename)
    pub extra_filename: Option<String>,
    /// Whether incremental compilation is enabled (-C incremental=...)
    pub incremental: Option<PathBuf>,
    /// Sysroot override (`--sysroot <path>`). Selects which std/core/
    /// proc-macro libs rustc links against, so it is codegen-relevant
    /// and must be part of the key (normalized at key time).
    pub sysroot: Option<PathBuf>,
    /// Native library search paths (`-L [KIND=]PATH`). Stored raw (kind
    /// prefix preserved); `compute_cache_key` path-normalizes the path
    /// and skips cargo's redundant `dependency=`/`crate=` entries.
    pub link_search: Vec<String>,
    /// Native libraries to link (`-l [KIND[:MODIFIERS]=]NAME`). Build
    /// scripts emit these via `cargo:rustc-link-lib`; they change a
    /// linked artifact without going through RUSTFLAGS, so they must be
    /// keyed. Machine-independent — hashed raw.
    pub link_libs: Vec<String>,
    /// Unstable `-Z` flags. Can change codegen (e.g. `-Zsanitizer`,
    /// `-Zshare-generics`) and arrive on argv outside RUSTFLAGS.
    pub unstable_flags: Vec<String>,
    /// Direct rustc `--remap-path-prefix FROM=TO` values in argv order.
    /// The key normalizes known machine-local FROM prefixes while retaining
    /// unrelated FROM values, TO values, and occurrence order.
    pub remap_path_prefixes: Vec<String>,
    /// Inner rustc path for double-wrapper case (RUSTC_WRAPPER + RUSTC_WORKSPACE_WRAPPER).
    /// When both wrappers are active, cargo passes: wrapper workspace_wrapper rustc <args>.
    /// This field holds the rustc path that the workspace wrapper expects as its first arg.
    pub inner_rustc: Option<PathBuf>,
    /// Effective rustc arguments after standard response-file expansion.
    /// Identical to the original arguments when no response file was used.
    pub all_args: Vec<String>,
    /// Original compact argv. Safe to reuse after a response transport
    /// failure only when Kache did not rewrite any effective argument.
    raw_args: Option<Vec<String>>,
    argfile_state: RustcArgfileState,
    /// Argv tokens not matched by any modeled flag above, excluding the
    /// diagnostics / lint / query / already-keyed path flags that parsing
    /// explicitly drops. These can still affect codegen (e.g. `-O`, `-g`, or a
    /// future rustc flag) yet were previously invisible to the cache key, so
    /// they are folded in under a versioned tag (kunobi-ninja/kache#324).
    pub residual_args: Vec<String>,
    /// Outcome-affecting lint gates (`-D`/`--deny`/`--forbid`/`-F`,
    /// `--force-warn`, `--cap-lints`) captured as flag+value token pairs in
    /// argv order, including attached spellings (`-Dwarnings`). These cannot
    /// change a successful compile's object bytes but DO change whether the
    /// compile fails, and hits replay success — so they are folded into the
    /// cache key (see `cache_key.rs`; review finding #2).
    pub outcome_lint_flags: Vec<String>,
    /// Whether this is a `--test` compilation (test harness binary)
    pub is_test: bool,
    /// Whether this looks like a primary compilation (has source file + crate name)
    pub is_primary: bool,
    /// Snapshot of the `KACHE_RUSTC_PATH_NORMALIZE` opt-out, read once at parse
    /// time. Both the cache-key `remap:` fold ([`crate::cache_key`]) and the
    /// rustc invocation ([`crate::compiler::rustc`]) consult this ONE snapshot
    /// via [`RustcArgs::skip_path_remap`], so they can never observe different
    /// process-global env values and desync the key from the artifact.
    pub path_normalize_disabled: bool,
    /// Path-normalization root selected once at parse time. Key construction,
    /// rustc injection, and dep-info transport all reuse this frozen value so
    /// filesystem drift cannot make them observe different remap rules.
    path_normalization_root: Option<PathBuf>,
}

#[derive(Debug, Clone)]
pub struct ExternDep {
    pub name: String,
    pub path: Option<PathBuf>,
}

fn parse_emit_value(value: &str, kinds: &mut Vec<String>, dep_info_output: &mut Option<PathBuf>) {
    for part in value.split(',') {
        let (kind, output) = part.split_once('=').unwrap_or((part, ""));
        kinds.push(kind.to_string());
        if kind == "dep-info" && !output.is_empty() {
            *dep_info_output = Some(PathBuf::from(output));
        }
    }
}

impl RustcArgs {
    /// Parse RUSTC_WRAPPER-style arguments.
    /// In RUSTC_WRAPPER mode, argv[0] = kache, argv[1] = rustc path, argv[2..] = rustc args.
    pub fn parse(args: &[String]) -> Result<Self> {
        if args.len() < 2 {
            bail!("expected at least rustc path as first argument");
        }

        let rustc = PathBuf::from(&args[0]);

        // Detect double-wrapper: if args[1] also looks like a compiler, this is
        // RUSTC_WRAPPER + RUSTC_WORKSPACE_WRAPPER. The inner path is the actual
        // rustc that the workspace wrapper (args[0]) expects as its first arg.
        let (inner_rustc, rustc_args) = if args.len() >= 3 && RustcCompiler::recognizes(&args[1..])
        {
            (Some(PathBuf::from(&args[1])), &args[2..])
        } else {
            (None, &args[1..])
        };

        let (parse_args, raw_args, argfile_state) = match expand_rustc_argfiles(rustc_args) {
            Ok(Some(expanded)) => (
                Cow::Owned(expanded),
                Some(rustc_args.to_vec()),
                RustcArgfileState::Expanded,
            ),
            Ok(None) => (Cow::Borrowed(rustc_args), None, RustcArgfileState::None),
            Err(error) => {
                tracing::debug!(
                    "rustc response-file expansion failed; passing through uncached: {error:#}"
                );
                (
                    Cow::Borrowed(rustc_args),
                    None,
                    RustcArgfileState::Unsupported,
                )
            }
        };
        let rustc_args = parse_args.as_ref();

        let mut parsed = RustcArgs {
            rustc,
            crate_name: None,
            crate_types: Vec::new(),
            output: None,
            out_dir: None,
            emit: Vec::new(),
            dep_info_output: None,
            source_file: None,
            externs: Vec::new(),
            target: None,
            edition: None,
            codegen_opts: Vec::new(),
            features: Vec::new(),
            cfgs: Vec::new(),
            extra_filename: None,
            incremental: None,
            sysroot: None,
            link_search: Vec::new(),
            link_libs: Vec::new(),
            unstable_flags: Vec::new(),
            remap_path_prefixes: Vec::new(),
            inner_rustc,
            all_args: rustc_args.to_vec(),
            raw_args,
            argfile_state,
            residual_args: Vec::new(),
            outcome_lint_flags: Vec::new(),
            is_test: false,
            is_primary: false,
            path_normalize_disabled: !crate::path_normalizer::rustc_path_normalize_enabled(),
            path_normalization_root: None,
        };
        // Some rustc queries accept a crate name and source path but only print
        // information to stdout; they deliberately emit no cacheable artifacts.
        // Keep this separate from ignored-key flags so source-bearing queries
        // cannot be mistaken for primary compilations.
        let mut is_query = false;

        let mut i = 0;
        while i < rustc_args.len() {
            let i_before = i;
            let arg = &rustc_args[i];

            match arg.as_str() {
                "--crate-name" => {
                    i += 1;
                    parsed.crate_name = rustc_args.get(i).cloned();
                }
                "--crate-type" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parsed.crate_types.push(val.clone());
                    }
                }
                "-o" => {
                    i += 1;
                    parsed.output = rustc_args.get(i).map(PathBuf::from);
                }
                "--out-dir" => {
                    i += 1;
                    parsed.out_dir = rustc_args.get(i).map(PathBuf::from);
                }
                "--emit" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parse_emit_value(val, &mut parsed.emit, &mut parsed.dep_info_output);
                    }
                }
                "--target" => {
                    i += 1;
                    parsed.target = rustc_args.get(i).cloned();
                }
                "--edition" => {
                    i += 1;
                    parsed.edition = rustc_args.get(i).cloned();
                }
                "--extern" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parsed.externs.push(parse_extern(val));
                    }
                }
                "--cfg" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parsed.cfgs.push(val.clone());
                        if let Some(feat) = parse_feature_cfg(val) {
                            parsed.features.push(feat);
                        }
                    }
                }
                "--extra-filename" if false => {
                    // --extra-filename is actually passed via -C extra-filename=...
                }
                _ if arg.starts_with("--emit=") => {
                    let val = &arg["--emit=".len()..];
                    parse_emit_value(val, &mut parsed.emit, &mut parsed.dep_info_output);
                }
                "--test" => {
                    parsed.is_test = true;
                }
                _ if arg.starts_with("--crate-type=") => {
                    let val = &arg["--crate-type=".len()..];
                    parsed.crate_types.push(val.to_string());
                }
                _ if arg.starts_with("--crate-name=") => {
                    parsed.crate_name = Some(arg["--crate-name=".len()..].to_string());
                }
                _ if arg.starts_with("--target=") => {
                    parsed.target = Some(arg["--target=".len()..].to_string());
                }
                _ if arg.starts_with("--edition=") => {
                    parsed.edition = Some(arg["--edition=".len()..].to_string());
                }
                _ if arg.starts_with("--extern=") => {
                    parsed.externs.push(parse_extern(&arg["--extern=".len()..]));
                }
                _ if arg.starts_with("--cfg=") => {
                    let val = &arg["--cfg=".len()..];
                    parsed.cfgs.push(val.to_string());
                    if let Some(feat) = parse_feature_cfg(val) {
                        parsed.features.push(feat);
                    }
                }
                "-C" | "--codegen" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        record_codegen_opt(&mut parsed, val);
                    }
                }
                _ if arg.starts_with("-C") && arg.len() > 2 => {
                    record_codegen_opt(&mut parsed, &arg[2..]);
                }
                _ if arg.starts_with("--codegen=") => {
                    record_codegen_opt(&mut parsed, &arg["--codegen=".len()..]);
                }
                // rustc defines these shorthands as exact -C aliases. Model
                // them in the same ordered bucket so last-wins combinations
                // such as `-O -Copt-level=0` cannot collide with the reverse.
                "-O" => {
                    parsed
                        .codegen_opts
                        .push(("opt-level".to_string(), Some("3".to_string())));
                }
                "-g" => {
                    parsed
                        .codegen_opts
                        .push(("debuginfo".to_string(), Some("2".to_string())));
                }
                "--sysroot" => {
                    i += 1;
                    parsed.sysroot = rustc_args.get(i).map(PathBuf::from);
                }
                _ if arg.starts_with("--sysroot=") => {
                    parsed.sysroot = Some(PathBuf::from(&arg["--sysroot=".len()..]));
                }
                // Native link search paths / libraries. cargo passes
                // `-L dependency=…` / `--extern` for rlib resolution (the
                // latter already content-hashed); build scripts add
                // `-L native=…` / `-l name` that change a linked artifact.
                // Both separate (`-L val`) and attached (`-Lval`) forms.
                "-L" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parsed.link_search.push(val.clone());
                    }
                }
                _ if arg.starts_with("-L") && arg.len() > 2 => {
                    parsed.link_search.push(arg["-L".len()..].to_string());
                }
                "-l" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parsed.link_libs.push(val.clone());
                    }
                }
                _ if arg.starts_with("-l") && arg.len() > 2 => {
                    parsed.link_libs.push(arg["-l".len()..].to_string());
                }
                "-Z" => {
                    i += 1;
                    if let Some(val) = rustc_args.get(i) {
                        parsed.unstable_flags.push(val.clone());
                    }
                }
                _ if arg.starts_with("-Z") && arg.len() > 2 => {
                    parsed.unstable_flags.push(arg["-Z".len()..].to_string());
                }
                "--remap-path-prefix" => {
                    i += 1;
                    if let Some(value) = rustc_args.get(i) {
                        parsed.remap_path_prefixes.push(value.clone());
                    }
                }
                _ if arg.starts_with("--remap-path-prefix=") => {
                    parsed
                        .remap_path_prefixes
                        .push(arg["--remap-path-prefix=".len()..].to_string());
                }
                "--print" | "--explain" => {
                    is_query = true;
                    i = i.saturating_add(1); // skip the value argument
                }
                _ if arg.starts_with("--print=") || arg.starts_with("--explain=") => {
                    is_query = true;
                }
                "-V" | "--version" | "-h" | "--help" | "-vV" => {
                    is_query = true;
                }
                // Outcome-affecting lint gates: capture flag + value for the
                // cache key before the generic diagnostics drop below. Must
                // precede the IGNORED_* arms — classification is first-match
                // (review finding #2).
                _ if OUTCOME_AFFECTING_VALUE_FLAGS.contains(&arg.as_str()) => {
                    parsed.outcome_lint_flags.push(arg.clone());
                    if let Some(value) = rustc_args.get(i + 1) {
                        parsed.outcome_lint_flags.push(value.clone());
                    }
                    i = i.saturating_add(1); // skip the value argument
                }
                _ if OUTCOME_LINT_ATTACHED_PREFIXES
                    .iter()
                    .any(|p| arg.starts_with(p)) =>
                {
                    parsed.outcome_lint_flags.push(arg.clone());
                }
                // Diagnostics / lint / query flags: never change the artifact,
                // so drop them (and their separated value) before the residual
                // catch-all (kunobi-ninja/kache#324).
                _ if IGNORED_VALUE_FLAGS.contains(&arg.as_str()) => {
                    i += 1; // skip the value argument
                }
                _ if IGNORED_BOOL_FLAGS.contains(&arg.as_str()) => {}
                _ if IGNORED_ATTACHED_PREFIXES.iter().any(|p| arg.starts_with(p)) => {}
                // Positional argument: source file (doesn't start with -)
                _ if !arg.starts_with('-')
                    && parsed.source_file.is_none()
                    && (arg.ends_with(".rs") || std::path::Path::new(arg).exists()) =>
                {
                    parsed.source_file = Some(PathBuf::from(arg));
                }
                // Anything else is an argv token kache does not model. It may
                // affect codegen, so keep it for the cache key (folded
                // normalized + sorted in `cache_key.rs`) rather than dropping
                // it silently (kunobi-ninja/kache#324).
                _ => {
                    parsed.residual_args.push(arg.clone());
                }
            }
            i += 1;
            // Every iteration must consume at least the token it just
            // classified. An arm that moves `i` backwards instead of forward
            // would spin here forever, growing whatever vector it pushes to
            // until the machine runs out of memory — a failure mode no test
            // can catch, since there is nothing to fail. Debug-only, so the
            // release parse is unchanged.
            debug_assert!(
                i > i_before,
                "argv parse must advance past index {i_before}"
            );
        }

        parsed.features.sort();
        parsed.is_primary =
            !is_query && parsed.crate_name.is_some() && parsed.source_file.is_some();
        parsed.path_normalization_root = std::env::current_dir()
            .ok()
            .map(|cwd| parsed.select_path_normalization_root(&cwd));

        Ok(parsed)
    }

    /// Original compact argv. This differs from [`Self::all_args`] only after
    /// a response file was expanded successfully.
    pub(crate) fn raw_args(&self) -> &[String] {
        self.raw_args.as_deref().unwrap_or(&self.all_args)
    }

    /// Whether cached child processes should receive the effective snapshot
    /// through a Kache-owned response file.
    pub(crate) fn has_expanded_argfiles(&self) -> bool {
        self.argfile_state == RustcArgfileState::Expanded
    }

    /// Whether expansion failed and the invocation must remain uncached.
    pub(crate) fn argfile_expansion_failed(&self) -> bool {
        self.argfile_state == RustcArgfileState::Unsupported
    }

    /// Whether this invocation produces an artifact the OS loads at runtime
    /// (executable, dylib, cdylib, proc-macro, or a `--test` harness binary).
    ///
    /// Derived from [`crate::compiler::rustc::classify_crate_type`] +
    /// [`crate::compiler::ArtifactKind::link_strategy`] — single source of
    /// truth shared with the per-file classifier in
    /// [`crate::compiler::Compiler::classify_output`]. Adding a new
    /// rustc crate-type to that mapping automatically updates this
    /// predicate (and every caller of it: cache_key linker hash,
    /// wrapper cache_executables gating, etc.).
    pub fn is_executable_output(&self) -> bool {
        use crate::compiler::rustc::classify_crate_type;
        use crate::link::LinkStrategy;
        self.is_test
            || self
                .crate_types
                .iter()
                .any(|t| classify_crate_type(t).link_strategy() == LinkStrategy::Copy)
    }

    /// Whether this compilation produces an artifact the user
    /// directly consumes (a `bin` they run, a `--test` they invoke).
    ///
    /// Distinct from [`Self::is_executable_output`]: that predicate
    /// is broader, covering every artifact whose link strategy is
    /// `Copy` — which includes `dylib` / `cdylib` / `proc-macro`.
    /// The wrapper uses this narrower check to gate the
    /// skip-cache-for-executables behavior, because proc-macros and
    /// dylibs are build-time concerns (rustc loads them, not the
    /// user) and ARE safely cacheable: PR #72's verify-then-sign
    /// handles macOS dyld signature checks on restore, so a cached
    /// proc-macro `.dylib` doesn't risk loading a stale or unsigned
    /// blob.
    ///
    /// Without this split, proc-macro deps recompile every build →
    /// non-byte-identical `.dylib` outputs → downstream crates that
    /// `--extern` them get unstable cache keys (the e422e55 relocate
    /// failure mode).
    pub fn is_user_facing_executable(&self) -> bool {
        self.is_test || self.crate_types.iter().any(|t| t == "bin")
    }

    /// Derive the workspace root from `--out-dir`. Cargo invokes
    /// rustc with `--out-dir <workspace>/target/<profile>/deps`, so
    /// three `parent()` steps land on the workspace root.
    ///
    /// Returns `None` if `--out-dir` wasn't set or doesn't have the
    /// expected three-level shape — defensive, but cargo always sets
    /// it for cacheable invocations.
    ///
    /// Centralized here so both the cache_key construction (in
    /// `wrapper::run`) and the rustc invocation construction (in
    /// `RustcCompiler::execute`) derive the workspace from the same
    /// source. Otherwise PathNormalizer would compute different
    /// rules for the two consumers and the cache key wouldn't reflect
    /// the actual remap injection.
    pub fn workspace_root(&self) -> Option<PathBuf> {
        self.target_dir()
            .and_then(|t| t.parent().map(Path::to_path_buf))
    }

    /// Return the target-derived workspace root only when it is consistent
    /// with Cargo's compiler working directory.
    ///
    /// An external `CARGO_TARGET_DIR` makes [`Self::workspace_root`] point at
    /// the target's parent rather than the source workspace. Treating that as
    /// a relocatable source root can alias unrelated files. Requiring a
    /// manifest at the candidate and the compiler cwd beneath it fails closed
    /// for external/shared targets while retaining normal workspace layouts.
    pub fn verified_workspace_root(&self, cwd: &Path) -> Option<PathBuf> {
        let candidate = self.workspace_root()?;
        if !candidate.join("Cargo.toml").is_file() {
            return None;
        }
        let canonical_candidate = candidate.canonicalize().ok()?;
        let canonical_cwd = cwd.canonicalize().ok()?;
        canonical_cwd
            .starts_with(&canonical_candidate)
            .then(|| std::path::absolute(&candidate).unwrap_or(candidate))
    }

    /// Workspace anchor shared by cache-key construction and rustc remapping.
    ///
    /// Cargo's output-derived candidate is valid for an in-workspace target,
    /// but an external `CARGO_TARGET_DIR` makes it point at the target's parent.
    /// Fall back to the compiler working directory in that case so key and
    /// artifact remapping never treat an unrelated target parent as sources.
    fn select_path_normalization_root(&self, cwd: &Path) -> PathBuf {
        self.verified_workspace_root(cwd)
            .unwrap_or_else(|| cwd.to_path_buf())
    }

    /// Frozen root shared by the key, rustc invocation, and dep-info rewrite.
    pub fn path_normalization_root(&self) -> Option<&Path> {
        self.path_normalization_root.as_deref()
    }

    /// Derive the cargo target directory (e.g. `<workspace>/target`) from
    /// the rustc args.
    ///
    /// This is the anchor for dep-info (`.d`) path rewriting. Cargo invokes
    /// rustc with cwd = the package source dir — *not* the target dir — so
    /// `std::env::current_dir()` cannot be used. Cargo's output layout is
    /// stable enough to infer the target dir from the args instead:
    ///
    /// - `--out-dir` is `<target>/<profile>/deps` for libs/bins → walk up 2.
    /// - `-o` for a build script is
    ///   `<target>/<profile>/build/<pkg>/build_script_build-<hash>`; walk up
    ///   to the ancestor named `deps` or `build`, then take its grandparent.
    ///
    /// Store and restore must agree on this anchor: the store side
    /// relativizes the `.d` against it (`<target>/...` → kache's dep-info
    /// sentinel) and the restore side expands that sentinel back against
    /// *this* invocation's target dir. Because the `.d`'s paths are all
    /// rooted under `<target>`, the relativize→expand round-trip yields paths
    /// valid at whatever location the restoring build runs from.
    ///
    /// Returns `None` for invocations outside cargo's layout (e.g. ad-hoc
    /// `rustc -o /tmp/prog`), so dep-info rewriting is skipped rather than
    /// anchored to a wrong directory.
    pub fn target_dir(&self) -> Option<PathBuf> {
        let is_cross = self.target.is_some();
        if let Some(od) = &self.out_dir {
            let mut p = od.parent()?;
            p = p.parent()?;
            if is_cross {
                p = p.parent()?;
            }
            return Some(p.to_path_buf());
        }
        let out = self.output.as_deref()?;
        let mut cursor = out.parent();
        while let Some(dir) = cursor {
            if let Some(name) = dir.file_name()
                && (name == "deps" || name == "build")
            {
                let mut p = dir.parent()?;
                p = p.parent()?;
                if is_cross {
                    p = p.parent()?;
                }
                return Some(p.to_path_buf());
            }
            cursor = dir.parent();
        }
        None
    }

    /// Whether this rustc invocation looks like a build-script feature probe.
    ///
    /// Crates such as `proc-macro2`, `thiserror`, and `anyhow` run small rustc
    /// probes from their build scripts to detect compiler features. Those
    /// commands intentionally may fail, usually emit metadata only, and write
    /// under the build script's `OUT_DIR`. They are not useful cache entries:
    /// pass them through so expected probe failures do not appear as kache
    /// cache errors.
    pub fn is_build_script_probe(&self, build_script_out_dir: Option<&Path>) -> bool {
        let Some(build_script_out_dir) = build_script_out_dir else {
            return false;
        };
        let metadata_only = !self.emit.is_empty() && !self.emit.iter().any(|e| e == "link");
        if !metadata_only {
            return false;
        }

        self.out_dir
            .as_deref()
            .is_some_and(|out_dir| out_dir.starts_with(build_script_out_dir))
            || self
                .source_file
                .as_deref()
                .is_some_and(|source| source.starts_with(build_script_out_dir))
    }

    /// Output filename stem (`crate_name` + optional `extra_filename`).
    #[cfg_attr(not(test), allow(dead_code))]
    pub fn output_stem(&self) -> Option<String> {
        Some(format_crate_output_stem(
            self.crate_name.as_ref()?,
            self.extra_filename.as_deref().unwrap_or(""),
        ))
    }

    /// Path of the Cargo-facing rustc dep-info output for this invocation.
    ///
    /// Cargo's normal invocation uses `<out-dir>/<crate><extra>.d`. Explicit
    /// `--emit=dep-info=<path>` and `-o` forms are direct-rustc layouts that the
    /// cache restore model does not preserve; those invocations pass through
    /// and retain their caller-owned freshness.
    pub fn dep_info_path(&self) -> Option<PathBuf> {
        if !self.emit.iter().any(|kind| kind == "dep-info") {
            return None;
        }
        if self.dep_info_output.is_some() {
            return None;
        }
        if self.output.is_some() {
            return None;
        }
        let name = self.crate_name.as_deref()?;
        let file_name = format!(
            "{}.d",
            format_crate_output_stem(name, self.extra_filename.as_deref().unwrap_or(""))
        );
        self.out_dir.as_ref().map(|dir| dir.join(file_name))
    }

    /// Cargo checksum freshness asks rustc to annotate every dep-info input.
    /// Directories cannot carry those annotations, so extra-input directory
    /// watches must currently reject this mode instead of staying perpetually
    /// dirty or silently missing additions.
    pub fn checksum_freshness_enabled(&self) -> bool {
        self.unstable_flags
            .iter()
            .any(|flag| flag.starts_with("checksum-hash-algorithm"))
    }

    /// This compile's own unit identity, for diagnostics only
    /// (kunobi-ninja/kache#627). `None` when cargo passed no `-C extra-filename`,
    /// which is the same case [`unit_id_from_artifact_path`] cannot resolve from
    /// the consumer side — so the two sides go unidentified together, and
    /// `why-miss` falls back to matching by crate name.
    pub fn unit_id(&self) -> Option<String> {
        unit_id_from_extra_filename(self.extra_filename.as_deref()?)
    }

    /// Whether this compilation has coverage instrumentation enabled (-C instrument-coverage).
    /// When active, path remapping must be skipped so coverage tools (tarpaulin, llvm-cov)
    /// can map profraw data back to source files.
    pub fn has_coverage_instrumentation(&self) -> bool {
        self.codegen_opts
            .iter()
            .any(|(k, _)| k == "instrument-coverage")
    }

    /// Whether kache should skip injecting its own `--remap-path-prefix` flags
    /// for this compile — either because coverage instrumentation needs real
    /// paths in the profraw, or because the user opted out via
    /// `KACHE_RUSTC_PATH_NORMALIZE=0` (kunobi-ninja/kache#480).
    ///
    /// Single source of truth for the injection decision
    /// ([`crate::compiler::rustc`]) and the cache-key `remap:` fold
    /// ([`crate::cache_key`]) — both MUST agree, or the key would claim one
    /// remap state while the binary was built with the other.
    pub fn skip_path_remap(&self) -> bool {
        self.has_coverage_instrumentation() || self.path_normalize_disabled
    }

    /// Get a codegen option value by key.
    pub fn get_codegen_opt(&self, key: &str) -> Option<&str> {
        self.codegen_opts
            .iter()
            .rev()
            .find(|(k, _)| k == key)
            .and_then(|(_, v)| v.as_deref())
    }
}

fn parse_extern(s: &str) -> ExternDep {
    // Format: name=path or just name
    // Can also be: priv:name=path or noprelude:name=path
    let s = s
        .strip_prefix("priv:")
        .or_else(|| s.strip_prefix("noprelude:"))
        .unwrap_or(s);

    if let Some((name, path)) = s.split_once('=') {
        ExternDep {
            name: name.to_string(),
            path: Some(PathBuf::from(path)),
        }
    } else {
        ExternDep {
            name: s.to_string(),
            path: None,
        }
    }
}

fn parse_feature_cfg(s: &str) -> Option<String> {
    // --cfg 'feature="derive"' -> "derive"
    let s = s.strip_prefix("feature=\"")?.strip_suffix('"')?;
    Some(s.to_string())
}

fn parse_codegen_opt(s: &str) -> (String, Option<String>) {
    if let Some((key, value)) = s.split_once('=') {
        (key.to_string(), Some(value.to_string()))
    } else {
        (s.to_string(), None)
    }
}

fn record_codegen_opt(parsed: &mut RustcArgs, value: &str) {
    let (key, value) = parse_codegen_opt(value);
    if key == "extra-filename" {
        parsed.extra_filename = value.clone();
    }
    if key == "incremental" {
        parsed.incremental = value.as_ref().map(PathBuf::from);
    }
    parsed.codegen_opts.push((key, value));
}

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

    /// Outcome-affecting lint gates must be captured exactly — flag AND
    /// value, separated and attached spellings — and must not leak into the
    /// residual catch-all. Asserting the captured tokens directly (not just
    /// "the key changed") pins the parse indices: a wrong skip or a wrong
    /// `get(i ± 1)` would capture the wrong token pair.
    #[test]
    fn outcome_lint_gates_capture_flag_and_value() {
        let parse = |extra: &[&str]| {
            let mut argv = vec![
                "rustc".to_string(),
                "--crate-name".to_string(),
                "m".to_string(),
            ];
            argv.extend(extra.iter().map(|s| s.to_string()));
            RustcArgs::parse(&argv).unwrap()
        };

        let separated = parse(&["-D", "warnings"]);
        assert_eq!(
            separated.outcome_lint_flags,
            ["-D", "warnings"],
            "separated gate must capture flag then value"
        );
        assert!(
            separated.residual_args.is_empty(),
            "gate tokens must not leak into residual: {:?}",
            separated.residual_args
        );

        let attached = parse(&["-Dwarnings"]);
        assert_eq!(attached.outcome_lint_flags, ["-Dwarnings"]);

        let long = parse(&["--cap-lints", "allow"]);
        assert_eq!(long.outcome_lint_flags, ["--cap-lints", "allow"]);
        assert!(long.residual_args.is_empty());

        let long_attached = parse(&["--forbid=unused"]);
        assert_eq!(long_attached.outcome_lint_flags, ["--forbid=unused"]);

        // Diagnostics-only levels stay out of the capture (#324).
        let warn = parse(&["-W", "unused"]);
        assert!(warn.outcome_lint_flags.is_empty());
        assert!(warn.residual_args.is_empty());
    }

    /// The two sides of the #627 join have to agree: what a producer records
    /// for itself (`-C extra-filename`) must equal what a consumer recovers
    /// from the artifact filename cargo built with that flag.
    #[test]
    fn unit_id_round_trips_between_the_producer_and_its_artifact() {
        let producer = unit_id_from_extra_filename("-843f02d6a46ebef1").unwrap();
        for artifact in [
            "/w/target/debug/deps/librust_check-843f02d6a46ebef1.rmeta",
            "/w/target/debug/deps/librust_check-843f02d6a46ebef1.rlib",
            "/w/target/debug/deps/librust_check-843f02d6a46ebef1.dylib",
            r"C:\w\target\debug\deps\librust_check-843f02d6a46ebef1.rlib",
        ] {
            assert_eq!(
                unit_id_from_artifact_path(Path::new(artifact)).as_deref(),
                Some(producer.as_str()),
                "{artifact}"
            );
        }
    }

    #[test]
    fn unit_id_declines_paths_without_a_cargo_hash_suffix() {
        // A sysroot crate, and a crate whose file name merely contains a dash:
        // inventing an identity for either would be worse than falling back to
        // matching by name.
        for artifact in [
            "/toolchain/lib/rustlib/x86_64/lib/libstd.rlib",
            "/w/target/debug/deps/libfoo-bar.rlib",
            "/w/target/debug/deps/libfoo-123.rlib",
        ] {
            assert_eq!(
                unit_id_from_artifact_path(Path::new(artifact)),
                None,
                "{artifact}"
            );
        }
        assert_eq!(unit_id_from_extra_filename(""), None);
        assert_eq!(unit_id_from_extra_filename("-"), None);
    }

    #[test]
    fn unit_id_reads_the_parsed_extra_filename() {
        let args: Vec<String> = ["rustc", "rustc", "--crate-name", "foo", "src/lib.rs"]
            .iter()
            .map(|s| s.to_string())
            .collect();
        let mut parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.unit_id(), None, "no -C extra-filename, no identity");

        parsed.extra_filename = Some("-d44c553abc12".to_string());
        assert_eq!(parsed.unit_id().as_deref(), Some("d44c553abc12"));
    }

    #[test]
    fn test_parse_basic_lib() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "serde",
            "--edition=2021",
            "src/lib.rs",
            "--crate-type",
            "lib",
            "--emit=dep-info,metadata,link",
            "-C",
            "opt-level=3",
            "-C",
            "extra-filename=-d44c553",
            "--extern",
            "serde_derive=/path/to/libserde_derive.so",
            "-o",
            "/project/target/debug/deps/libserde-d44c553.rlib",
            "--cfg",
            "feature=\"derive\"",
            "--cfg",
            "feature=\"std\"",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.crate_name.as_deref(), Some("serde"));
        assert_eq!(parsed.crate_types, vec!["lib"]);
        assert_eq!(parsed.edition.as_deref(), Some("2021"));
        assert_eq!(parsed.emit, vec!["dep-info", "metadata", "link"]);
        assert_eq!(parsed.extra_filename.as_deref(), Some("-d44c553"));
        assert!(parsed.source_file.is_some());
        assert_eq!(parsed.externs.len(), 1);
        assert_eq!(parsed.externs[0].name, "serde_derive");
        assert_eq!(parsed.features, vec!["derive", "std"]);
        assert_eq!(
            parsed.output.as_ref().unwrap().to_string_lossy(),
            "/project/target/debug/deps/libserde-d44c553.rlib"
        );
        assert!(!parsed.is_executable_output());
        assert!(parsed.is_primary);
    }

    #[test]
    fn rustc_response_file_is_expanded_before_parsing() {
        let dir = tempfile::tempdir().unwrap();
        let source = dir.path().join("lib.rs");
        std::fs::write(&source, "pub fn answer() -> u32 { 42 }\n").unwrap();
        let response = dir.path().join("rustc.args");
        std::fs::write(
            &response,
            format!(
                "--crate-name\nresponse_file\n{}\n--crate-type\nlib\n-C\nopt-level=2\n",
                source.display()
            ),
        )
        .unwrap();

        let parsed =
            RustcArgs::parse(&["rustc".to_string(), format!("@{}", response.display())]).unwrap();

        assert!(parsed.is_primary);
        assert_eq!(parsed.crate_name.as_deref(), Some("response_file"));
        assert_eq!(parsed.source_file.as_deref(), Some(source.as_path()));
        assert_eq!(parsed.get_codegen_opt("opt-level"), Some("2"));
        assert!(parsed.has_expanded_argfiles());
        assert_eq!(parsed.raw_args(), &[format!("@{}", response.display())]);
        assert!(
            !parsed
                .all_args
                .iter()
                .any(|arg| arg == &format!("@{}", response.display()))
        );
    }

    #[test]
    fn rustc_response_file_line_semantics_match_rustc() {
        let dir = tempfile::tempdir().unwrap();
        let nested = dir.path().join("nested.args");
        std::fs::write(&nested, "must-not-be-expanded\n").unwrap();
        let response = dir.path().join("rustc.args");
        std::fs::write(
            &response,
            format!(
                "--crate-name\r\nfoo\r\n\r\n  spaced  \r\n@{}\r\n",
                nested.display()
            ),
        )
        .unwrap();

        let parsed =
            RustcArgs::parse(&["rustc".to_string(), format!("@{}", response.display())]).unwrap();

        assert_eq!(
            parsed.all_args,
            vec![
                "--crate-name".to_string(),
                "foo".to_string(),
                String::new(),
                "  spaced  ".to_string(),
                format!("@{}", nested.display()),
            ]
        );
    }

    #[test]
    fn invalid_utf8_response_file_fails_closed() {
        let dir = tempfile::tempdir().unwrap();
        let response = dir.path().join("invalid.args");
        std::fs::write(&response, [0xff, 0xfe]).unwrap();
        let raw = vec![
            "rustc".to_string(),
            "--crate-name".to_string(),
            "foo".to_string(),
            "src/lib.rs".to_string(),
            format!("@{}", response.display()),
        ];

        let parsed = RustcArgs::parse(&raw).unwrap();
        assert!(parsed.argfile_expansion_failed());
        assert!(!parsed.has_expanded_argfiles());
        assert_eq!(parsed.all_args, raw[1..]);
        assert_eq!(parsed.raw_args(), &raw[1..]);
    }

    #[test]
    fn shell_style_response_file_fails_closed() {
        for option in ["-Zshell-argfiles", "-Zshell-argfiles=yes"] {
            let raw = vec![
                "rustc".to_string(),
                option.to_string(),
                "@shell:rustc.args".to_string(),
            ];

            let parsed = RustcArgs::parse(&raw).unwrap();
            assert!(parsed.argfile_expansion_failed(), "option: {option}");
            assert_eq!(parsed.all_args, raw[1..], "option: {option}");
        }
    }

    #[test]
    fn shell_argfiles_option_recognition_is_precise() {
        assert!(RustcArgfileExpander::is_shell_argfiles_option(
            "shell-argfiles"
        ));
        assert!(RustcArgfileExpander::is_shell_argfiles_option(
            "shell-argfiles=yes"
        ));
        assert!(!RustcArgfileExpander::is_shell_argfiles_option(
            "other-option"
        ));
        assert!(!RustcArgfileExpander::is_shell_argfiles_option(
            "shell-argfiles-extra"
        ));
    }

    #[test]
    fn regular_response_file_expands_with_shell_mode_enabled() {
        let dir = tempfile::tempdir().unwrap();
        let response = dir.path().join("rustc.args");
        std::fs::write(&response, "--crate-name\nfoo\nsrc/lib.rs\n").unwrap();
        let raw = vec![
            "rustc".to_string(),
            "-Zshell-argfiles".to_string(),
            format!("@{}", response.display()),
        ];

        let parsed = RustcArgs::parse(&raw).unwrap();
        assert!(parsed.has_expanded_argfiles());
        assert!(!parsed.argfile_expansion_failed());
        assert_eq!(parsed.crate_name.as_deref(), Some("foo"));
        assert_eq!(parsed.source_file.as_deref(), Some(Path::new("src/lib.rs")));
    }

    #[test]
    fn response_file_with_unrepresentable_direct_arg_fails_closed() {
        let dir = tempfile::tempdir().unwrap();
        let response = dir.path().join("rustc.args");
        std::fs::write(&response, "--crate-name\nfoo\n").unwrap();

        for argument in ["line\nbreak", "carriage\rreturn"] {
            let raw = vec![
                "rustc".to_string(),
                argument.to_string(),
                format!("@{}", response.display()),
            ];

            let parsed = RustcArgs::parse(&raw).unwrap();
            assert!(parsed.argfile_expansion_failed(), "argument: {argument:?}");
            assert_eq!(parsed.all_args, raw[1..], "argument: {argument:?}");
        }
    }

    #[test]
    fn test_parse_bin_crate() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "myapp",
            "src/main.rs",
            "--crate-type",
            "bin",
            "-o",
            "/project/target/debug/myapp",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(parsed.is_executable_output());
    }

    #[test]
    fn test_parse_extern_with_prefix() {
        let dep = parse_extern("priv:core=/path/to/libcore.rlib");
        assert_eq!(dep.name, "core");
        assert!(dep.path.is_some());
    }

    #[test]
    fn test_feature_cfg_parsing() {
        assert_eq!(
            parse_feature_cfg("feature=\"derive\""),
            Some("derive".to_string())
        );
        assert_eq!(parse_feature_cfg("unix"), None);
    }

    #[test]
    fn test_parse_too_few_args() {
        let args: Vec<String> = vec!["rustc".into()];
        assert!(RustcArgs::parse(&args).is_err());
    }

    #[test]
    fn test_parse_empty_args() {
        let args: Vec<String> = vec![];
        assert!(RustcArgs::parse(&args).is_err());
    }

    #[test]
    fn test_parse_non_primary_no_source() {
        let args: Vec<String> = vec!["rustc", "--crate-name", "foo", "-C", "opt-level=3"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(!parsed.is_primary);
    }

    #[test]
    fn test_parse_codegen_opt_lookup() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "-C",
            "opt-level=3",
            "-Cmetadata=abc123",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.get_codegen_opt("opt-level"), Some("3"));
        assert_eq!(parsed.get_codegen_opt("metadata"), Some("abc123"));
        assert_eq!(parsed.get_codegen_opt("nonexistent"), None);
    }

    #[test]
    fn test_parse_codegen_shorthands_preserves_override_order() {
        let args: Vec<String> = vec![
            "rustc",
            "src/lib.rs",
            "-O",
            "--codegen=opt-level=0",
            "--codegen",
            "debuginfo=0",
            "-g",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();

        assert_eq!(
            parsed.codegen_opts,
            vec![
                ("opt-level".to_string(), Some("3".to_string())),
                ("opt-level".to_string(), Some("0".to_string())),
                ("debuginfo".to_string(), Some("0".to_string())),
                ("debuginfo".to_string(), Some("2".to_string())),
            ]
        );
        assert_eq!(parsed.get_codegen_opt("opt-level"), Some("0"));
        assert_eq!(parsed.get_codegen_opt("debuginfo"), Some("2"));
        assert!(parsed.residual_args.is_empty());
    }

    #[test]
    fn test_parse_direct_remap_path_prefixes() {
        let args: Vec<String> = vec![
            "rustc",
            "src/lib.rs",
            "--remap-path-prefix",
            "/work/a=/src",
            "--remap-path-prefix=/work/b=/generated",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();

        assert_eq!(
            parsed.remap_path_prefixes,
            vec!["/work/a=/src".to_string(), "/work/b=/generated".to_string()]
        );
        assert!(parsed.residual_args.is_empty());
    }

    #[test]
    fn test_is_executable_output_variants() {
        for crate_type in ["bin", "dylib", "cdylib", "proc-macro"] {
            let args: Vec<String> = vec!["rustc", "--crate-type", crate_type, "src/lib.rs"]
                .into_iter()
                .map(String::from)
                .collect();
            let parsed = RustcArgs::parse(&args).unwrap();
            assert!(
                parsed.is_executable_output(),
                "{crate_type} should be executable"
            );
        }
        for crate_type in ["lib", "rlib", "staticlib"] {
            let args: Vec<String> = vec!["rustc", "--crate-type", crate_type, "src/lib.rs"]
                .into_iter()
                .map(String::from)
                .collect();
            let parsed = RustcArgs::parse(&args).unwrap();
            assert!(
                !parsed.is_executable_output(),
                "{crate_type} should not be executable"
            );
        }

        // --test flag makes output executable regardless of crate type
        let args: Vec<String> = vec!["rustc", "--crate-type", "lib", "--test", "src/lib.rs"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(parsed.is_test, "--test should set is_test");
        assert!(parsed.is_executable_output(), "--test should be executable");
    }

    #[test]
    fn test_is_user_facing_executable_excludes_proc_macro_and_dylib() {
        // The narrower predicate: only `bin` + `--test` count.
        // proc-macro / dylib / cdylib are build-time artifacts that
        // should be cacheable, not skipped via the
        // cache_executables gate. This is the contract that lets
        // multi-dep's relocate phase get to zero misses — a
        // recompiled-every-build proc-macro produces non-byte-
        // identical output that breaks downstream `extern:` keys.
        for (crate_type, expected) in [
            ("bin", true),
            ("lib", false),
            ("rlib", false),
            ("staticlib", false),
            ("dylib", false),
            ("cdylib", false),
            ("proc-macro", false),
        ] {
            let args: Vec<String> = vec!["rustc", "--crate-type", crate_type, "src/lib.rs"]
                .into_iter()
                .map(String::from)
                .collect();
            let parsed = RustcArgs::parse(&args).unwrap();
            assert_eq!(
                parsed.is_user_facing_executable(),
                expected,
                "{crate_type}: is_user_facing_executable mismatch"
            );
        }

        // --test makes any compilation user-facing (test harness).
        let args: Vec<String> = vec!["rustc", "--crate-type", "lib", "--test", "src/lib.rs"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(
            parsed.is_user_facing_executable(),
            "--test must count as user-facing"
        );
    }

    #[test]
    fn test_format_crate_output_stem() {
        assert_eq!(format_crate_output_stem("serde", "-9f2a1b"), "serde-9f2a1b");
        assert_eq!(format_crate_output_stem("serde", ""), "serde");
        assert_eq!(format_crate_output_stem("app", "-abc"), "app-abc");
    }

    #[test]
    fn test_output_stem() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "mylib",
            "src/lib.rs",
            "-C",
            "extra-filename=-abc123",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.output_stem(), Some("mylib-abc123".to_string()));
    }

    #[test]
    fn test_output_stem_no_extra() {
        let args: Vec<String> = vec!["rustc", "--crate-name", "mylib", "src/lib.rs"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.output_stem(), Some("mylib".to_string()));
    }

    #[test]
    fn test_output_stem_no_name() {
        let args: Vec<String> = vec!["rustc", "src/lib.rs"]
            .into_iter()
            .map(String::from)
            .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.output_stem(), None);
    }

    #[test]
    fn test_parse_extern_name_only() {
        let dep = parse_extern("core");
        assert_eq!(dep.name, "core");
        assert!(dep.path.is_none());
    }

    #[test]
    fn test_parse_extern_noprelude() {
        let dep = parse_extern("noprelude:std=/path/to/libstd.rlib");
        assert_eq!(dep.name, "std");
        assert!(dep.path.is_some());
    }

    #[test]
    fn test_parse_codegen_opt_no_value() {
        let (key, value) = parse_codegen_opt("debuginfo");
        assert_eq!(key, "debuginfo");
        assert!(value.is_none());
    }

    #[test]
    fn test_parse_codegen_opt_with_value() {
        let (key, value) = parse_codegen_opt("opt-level=3");
        assert_eq!(key, "opt-level");
        assert_eq!(value, Some("3".to_string()));
    }

    #[test]
    fn test_parse_incremental_flag() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "-C",
            "incremental=/tmp/incr",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.incremental, Some(PathBuf::from("/tmp/incr")));
    }

    #[test]
    fn test_parse_target_and_out_dir() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "--target",
            "aarch64-apple-darwin",
            "--out-dir",
            "/project/target/debug/deps",
            "src/lib.rs",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.target.as_deref(), Some("aarch64-apple-darwin"));
        assert_eq!(
            parsed.out_dir,
            Some(PathBuf::from("/project/target/debug/deps"))
        );
    }

    #[test]
    fn test_parse_equals_form_args() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name=mylib",
            "--crate-type=rlib",
            "--target=x86_64-unknown-linux-gnu",
            "--edition=2021",
            "--cfg=unix",
            "--extern=serde=/path/lib.rlib",
            "src/lib.rs",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.crate_name.as_deref(), Some("mylib"));
        assert_eq!(parsed.crate_types, vec!["rlib"]);
        assert_eq!(parsed.target.as_deref(), Some("x86_64-unknown-linux-gnu"));
        assert_eq!(parsed.edition.as_deref(), Some("2021"));
        assert!(parsed.cfgs.contains(&"unix".to_string()));
        assert_eq!(parsed.externs[0].name, "serde");
    }

    #[test]
    fn test_parse_double_wrapper() {
        // Simulates: kache clippy-driver /path/to/rustc --crate-name foo src/lib.rs --crate-type lib
        // After main.rs strips argv[0], parse receives: [clippy-driver, /path/to/rustc, ...]
        let args: Vec<String> = vec![
            "clippy-driver",
            "/home/user/.rustup/toolchains/stable/bin/rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "--crate-type",
            "lib",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.rustc, PathBuf::from("clippy-driver"));
        assert_eq!(
            parsed.inner_rustc,
            Some(PathBuf::from(
                "/home/user/.rustup/toolchains/stable/bin/rustc"
            ))
        );
        assert_eq!(parsed.crate_name.as_deref(), Some("foo"));
        // inner rustc path should NOT appear in all_args
        assert!(!parsed.all_args.iter().any(|a| a.contains("rustc")));
        // inner rustc should NOT be picked up as the source file
        assert!(parsed.inner_rustc.is_some());
    }

    #[test]
    fn test_parse_double_wrapper_windows_exe() {
        // Regression for issue #287: the double-wrapper split keys off
        // `RustcCompiler::recognizes(&args[1..])`, so it must also fire when
        // the inner rustc is a Windows `.exe` path. Before the `.exe`/backslash
        // fix, `clippy-driver.exe` was not recognized at all — and here the
        // inner `rustc.exe` would likewise have been missed, mis-parsing the
        // inner compiler as a positional source file. Holds on every host OS.
        let args: Vec<String> = vec![
            r"G:\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\clippy-driver.exe",
            r"C:\Program Files\Rust\bin\rustc.exe",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "--crate-type",
            "lib",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(
            parsed.rustc,
            PathBuf::from(
                r"G:\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\clippy-driver.exe"
            )
        );
        assert_eq!(
            parsed.inner_rustc,
            Some(PathBuf::from(r"C:\Program Files\Rust\bin\rustc.exe"))
        );
        assert_eq!(parsed.crate_name.as_deref(), Some("foo"));
        // The inner rustc.exe must be consumed by the split, not left in
        // all_args where it could be mistaken for a source positional.
        assert!(!parsed.all_args.iter().any(|a| a.contains("rustc.exe")));
    }

    #[test]
    fn test_parse_double_wrapper_unrecognized_driver() {
        // Issue #505: dylint-driver (or any future RUSTC_WORKSPACE_WRAPPER
        // tool) in the double-wrapper chain. The split keys off the inner
        // rustc, so an unrecognized workspace wrapper is forwarded correctly.
        let args: Vec<String> = vec![
            "/Users/dev/.dylint_drivers/nightly/dylint-driver",
            "/home/user/.rustup/toolchains/stable/bin/rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "--crate-type",
            "lib",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(
            parsed.rustc,
            PathBuf::from("/Users/dev/.dylint_drivers/nightly/dylint-driver")
        );
        assert_eq!(
            parsed.inner_rustc,
            Some(PathBuf::from(
                "/home/user/.rustup/toolchains/stable/bin/rustc"
            ))
        );
        assert_eq!(parsed.crate_name.as_deref(), Some("foo"));
        assert!(!parsed.all_args.iter().any(|a| a.contains("rustc")));
    }

    #[test]
    fn test_parse_single_wrapper_unchanged() {
        // Normal case: kache /path/to/rustc --crate-name foo src/lib.rs
        // After main.rs strips argv[0], parse receives: [/path/to/rustc, ...]
        let args: Vec<String> = vec![
            "/home/user/.rustup/toolchains/stable/bin/rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "--crate-type",
            "lib",
        ]
        .into_iter()
        .map(String::from)
        .collect();

        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(
            parsed.rustc,
            PathBuf::from("/home/user/.rustup/toolchains/stable/bin/rustc")
        );
        assert!(parsed.inner_rustc.is_none());
        assert_eq!(parsed.crate_name.as_deref(), Some("foo"));
    }

    #[test]
    fn test_has_coverage_instrumentation_joined() {
        // -Cinstrument-coverage (joined form, used by tarpaulin via RUSTFLAGS)
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "-Cinstrument-coverage",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(parsed.has_coverage_instrumentation());
    }

    #[test]
    fn test_has_coverage_instrumentation_two_arg() {
        // -C instrument-coverage (two-arg form)
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "-C",
            "instrument-coverage",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(parsed.has_coverage_instrumentation());
    }

    #[test]
    fn test_no_coverage_instrumentation() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "-Copt-level=3",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert!(!parsed.has_coverage_instrumentation());
    }

    #[test]
    fn test_target_dir_from_out_dir() {
        // Lib/bin compiles: --out-dir is `<target>/<profile>/deps`.
        let args = RustcArgs {
            out_dir: Some(PathBuf::from("/work/proj/target/debug/deps")),
            ..Default::default()
        };
        assert_eq!(args.target_dir(), Some(PathBuf::from("/work/proj/target")));
    }

    #[test]
    fn test_target_dir_from_build_script_output() {
        // Build scripts: -o is
        // `<target>/<profile>/build/<pkg>/build_script_build-<hash>`.
        let args = RustcArgs {
            output: Some(PathBuf::from(
                "/work/proj/target/debug/build/serde-abc123/build_script_build-abc123",
            )),
            ..Default::default()
        };
        assert_eq!(args.target_dir(), Some(PathBuf::from("/work/proj/target")));
    }

    #[test]
    fn test_target_dir_prefers_out_dir_over_output() {
        // Cargo passes both --out-dir and -o for a lib/bin; --out-dir is
        // the reliable `<target>/<profile>/deps` shape, so it wins.
        let args = RustcArgs {
            out_dir: Some(PathBuf::from("/work/proj/target/release/deps")),
            output: Some(PathBuf::from(
                "/work/proj/target/release/deps/libfoo-abc.rlib",
            )),
            ..Default::default()
        };
        assert_eq!(args.target_dir(), Some(PathBuf::from("/work/proj/target")));
    }

    #[test]
    fn test_target_dir_returns_none_for_ad_hoc_rustc() {
        // An ad-hoc `rustc -o /tmp/prog` has no cargo layout to anchor
        // to — return None so dep-info rewriting is skipped.
        let args = RustcArgs {
            output: Some(PathBuf::from("/tmp/somewhere/myprog")),
            ..Default::default()
        };
        assert_eq!(args.target_dir(), None);
    }

    #[test]
    fn test_target_dir_none_when_no_paths() {
        assert_eq!(RustcArgs::default().target_dir(), None);
    }

    #[test]
    fn dep_info_path_uses_cargo_output_stem() {
        let args = RustcArgs {
            crate_name: Some("serde".to_string()),
            extra_filename: Some("-abc123".to_string()),
            out_dir: Some(PathBuf::from("/work/proj/target/debug/deps")),
            emit: vec!["dep-info".to_string(), "metadata".to_string()],
            ..Default::default()
        };
        assert_eq!(
            args.dep_info_path(),
            Some(PathBuf::from("/work/proj/target/debug/deps/serde-abc123.d"))
        );
    }

    #[test]
    fn dep_info_path_skips_explicit_emit_path() {
        let args: Vec<String> = [
            "rustc",
            "--crate-name",
            "x",
            "src/lib.rs",
            "--emit=metadata,dep-info=custom/deps.mk",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(
            parsed.dep_info_output,
            Some(PathBuf::from("custom/deps.mk"))
        );
        assert_eq!(parsed.dep_info_path(), None);
    }

    #[test]
    fn dep_info_path_is_none_when_not_requested() {
        let args = RustcArgs {
            crate_name: Some("x".to_string()),
            out_dir: Some(PathBuf::from("/tmp/out")),
            emit: vec!["metadata".to_string()],
            ..Default::default()
        };
        assert_eq!(args.dep_info_path(), None);
    }

    #[test]
    fn dep_info_path_does_not_guess_direct_rustc_o_naming() {
        for emit in [vec!["dep-info"], vec!["dep-info", "link"]] {
            let args = RustcArgs {
                crate_name: Some("x".to_string()),
                output: Some(PathBuf::from("named.bin")),
                emit: emit.into_iter().map(str::to_string).collect(),
                ..Default::default()
            };
            assert_eq!(args.dep_info_path(), None);
        }
    }

    #[test]
    fn dep_info_path_skips_stdout_and_out_dir_plus_o_forms() {
        let stdout = RustcArgs {
            emit: vec!["dep-info".to_string()],
            dep_info_output: Some(PathBuf::from("-")),
            ..Default::default()
        };
        assert_eq!(stdout.dep_info_path(), None);

        let overridden = RustcArgs {
            crate_name: Some("x".to_string()),
            output: Some(PathBuf::from("named.bin")),
            out_dir: Some(PathBuf::from("out")),
            emit: vec!["dep-info".to_string(), "link".to_string()],
            ..Default::default()
        };
        assert_eq!(overridden.dep_info_path(), None);
    }

    #[test]
    fn parses_checksum_freshness_rustc_flag() {
        let args: Vec<String> = [
            "rustc",
            "src/lib.rs",
            "-Z",
            "checksum-hash-algorithm=blake3",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        assert!(
            RustcArgs::parse(&args)
                .unwrap()
                .checksum_freshness_enabled()
        );
    }

    #[test]
    fn test_target_dir_cross_compiling() {
        let args = RustcArgs {
            target: Some("aarch64-unknown-linux-gnu".to_string()),
            out_dir: Some(PathBuf::from(
                "/work/proj/target/aarch64-unknown-linux-gnu/debug/deps",
            )),
            ..Default::default()
        };
        assert_eq!(args.target_dir(), Some(PathBuf::from("/work/proj/target")));
    }

    #[test]
    fn test_workspace_root_cross_compiling() {
        let args = RustcArgs {
            target: Some("aarch64-unknown-linux-gnu".to_string()),
            out_dir: Some(PathBuf::from(
                "/work/proj/target/aarch64-unknown-linux-gnu/debug/deps",
            )),
            ..Default::default()
        };
        assert_eq!(args.workspace_root(), Some(PathBuf::from("/work/proj")));
    }

    #[test]
    fn verified_workspace_root_rejects_external_target_parents() {
        let dir = tempfile::tempdir().unwrap();
        let workspace = dir.path().join("workspace");
        let member = workspace.join("member");
        std::fs::create_dir_all(&member).unwrap();
        std::fs::write(workspace.join("Cargo.toml"), "[workspace]\n").unwrap();

        let local = RustcArgs {
            out_dir: Some(workspace.join("target/debug/deps")),
            ..Default::default()
        };
        assert_eq!(
            local.verified_workspace_root(&member),
            Some(workspace.clone())
        );
        assert_eq!(local.select_path_normalization_root(&member), workspace);

        let external = RustcArgs {
            out_dir: Some(dir.path().join("external/shared-target/debug/deps")),
            ..Default::default()
        };
        assert_eq!(external.verified_workspace_root(&member), None);
        assert_eq!(external.select_path_normalization_root(&member), member);
    }

    #[test]
    fn path_normalization_root_exposes_the_frozen_root() {
        // The key, the rustc invocation, and the dep-info rewrite all read this
        // one accessor. If it stops reporting the frozen root they silently
        // disagree about which anchor a source path is relative to, so a
        // relocated hit rewrites dep-info against the wrong tree.
        let dir = tempfile::tempdir().unwrap();
        let workspace = dir.path().join("workspace");
        let member = workspace.join("member");
        std::fs::create_dir_all(&member).unwrap();
        std::fs::write(workspace.join("Cargo.toml"), "[workspace]\n").unwrap();

        let mut args = RustcArgs {
            out_dir: Some(workspace.join("target/debug/deps")),
            ..Default::default()
        };
        assert_eq!(
            args.path_normalization_root(),
            None,
            "unset until the parse freezes it"
        );

        args.path_normalization_root = Some(args.select_path_normalization_root(&member));
        assert_eq!(
            args.path_normalization_root(),
            Some(workspace.as_path()),
            "the frozen workspace anchor must be readable back"
        );
    }

    #[test]
    fn test_build_script_probe_detected_from_probe_out_dir() {
        let args: Vec<String> = vec![
            "rustc",
            "--edition=2021",
            "--crate-name=proc_macro2",
            "--crate-type=lib",
            "--emit=dep-info,metadata",
            "--out-dir",
            "/work/proj/target/release/build/proc-macro2-abc/out/probe",
            "src/probe/proc_macro_span.rs",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();

        assert!(parsed.is_build_script_probe(Some(Path::new(
            "/work/proj/target/release/build/proc-macro2-abc/out"
        ))));
    }

    #[test]
    fn test_build_script_probe_detected_from_source_in_out_dir() {
        let args: Vec<String> = vec![
            "rustc",
            "--edition=2018",
            "--crate-name=anyhow_build",
            "--crate-type=lib",
            "--emit=metadata",
            "--out-dir",
            "/work/proj/target/release/build/anyhow-abc/out",
            "/work/proj/target/release/build/anyhow-abc/out/probe.rs",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();

        assert!(parsed.is_build_script_probe(Some(Path::new(
            "/work/proj/target/release/build/anyhow-abc/out"
        ))));
    }

    #[test]
    fn test_normal_cargo_compile_is_not_build_script_probe() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name=foo",
            "--crate-type=lib",
            "--emit=dep-info,metadata,link",
            "--out-dir",
            "/work/proj/target/release/deps",
            "src/lib.rs",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();

        assert!(!parsed.is_build_script_probe(Some(Path::new(
            "/work/proj/target/release/build/foo-abc/out"
        ))));
    }

    #[test]
    fn test_features_are_sorted() {
        let args: Vec<String> = vec![
            "rustc",
            "--crate-name",
            "foo",
            "src/lib.rs",
            "--cfg",
            "feature=\"std\"",
            "--cfg",
            "feature=\"alloc\"",
            "--cfg",
            "feature=\"derive\"",
        ]
        .into_iter()
        .map(String::from)
        .collect();
        let parsed = RustcArgs::parse(&args).unwrap();
        assert_eq!(parsed.features, vec!["alloc", "derive", "std"]);
    }
}