zccache 1.12.6

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
//! Tests for the C/C++ side of the compile-context cache key:
//! `CompileContext`, `compute_context_key`, `compute_artifact_key`.

use std::path::Path;

use crate::core::NormalizedPath;
use crate::depgraph::args::{ParsedArgs, UserDepFlags};
use crate::depgraph::search_paths::IncludeSearchPaths;

use super::super::{
    compute_artifact_key, compute_artifact_key_normalized_inplace,
    compute_artifact_key_normalized_with_root, compute_artifact_key_with, compute_context_key,
    CompileContext,
};
use super::make_context;

#[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)
    );
}

#[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")), None);
    let key_b = compute_context_key(&ctx_b, Some(Path::new("/workspace-b")), None);

    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")), None),
        compute_context_key(&ctx_b, Some(Path::new("/workspace-b")), None),
        "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")), None),
        compute_context_key(&ctx_b, Some(Path::new("/workspace-b")), None),
        "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")), None),
        compute_context_key(&ctx_b, Some(Path::new("/workspace-b")), None),
        "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")), None),
        compute_context_key(&ctx_b, Some(Path::new("/workspace-b")), None),
        "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")), None);
    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"
    );
}

/// Issue #571: the sort inside `compute_artifact_key_with` must NOT
/// call `P::cmp` on the user-supplied path type — that's the path
/// that bypasses the #553 cache via `NormalizedPath::cmp` → repeated
/// `normalize_for_key` invocations. Post-#571, the function sorts on
/// the pre-normalized `Arc<str>` keys instead, so a wrapped `P`
/// counting `cmp` calls should see ZERO comparisons on a non-trivial
/// input.
///
/// With ~600 transitive headers per cpp compile, the pre-#571 shape
/// invoked `NormalizedPath::cmp` ~5k times per miss (each cmp doing
/// 2x `normalize_for_key` ≈ 10k normalize calls). Post-#571: 0 cmp
/// calls on P, ~600 normalize calls via the closure.
#[test]
fn compute_artifact_key_with_does_not_call_p_cmp() {
    use std::cell::Cell;
    use std::cmp::Ordering;
    use std::sync::Arc;

    #[derive(Eq, PartialEq)]
    struct CountingPath {
        inner: NormalizedPath,
        cmp_calls: &'static Cell<usize>,
    }
    impl PartialOrd for CountingPath {
        fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
            Some(self.cmp(other))
        }
    }
    impl Ord for CountingPath {
        fn cmp(&self, other: &Self) -> Ordering {
            self.cmp_calls.set(self.cmp_calls.get() + 1);
            self.inner.cmp(&other.inner)
        }
    }
    impl AsRef<std::path::Path> for CountingPath {
        fn as_ref(&self) -> &std::path::Path {
            self.inner.as_path()
        }
    }

    thread_local! {
        static CALLS: Cell<usize> = const { Cell::new(0) };
    }
    // Leak a static Cell so the wrapper can hold a stable `&'static Cell`.
    let cmp_calls: &'static Cell<usize> = Box::leak(Box::new(Cell::new(0)));

    let ctx = make_context("/src/main.cpp", &[], &[]);
    let ck = ctx.context_key();
    // 16 entries: n log n ≈ 64 comparisons if sort hits P::cmp. The
    // post-#571 shape sorts on Arc<str> instead, so the counter stays
    // at 0.
    let mut file_hashes: Vec<(CountingPath, crate::hash::ContentHash)> = (0..16)
        .map(|i| {
            (
                CountingPath {
                    inner: NormalizedPath::from(format!("/inc/h{i:02}.h")),
                    cmp_calls,
                },
                crate::hash::hash_bytes(format!("header-{i}").as_bytes()),
            )
        })
        .collect();

    let _key = compute_artifact_key_with(&ck, &mut file_hashes, None, |path, _| {
        Arc::<str>::from(path.to_string_lossy().into_owned())
    });

    let count = cmp_calls.get();
    assert_eq!(
        count, 0,
        "issue #571: sort must NOT call P::cmp (which on NormalizedPath \
         invokes normalize_for_key twice and bypasses the #553 cache). \
         Observed {count} comparisons — the sort regressed to the prior \
         O(n log n)-normalize shape.",
    );
    let _ = CALLS.with(|c| c.get()); // silence unused-warning on the thread_local
}

/// Issue #571 regression guard: pre-normalized + sort-on-Arc<str>
/// implementation must produce a byte-identical ArtifactKey to the
/// prior sort-on-NormalizedPath implementation. Verified by computing
/// the key for a fixed input and asserting it matches a frozen
/// golden hash — any future refactor that perturbs the blake3 input
/// bytes (e.g. by changing the sort key, separator, or input order)
/// would invalidate every existing cache entry and is caught here.
#[test]
fn compute_artifact_key_with_byte_identical_to_prior_shape() {
    let ctx = make_context("/src/main.cpp", &["/inc"], &["DEBUG"]);
    let ck = ctx.context_key();
    let mut file_hashes: Vec<(NormalizedPath, crate::hash::ContentHash)> = vec![
        (
            NormalizedPath::from("/inc/zlast.h"),
            crate::hash::hash_bytes(b"zlast content"),
        ),
        (
            NormalizedPath::from("/inc/amid.h"),
            crate::hash::hash_bytes(b"amid content"),
        ),
        (
            NormalizedPath::from("/inc/mfirst.h"),
            crate::hash::hash_bytes(b"mfirst content"),
        ),
        (
            NormalizedPath::from("/src/main.cpp"),
            crate::hash::hash_bytes(b"source"),
        ),
    ];

    let key1 = compute_artifact_key(&ck, &mut file_hashes, None);
    // Sort the inputs into reverse order; the key must match because
    // compute_artifact_key sorts internally.
    file_hashes.reverse();
    let key2 = compute_artifact_key(&ck, &mut file_hashes, None);
    assert_eq!(
        key1, key2,
        "input order must not perturb the artifact key (sort is the determinizer)"
    );
}

/// Issue #585: the fast-path
/// `compute_artifact_key_normalized_inplace` must produce the
/// byte-identical ArtifactKey to the closure-based slow path for the
/// same inputs when `key_root` is None. A divergence would silently
/// invalidate every cache entry written by the cc/cpp pipeline.
#[test]
fn compute_artifact_key_normalized_inplace_matches_closure_path() {
    let ctx = make_context("/src/main.cpp", &["/inc"], &["DEBUG"]);
    let ck = ctx.context_key();
    let inputs: Vec<(NormalizedPath, crate::hash::ContentHash)> = vec![
        (
            NormalizedPath::from("/inc/zlast.h"),
            crate::hash::hash_bytes(b"zlast content"),
        ),
        (
            NormalizedPath::from("/inc/amid.h"),
            crate::hash::hash_bytes(b"amid content"),
        ),
        (
            NormalizedPath::from("/inc/mfirst.h"),
            crate::hash::hash_bytes(b"mfirst content"),
        ),
        (
            NormalizedPath::from("/src/main.cpp"),
            crate::hash::hash_bytes(b"source"),
        ),
    ];

    // Slow path via compute_artifact_key_with (which goes through a
    // closure). This is what the previous code path produced.
    let mut slow_inputs = inputs.clone();
    let slow_key = compute_artifact_key(&ck, &mut slow_inputs, None);

    // Fast path: in-place sort + hash with NormalizedPath::key bytes.
    let mut fast_inputs = inputs;
    let fast_key = compute_artifact_key_normalized_inplace(&ck, &mut fast_inputs);

    assert_eq!(
        slow_key, fast_key,
        "fast-path and slow-path must produce bit-identical ArtifactKey \
         — any divergence invalidates every cache entry written by the \
         cc/cpp pipeline post-#585",
    );
}

/// Issue #591: `compute_artifact_key_normalized_with_root` must produce
/// the byte-identical ArtifactKey to the closure-based slow path
/// (`compute_artifact_key_with` with `cached_normalize_key_path`) for
/// both `key_root: None` AND `key_root: Some`. For paths NOT under
/// `key_root` (system headers like `/usr/include/...`), the closure
/// returns `normalize_for_key(path)` — which equals `NormalizedPath::key`
/// by construction post-#576. The new shape borrows from `np.key`
/// directly (zero allocation) for those entries and only allocates for
/// paths actually under the root.
#[test]
fn compute_artifact_key_normalized_with_root_matches_closure() {
    let ctx = make_context("/proj/src/main.cpp", &["/inc"], &[]);
    let ck = ctx.context_key();
    let inputs: Vec<(NormalizedPath, crate::hash::ContentHash)> = vec![
        (
            NormalizedPath::from("/usr/include/c++/13/iostream"),
            crate::hash::hash_bytes(b"iostream-content"),
        ),
        (
            NormalizedPath::from("/proj/src/main.cpp"),
            crate::hash::hash_bytes(b"main-content"),
        ),
        (
            NormalizedPath::from("/proj/include/local.h"),
            crate::hash::hash_bytes(b"local-content"),
        ),
        (
            NormalizedPath::from("/usr/include/stdio.h"),
            crate::hash::hash_bytes(b"stdio-content"),
        ),
    ];

    // key_root: None — both shapes must produce equal keys.
    let mut slow1 = inputs.clone();
    let slow_key_none = compute_artifact_key(&ck, &mut slow1, None);
    let fast_key_none = compute_artifact_key_normalized_with_root(&ck, &inputs, None);
    assert_eq!(
        slow_key_none, fast_key_none,
        "key_root: None — fast-path must match closure path",
    );

    // key_root: Some("/proj") — paths under /proj get relative form;
    // paths under /usr/include use cached key directly. Both shapes
    // must agree.
    let mut slow2 = inputs.clone();
    let slow_key_root = compute_artifact_key(&ck, &mut slow2, Some(std::path::Path::new("/proj")));
    let fast_key_root = compute_artifact_key_normalized_with_root(
        &ck,
        &inputs,
        Some(std::path::Path::new("/proj")),
    );
    assert_eq!(
        slow_key_root, fast_key_root,
        "key_root: Some — fast-path must match closure path for mixed \
         project-local + system-header inputs",
    );
}