zccache 1.11.5

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
//! Parser for GNU make dependency files (`.d` files).
//!
//! GCC/Clang emit these with `-MD -MF`. The format is:
//!
//! ```text
//! target.o: source.c header1.h \
//!   /usr/include/stdio.h path\ with\ spaces/foo.h
//! ```
//!
//! The parser extracts dependency paths, resolves relative paths against
//! a working directory, excludes the source file itself, and deduplicates.

use std::collections::HashSet;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};

use super::args::UserDepFlags;
use super::scanner::ScanResult;
use crate::core::NormalizedPath;

/// Errors that can occur while parsing a `.d` file.
#[derive(Debug)]
pub enum DepfileError {
    /// I/O error reading the file.
    Io(std::io::Error),
    /// The depfile content is malformed (empty or missing colon separator).
    Malformed(String),
}

impl std::fmt::Display for DepfileError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DepfileError::Io(e) => write!(f, "depfile I/O error: {e}"),
            DepfileError::Malformed(msg) => write!(f, "malformed depfile: {msg}"),
        }
    }
}

impl std::error::Error for DepfileError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            DepfileError::Io(e) => Some(e),
            DepfileError::Malformed(_) => None,
        }
    }
}

impl From<std::io::Error> for DepfileError {
    fn from(e: std::io::Error) -> Self {
        DepfileError::Io(e)
    }
}

/// Parse `.d` file content into a [`ScanResult`].
///
/// The source file is excluded from the resolved list. Relative paths are
/// resolved against `cwd`. Duplicate paths are collapsed (preserving order).
pub fn parse_depfile(content: &str, source: &Path, cwd: &Path) -> Result<ScanResult, DepfileError> {
    if content.trim().is_empty() {
        return Err(DepfileError::Malformed("empty depfile content".to_string()));
    }

    // Step 1: Join continuation lines (replace `\<newline>` with space).
    let joined = join_continuations(content);

    // Step 2: Find the colon separator (handling Windows drive letters).
    let colon_pos = find_separator_colon(&joined)?;

    // Step 3: Everything after the colon is the dependency list.
    let deps_str = &joined[colon_pos + 1..];

    // Step 4: Split on unescaped whitespace and unescape tokens.
    let tokens = split_and_unescape(deps_str);

    // Step 5-7: Resolve paths, filter source, deduplicate.
    let source_canonical = canonicalize_path(source, cwd);

    let mut seen = HashSet::new();
    let mut resolved = Vec::new();

    for token in tokens {
        if token.is_empty() {
            continue;
        }

        let dep_path = Path::new(&token);
        let abs_path = if dep_path.is_absolute() {
            canonicalize_path(dep_path, cwd)
        } else {
            canonicalize_path(&cwd.join(dep_path), cwd)
        };

        // Exclude the source file itself.
        if abs_path == source_canonical {
            continue;
        }

        // Deduplicate, preserving insertion order.
        if seen.insert(abs_path.clone()) {
            resolved.push(abs_path);
        }
    }

    Ok(ScanResult {
        resolved,
        unresolved: Vec::new(),
        has_computed: false,
    })
}

/// Read and parse a `.d` file from disk.
///
/// Reads the file at `path`, then delegates to [`parse_depfile`].
pub fn parse_depfile_path(
    path: &Path,
    source: &Path,
    cwd: &Path,
) -> Result<ScanResult, DepfileError> {
    let content = std::fs::read_to_string(path)?;
    parse_depfile(&content, source, cwd)
}

// ── Depfile Strategy ────────────────────────────────────────────────

/// How to obtain the depfile for a compilation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DepfileStrategy {
    /// We injected `-MD -MF <path>` — read and clean up after compilation.
    Injected { path: NormalizedPath },
    /// User already had `-MF <path>` — read it (don't delete).
    UserSpecified { path: NormalizedPath },
    /// User had `-MD` but no `-MF` — derive path from output stem.
    UserDefault { path: NormalizedPath },
    /// MSVC `/showIncludes` — parse stderr after compilation.
    ShowIncludes,
    /// Compiler doesn't support depfiles — use fallback scanner.
    Unsupported,
}

/// Determine depfile strategy and return extra args to append to the compiler.
///
/// `supports_depfile`: whether the compiler family supports `-MD -MF`.
/// `dep_flags`: user's existing dependency flags.
/// `output_file`: the `-o` output file path (used to derive default `.d` path).
/// `tmpdir`: directory for injected depfiles.
///
/// Returns `(extra_args, strategy)`. `extra_args` is empty unless we inject flags.
pub fn prepare_depfile(
    supports_depfile: bool,
    dep_flags: &UserDepFlags,
    output_file: &Path,
    tmpdir: &Path,
) -> (Vec<String>, DepfileStrategy) {
    if !supports_depfile {
        return (Vec::new(), DepfileStrategy::Unsupported);
    }

    // User already specified -MF <path>: use their file.
    if let Some(ref mf_path) = dep_flags.mf_path {
        return (
            Vec::new(),
            DepfileStrategy::UserSpecified {
                path: mf_path.clone(),
            },
        );
    }

    // User has -MD/-MMD but no -MF: derive from output file stem.
    if dep_flags.has_md {
        let d_path = output_file.with_extension("d");
        return (
            Vec::new(),
            DepfileStrategy::UserDefault {
                path: d_path.into(),
            },
        );
    }

    // No user dep flags: inject -MD -MF <tmpfile>.
    // Re-create tmpdir if it was deleted (e.g. by Windows temp cleanup)
    // while the daemon is still running. Without this, the compiler fails
    // with "error opening ... no such file or directory".
    if !tmpdir.exists() {
        let _ = std::fs::create_dir_all(tmpdir);
    }
    static DEPFILE_COUNTER: AtomicU64 = AtomicU64::new(0);
    let unique = DEPFILE_COUNTER.fetch_add(1, Ordering::Relaxed);
    let stem = output_file
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("depfile");
    let tmp_path = tmpdir.join(format!("{stem}_{}_{unique}.d", std::process::id()));
    let tmp_path: NormalizedPath = tmp_path.into();
    let extra_args = vec![
        "-MD".to_string(),
        "-MF".to_string(),
        tmp_path.to_string_lossy().into_owned(),
    ];
    (extra_args, DepfileStrategy::Injected { path: tmp_path })
}

// ── Internal helpers ─────────────────────────────────────────────────

/// Join backslash-continued lines: replace `\<newline>` sequences with a
/// single space so that the entire depfile becomes one logical line.
fn join_continuations(content: &str) -> String {
    let mut result = String::with_capacity(content.len());
    let mut chars = content.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '\\' {
            match chars.peek() {
                Some('\n') => {
                    chars.next();
                    result.push(' ');
                }
                Some('\r') => {
                    chars.next();
                    if chars.peek() == Some(&'\n') {
                        chars.next();
                    }
                    result.push(' ');
                }
                _ => {
                    result.push(ch);
                }
            }
        } else {
            result.push(ch);
        }
    }

    result
}

/// Find the position of the colon that separates target(s) from dependencies.
///
/// Handles Windows drive letters: if the character before `:` is a single
/// ASCII letter and the character after `:` is `\` or `/`, it is a drive
/// letter, not the target separator. Keep scanning.
fn find_separator_colon(line: &str) -> Result<usize, DepfileError> {
    let bytes = line.as_bytes();
    let len = bytes.len();
    let mut i = 0;

    while i < len {
        if bytes[i] == b':' {
            // Check if this is a Windows drive letter (e.g., C:\...).
            // A drive letter colon has: single ASCII letter before it,
            // and `\` or `/` after it.
            let is_drive_letter = i > 0
                && (i == 1 || !bytes[i - 2].is_ascii_alphanumeric())
                && bytes[i - 1].is_ascii_alphabetic()
                && i + 1 < len
                && (bytes[i + 1] == b'\\' || bytes[i + 1] == b'/');

            if !is_drive_letter {
                return Ok(i);
            }
        }
        i += 1;
    }

    Err(DepfileError::Malformed(
        "no colon separator found".to_string(),
    ))
}

/// Split the dependency string on unescaped whitespace, then unescape each
/// token (`\ ` → ` `, `\#` → `#`).
fn split_and_unescape(deps: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let mut current = String::new();
    let mut chars = deps.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '\\' {
            match chars.peek() {
                Some(' ') => {
                    chars.next();
                    current.push(' ');
                }
                Some('#') => {
                    chars.next();
                    current.push('#');
                }
                _ => {
                    // Preserve other backslashes (e.g., Windows path separators).
                    current.push(ch);
                }
            }
        } else if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
            if !current.is_empty() {
                tokens.push(std::mem::take(&mut current));
            }
        } else {
            current.push(ch);
        }
    }

    if !current.is_empty() {
        tokens.push(current);
    }

    tokens
}

/// Canonicalize a path, falling back to the joined path if canonicalization
/// fails (e.g., the file does not exist on disk).
///
/// On Windows, `std::fs::canonicalize` produces `\\?\` extended-length paths.
/// These must be stripped so paths match the format used by the file watcher
/// (which also strips `\\?\`), ensuring journal/metadata lookups work correctly.
pub(crate) fn canonicalize_path(path: &Path, cwd: &Path) -> NormalizedPath {
    let canonical = std::fs::canonicalize(path).unwrap_or_else(|_| {
        if path.is_absolute() {
            path.to_path_buf()
        } else {
            cwd.join(path)
        }
    });
    strip_win_prefix(canonical.into())
}

/// Strip the `\\?\` extended-length prefix on Windows.
/// No-op on other platforms.
pub(crate) fn strip_win_prefix(path: NormalizedPath) -> NormalizedPath {
    #[cfg(windows)]
    {
        let s = path.to_string_lossy();
        if let Some(stripped) = s.strip_prefix(r"\\?\") {
            return NormalizedPath::from(stripped);
        }
    }
    path
}

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

    use super::*;
    use tempfile::TempDir;

    /// Helper: create a file with empty content inside a temp dir.
    fn touch(dir: &Path, name: &str) -> NormalizedPath {
        let p = dir.join(name);
        if let Some(parent) = p.parent() {
            std::fs::create_dir_all(parent).unwrap();
        }
        std::fs::write(&p, "").unwrap();
        p.into()
    }

    /// Helper: canonicalize a path (or return it unchanged), stripping \\?\ on Windows.
    fn canon(p: &Path) -> NormalizedPath {
        strip_win_prefix(
            std::fs::canonicalize(p)
                .unwrap_or_else(|_| p.to_path_buf())
                .into(),
        )
    }

    // ── 1. parse_single_line ─────────────────────────────────────────

    #[test]
    fn parse_single_line() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "foo.c");
        let bar_h = touch(cwd, "bar.h");

        let content = "foo.o: foo.c bar.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 1);
        assert_eq!(result.resolved[0], canon(&bar_h));
        assert!(result.unresolved.is_empty());
        assert!(!result.has_computed);
    }

    // ── 2. parse_continuations ───────────────────────────────────────

    #[test]
    fn parse_continuations() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "foo.c");
        let bar_h = touch(cwd, "bar.h");
        let baz_h = touch(cwd, "baz.h");

        let content = "foo.o: foo.c bar.h \\\n  baz.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 2);
        assert!(result.resolved.contains(&canon(&bar_h)));
        assert!(result.resolved.contains(&canon(&baz_h)));
    }

    // ── 3. parse_escaped_spaces ──────────────────────────────────────

    #[test]
    fn parse_escaped_spaces() {
        // Use a synthetic depfile. We can't easily create dirs with spaces
        // in tempdir on all platforms, so just verify parsing logic: the
        // token should have the space unescaped.
        let content = r"foo.o: foo.c path\ with\ spaces/foo.h";
        let source = Path::new("/nonexistent/foo.c");
        let cwd = Path::new("/nonexistent");

        let result = parse_depfile(content, source, cwd).unwrap();

        // The resolved path won't canonicalize (files don't exist), so
        // check that it ends with the unescaped name.
        assert_eq!(result.resolved.len(), 1);
        let dep = &result.resolved[0];
        let dep_str = dep.to_string_lossy();
        assert!(
            dep_str.contains("path with spaces"),
            "expected unescaped space in path, got: {dep_str}"
        );
    }

    // ── 4. parse_multiple_targets ────────────────────────────────────

    #[test]
    fn parse_multiple_targets() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "foo.c");
        let bar_h = touch(cwd, "bar.h");

        // Multiple targets before the colon (gcc -MD -MT produces this).
        let content = "foo.o foo.d: foo.c bar.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 1);
        assert_eq!(result.resolved[0], canon(&bar_h));
    }

    // ── 5. parse_empty_deps ──────────────────────────────────────────

    #[test]
    fn parse_empty_deps() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "foo.c");

        // Source is the only dependency — it gets excluded.
        let content = "foo.o: foo.c";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert!(result.resolved.is_empty());
    }

    // ── 6. parse_relative_paths_resolved ─────────────────────────────

    #[test]
    fn parse_relative_paths_resolved() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "src/main.c");
        let header = touch(cwd, "inc/util.h");

        // Relative paths in the depfile should be resolved against cwd.
        let content = "src/main.o: src/main.c inc/util.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 1);
        assert_eq!(result.resolved[0], canon(&header));
    }

    // ── 7. parse_source_excluded ─────────────────────────────────────

    #[test]
    fn parse_source_excluded() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "main.c");
        let alpha = touch(cwd, "alpha.h");
        let beta = touch(cwd, "beta.h");

        let content = "main.o: main.c alpha.h beta.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        // main.c should not appear in resolved.
        assert_eq!(result.resolved.len(), 2);
        assert!(result.resolved.contains(&canon(&alpha)));
        assert!(result.resolved.contains(&canon(&beta)));
        assert!(!result.resolved.contains(&canon(&source)));
    }

    // ── 8. parse_deduplicates ────────────────────────────────────────

    #[test]
    fn parse_deduplicates() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "foo.c");
        let bar_h = touch(cwd, "bar.h");

        // bar.h appears twice — should be deduplicated.
        let content = "foo.o: foo.c bar.h bar.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 1);
        assert_eq!(result.resolved[0], canon(&bar_h));
    }

    // ── 9. parse_windows_drive_letters ───────────────────────────────

    #[test]
    #[cfg(windows)]
    fn parse_windows_drive_letters() {
        // The colon after `C` should not be treated as the target separator.
        let content = r"C:\build\foo.o: C:\src\foo.c C:\inc\bar.h";
        let source = Path::new(r"C:\src\foo.c");
        let cwd = Path::new(r"C:\build");

        let result = parse_depfile(content, source, cwd).unwrap();

        // bar.h should be present (foo.c excluded as source).
        assert_eq!(result.resolved.len(), 1);
        let dep = &result.resolved[0];
        let dep_str = dep.to_string_lossy();
        assert!(
            dep_str.contains("bar.h"),
            "expected bar.h in resolved, got: {dep_str}"
        );
    }

    #[test]
    #[cfg(not(windows))]
    fn parse_windows_drive_letters() {
        // On non-Windows, just verify the colon parser doesn't choke on
        // drive-letter colons and finds the correct separator.
        let content = r"C:\build\foo.o: C:\src\foo.c C:\inc\bar.h";
        let source = Path::new(r"C:\src\foo.c");
        let cwd = Path::new(r"C:\build");

        let result = parse_depfile(content, source, cwd).unwrap();

        // Source exclusion relies on std::path which handles `\` differently
        // on Unix, so just check that parsing succeeded and bar.h is present.
        let dep_strs: Vec<String> = result
            .resolved
            .iter()
            .map(|p| p.to_string_lossy().into_owned())
            .collect();
        assert!(
            dep_strs.iter().any(|s| s.contains("bar.h")),
            "expected bar.h in resolved, got: {dep_strs:?}"
        );
    }

    // ── 10. parse_empty_content_errors ───────────────────────────────

    #[test]
    fn parse_empty_content_errors() {
        let result = parse_depfile("", Path::new("foo.c"), Path::new("/tmp"));
        assert!(result.is_err());
        match result.unwrap_err() {
            DepfileError::Malformed(msg) => {
                assert!(msg.contains("empty"), "unexpected message: {msg}");
            }
            other => panic!("expected Malformed, got: {other:?}"),
        }
    }

    // ── 11. parse_real_gcc_output ────────────────────────────────────

    #[test]
    fn parse_real_gcc_output() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "main.c");
        let config_h = touch(cwd, "config.h");

        // Create a fake system include dir structure.
        let stdio_h = touch(cwd, "usr/include/stdio.h");
        let stdlib_h = touch(cwd, "usr/include/stdlib.h");

        let content = format!(
            "main.o: main.c config.h \\\n {} \\\n {}",
            stdio_h.display(),
            stdlib_h.display(),
        );

        let result = parse_depfile(&content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 3);
        assert!(result.resolved.contains(&canon(&config_h)));
        assert!(result.resolved.contains(&canon(&stdio_h)));
        assert!(result.resolved.contains(&canon(&stdlib_h)));
        assert!(!result.has_computed);
        assert!(result.unresolved.is_empty());
    }

    // ── 12. parse_real_clang_output ──────────────────────────────────

    #[test]
    fn parse_real_clang_output() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "app.cpp");
        let app_h = touch(cwd, "app.h");
        let types_h = touch(cwd, "include/types.h");
        let vector_h = touch(cwd, "usr/include/c++/vector");

        // Clang-style: uses absolute paths, continuation lines, targets
        // include both .o and .d.
        let content = format!(
            "app.o app.d: {} {} \\\n  {} \\\n  {}",
            source.display(),
            app_h.display(),
            types_h.display(),
            vector_h.display(),
        );

        let result = parse_depfile(&content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 3);
        assert!(result.resolved.contains(&canon(&app_h)));
        assert!(result.resolved.contains(&canon(&types_h)));
        assert!(result.resolved.contains(&canon(&vector_h)));
        assert!(!result.has_computed);
    }

    // ── Additional edge cases ────────────────────────────────────────

    #[test]
    fn whitespace_only_is_malformed() {
        let result = parse_depfile("   \n  \t  \n", Path::new("x.c"), Path::new("/tmp"));
        assert!(matches!(result, Err(DepfileError::Malformed(_))));
    }

    #[test]
    fn no_colon_is_malformed() {
        let result = parse_depfile("foo.o foo.c bar.h", Path::new("foo.c"), Path::new("/tmp"));
        assert!(matches!(result, Err(DepfileError::Malformed(_))));
    }

    #[test]
    fn parse_depfile_path_reads_file() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "src.c");
        let hdr = touch(cwd, "hdr.h");

        let depfile = cwd.join("src.d");
        std::fs::write(&depfile, "src.o: src.c hdr.h").unwrap();

        let result = parse_depfile_path(&depfile, &source, cwd).unwrap();
        assert_eq!(result.resolved.len(), 1);
        assert_eq!(result.resolved[0], canon(&hdr));
    }

    #[test]
    fn parse_depfile_path_missing_file() {
        let result = parse_depfile_path(
            Path::new("/nonexistent.d"),
            Path::new("x.c"),
            Path::new("/tmp"),
        );
        assert!(matches!(result, Err(DepfileError::Io(_))));
    }

    #[test]
    fn escaped_hash_in_path() {
        let content = r"foo.o: foo.c path\#2/bar.h";
        let source = Path::new("/nonexistent/foo.c");
        let cwd = Path::new("/nonexistent");

        let result = parse_depfile(content, source, cwd).unwrap();
        assert_eq!(result.resolved.len(), 1);
        let dep_str = result.resolved[0].to_string_lossy();
        assert!(
            dep_str.contains("path#2"),
            "expected unescaped '#' in path, got: {dep_str}"
        );
    }

    #[test]
    fn crlf_continuations() {
        let dir = TempDir::new().unwrap();
        let cwd = dir.path();
        let source = touch(cwd, "foo.c");
        let bar_h = touch(cwd, "bar.h");

        let content = "foo.o: foo.c \\\r\n  bar.h";
        let result = parse_depfile(content, &source, cwd).unwrap();

        assert_eq!(result.resolved.len(), 1);
        assert_eq!(result.resolved[0], canon(&bar_h));
    }

    #[test]
    fn display_impl_for_errors() {
        let io_err = DepfileError::Io(std::io::Error::new(
            std::io::ErrorKind::NotFound,
            "not found",
        ));
        let msg = format!("{io_err}");
        assert!(msg.contains("I/O error"));

        let mal_err = DepfileError::Malformed("bad content".to_string());
        let msg = format!("{mal_err}");
        assert!(msg.contains("malformed"));
        assert!(msg.contains("bad content"));
    }

    // ── Unit tests for internal helpers ──────────────────────────────

    #[test]
    fn join_continuations_replaces_with_space() {
        assert_eq!(join_continuations("a \\\n  b"), "a    b");
        assert_eq!(join_continuations("a \\\r\n  b"), "a    b");
    }

    #[test]
    fn join_continuations_preserves_other_backslashes() {
        assert_eq!(join_continuations(r"C:\path\file"), r"C:\path\file");
    }

    #[test]
    fn find_separator_colon_simple() {
        assert_eq!(find_separator_colon("foo.o: bar.c").unwrap(), 5);
    }

    #[test]
    fn find_separator_colon_skips_drive_letter() {
        // The colon at position 1 (C:) should be skipped; the real
        // separator is after "foo.o".
        let line = r"C:\build\foo.o: C:\src\bar.c";
        let pos = find_separator_colon(line).unwrap();
        assert_eq!(&line[pos..pos + 1], ":");
        // The separator should be at position 14 (after "C:\build\foo.o").
        assert_eq!(pos, 14);
    }

    #[test]
    fn split_and_unescape_basic() {
        let tokens = split_and_unescape(" foo.c  bar.h  baz.h ");
        assert_eq!(tokens, vec!["foo.c", "bar.h", "baz.h"]);
    }

    #[test]
    fn split_and_unescape_escaped_space() {
        let tokens = split_and_unescape(r" path\ with\ spaces/foo.h bar.h ");
        assert_eq!(tokens, vec!["path with spaces/foo.h", "bar.h"]);
    }

    #[test]
    fn split_and_unescape_escaped_hash() {
        let tokens = split_and_unescape(r" file\#1.h ");
        assert_eq!(tokens, vec!["file#1.h"]);
    }

    // ── Strategy tests ──────────────────────────────────────────────

    #[test]
    fn strategy_unsupported() {
        let dep_flags = UserDepFlags::default();
        let (args, strategy) =
            prepare_depfile(false, &dep_flags, Path::new("foo.o"), Path::new("/tmp"));
        assert!(args.is_empty());
        assert_eq!(strategy, DepfileStrategy::Unsupported);
    }

    #[test]
    fn strategy_user_mf() {
        let dep_flags = UserDepFlags {
            has_md: true,
            mf_path: Some(NormalizedPath::from("/build/deps.d")),
        };
        let (args, strategy) =
            prepare_depfile(true, &dep_flags, Path::new("foo.o"), Path::new("/tmp"));
        assert!(args.is_empty());
        assert_eq!(
            strategy,
            DepfileStrategy::UserSpecified {
                path: NormalizedPath::from("/build/deps.d")
            }
        );
    }

    #[test]
    fn strategy_user_md_no_mf() {
        let dep_flags = UserDepFlags {
            has_md: true,
            mf_path: None,
        };
        let (args, strategy) =
            prepare_depfile(true, &dep_flags, Path::new("foo.o"), Path::new("/tmp"));
        assert!(args.is_empty());
        assert_eq!(
            strategy,
            DepfileStrategy::UserDefault {
                path: NormalizedPath::from("foo.d")
            }
        );
    }

    #[test]
    fn strategy_injected() {
        let dep_flags = UserDepFlags::default();
        let (args, strategy) =
            prepare_depfile(true, &dep_flags, Path::new("foo.o"), Path::new("/tmp"));

        assert_eq!(args.len(), 3);
        assert_eq!(args[0], "-MD");
        assert_eq!(args[1], "-MF");
        assert!(args[2].ends_with(".d"));

        match strategy {
            DepfileStrategy::Injected { path } => {
                assert!(path.to_string_lossy().ends_with(".d"));
                assert!(path.starts_with("/tmp"));
            }
            other => panic!("expected Injected, got: {other:?}"),
        }
    }

    #[test]
    fn strategy_injected_adds_args() {
        let dep_flags = UserDepFlags::default();
        let (args, _) = prepare_depfile(true, &dep_flags, Path::new("bar.o"), Path::new("/tmp"));
        assert_eq!(args[0], "-MD");
        assert_eq!(args[1], "-MF");
        // The path should contain the stem "bar".
        assert!(
            args[2].contains("bar"),
            "expected 'bar' in path: {}",
            args[2]
        );
    }
}