zccache 1.9.1

Local-first compiler cache for C/C++/Rust/Emscripten
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
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
//! Compilation context and cache key computation.
//!
//! The context key identifies a unique (source + flags) combination
//! and maps to an include list. The artifact key incorporates content
//! hashes of all files for artifact store lookup.

use std::path::Path;

use crate::core::path::normalize_for_key;
use crate::core::NormalizedPath;
use crate::hash::ContentHash;

use super::args::ParsedArgs;
use super::rustc_args::RustcParsedArgs;
use super::search_paths::IncludeSearchPaths;

/// blake3 hash identifying a (source + include_dirs + defines + flags) combination.
/// Same context key = same set of resolved headers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ContextKey(ContentHash);

impl ContextKey {
    /// Returns the underlying hash.
    #[must_use]
    pub fn hash(&self) -> &ContentHash {
        &self.0
    }

    /// Construct from raw 32-byte hash (for deserialization).
    #[must_use]
    pub fn from_raw(bytes: [u8; 32]) -> Self {
        Self(ContentHash::from_bytes(bytes))
    }
}

impl std::fmt::Display for ContextKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ctx:{}", self.0.to_hex())
    }
}

/// blake3 hash identifying a specific compilation output.
/// Same artifact key = the exact same `.o` file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ArtifactKey(ContentHash);

impl ArtifactKey {
    /// Returns the underlying hash.
    #[must_use]
    pub fn hash(&self) -> &ContentHash {
        &self.0
    }

    /// Construct from raw 32-byte hash (for deserialization).
    #[must_use]
    pub fn from_raw(bytes: [u8; 32]) -> Self {
        Self(ContentHash::from_bytes(bytes))
    }
}

impl std::fmt::Display for ArtifactKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "art:{}", self.0.to_hex())
    }
}

/// All inputs defining a compilation context.
#[derive(Debug, Clone)]
pub struct CompileContext {
    /// Absolute path to the source file.
    pub source_file: NormalizedPath,
    /// Ordered include search paths.
    pub include_search: IncludeSearchPaths,
    /// Sorted defines (-D flags).
    pub defines: Vec<String>,
    /// Sorted cache-relevant flags (-std, -O, -f, etc.).
    pub flags: Vec<String>,
    /// Force-included files (-include).
    pub force_includes: Vec<NormalizedPath>,
    /// Sorted unknown flags — not recognized by the parser but still
    /// affect compilation output, so they must be part of the cache key.
    pub unknown_flags: Vec<String>,
}

impl CompileContext {
    /// Build a `CompileContext` from parsed arguments (consumes the args to avoid cloning).
    #[must_use]
    pub fn from_parsed_args(args: ParsedArgs) -> Self {
        let mut defines = args.defines;
        defines.sort();
        let mut flags = args.flags;
        flags.sort();
        let mut unknown_flags = args.unknown_flags;
        unknown_flags.sort();

        Self {
            source_file: args.source_file,
            include_search: args.include_search,
            defines,
            flags,
            force_includes: args.force_includes,
            unknown_flags,
        }
    }

    /// Compute the context key.
    ///
    /// Includes: source file path, include dirs (in order), sorted defines,
    /// sorted flags, unknown flags, force includes.
    #[must_use]
    pub fn context_key(&self) -> ContextKey {
        compute_context_key(self, None)
    }
}

/// Reduce an `--extern name=path` value to its identity-bearing tail.
///
/// Cargo embeds a per-package `metadata=` hash in the file name (e.g.
/// `libserde-abc123.rmeta`), so the file name alone uniquely identifies the
/// extern. The directory prefix is incidental (changes per workspace
/// location, target dir, profile dir layout) and must NOT enter the cache key.
///
/// If the path has no file-name component (defensively — shouldn't happen for
/// real `--extern` values), fall back to the full string so we still hash
/// _something_ stable rather than silently collapsing distinct externs.
fn extern_path_key(path: &str) -> &str {
    Path::new(path)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(path)
}

fn normalize_key_path(path: &Path, key_root: Option<&Path>) -> String {
    if let Some(root) = key_root {
        if let Ok(stripped) = path.strip_prefix(root) {
            return normalize_for_key(stripped);
        }
    }

    normalize_for_key(path)
}

fn normalize_remap_path_prefix_for_key(remap: &str, key_root: Option<&Path>) -> String {
    let Some(root) = key_root else {
        return remap.to_string();
    };
    let Some((from, to)) = remap.split_once('=') else {
        return remap.to_string();
    };

    let from_path = Path::new(from);
    if from_path.strip_prefix(root).is_ok() {
        format!("{}={}", normalize_key_path(from_path, key_root), to)
    } else {
        remap.to_string()
    }
}

fn normalize_cxx_prefix_map_flag_for_key(flag: &str, key_root: Option<&Path>) -> String {
    const PREFIX_MAP_FLAGS: [&str; 5] = [
        "-ffile-prefix-map=",
        "-fdebug-prefix-map=",
        "-fmacro-prefix-map=",
        "-fcoverage-prefix-map=",
        "-fprofile-prefix-map=",
    ];

    for prefix in PREFIX_MAP_FLAGS {
        if let Some(remap) = flag.strip_prefix(prefix) {
            return format!(
                "{}{}",
                prefix,
                normalize_remap_path_prefix_for_key(remap, key_root)
            );
        }
    }

    flag.to_string()
}

/// Compute the context key for a C/C++ compilation context.
///
/// When `key_root` is provided, paths under that root are hashed relative to it
/// so equivalent workspaces can share cache keys across root-directory renames.
#[must_use]
pub fn compute_context_key(ctx: &CompileContext, key_root: Option<&Path>) -> ContextKey {
    let mut hasher = blake3::Hasher::new();

    hasher.update(b"zccache-context-key-v1\0");

    hasher.update(normalize_key_path(&ctx.source_file, key_root).as_bytes());
    hasher.update(b"\0");

    hasher.update(b"iquote\0");
    for dir in &ctx.include_search.iquote {
        hasher.update(normalize_key_path(dir, key_root).as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"user\0");
    for dir in &ctx.include_search.user {
        hasher.update(normalize_key_path(dir, key_root).as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"system\0");
    for dir in &ctx.include_search.system {
        hasher.update(normalize_key_path(dir, key_root).as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"after\0");
    for dir in &ctx.include_search.after {
        hasher.update(normalize_key_path(dir, key_root).as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"defines\0");
    for def in &ctx.defines {
        hasher.update(def.as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"flags\0");
    for flag in &ctx.flags {
        let flag = normalize_cxx_prefix_map_flag_for_key(flag, key_root);
        hasher.update(flag.as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"force-include\0");
    for fi in &ctx.force_includes {
        hasher.update(normalize_key_path(fi, key_root).as_bytes());
        hasher.update(b"\0");
    }

    hasher.update(b"unknown\0");
    for flag in &ctx.unknown_flags {
        let flag = normalize_cxx_prefix_map_flag_for_key(flag, key_root);
        hasher.update(flag.as_bytes());
        hasher.update(b"\0");
    }

    ContextKey(ContentHash::from_bytes(*hasher.finalize().as_bytes()))
}

/// Compute the artifact key from a context key and file content hashes.
///
/// The artifact key uniquely identifies a specific compilation output.
/// `file_hashes` should contain `(path, content_hash)` pairs for the
/// source file and all resolved headers, sorted by path.
pub fn compute_artifact_key<P: AsRef<Path> + Ord>(
    context_key: &ContextKey,
    file_hashes: &mut [(P, ContentHash)],
    key_root: Option<&Path>,
) -> ArtifactKey {
    // Sort by path for determinism.
    file_hashes.sort_by(|a, b| a.0.cmp(&b.0));

    let mut hasher = blake3::Hasher::new();
    hasher.update(b"zccache-artifact-key-v1\0");
    hasher.update(context_key.0.as_bytes());
    hasher.update(b"\0");

    for (path, hash) in file_hashes.iter() {
        let path = normalize_key_path(path.as_ref(), key_root);
        hasher.update(path.as_bytes());
        hasher.update(b"\0");
        hasher.update(hash.as_bytes());
        hasher.update(b"\0");
    }

    ArtifactKey(ContentHash::from_bytes(*hasher.finalize().as_bytes()))
}

/// CARGO_* environment variables that must NOT participate in the cache key.
///
/// These are volatile (absolute paths or build-host transients) and either do
/// not affect compiled output or affect it only via paths that should already
/// be normalized elsewhere. Including them cascades cache invalidation across
/// the entire dep graph whenever the workspace is moved, cloned, or re-checked
/// out at a different on-disk location.
///
/// What stays in the key (everything else starting with `CARGO_`):
/// - `CARGO_PKG_VERSION`, `CARGO_PKG_NAME`, `CARGO_PKG_AUTHORS`,
///   `CARGO_PKG_DESCRIPTION`, `CARGO_PKG_HOMEPAGE`, `CARGO_PKG_REPOSITORY`,
///   `CARGO_PKG_LICENSE`, `CARGO_PKG_RUST_VERSION`, `CARGO_CRATE_NAME`, etc.
///   These feed `env!()` macros and are baked into the compiled artifact.
///
/// Already excluded earlier in the filter (orthogonal reasons):
/// - `CARGO_MAKEFLAGS` (job-server token, transient).
/// - `CARGO_INCREMENTAL` (handled by stripping `-C incremental` from args).
///
/// Filtered here (this list):
/// - `CARGO_MANIFEST_DIR` — absolute path to the crate dir; changes per
///   checkout location. Cascades the cache.
/// - `CARGO_MANIFEST_PATH` — absolute path to `Cargo.toml`; same issue.
const VOLATILE_CARGO_ENV_VARS: &[&str] = &["CARGO_MANIFEST_DIR", "CARGO_MANIFEST_PATH"];

/// All inputs defining a rustc compilation context.
///
/// Separate from `CompileContext` because Rust's compilation model differs
/// fundamentally from C/C++: no include paths, `--cfg` instead of `-D`,
/// `--extern` crates instead of headers, etc.
#[derive(Debug, Clone)]
pub struct RustcCompileContext {
    /// Absolute path to the source file.
    pub source_file: NormalizedPath,
    /// `--crate-name` value.
    pub crate_name: Option<String>,
    /// Sorted `--crate-type` values.
    pub crate_types: Vec<String>,
    /// `--edition` value.
    pub edition: Option<String>,
    /// Sorted `--emit` types.
    pub emit_types: Vec<String>,
    /// Sorted `--cfg` values.
    pub cfgs: Vec<String>,
    /// Sorted `--check-cfg` values.
    pub check_cfgs: Vec<String>,
    /// Sorted cache-relevant `-C` codegen options.
    pub codegen_flags: Vec<String>,
    /// Cargo's `-C metadata=` disambiguator for this compilation unit.
    pub cargo_metadata: Option<String>,
    /// Cargo's `-C extra-filename=` suffix for output artifact names.
    pub extra_filename: Option<String>,
    /// `--target` triple.
    pub target: Option<String>,
    /// `--cap-lints` value.
    pub cap_lints: Option<String>,
    /// Extern crate `(name, path)` pairs, sorted. Paths included so that
    /// `--extern a=v1.rlib` and `--extern a=v2.rlib` get different context keys.
    pub extern_crates: Vec<(String, String)>,
    /// Sorted lint flags (`-A`, `-W`, `-D`, `-F`).
    pub lint_flags: Vec<String>,
    /// Sorted unknown flags.
    pub unknown_flags: Vec<String>,
    /// Sorted `--remap-path-prefix` values (affect embedded paths in output).
    pub remap_path_prefixes: Vec<String>,
    /// Sorted CARGO_* environment variables that affect compilation via `env!()`.
    pub env_vars: Vec<(String, String)>,
    /// Hash of the compiler binary (different rustc versions produce different output).
    pub compiler_hash: Option<ContentHash>,
}

impl RustcCompileContext {
    /// Build from parsed rustc args and client environment.
    ///
    /// `client_env` should be the CARGO_* env vars from the client process.
    /// These affect compilation via `env!()` macros and must be in the cache key.
    #[must_use]
    pub fn from_parsed_args(
        args: &RustcParsedArgs,
        client_env: &[(String, String)],
        compiler_hash: Option<ContentHash>,
    ) -> Self {
        let mut crate_types = args.crate_types.clone();
        crate_types.sort();
        let mut emit_types = args.emit_types.clone();
        emit_types.sort();
        let mut extern_crates: Vec<(String, String)> = args
            .externs
            .iter()
            .map(|e| (e.name.clone(), e.path.to_string_lossy().into_owned()))
            .collect();
        extern_crates.sort();
        let mut remap_path_prefixes = args.remap_path_prefixes.clone();
        remap_path_prefixes.sort();

        // Filter CARGO_* env vars — these affect compilation output via env!() macro.
        // Exclude CARGO_MAKEFLAGS (job server, not output-affecting),
        // CARGO_INCREMENTAL (handled by stripping -C incremental), and
        // VOLATILE_CARGO_ENV_VARS (absolute paths that cascade cache misses).
        let mut env_vars: Vec<(String, String)> = client_env
            .iter()
            .filter(|(k, _)| {
                k.starts_with("CARGO_")
                    && k != "CARGO_MAKEFLAGS"
                    && k != "CARGO_INCREMENTAL"
                    && !VOLATILE_CARGO_ENV_VARS.contains(&k.as_str())
            })
            .cloned()
            .collect();
        env_vars.sort();

        Self {
            source_file: args.source_file.clone(),
            crate_name: args.crate_name.clone(),
            crate_types,
            edition: args.edition.clone(),
            emit_types,
            cfgs: args.cfgs.clone(),
            check_cfgs: args.check_cfgs.clone(),
            codegen_flags: args.codegen_flags.clone(),
            cargo_metadata: args.cargo_metadata.clone(),
            extra_filename: args.extra_filename.clone(),
            target: args.target.clone(),
            cap_lints: args.cap_lints.clone(),
            extern_crates,
            lint_flags: args.lint_flags.clone(),
            unknown_flags: args.unknown_flags.clone(),
            remap_path_prefixes,
            env_vars,
            compiler_hash,
        }
    }

    /// Compute the context key.
    ///
    /// Uses a different domain tag from C/C++ to avoid collisions.
    #[must_use]
    pub fn context_key(&self) -> ContextKey {
        self.context_key_with_root(None)
    }

    /// Compute the context key, optionally normalizing project-local paths.
    ///
    /// When `key_root` is provided, source paths and safe path-bearing key
    /// fields under that root are hashed relative to it so equivalent
    /// workspaces can share cache keys across root-directory renames.
    #[must_use]
    pub fn context_key_with_root(&self, key_root: Option<&Path>) -> ContextKey {
        let mut hasher = blake3::Hasher::new();

        hasher.update(b"zccache-rustc-context-key-v2\0");

        // Compiler binary hash (different rustc versions -> different output).
        if let Some(ref ch) = self.compiler_hash {
            hasher.update(b"compiler\0");
            hasher.update(ch.as_bytes());
            hasher.update(b"\0");
        }

        // Source file.
        let source_file = normalize_key_path(&self.source_file, key_root);
        hasher.update(source_file.as_bytes());
        hasher.update(b"\0");

        // Crate name.
        if let Some(ref name) = self.crate_name {
            hasher.update(b"crate-name\0");
            hasher.update(name.as_bytes());
            hasher.update(b"\0");
        }

        // Crate types (sorted).
        hasher.update(b"crate-types\0");
        for ct in &self.crate_types {
            hasher.update(ct.as_bytes());
            hasher.update(b"\0");
        }

        // Edition.
        if let Some(ref edition) = self.edition {
            hasher.update(b"edition\0");
            hasher.update(edition.as_bytes());
            hasher.update(b"\0");
        }

        // Emit types (sorted).
        hasher.update(b"emit\0");
        for et in &self.emit_types {
            hasher.update(et.as_bytes());
            hasher.update(b"\0");
        }

        // Cfg values (sorted).
        hasher.update(b"cfg\0");
        for cfg in &self.cfgs {
            hasher.update(cfg.as_bytes());
            hasher.update(b"\0");
        }

        // Check-cfg values (sorted).
        hasher.update(b"check-cfg\0");
        for cfg in &self.check_cfgs {
            hasher.update(cfg.as_bytes());
            hasher.update(b"\0");
        }

        // Codegen flags (sorted).
        hasher.update(b"codegen\0");
        for flag in &self.codegen_flags {
            hasher.update(flag.as_bytes());
            hasher.update(b"\0");
        }

        if let Some(ref metadata) = self.cargo_metadata {
            hasher.update(b"cargo-metadata\0");
            hasher.update(metadata.as_bytes());
            hasher.update(b"\0");
        }

        if let Some(ref extra_filename) = self.extra_filename {
            hasher.update(b"extra-filename\0");
            hasher.update(extra_filename.as_bytes());
            hasher.update(b"\0");
        }

        // Target.
        if let Some(ref target) = self.target {
            hasher.update(b"target\0");
            hasher.update(target.as_bytes());
            hasher.update(b"\0");
        }

        // Cap lints.
        if let Some(ref cap) = self.cap_lints {
            hasher.update(b"cap-lints\0");
            hasher.update(cap.as_bytes());
            hasher.update(b"\0");
        }

        // Extern crate (name, path) pairs - hash only the file name component,
        // not the absolute directory prefix. The file name carries cargo's
        // per-package `metadata=` hash (e.g. `libserde-abc123.rmeta`), which
        // uniquely identifies the extern's identity. Including the directory
        // prefix would cascade cache misses across workspace clones / renames
        // (issue #139, fix #1). Different `--extern a=v1.rmeta` vs
        // `--extern a=v2.rmeta` still get different keys because the metadata
        // suffix is part of the file name.
        hasher.update(b"externs\0");
        for (name, path) in &self.extern_crates {
            hasher.update(name.as_bytes());
            hasher.update(b"=");
            hasher.update(extern_path_key(path).as_bytes());
            hasher.update(b"\0");
        }

        // Lint flags (sorted).
        hasher.update(b"lints\0");
        for flag in &self.lint_flags {
            hasher.update(flag.as_bytes());
            hasher.update(b"\0");
        }

        // Unknown flags (sorted).
        hasher.update(b"unknown\0");
        for flag in &self.unknown_flags {
            hasher.update(flag.as_bytes());
            hasher.update(b"\0");
        }

        // --remap-path-prefix values (sorted, affect embedded paths in output).
        hasher.update(b"remap\0");
        if key_root.is_some() {
            let mut remap_path_prefixes: Vec<String> = self
                .remap_path_prefixes
                .iter()
                .map(|remap| normalize_remap_path_prefix_for_key(remap, key_root))
                .collect();
            remap_path_prefixes.sort();
            for remap in &remap_path_prefixes {
                hasher.update(remap.as_bytes());
                hasher.update(b"\0");
            }
        } else {
            for remap in &self.remap_path_prefixes {
                hasher.update(remap.as_bytes());
                hasher.update(b"\0");
            }
        }

        // CARGO_* environment variables (sorted, affect env!() macro output).
        //
        // Defense-in-depth: we ALSO filter VOLATILE_CARGO_ENV_VARS here, not
        // only in `from_parsed_args`. The struct is public and may be built
        // directly (in tests or by future call sites). Hashing must be the
        // single source of truth on what counts. See `VOLATILE_CARGO_ENV_VARS`
        // for the rationale (issue #139).
        hasher.update(b"env\0");
        for (key, val) in &self.env_vars {
            if VOLATILE_CARGO_ENV_VARS.contains(&key.as_str()) {
                continue;
            }
            hasher.update(key.as_bytes());
            hasher.update(b"=");
            hasher.update(val.as_bytes());
            hasher.update(b"\0");
        }

        ContextKey(ContentHash::from_bytes(*hasher.finalize().as_bytes()))
    }
}

/// Compute the artifact key for a rustc compilation.
///
/// Like `compute_artifact_key` for C/C++, but also incorporates
/// extern crate content hashes (analogous to header content hashes).
pub fn compute_rustc_artifact_key<P: AsRef<Path> + Ord>(
    context_key: &ContextKey,
    file_hashes: &mut [(P, ContentHash)],
    extern_hashes: &mut [(String, ContentHash)],
) -> ArtifactKey {
    compute_rustc_artifact_key_with_root(context_key, file_hashes, extern_hashes, None)
}

/// Compute the rustc artifact key, optionally normalizing project-local files.
///
/// When `key_root` is provided, source and dependency file paths under that
/// root are hashed relative to it. Extern hashes remain keyed by crate name.
pub fn compute_rustc_artifact_key_with_root<P: AsRef<Path> + Ord>(
    context_key: &ContextKey,
    file_hashes: &mut [(P, ContentHash)],
    extern_hashes: &mut [(String, ContentHash)],
    key_root: Option<&Path>,
) -> ArtifactKey {
    if key_root.is_some() {
        file_hashes.sort_by(|a, b| {
            normalize_key_path(a.0.as_ref(), key_root)
                .cmp(&normalize_key_path(b.0.as_ref(), key_root))
        });
    } else {
        file_hashes.sort_by(|a, b| a.0.cmp(&b.0));
    }
    extern_hashes.sort_by(|a, b| a.0.cmp(&b.0));

    let mut hasher = blake3::Hasher::new();
    hasher.update(b"zccache-rustc-artifact-key-v1\0");
    hasher.update(context_key.0.as_bytes());
    hasher.update(b"\0");

    // Source + dependency file hashes.
    for (path, hash) in file_hashes.iter() {
        let path = normalize_key_path(path.as_ref(), key_root);
        hasher.update(path.as_bytes());
        hasher.update(b"\0");
        hasher.update(hash.as_bytes());
        hasher.update(b"\0");
    }

    // Extern crate content hashes.
    hasher.update(b"externs\0");
    for (name, hash) in extern_hashes.iter() {
        hasher.update(name.as_bytes());
        hasher.update(b"\0");
        hasher.update(hash.as_bytes());
        hasher.update(b"\0");
    }

    ArtifactKey(ContentHash::from_bytes(*hasher.finalize().as_bytes()))
}

#[cfg(test)]
mod tests {
    use crate::core::NormalizedPath;

    use super::super::args::UserDepFlags;
    use super::super::rustc_args::ExternCrate;
    use super::*;

    fn make_context(source: &str, user_dirs: &[&str], defines: &[&str]) -> CompileContext {
        CompileContext {
            source_file: NormalizedPath::from(source),
            include_search: IncludeSearchPaths {
                user: user_dirs
                    .iter()
                    .map(|dir| NormalizedPath::from(*dir))
                    .collect(),
                ..Default::default()
            },
            defines: {
                let mut d: Vec<String> = defines.iter().map(|s| s.to_string()).collect();
                d.sort();
                d
            },
            flags: Vec::new(),
            force_includes: Vec::new(),
            unknown_flags: Vec::new(),
        }
    }

    #[test]
    fn context_key_deterministic() {
        let ctx = make_context("/src/foo.c", &["/inc"], &["DEBUG"]);
        let k1 = ctx.context_key();
        let k2 = ctx.context_key();
        assert_eq!(k1, k2);
    }

    #[test]
    fn different_source_different_key() {
        let k1 = make_context("/src/a.c", &["/inc"], &[]).context_key();
        let k2 = make_context("/src/b.c", &["/inc"], &[]).context_key();
        assert_ne!(k1, k2);
    }

    #[test]
    fn different_defines_different_key() {
        let k1 = make_context("/src/a.c", &["/inc"], &["DEBUG"]).context_key();
        let k2 = make_context("/src/a.c", &["/inc"], &["RELEASE"]).context_key();
        assert_ne!(k1, k2);
    }

    #[test]
    fn define_order_irrelevant() {
        let k1 = make_context("/src/a.c", &[], &["AAA", "BBB"]).context_key();
        let k2 = make_context("/src/a.c", &[], &["BBB", "AAA"]).context_key();
        assert_eq!(k1, k2, "define order should not affect context key");
    }

    #[test]
    fn include_dir_order_matters() {
        let k1 = make_context("/src/a.c", &["/first", "/second"], &[]).context_key();
        let k2 = make_context("/src/a.c", &["/second", "/first"], &[]).context_key();
        assert_ne!(k1, k2, "include dir order should affect context key");
    }

    #[cfg(windows)]
    #[test]
    fn windows_context_key_normalizes_equivalent_path_spellings() {
        let ctx1 = CompileContext {
            source_file: NormalizedPath::from(r"C:\work\src\main.cpp"),
            include_search: IncludeSearchPaths {
                user: vec![NormalizedPath::from(r"C:\work\include")],
                ..Default::default()
            },
            defines: Vec::new(),
            flags: Vec::new(),
            force_includes: vec![NormalizedPath::from(r"C:\work\pch\base.h")],
            unknown_flags: Vec::new(),
        };
        let ctx2 = CompileContext {
            source_file: NormalizedPath::from("c:/work/src/main.cpp"),
            include_search: IncludeSearchPaths {
                user: vec![NormalizedPath::from("c:/work/include")],
                ..Default::default()
            },
            defines: Vec::new(),
            flags: Vec::new(),
            force_includes: vec![NormalizedPath::from("c:/work/pch/base.h")],
            unknown_flags: Vec::new(),
        };

        assert_eq!(ctx1.context_key(), ctx2.context_key());
    }

    #[cfg(windows)]
    #[test]
    fn windows_artifact_key_normalizes_equivalent_path_spellings() {
        let ctx = CompileContext {
            source_file: NormalizedPath::from(r"C:\work\src\main.cpp"),
            include_search: IncludeSearchPaths::default(),
            defines: Vec::new(),
            flags: Vec::new(),
            force_includes: Vec::new(),
            unknown_flags: Vec::new(),
        };
        let key = ctx.context_key();

        let mut file_hashes_a = vec![
            (
                NormalizedPath::from(r"C:\work\include\foo.h"),
                crate::hash::hash_bytes(b"header"),
            ),
            (
                NormalizedPath::from(r"C:\work\src\main.cpp"),
                crate::hash::hash_bytes(b"source"),
            ),
        ];
        let mut file_hashes_b = vec![
            (
                NormalizedPath::from("c:/work/include/foo.h"),
                crate::hash::hash_bytes(b"header"),
            ),
            (
                NormalizedPath::from("c:/work/src/main.cpp"),
                crate::hash::hash_bytes(b"source"),
            ),
        ];

        assert_eq!(
            compute_artifact_key(&key, &mut file_hashes_a, None),
            compute_artifact_key(&key, &mut file_hashes_b, None)
        );
    }

    #[cfg(windows)]
    #[test]
    fn windows_rustc_context_key_normalizes_equivalent_source_path_spellings() {
        let ctx1 = RustcCompileContext {
            source_file: NormalizedPath::from(r"C:\work\src\lib.rs"),
            crate_name: Some("demo".to_string()),
            crate_types: vec!["rlib".to_string()],
            edition: Some("2021".to_string()),
            emit_types: vec!["link".to_string()],
            cfgs: Vec::new(),
            check_cfgs: Vec::new(),
            codegen_flags: Vec::new(),
            cargo_metadata: None,
            extra_filename: None,
            target: None,
            cap_lints: None,
            extern_crates: Vec::new(),
            lint_flags: Vec::new(),
            unknown_flags: Vec::new(),
            remap_path_prefixes: Vec::new(),
            env_vars: Vec::new(),
            compiler_hash: None,
        };
        let mut ctx2 = ctx1.clone();
        ctx2.source_file = NormalizedPath::from("c:/work/src/lib.rs");

        assert_eq!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn artifact_key_changes_with_content() {
        let ctx = make_context("/src/a.c", &[], &[]);
        let ck = ctx.context_key();

        let hash_a = crate::hash::hash_bytes(b"content A");
        let hash_b = crate::hash::hash_bytes(b"content B");

        let ak1 =
            compute_artifact_key(&ck, &mut [(NormalizedPath::from("/src/a.c"), hash_a)], None);
        let ak2 =
            compute_artifact_key(&ck, &mut [(NormalizedPath::from("/src/a.c"), hash_b)], None);
        assert_ne!(ak1, ak2);
    }

    #[test]
    fn artifact_key_stable_same_content() {
        let ctx = make_context("/src/a.c", &[], &[]);
        let ck = ctx.context_key();

        let hash = crate::hash::hash_bytes(b"content");

        let ak1 = compute_artifact_key(&ck, &mut [(NormalizedPath::from("/src/a.c"), hash)], None);
        let ak2 = compute_artifact_key(&ck, &mut [(NormalizedPath::from("/src/a.c"), hash)], None);
        assert_eq!(ak1, ak2);
    }

    #[test]
    fn artifact_key_file_order_irrelevant() {
        let ctx = make_context("/src/a.c", &[], &[]);
        let ck = ctx.context_key();

        let h1 = crate::hash::hash_bytes(b"content 1");
        let h2 = crate::hash::hash_bytes(b"content 2");

        let ak1 = compute_artifact_key(
            &ck,
            &mut [
                (NormalizedPath::from("/a.h"), h1),
                (NormalizedPath::from("/b.h"), h2),
            ],
            None,
        );
        let ak2 = compute_artifact_key(
            &ck,
            &mut [
                (NormalizedPath::from("/b.h"), h2),
                (NormalizedPath::from("/a.h"), h1),
            ],
            None,
        );
        assert_eq!(ak1, ak2, "file order should not affect artifact key");
    }

    #[test]
    fn context_key_ignores_workspace_root_when_key_root_is_stable() {
        let ctx_a = make_context(
            "/workspace-a/src/main.cpp",
            &["/workspace-a/include"],
            &["DEBUG"],
        );
        let ctx_b = make_context(
            "/workspace-b/src/main.cpp",
            &["/workspace-b/include"],
            &["DEBUG"],
        );

        let key_a = compute_context_key(&ctx_a, Some(Path::new("/workspace-a")));
        let key_b = compute_context_key(&ctx_b, Some(Path::new("/workspace-b")));

        assert_eq!(key_a, key_b);
    }

    #[test]
    fn cxx_context_key_with_root_normalizes_file_prefix_map_roots() {
        let mut ctx_a = make_context("/workspace-a/src/main.cpp", &[], &[]);
        ctx_a.flags = vec!["-ffile-prefix-map=/workspace-a=.".to_string()];
        let mut ctx_b = make_context("/workspace-b/src/main.cpp", &[], &[]);
        ctx_b.flags = vec!["-ffile-prefix-map=/workspace-b=.".to_string()];

        assert_eq!(
            compute_context_key(&ctx_a, Some(Path::new("/workspace-a"))),
            compute_context_key(&ctx_b, Some(Path::new("/workspace-b"))),
            "equivalent file-prefix-map old prefixes under the key root should match"
        );
    }

    #[test]
    fn cxx_context_key_with_root_preserves_file_prefix_map_new_prefixes() {
        let mut ctx_a = make_context("/workspace-a/src/main.cpp", &[], &[]);
        ctx_a.flags = vec!["-ffile-prefix-map=/workspace-a=.".to_string()];
        let mut ctx_b = make_context("/workspace-b/src/main.cpp", &[], &[]);
        ctx_b.flags = vec!["-ffile-prefix-map=/workspace-b=/src".to_string()];

        assert_ne!(
            compute_context_key(&ctx_a, Some(Path::new("/workspace-a"))),
            compute_context_key(&ctx_b, Some(Path::new("/workspace-b"))),
            "different file-prefix-map new prefixes should remain key-significant"
        );
    }

    #[test]
    fn cxx_context_key_with_root_keeps_external_file_prefix_map_old_prefixes_distinct() {
        let mut ctx_a = make_context("/workspace-a/src/main.cpp", &[], &[]);
        ctx_a.flags = vec!["-ffile-prefix-map=/external-a=.".to_string()];
        let mut ctx_b = make_context("/workspace-b/src/main.cpp", &[], &[]);
        ctx_b.flags = vec!["-ffile-prefix-map=/external-b=.".to_string()];

        assert_ne!(
            compute_context_key(&ctx_a, Some(Path::new("/workspace-a"))),
            compute_context_key(&ctx_b, Some(Path::new("/workspace-b"))),
            "file-prefix-map old prefixes outside the key root should keep absolute identity"
        );
    }

    #[test]
    fn cxx_context_key_with_root_normalizes_prefix_maps_in_unknown_flags() {
        let mut ctx_a = make_context("/workspace-a/src/main.cpp", &[], &[]);
        ctx_a.unknown_flags = vec![
            "-fcoverage-prefix-map=/workspace-a=/coverage".to_string(),
            "-fdebug-prefix-map=/workspace-a=/debug".to_string(),
            "-fmacro-prefix-map=/workspace-a=/macro".to_string(),
            "-fprofile-prefix-map=/workspace-a=/profile".to_string(),
        ];
        let mut ctx_b = make_context("/workspace-b/src/main.cpp", &[], &[]);
        ctx_b.unknown_flags = vec![
            "-fcoverage-prefix-map=/workspace-b=/coverage".to_string(),
            "-fdebug-prefix-map=/workspace-b=/debug".to_string(),
            "-fmacro-prefix-map=/workspace-b=/macro".to_string(),
            "-fprofile-prefix-map=/workspace-b=/profile".to_string(),
        ];

        assert_eq!(
            compute_context_key(&ctx_a, Some(Path::new("/workspace-a"))),
            compute_context_key(&ctx_b, Some(Path::new("/workspace-b"))),
            "C/C++ prefix-map flags should normalize under unknown_flags"
        );
    }

    #[test]
    fn artifact_key_ignores_workspace_root_when_key_root_is_stable() {
        let ctx = make_context("/workspace-a/src/main.cpp", &["/workspace-a/include"], &[]);
        let key = compute_context_key(&ctx, Some(Path::new("/workspace-a")));
        let mut hashes_a = vec![
            (
                NormalizedPath::from("/workspace-a/include/foo.h"),
                crate::hash::hash_bytes(b"header"),
            ),
            (
                NormalizedPath::from("/workspace-a/src/main.cpp"),
                crate::hash::hash_bytes(b"source"),
            ),
        ];
        let mut hashes_b = vec![
            (
                NormalizedPath::from("/workspace-b/include/foo.h"),
                crate::hash::hash_bytes(b"header"),
            ),
            (
                NormalizedPath::from("/workspace-b/src/main.cpp"),
                crate::hash::hash_bytes(b"source"),
            ),
        ];

        assert_eq!(
            compute_artifact_key(&key, &mut hashes_a, Some(Path::new("/workspace-a"))),
            compute_artifact_key(&key, &mut hashes_b, Some(Path::new("/workspace-b")))
        );
    }

    #[test]
    fn context_key_display() {
        let ctx = make_context("/src/a.c", &[], &[]);
        let key = ctx.context_key();
        let display = format!("{key}");
        assert!(display.starts_with("ctx:"));
        assert_eq!(display.len(), 4 + 64); // "ctx:" + 64 hex chars
    }

    #[test]
    fn from_parsed_args_sorts() {
        let args = ParsedArgs {
            source_file: NormalizedPath::from("/src/a.c"),
            output_file: None,
            include_search: IncludeSearchPaths::default(),
            defines: vec!["ZZZ".into(), "AAA".into()],
            undefines: Vec::new(),
            flags: vec!["-Wall".into(), "-O2".into()],
            force_includes: Vec::new(),
            compiler: None,
            dep_flags: UserDepFlags::default(),
            unknown_flags: vec!["--zzz".into(), "--aaa".into()],
        };
        let ctx = CompileContext::from_parsed_args(args);
        assert_eq!(ctx.defines, vec!["AAA", "ZZZ"]);
        assert_eq!(ctx.flags, vec!["-O2", "-Wall"]);
        assert_eq!(ctx.unknown_flags, vec!["--aaa", "--zzz"]);
    }

    #[test]
    fn different_flags_different_key() {
        let mut ctx1 = make_context("/src/a.c", &[], &[]);
        ctx1.flags = vec!["-std=c++17".into()];
        let mut ctx2 = make_context("/src/a.c", &[], &[]);
        ctx2.flags = vec!["-std=c++20".into()];
        assert_ne!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn force_include_affects_key() {
        let ctx1 = make_context("/src/a.c", &[], &[]);
        let mut ctx2 = make_context("/src/a.c", &[], &[]);
        ctx2.force_includes = vec![NormalizedPath::from("/pch.h")];
        assert_ne!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn unknown_flags_affect_key() {
        let ctx1 = make_context("/src/a.c", &[], &[]);
        let mut ctx2 = make_context("/src/a.c", &[], &[]);
        ctx2.unknown_flags = vec!["--deploy-dependencies".into()];
        assert_ne!(
            ctx1.context_key(),
            ctx2.context_key(),
            "unknown flags should affect context key"
        );
    }

    #[test]
    fn unknown_flags_order_irrelevant() {
        let mut ctx1 = make_context("/src/a.c", &[], &[]);
        ctx1.unknown_flags = vec!["--aaa".into(), "--bbb".into()];
        let mut ctx2 = make_context("/src/a.c", &[], &[]);
        ctx2.unknown_flags = vec!["--bbb".into(), "--aaa".into()];
        // Both are sorted in make_context... but actually make_context doesn't sort unknown_flags.
        // from_parsed_args sorts them. In the test helper we set them directly,
        // so we need to sort manually for this test to be meaningful.
        ctx1.unknown_flags.sort();
        ctx2.unknown_flags.sort();
        assert_eq!(
            ctx1.context_key(),
            ctx2.context_key(),
            "unknown flag order should not affect context key"
        );
    }

    // ─── RustcCompileContext tests ───────────────────────────────────

    fn make_rustc_context(source: &str, edition: &str) -> RustcCompileContext {
        RustcCompileContext {
            source_file: NormalizedPath::from(source),
            crate_name: Some("mylib".to_string()),
            crate_types: vec!["lib".to_string()],
            edition: Some(edition.to_string()),
            emit_types: vec!["link".to_string()],
            cfgs: Vec::new(),
            check_cfgs: Vec::new(),
            codegen_flags: Vec::new(),
            cargo_metadata: None,
            extra_filename: None,
            target: None,
            cap_lints: None,
            extern_crates: Vec::new(),
            lint_flags: Vec::new(),
            unknown_flags: Vec::new(),
            remap_path_prefixes: Vec::new(),
            env_vars: Vec::new(),
            compiler_hash: None,
        }
    }

    #[test]
    fn rustc_context_key_deterministic() {
        let ctx = make_rustc_context("/src/lib.rs", "2021");
        let k1 = ctx.context_key();
        let k2 = ctx.context_key();
        assert_eq!(k1, k2);
    }

    #[test]
    fn rustc_context_key_delegates_to_rootless_helper() {
        let ctx = make_rustc_context("/src/lib.rs", "2021");
        assert_eq!(ctx.context_key(), ctx.context_key_with_root(None));
    }

    #[test]
    fn rustc_context_key_with_root_matches_equivalent_roots() {
        let ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        let ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");

        assert_ne!(
            ctx_a.context_key(),
            ctx_b.context_key(),
            "rootless rustc context keys should keep the existing absolute-path behavior"
        );
        assert_eq!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "source paths under equivalent roots should hash relative to those roots"
        );
    }

    #[test]
    fn rustc_context_key_with_root_keeps_external_sources_distinct() {
        let ctx_a = make_rustc_context("/external-a/generated/lib.rs", "2021");
        let ctx_b = make_rustc_context("/external-b/generated/lib.rs", "2021");

        assert_ne!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "sources outside the supplied roots must retain absolute path identity"
        );
    }

    #[test]
    fn rustc_context_key_with_root_normalizes_remap_left_side_under_root() {
        let mut ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        ctx_a.remap_path_prefixes = vec!["/workspace-a=/src".to_string()];
        let mut ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        ctx_b.remap_path_prefixes = vec!["/workspace-b=/src".to_string()];

        assert_eq!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "remap left sides under the root should hash relative to the root"
        );
    }

    #[test]
    fn rustc_context_key_with_root_normalizes_root_remap_left_side() {
        let mut ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        ctx_a.remap_path_prefixes = vec!["/workspace-a=.".to_string()];
        let mut ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        ctx_b.remap_path_prefixes = vec!["/workspace-b=.".to_string()];

        assert_eq!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "root-covering remaps should hash equivalently across roots"
        );
    }

    #[test]
    fn rustc_context_key_with_root_keeps_external_remap_left_sides_distinct() {
        let mut ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        ctx_a.remap_path_prefixes = vec!["/external-a=/src".to_string()];
        let mut ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        ctx_b.remap_path_prefixes = vec!["/external-b=/src".to_string()];

        assert_ne!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "remap left sides outside the root should keep absolute path identity"
        );
    }

    #[test]
    fn rustc_context_key_with_root_does_not_normalize_remap_right_side() {
        let mut ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        ctx_a.remap_path_prefixes = vec!["/workspace-a=/workspace-a".to_string()];
        let mut ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        ctx_b.remap_path_prefixes = vec!["/workspace-b=/workspace-b".to_string()];

        assert_ne!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "only the remap left side is root-normalized"
        );
    }

    #[test]
    fn rustc_context_key_with_root_preserves_remap_right_side() {
        let mut ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        ctx_a.remap_path_prefixes = vec!["/workspace-a=.".to_string()];
        let mut ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        ctx_b.remap_path_prefixes = vec!["/workspace-b=/src".to_string()];

        assert_ne!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "different remap new prefixes must remain cache-significant"
        );
    }

    #[test]
    fn rustc_context_key_with_root_keeps_malformed_remaps_distinct() {
        let mut ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        ctx_a.remap_path_prefixes = vec!["/workspace-a".to_string()];
        let mut ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        ctx_b.remap_path_prefixes = vec!["/workspace-b".to_string()];

        assert_ne!(
            ctx_a.context_key_with_root(Some(Path::new("/workspace-a"))),
            ctx_b.context_key_with_root(Some(Path::new("/workspace-b"))),
            "malformed remap values should not be root-normalized"
        );
    }

    #[test]
    fn rustc_different_edition_different_key() {
        let k1 = make_rustc_context("/src/lib.rs", "2021").context_key();
        let k2 = make_rustc_context("/src/lib.rs", "2024").context_key();
        assert_ne!(k1, k2);
    }

    #[test]
    fn rustc_different_cfg_different_key() {
        let mut ctx1 = make_rustc_context("/src/lib.rs", "2021");
        ctx1.cfgs = vec!["feature=\"std\"".to_string()];
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.cfgs = vec!["feature=\"alloc\"".to_string()];
        assert_ne!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn rustc_different_codegen_different_key() {
        let mut ctx1 = make_rustc_context("/src/lib.rs", "2021");
        ctx1.codegen_flags = vec!["opt-level=2".to_string()];
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.codegen_flags = vec!["opt-level=3".to_string()];
        assert_ne!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn rustc_cargo_metadata_affects_key() {
        let mut ctx1 = make_rustc_context("/src/lib.rs", "2021");
        ctx1.cargo_metadata = Some("worktree-a".to_string());
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.cargo_metadata = Some("worktree-b".to_string());
        assert_ne!(
            ctx1.context_key(),
            ctx2.context_key(),
            "-C metadata participates in crate disambiguation and must affect the key"
        );
    }

    #[test]
    fn rustc_extra_filename_affects_key() {
        let mut ctx1 = make_rustc_context("/src/lib.rs", "2021");
        ctx1.extra_filename = Some("-aaa111".to_string());
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.extra_filename = Some("-bbb222".to_string());
        assert_ne!(
            ctx1.context_key(),
            ctx2.context_key(),
            "-C extra-filename controls emitted artifact names and must affect the key"
        );
    }

    #[test]
    fn rustc_context_key_differs_from_cc() {
        // The domain separation tags differ, so even identical-looking contexts
        // produce different keys.
        let cc_ctx = make_context("/src/lib.rs", &[], &[]);
        let rustc_ctx = make_rustc_context("/src/lib.rs", "2021");
        assert_ne!(
            cc_ctx.context_key(),
            rustc_ctx.context_key(),
            "C and Rust context keys must differ (domain separation)"
        );
    }

    #[test]
    fn rustc_compiler_hash_affects_key() {
        let ctx1 = make_rustc_context("/src/lib.rs", "2021");
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.compiler_hash = Some(crate::hash::hash_bytes(b"rustc-1.94.1"));
        assert_ne!(
            ctx1.context_key(),
            ctx2.context_key(),
            "different compiler hash must produce different context key"
        );
    }

    #[test]
    fn rustc_different_compiler_versions_different_key() {
        let mut ctx1 = make_rustc_context("/src/lib.rs", "2021");
        ctx1.compiler_hash = Some(crate::hash::hash_bytes(b"rustc-1.94.1"));
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.compiler_hash = Some(crate::hash::hash_bytes(b"rustc-1.94.2"));
        assert_ne!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn rustc_extern_crates_affect_key() {
        let ctx1 = make_rustc_context("/src/lib.rs", "2021");
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.extern_crates = vec![("serde".into(), "/deps/libserde.rlib".into())];
        assert_ne!(ctx1.context_key(), ctx2.context_key());
    }

    #[test]
    fn rustc_different_extern_paths_different_key() {
        let mut ctx1 = make_rustc_context("/src/lib.rs", "2021");
        ctx1.extern_crates = vec![("a".into(), "/deps/liba_v1.rlib".into())];
        let mut ctx2 = make_rustc_context("/src/lib.rs", "2021");
        ctx2.extern_crates = vec![("a".into(), "/deps/liba_v2.rlib".into())];
        assert_ne!(
            ctx1.context_key(),
            ctx2.context_key(),
            "different extern paths must produce different context keys"
        );
    }

    #[test]
    fn rustc_from_parsed_args() {
        let args = RustcParsedArgs {
            source_file: NormalizedPath::from("/src/lib.rs"),
            crate_name: Some("mylib".to_string()),
            crate_types: vec!["rlib".to_string(), "lib".to_string()],
            edition: Some("2021".to_string()),
            emit_types: vec!["link".to_string(), "dep-info".to_string()],
            cfgs: vec!["unix".to_string(), "feature=\"std\"".to_string()],
            check_cfgs: Vec::new(),
            codegen_flags: vec!["opt-level=2".to_string()],
            target: None,
            cap_lints: Some("allow".to_string()),
            externs: vec![
                ExternCrate {
                    name: "serde".to_string(),
                    path: NormalizedPath::from("/deps/libserde.rlib"),
                },
                ExternCrate {
                    name: "log".to_string(),
                    path: NormalizedPath::from("/deps/liblog.rlib"),
                },
            ],
            lint_flags: Vec::new(),
            unknown_flags: Vec::new(),
            out_dir: None,
            extra_filename: Some("-abc123".to_string()),
            cargo_metadata: Some("abc123".to_string()),
            incremental_dir: None,
            error_format: None,
            json_format: None,
            color: None,
            diagnostic_width: None,
            search_paths: Vec::new(),
            remap_path_prefixes: Vec::new(),
            sysroot: None,
            output_file: None,
        };
        let ctx = RustcCompileContext::from_parsed_args(&args, &[], None);
        // Crate types sorted
        assert_eq!(ctx.crate_types, vec!["lib", "rlib"]);
        // Emit types sorted
        assert_eq!(ctx.emit_types, vec!["dep-info", "link"]);
        // Extern crates extracted and sorted by name
        assert_eq!(ctx.extern_crates.len(), 2);
        assert_eq!(ctx.extern_crates[0].0, "log");
        assert_eq!(ctx.extern_crates[1].0, "serde");
        assert_eq!(ctx.cargo_metadata.as_deref(), Some("abc123"));
        assert_eq!(ctx.extra_filename.as_deref(), Some("-abc123"));
    }

    #[test]
    fn rustc_artifact_key_changes_with_extern_content() {
        let ctx = make_rustc_context("/src/lib.rs", "2021");
        let ck = ctx.context_key();

        let src_hash = crate::hash::hash_bytes(b"source");
        let ext_hash_a = crate::hash::hash_bytes(b"extern A");
        let ext_hash_b = crate::hash::hash_bytes(b"extern B");

        let ak1 = compute_rustc_artifact_key(
            &ck,
            &mut [(NormalizedPath::from("/src/lib.rs"), src_hash)],
            &mut [("serde".to_string(), ext_hash_a)],
        );
        let ak2 = compute_rustc_artifact_key(
            &ck,
            &mut [(NormalizedPath::from("/src/lib.rs"), src_hash)],
            &mut [("serde".to_string(), ext_hash_b)],
        );
        assert_ne!(
            ak1, ak2,
            "different extern content should produce different artifact key"
        );
    }

    // ─── Cache-key path-independence tests (issue #139, fix #1) ────────────────
    //
    // These tests pin the contract that cache keys are independent of the absolute
    // path at which a workspace happens to live on disk. The same project checked
    // out at `/tmp/proj-a` and `/tmp/proj-b` must produce the same rustc cache key,
    // otherwise every `cargo {check,clippy,test}` after moving / re-cloning the
    // repo cold-misses through the entire dep graph.

    fn make_rustc_context_with_env(env: Vec<(String, String)>) -> RustcCompileContext {
        let mut ctx = make_rustc_context("/src/lib.rs", "2021");
        ctx.env_vars = env;
        ctx.env_vars.sort();
        ctx
    }

    /// T1 — Two contexts that differ only in `CARGO_MANIFEST_DIR` must have
    /// the same cache key. This is the headline regression: the same crate
    /// checked out at two paths should not invalidate the cache.
    #[test]
    fn rustc_context_key_ignores_cargo_manifest_dir() {
        let ctx_a = make_rustc_context_with_env(vec![
            ("CARGO_MANIFEST_DIR".into(), "/tmp/proj-a/crates/foo".into()),
            ("CARGO_PKG_NAME".into(), "foo".into()),
            ("CARGO_PKG_VERSION".into(), "1.2.3".into()),
        ]);
        let ctx_b = make_rustc_context_with_env(vec![
            ("CARGO_MANIFEST_DIR".into(), "/tmp/proj-b/crates/foo".into()),
            ("CARGO_PKG_NAME".into(), "foo".into()),
            ("CARGO_PKG_VERSION".into(), "1.2.3".into()),
        ]);
        assert_eq!(
            ctx_a.context_key(),
            ctx_b.context_key(),
            "CARGO_MANIFEST_DIR is volatile (absolute path) and must NOT \
             contribute to the cache key; otherwise a project clone or rename \
             invalidates every dependent compile"
        );
    }

    /// T2 — Same idea for `CARGO_MANIFEST_PATH`. Cargo started exporting this
    /// in newer versions; it's similarly an absolute path to `Cargo.toml`.
    #[test]
    fn rustc_context_key_ignores_cargo_manifest_path() {
        let ctx_a = make_rustc_context_with_env(vec![
            (
                "CARGO_MANIFEST_PATH".into(),
                "/tmp/proj-a/crates/foo/Cargo.toml".into(),
            ),
            ("CARGO_PKG_NAME".into(), "foo".into()),
            ("CARGO_PKG_VERSION".into(), "1.2.3".into()),
        ]);
        let ctx_b = make_rustc_context_with_env(vec![
            (
                "CARGO_MANIFEST_PATH".into(),
                "/tmp/proj-b/crates/foo/Cargo.toml".into(),
            ),
            ("CARGO_PKG_NAME".into(), "foo".into()),
            ("CARGO_PKG_VERSION".into(), "1.2.3".into()),
        ]);
        assert_eq!(
            ctx_a.context_key(),
            ctx_b.context_key(),
            "CARGO_MANIFEST_PATH is volatile (absolute path) and must NOT \
             contribute to the cache key"
        );
    }

    /// T3 — Negative control: `CARGO_PKG_VERSION` MUST still affect the key
    /// because `env!("CARGO_PKG_VERSION")` is embedded in compiled output.
    /// This guards against an over-eager filter that strips too much.
    #[test]
    fn rustc_context_key_sensitive_to_cargo_pkg_version() {
        let ctx_a = make_rustc_context_with_env(vec![("CARGO_PKG_VERSION".into(), "1.2.3".into())]);
        let ctx_b = make_rustc_context_with_env(vec![("CARGO_PKG_VERSION".into(), "1.2.4".into())]);
        assert_ne!(
            ctx_a.context_key(),
            ctx_b.context_key(),
            "CARGO_PKG_VERSION feeds env!() macros and MUST be in the cache key"
        );
    }

    /// T4 — Extern rmeta paths that share a filename (and therefore the same
    /// `metadata=` hash from cargo) but differ in their absolute directory
    /// prefix must produce equal cache keys. This is the cascade-killer: when
    /// a dep crate is rebuilt at the same content but in a different target
    /// dir, all downstream crates should still hit.
    #[test]
    fn rustc_context_key_ignores_extern_directory_prefix() {
        let mut ctx_a = make_rustc_context("/src/lib.rs", "2021");
        ctx_a.extern_crates = vec![(
            "serde".into(),
            "/tmp/proj-a/target/debug/deps/libserde-abc123.rmeta".into(),
        )];
        let mut ctx_b = make_rustc_context("/src/lib.rs", "2021");
        ctx_b.extern_crates = vec![(
            "serde".into(),
            "/tmp/proj-b/target/debug/deps/libserde-abc123.rmeta".into(),
        )];
        assert_eq!(
            ctx_a.context_key(),
            ctx_b.context_key(),
            "extern rmeta paths with the same filename (= same cargo metadata \
             hash) but different absolute prefixes must produce equal cache \
             keys; otherwise relocating the workspace cascades through every \
             downstream crate"
        );
    }

    #[test]
    fn rustc_artifact_key_stable() {
        let ctx = make_rustc_context("/src/lib.rs", "2021");
        let ck = ctx.context_key();

        let src_hash = crate::hash::hash_bytes(b"source");
        let ext_hash = crate::hash::hash_bytes(b"extern");

        let ak1 = compute_rustc_artifact_key(
            &ck,
            &mut [(NormalizedPath::from("/src/lib.rs"), src_hash)],
            &mut [("serde".to_string(), ext_hash)],
        );
        let ak2 = compute_rustc_artifact_key(
            &ck,
            &mut [(NormalizedPath::from("/src/lib.rs"), src_hash)],
            &mut [("serde".to_string(), ext_hash)],
        );
        assert_eq!(ak1, ak2);
    }

    #[test]
    fn rustc_artifact_key_with_root_matches_equivalent_source_and_dependency_paths() {
        let ctx_a = make_rustc_context("/workspace-a/crates/demo/src/lib.rs", "2021");
        let ctx_b = make_rustc_context("/workspace-b/crates/demo/src/lib.rs", "2021");
        let root_a = Path::new("/workspace-a");
        let root_b = Path::new("/workspace-b");

        let ck_a = ctx_a.context_key_with_root(Some(root_a));
        let ck_b = ctx_b.context_key_with_root(Some(root_b));
        assert_eq!(ck_a, ck_b);

        let src_hash = crate::hash::hash_bytes(b"source");
        let dep_hash = crate::hash::hash_bytes(b"dependency");
        let ext_hash = crate::hash::hash_bytes(b"extern");

        let ak_a = compute_rustc_artifact_key_with_root(
            &ck_a,
            &mut [
                (
                    NormalizedPath::from("/workspace-a/crates/demo/src/lib.rs"),
                    src_hash,
                ),
                (
                    NormalizedPath::from("/workspace-a/crates/demo/src/generated.rs"),
                    dep_hash,
                ),
            ],
            &mut [("serde".to_string(), ext_hash)],
            Some(root_a),
        );
        let ak_b = compute_rustc_artifact_key_with_root(
            &ck_b,
            &mut [
                (
                    NormalizedPath::from("/workspace-b/crates/demo/src/generated.rs"),
                    dep_hash,
                ),
                (
                    NormalizedPath::from("/workspace-b/crates/demo/src/lib.rs"),
                    src_hash,
                ),
            ],
            &mut [("serde".to_string(), ext_hash)],
            Some(root_b),
        );

        assert_eq!(
            ak_a, ak_b,
            "source and dependency files under equivalent roots should hash relative to those roots"
        );
    }
}