zccache 1.11.0

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
//! Watcher integration support for the dependency graph.
//!
//! Provides:
//! - `WatchSet` — tracks which directories should be watched and which
//!   filenames within them are relevant.
//! - Shadow detection — identifies when a newly created file in a
//!   higher-priority include directory would shadow an existing resolved
//!   include.
//! - Unresolved include resolution — identifies when a newly created file
//!   matches a previously unresolved `#include`.

use std::collections::{HashMap, HashSet};
use std::path::Path;

use super::context::ContextKey;
use super::graph::DepGraph;
use crate::core::NormalizedPath;

/// Set of directories that should be watched, with tracked filenames per directory.
///
/// The watcher layer uses this to decide which directories to register with
/// the OS file-watcher (non-recursive) and which events to filter for.
#[derive(Debug, Clone, Default)]
pub struct WatchSet {
    /// Maps directory path → set of tracked file names within it.
    dirs: HashMap<NormalizedPath, HashSet<String>>,
}

fn normalize_watch_filename(name: &std::ffi::OsStr) -> String {
    #[cfg(windows)]
    {
        name.to_string_lossy().to_ascii_lowercase()
    }

    #[cfg(not(windows))]
    {
        name.to_string_lossy().into_owned()
    }
}

impl WatchSet {
    /// Create an empty watch set.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Build a watch set from a list of absolute file paths.
    /// Each file's parent directory is added, with the filename tracked.
    #[must_use]
    pub fn from_paths(paths: impl IntoIterator<Item = impl AsRef<Path>>) -> Self {
        let mut dirs: HashMap<NormalizedPath, HashSet<String>> = HashMap::new();
        for path in paths {
            let path = path.as_ref();
            if let (Some(parent), Some(name)) = (path.parent(), path.file_name()) {
                dirs.entry(parent.to_path_buf().into())
                    .or_default()
                    .insert(normalize_watch_filename(name));
            }
        }
        Self { dirs }
    }

    /// Add a directory to watch (even if it has no tracked files yet).
    /// Used for include search directories where new files might appear.
    pub fn add_dir(&mut self, dir: NormalizedPath) {
        self.dirs.entry(dir).or_default();
    }

    /// Add a specific file path to the watch set.
    pub fn add_path(&mut self, path: &Path) {
        if let (Some(parent), Some(name)) = (path.parent(), path.file_name()) {
            self.dirs
                .entry(parent.to_path_buf().into())
                .or_default()
                .insert(normalize_watch_filename(name));
        }
    }

    /// Get all directories that need to be watched.
    pub fn dirs(&self) -> impl Iterator<Item = &NormalizedPath> {
        self.dirs.keys()
    }

    /// Check if a path is in the tracked file set.
    #[must_use]
    pub fn is_tracked(&self, path: &Path) -> bool {
        if let (Some(parent), Some(name)) = (path.parent(), path.file_name()) {
            let parent = NormalizedPath::new(parent);
            self.dirs
                .get(&parent)
                .is_some_and(|names| names.contains(&normalize_watch_filename(name)))
        } else {
            false
        }
    }

    /// Check if a directory is in the watch set.
    #[must_use]
    pub fn is_watched(&self, dir: &Path) -> bool {
        self.dirs.contains_key(&NormalizedPath::new(dir))
    }

    /// Number of watched directories.
    #[must_use]
    pub fn dir_count(&self) -> usize {
        self.dirs.len()
    }

    /// Total number of tracked files across all directories.
    #[must_use]
    pub fn file_count(&self) -> usize {
        self.dirs.values().map(HashSet::len).sum()
    }

    /// Directories in `self` that are not in `previous`.
    /// These are newly added directories that need watch registration.
    #[must_use]
    pub fn new_dirs_vs(&self, previous: &WatchSet) -> Vec<NormalizedPath> {
        self.dirs
            .keys()
            .filter(|d| !previous.dirs.contains_key(*d))
            .cloned()
            .collect()
    }

    /// Directories in `previous` that are not in `self`.
    /// These are removed directories whose watches can be dropped.
    #[must_use]
    pub fn removed_dirs_vs(&self, previous: &WatchSet) -> Vec<NormalizedPath> {
        previous
            .dirs
            .keys()
            .filter(|d| !self.dirs.contains_key(*d))
            .cloned()
            .collect()
    }
}

/// Check if `dir_a` appears before `dir_b` in the given search path order.
///
/// Returns `true` if `dir_a` has higher priority (appears earlier) than `dir_b`.
/// Returns `false` if either directory is not in the search paths or they are equal.
fn is_higher_priority(
    dir_a: &Path,
    dir_b: &Path,
    search: &super::search_paths::IncludeSearchPaths,
) -> bool {
    let all_dirs: Vec<&Path> = search.all_search_dirs().collect();

    let pos_a = all_dirs.iter().position(|d| *d == dir_a);
    let pos_b = all_dirs.iter().position(|d| *d == dir_b);

    match (pos_a, pos_b) {
        (Some(a), Some(b)) => a < b,
        _ => false,
    }
}

impl DepGraph {
    /// Compute the set of directories that should be watched.
    ///
    /// Includes:
    /// - Parent directories of all resolved include paths (to detect modifications)
    /// - Parent directories of source files (to detect source changes)
    /// - All include search directories from all contexts (to detect new files)
    #[must_use]
    pub fn watch_set(&self) -> WatchSet {
        let mut ws = WatchSet::new();

        for entry in self.contexts_iter() {
            let ctx_entry = entry.value();

            // Source file parent dir.
            ws.add_path(&ctx_entry.context.source_file);

            // All resolved include parent dirs.
            for inc in &ctx_entry.resolved_includes {
                ws.add_path(inc);
            }

            // All include search dirs (for new-file detection).
            for dir in ctx_entry.context.include_search.all_search_dirs() {
                ws.add_dir(dir.into());
            }
        }

        ws
    }

    /// Check if a newly created file shadows any existing resolved include
    /// in any context. Returns context keys that should be marked stale.
    ///
    /// A shadow occurs when `new_file` has the same filename as an existing
    /// resolved include, and `new_file`'s directory appears earlier (higher
    /// priority) in that context's include search path.
    #[must_use]
    pub fn check_shadow(&self, new_file: &Path) -> Vec<ContextKey> {
        let new_name = match new_file.file_name() {
            Some(n) => n.to_string_lossy().into_owned(),
            None => return Vec::new(),
        };
        let new_dir = match new_file.parent() {
            Some(d) => d,
            None => return Vec::new(),
        };

        let mut affected = Vec::new();

        for entry in self.contexts_iter() {
            let ctx_entry = entry.value();
            let search = &ctx_entry.context.include_search;

            for resolved_path in &ctx_entry.resolved_includes {
                let resolved_name = match resolved_path.file_name() {
                    Some(n) => n.to_string_lossy(),
                    None => continue,
                };

                if *resolved_name != new_name {
                    continue;
                }

                let resolved_dir = match resolved_path.parent() {
                    Some(d) => d,
                    None => continue,
                };

                // Same directory — not a shadow, just a replacement (handled
                // by the watcher's Modified event).
                if resolved_dir == new_dir {
                    continue;
                }

                if is_higher_priority(new_dir, resolved_dir, search) {
                    affected.push(*entry.key());
                    break; // Context already affected, move to next.
                }
            }
        }

        affected
    }

    /// Check if a newly created file resolves any previously unresolved
    /// `#include` in any context. Returns affected context keys.
    #[must_use]
    pub fn check_new_resolve(&self, new_file: &Path) -> Vec<ContextKey> {
        let new_name = match new_file.file_name() {
            Some(n) => n.to_string_lossy().into_owned(),
            None => return Vec::new(),
        };

        let mut affected = Vec::new();

        for entry in self.contexts_iter() {
            let ctx_entry = entry.value();

            for unresolved in &ctx_entry.unresolved_includes {
                // Unresolved includes may be bare names ("foo.h") or paths
                // ("path/to/foo.h"). Compare against the filename.
                let unresolved_name = Path::new(unresolved)
                    .file_name()
                    .map(|n| n.to_string_lossy().into_owned())
                    .unwrap_or_default();

                if unresolved_name == new_name {
                    affected.push(*entry.key());
                    break;
                }
            }
        }

        affected
    }
}

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

    use super::super::context::CompileContext;
    use super::super::scanner::ScanResult;
    use super::super::search_paths::IncludeSearchPaths;
    use crate::hash::ContentHash;

    fn dummy_hash(path: &Path) -> Option<ContentHash> {
        Some(crate::hash::hash_bytes(path.to_string_lossy().as_bytes()))
    }

    fn make_ctx_with_search(source: &str, search: IncludeSearchPaths) -> CompileContext {
        CompileContext {
            source_file: NormalizedPath::from(source),
            include_search: search,
            defines: Vec::new(),
            flags: Vec::new(),
            force_includes: Vec::new(),
            unknown_flags: Vec::new(),
        }
    }

    // --- WatchSet tests ---

    #[test]
    fn watch_set_from_paths_groups_by_dir() {
        let ws = WatchSet::from_paths([
            NormalizedPath::from("/inc/a.h"),
            NormalizedPath::from("/inc/b.h"),
            NormalizedPath::from("/src/main.c"),
        ]);
        assert_eq!(ws.dir_count(), 2);
        assert_eq!(ws.file_count(), 3);
        assert!(ws.is_watched(Path::new("/inc")));
        assert!(ws.is_watched(Path::new("/src")));
    }

    #[test]
    fn watch_set_deduplication() {
        let ws = WatchSet::from_paths([
            NormalizedPath::from("/inc/a.h"),
            NormalizedPath::from("/inc/a.h"), // duplicate
        ]);
        assert_eq!(ws.dir_count(), 1);
        assert_eq!(ws.file_count(), 1);
    }

    #[test]
    fn watch_set_is_tracked() {
        let ws = WatchSet::from_paths([NormalizedPath::from("/inc/a.h")]);
        assert!(ws.is_tracked(Path::new("/inc/a.h")));
        assert!(!ws.is_tracked(Path::new("/inc/b.h")));
        assert!(!ws.is_tracked(Path::new("/other/a.h")));
    }

    #[cfg(windows)]
    #[test]
    fn watch_set_is_tracked_ignores_filename_case_on_windows() {
        let ws = WatchSet::from_paths([NormalizedPath::from(r"C:\inc\Config.h")]);
        assert!(ws.is_tracked(Path::new(r"C:\inc\config.h")));
        assert!(ws.is_tracked(Path::new(r"C:\inc\CONFIG.H")));
    }

    #[test]
    fn watch_set_add_dir_empty() {
        let mut ws = WatchSet::new();
        ws.add_dir(NormalizedPath::from("/usr/include"));
        assert!(ws.is_watched(Path::new("/usr/include")));
        assert_eq!(ws.file_count(), 0);
        assert_eq!(ws.dir_count(), 1);
    }

    #[test]
    fn watch_set_add_path() {
        let mut ws = WatchSet::new();
        ws.add_path(Path::new("/inc/foo.h"));
        assert!(ws.is_tracked(Path::new("/inc/foo.h")));
        assert!(ws.is_watched(Path::new("/inc")));
    }

    #[test]
    fn watch_set_new_dirs_vs() {
        let old = WatchSet::from_paths([NormalizedPath::from("/inc/a.h")]);
        let new = WatchSet::from_paths([
            NormalizedPath::from("/inc/a.h"),
            NormalizedPath::from("/new/b.h"),
        ]);
        let added = new.new_dirs_vs(&old);
        assert_eq!(added, vec![NormalizedPath::from("/new")]);
    }

    #[test]
    fn watch_set_removed_dirs_vs() {
        let old = WatchSet::from_paths([
            NormalizedPath::from("/inc/a.h"),
            NormalizedPath::from("/old/b.h"),
        ]);
        let new = WatchSet::from_paths([NormalizedPath::from("/inc/a.h")]);
        let removed = new.removed_dirs_vs(&old);
        assert_eq!(removed, vec![NormalizedPath::from("/old")]);
    }

    // --- DepGraph::watch_set() tests ---

    #[test]
    fn watch_set_includes_source_and_headers() {
        let graph = DepGraph::new();
        let ctx = make_ctx_with_search("/src/main.c", IncludeSearchPaths::default());
        let key = graph.register(ctx);

        let scan = ScanResult {
            resolved: vec![
                NormalizedPath::from("/inc/a.h"),
                NormalizedPath::from("/inc/b.h"),
            ],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        let ws = graph.watch_set();
        assert!(ws.is_tracked(Path::new("/src/main.c")));
        assert!(ws.is_tracked(Path::new("/inc/a.h")));
        assert!(ws.is_tracked(Path::new("/inc/b.h")));
    }

    #[test]
    fn watch_set_includes_search_dirs() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/project/include")],
            system: vec![NormalizedPath::from("/usr/include")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        graph.register(ctx);

        let ws = graph.watch_set();
        // Search dirs are watched even if no files resolve there yet.
        assert!(ws.is_watched(Path::new("/project/include")));
        assert!(ws.is_watched(Path::new("/usr/include")));
    }

    #[test]
    fn watch_set_dedupes_across_contexts() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/inc")],
            ..Default::default()
        };

        let ctx1 = make_ctx_with_search("/src/a.c", search.clone());
        let key1 = graph.register(ctx1);
        let ctx2 = make_ctx_with_search("/src/b.c", search);
        let key2 = graph.register(ctx2);

        let scan = ScanResult {
            resolved: vec![NormalizedPath::from("/inc/common.h")],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key1, scan.clone(), dummy_hash);
        graph.update(&key2, scan, dummy_hash);

        let ws = graph.watch_set();
        // /inc should appear once, not twice.
        let inc_count = ws
            .dirs()
            .filter(|d| d.as_path() == Path::new("/inc"))
            .count();
        assert_eq!(inc_count, 1);
    }

    // --- Shadow detection tests ---

    #[test]
    fn check_shadow_detects_higher_priority() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/high"), NormalizedPath::from("/low")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        let key = graph.register(ctx);

        // foo.h currently resolves from /low.
        let scan = ScanResult {
            resolved: vec![NormalizedPath::from("/low/foo.h")],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        // New foo.h appears in /high (higher priority).
        let affected = graph.check_shadow(Path::new("/high/foo.h"));
        assert_eq!(affected, vec![key]);
    }

    #[test]
    fn check_shadow_no_false_positive_lower_priority() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/high"), NormalizedPath::from("/low")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        let key = graph.register(ctx);

        // foo.h already resolves from /high.
        let scan = ScanResult {
            resolved: vec![NormalizedPath::from("/high/foo.h")],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        // New foo.h appears in /low (lower priority) — NOT a shadow.
        let affected = graph.check_shadow(Path::new("/low/foo.h"));
        assert!(affected.is_empty());
    }

    #[test]
    fn check_shadow_different_filename_no_match() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/high"), NormalizedPath::from("/low")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        let key = graph.register(ctx);

        let scan = ScanResult {
            resolved: vec![NormalizedPath::from("/low/foo.h")],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        // bar.h in /high — different name, not a shadow.
        let affected = graph.check_shadow(Path::new("/high/bar.h"));
        assert!(affected.is_empty());
    }

    #[test]
    fn check_shadow_same_dir_not_shadow() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/inc")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        let key = graph.register(ctx);

        let scan = ScanResult {
            resolved: vec![NormalizedPath::from("/inc/foo.h")],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        // Same dir — this is a modify/replace, not a shadow.
        let affected = graph.check_shadow(Path::new("/inc/foo.h"));
        assert!(affected.is_empty());
    }

    #[test]
    fn check_shadow_iquote_over_user() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            iquote: vec![NormalizedPath::from("/iquote")],
            user: vec![NormalizedPath::from("/user")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        let key = graph.register(ctx);

        // foo.h resolves from -I dir.
        let scan = ScanResult {
            resolved: vec![NormalizedPath::from("/user/foo.h")],
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        // New foo.h in -iquote dir (higher priority).
        let affected = graph.check_shadow(Path::new("/iquote/foo.h"));
        assert_eq!(affected, vec![key]);
    }

    #[test]
    fn check_shadow_cold_context_not_affected() {
        let graph = DepGraph::new();
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/high"), NormalizedPath::from("/low")],
            ..Default::default()
        };
        let ctx = make_ctx_with_search("/src/main.c", search);
        graph.register(ctx);

        // Cold context has no resolved includes — nothing to shadow.
        let affected = graph.check_shadow(Path::new("/high/foo.h"));
        assert!(affected.is_empty());
    }

    // --- New resolve detection tests ---

    #[test]
    fn check_new_resolve_matches_unresolved() {
        let graph = DepGraph::new();
        let ctx = make_ctx_with_search("/src/main.c", IncludeSearchPaths::default());
        let key = graph.register(ctx);

        let scan = ScanResult {
            resolved: Vec::new(),
            unresolved: vec!["missing.h".to_string()],
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        let affected = graph.check_new_resolve(Path::new("/inc/missing.h"));
        assert_eq!(affected, vec![key]);
    }

    #[test]
    fn check_new_resolve_no_match() {
        let graph = DepGraph::new();
        let ctx = make_ctx_with_search("/src/main.c", IncludeSearchPaths::default());
        let key = graph.register(ctx);

        let scan = ScanResult {
            resolved: Vec::new(),
            unresolved: vec!["missing.h".to_string()],
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        let affected = graph.check_new_resolve(Path::new("/inc/other.h"));
        assert!(affected.is_empty());
    }

    #[test]
    fn check_new_resolve_path_include() {
        let graph = DepGraph::new();
        let ctx = make_ctx_with_search("/src/main.c", IncludeSearchPaths::default());
        let key = graph.register(ctx);

        // Unresolved include with a path component.
        let scan = ScanResult {
            resolved: Vec::new(),
            unresolved: vec!["sub/missing.h".to_string()],
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);

        // New file with matching filename.
        let affected = graph.check_new_resolve(Path::new("/inc/sub/missing.h"));
        assert_eq!(affected, vec![key]);
    }

    // --- mark_stale tests ---

    #[test]
    fn mark_stale_changes_state() {
        let graph = DepGraph::new();
        let ctx = make_ctx_with_search("/src/main.c", IncludeSearchPaths::default());
        let key = graph.register(ctx);

        let scan = ScanResult {
            resolved: Vec::new(),
            unresolved: Vec::new(),
            has_computed: false,
        };
        graph.update(&key, scan, dummy_hash);
        assert_eq!(
            graph.get_state(&key),
            Some(super::super::graph::ContextState::Warm)
        );

        assert!(graph.mark_stale(&key));
        assert_eq!(
            graph.get_state(&key),
            Some(super::super::graph::ContextState::Stale)
        );
    }

    #[test]
    fn mark_stale_nonexistent_returns_false() {
        let graph = DepGraph::new();
        let ctx = make_ctx_with_search("/src/main.c", IncludeSearchPaths::default());
        let key = ctx.context_key();
        assert!(!graph.mark_stale(&key));
    }

    // --- is_higher_priority tests ---

    #[test]
    fn priority_iquote_before_user() {
        let search = IncludeSearchPaths {
            iquote: vec![NormalizedPath::from("/q")],
            user: vec![NormalizedPath::from("/u")],
            ..Default::default()
        };
        assert!(is_higher_priority(
            Path::new("/q"),
            Path::new("/u"),
            &search
        ));
        assert!(!is_higher_priority(
            Path::new("/u"),
            Path::new("/q"),
            &search
        ));
    }

    #[test]
    fn priority_user_before_system() {
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/u")],
            system: vec![NormalizedPath::from("/s")],
            ..Default::default()
        };
        assert!(is_higher_priority(
            Path::new("/u"),
            Path::new("/s"),
            &search
        ));
    }

    #[test]
    fn priority_unknown_dir_returns_false() {
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/u")],
            ..Default::default()
        };
        assert!(!is_higher_priority(
            Path::new("/unknown"),
            Path::new("/u"),
            &search
        ));
    }

    #[test]
    fn priority_same_dir_returns_false() {
        let search = IncludeSearchPaths {
            user: vec![NormalizedPath::from("/u")],
            ..Default::default()
        };
        assert!(!is_higher_priority(
            Path::new("/u"),
            Path::new("/u"),
            &search
        ));
    }
}