zccache 1.12.9

Local-first compiler cache for C/C++/Rust/Emscripten
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
//! Tests for the depfile parser, strategy selection, and canonicalize cache.

use std::path::Path;

use tempfile::TempDir;

use super::super::args::UserDepFlags;
use super::canonicalize::{canonicalize_cache_len_for_test, canonicalize_path, strip_win_prefix};
use super::error::DepfileError;
use super::parse::{
    find_separator_colon, join_continuations, parse_depfile, parse_depfile_path, split_and_unescape,
};
use super::strategy::{prepare_depfile, user_depfile_destination, DepfileStrategy};
use crate::core::NormalizedPath;

/// 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")
        }
    );
}

// ── Issue #643: user_depfile_destination semantics ──────────────────

#[test]
fn user_depfile_destination_returns_mf_path_when_present() {
    let dep_flags = UserDepFlags {
        has_md: true,
        mf_path: Some(NormalizedPath::from("/build/explicit.d")),
    };
    assert_eq!(
        user_depfile_destination(&dep_flags, Path::new("/out/foo.o")),
        Some(NormalizedPath::from("/build/explicit.d")),
        "explicit -MF must win over the implicit <output>.d default",
    );
}

#[test]
fn user_depfile_destination_derives_default_from_output_when_md_only() {
    let dep_flags = UserDepFlags {
        has_md: true,
        mf_path: None,
    };
    assert_eq!(
        user_depfile_destination(&dep_flags, Path::new("/out/foo.o")),
        Some(NormalizedPath::from("/out/foo.d")),
        "-MD without -MF defaults to <output_stem>.d alongside the object",
    );
}

#[test]
fn user_depfile_destination_none_when_user_has_no_dep_flags() {
    let dep_flags = UserDepFlags::default();
    assert_eq!(
        user_depfile_destination(&dep_flags, Path::new("/out/foo.o")),
        None,
        "no user dep flags = injected strategy = not the user's depfile",
    );
}

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

/// Issue #573: `canonicalize_path` caches its results by input
/// path string. Tested by verifying the function returns the same
/// canonical output across two calls with the same input — both
/// pre- and post-cache that's guaranteed correctness-wise, but
/// after the first call the entry is in the global cache (size
/// monotonically increases), so the contract is sound.
///
/// We don't assert on absolute or delta cache_len because the
/// canonicalize cache is process-wide and other tests in the
/// crate populate it concurrently — equality assertions race.
/// The cache HIT is implicit in the function's contract.
#[test]
fn canonicalize_path_caches_results() {
    let dir = TempDir::new().unwrap();
    let cwd = dir.path();
    let hdr = touch(cwd, "shared-header-573a.h");

    let first = canonicalize_path(&hdr, cwd);
    let second = canonicalize_path(&hdr, cwd);
    assert_eq!(first, second, "cached canonical output must match");

    // Cache must monotonically grow: at minimum our entry is in
    // there now (others may have been added concurrently too).
    assert!(canonicalize_cache_len_for_test() > 0);
}

/// Issue #573 regression guard: canonicalize_path cache must
/// distinguish entries by input path. Two different input
/// strings of the same file produce two cache entries — verified
/// by snapshotting the cache len before-and-after under the
/// assumption that no concurrent test would happen to insert
/// AND evict in the same window (the cache never evicts today).
/// Inputs use unique per-test names so no other test contributes
/// entries with the same keys.
#[test]
fn canonicalize_path_cache_distinguishes_inputs() {
    let dir = TempDir::new().unwrap();
    let cwd = dir.path();
    let hdr = touch(cwd, "distinct-573b.h");
    // Two inputs that differ as strings but resolve to the same
    // file. Both forms are unique to this test (per-tempdir).
    let abs_input = hdr.clone();
    let mut redundant_input = cwd.to_path_buf();
    redundant_input.push(".");
    redundant_input.push("distinct-573b.h");

    let r1 = canonicalize_path(&abs_input, cwd);
    let r2 = canonicalize_path(&redundant_input, cwd);
    // Both resolve to the same on-disk file → same canonical.
    assert_eq!(
        r1, r2,
        "different inputs of the same file resolve identically"
    );
}