zccache-compiler 1.2.12

Compiler detection and argument parsing for zccache
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
//! Response file (`@file`) expansion for compiler arguments.
//!
//! Both GCC and Clang support `@filename` syntax where the file contains
//! additional command-line arguments. This module handles reading and
//! expanding those files into the argument list.

use std::collections::HashSet;
use std::path::Path;

use zccache_core::NormalizedPath;

/// Maximum nesting depth for response files to prevent stack overflow.
const MAX_DEPTH: usize = 10;

/// Errors that can occur during response file expansion.
#[derive(Debug, thiserror::Error)]
pub enum ResponseFileError {
    /// The response file could not be read.
    #[error("failed to read response file '{path}': {source}")]
    ReadError {
        path: NormalizedPath,
        source: std::io::Error,
    },
    /// Circular reference detected among response files.
    #[error("circular response file reference: '{path}'")]
    CircularReference { path: NormalizedPath },
    /// Response file nesting exceeded the maximum depth.
    #[error("response file nesting too deep (max {MAX_DEPTH}): '{path}'")]
    TooDeep { path: NormalizedPath },
}

/// Parse the content of a response file into individual arguments.
///
/// Follows GCC/Clang response file conventions:
/// - Arguments are separated by whitespace (spaces, tabs, newlines)
/// - Arguments can be quoted with single (`'`) or double (`"`) quotes
/// - Inside double quotes, backslash escapes `\\`, `\"`, and `\n`
/// - Inside single quotes, no escape processing (literal content)
/// - Unquoted backslash is literal (important for Windows paths)
#[must_use]
pub fn parse_response_file_content(content: &str) -> Vec<String> {
    let mut result = Vec::new();
    let mut current = String::new();
    let mut in_arg = false;
    let mut chars = content.chars().peekable();

    while let Some(&ch) = chars.peek() {
        match ch {
            // Whitespace separates arguments
            ' ' | '\t' | '\n' | '\r' => {
                if in_arg {
                    result.push(std::mem::take(&mut current));
                    in_arg = false;
                }
                chars.next();
            }
            // Double-quoted string
            '"' => {
                in_arg = true;
                chars.next(); // consume opening quote
                loop {
                    match chars.next() {
                        Some('"') | None => break,
                        Some('\\') => match chars.next() {
                            Some('n') => current.push('\n'),
                            Some(c) => current.push(c),
                            None => break,
                        },
                        Some(c) => current.push(c),
                    }
                }
            }
            // Single-quoted string (no escapes)
            '\'' => {
                in_arg = true;
                chars.next(); // consume opening quote
                loop {
                    match chars.next() {
                        Some('\'') | None => break,
                        Some(c) => current.push(c),
                    }
                }
            }
            // Regular character (backslash is literal in unquoted context)
            _ => {
                in_arg = true;
                current.push(ch);
                chars.next();
            }
        }
    }

    if in_arg {
        result.push(current);
    }

    result
}

/// Expand response file references (`@filename`) in an argument list.
///
/// Scans `args` for arguments starting with `@`. For each such argument,
/// reads the referenced file, parses its contents, and splices the
/// resulting arguments into the list. Supports nested response files
/// up to `MAX_DEPTH` levels. Detects circular references.
///
/// Arguments that are exactly `@` (with no filename) are passed through
/// unchanged, as they are not valid response file references.
///
/// Resolves relative `@file` paths against the process's current working
/// directory. Use [`expand_response_files_in`] to specify a custom base directory.
pub fn expand_response_files(args: &[String]) -> Result<Vec<String>, ResponseFileError> {
    let cwd = std::env::current_dir().map_err(|e| ResponseFileError::ReadError {
        path: NormalizedPath::new("."),
        source: e,
    })?;
    expand_response_files_in(args, &cwd)
}

/// Expand response file references (`@filename`) with a custom base directory.
///
/// Like [`expand_response_files`], but resolves relative `@file` paths against
/// `base_dir` instead of the process's current working directory. For nested
/// `@file` references inside a response file, paths are resolved against the
/// parent file's directory (matching compiler behavior).
pub fn expand_response_files_in(
    args: &[String],
    base_dir: &Path,
) -> Result<Vec<String>, ResponseFileError> {
    let mut seen = HashSet::new();
    expand_recursive(args, base_dir, &mut seen, 0)
}

fn expand_recursive(
    args: &[String],
    base_dir: &Path,
    seen: &mut HashSet<NormalizedPath>,
    depth: usize,
) -> Result<Vec<String>, ResponseFileError> {
    let mut result = Vec::new();

    for arg in args {
        if let Some(filename) = arg.strip_prefix('@') {
            if filename.is_empty() {
                // Bare `@` is not a response file reference
                result.push(arg.clone());
                continue;
            }

            let raw_path = Path::new(filename);
            let resolved = if raw_path.is_absolute() {
                NormalizedPath::new(raw_path)
            } else {
                base_dir.join(raw_path).into()
            };
            let canonical = NormalizedPath::new(resolved.canonicalize().map_err(|e| {
                ResponseFileError::ReadError {
                    path: resolved.clone(),
                    source: e,
                }
            })?);

            if !seen.insert(canonical.clone()) {
                return Err(ResponseFileError::CircularReference { path: resolved });
            }

            if depth >= MAX_DEPTH {
                return Err(ResponseFileError::TooDeep { path: resolved });
            }

            let content =
                std::fs::read_to_string(&canonical).map_err(|e| ResponseFileError::ReadError {
                    path: resolved,
                    source: e,
                })?;

            // Nested @file references resolve against the parent file's directory
            let parent_dir = canonical.parent().unwrap_or(base_dir);

            let expanded_args = parse_response_file_content(&content);
            let nested = expand_recursive(&expanded_args, parent_dir, seen, depth + 1)?;
            result.extend(nested);

            // Remove from seen so the same file can appear in sibling branches
            // (circular = same file in ancestor chain, not sibling)
            seen.remove(&canonical);
        } else {
            result.push(arg.clone());
        }
    }

    Ok(result)
}

/// Maximum command-line length (in bytes) before we spill to a response file.
/// Windows `CreateProcess` has a 32,767 character limit. We use a conservative
/// threshold to account for the compiler path, env block, and quoting overhead.
#[cfg(windows)]
const MAX_CMDLINE_LEN: usize = 30_000;

/// Atomic counter for unique response file names.
#[cfg(windows)]
static RSP_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

/// Format arguments as response file content with proper quoting.
///
/// Each argument is written on its own line. Arguments containing spaces,
/// double quotes, or starting with `@` are double-quoted to prevent the
/// compiler from misinterpreting them. Inside quotes, `"` and `\` are
/// backslash-escaped.
#[cfg(any(windows, test))]
#[cfg_attr(not(test), allow(dead_code))]
fn format_rsp_content(args: &[String]) -> String {
    let estimated_len: usize = args.iter().map(|a| a.len() + 3).sum();
    let mut content = String::with_capacity(estimated_len);
    for arg in args {
        content.push_str(&format_rsp_argument(arg).expect("argument should be representable"));
        content.push('\n');
    }
    content
}

#[cfg(any(windows, test))]
fn format_rsp_content_if_safe(args: &[String]) -> Option<String> {
    let estimated_len: usize = args.iter().map(|a| a.len() + 3).sum();
    let mut content = String::with_capacity(estimated_len);
    for arg in args {
        content.push_str(&format_rsp_argument(arg)?);
        content.push('\n');
    }
    Some(content)
}

#[cfg(any(windows, test))]
fn format_rsp_argument(arg: &str) -> Option<String> {
    if arg.is_empty() {
        return Some("''".to_string());
    }

    let needs_quoting = arg.contains(char::is_whitespace)
        || arg.contains('"')
        || arg.contains('\'')
        || arg.starts_with('@');

    if !needs_quoting {
        return Some(arg.to_string());
    }

    // GCC/Clang response files on Windows treat single quotes literally.
    // Prefer them when possible so we preserve backslashes and embedded
    // double quotes exactly as they appeared in the original argv.
    if !arg.contains('\'') {
        return Some(format!("'{arg}'"));
    }

    // Double-quoted response file args are only safe when the argument
    // contains no backslashes, since our parser/compiler model would treat
    // backslashes as escapes and change Windows path semantics.
    if !arg.contains('\\') {
        let escaped = arg.replace('"', "\\\"");
        return Some(format!("\"{escaped}\""));
    }

    None
}

/// If the total length of `args` exceeds the Windows command-line limit, write
/// them to a temporary `.rsp` file and return a single `@path` argument.
/// Otherwise return `None` (caller should pass args directly).
///
/// The returned [`TempResponseFile`] keeps the temporary file alive via RAII.
/// Drop it after the compiler process finishes.
#[cfg(windows)]
pub fn write_response_file_if_needed(
    args: &[String],
    tmp_dir: &Path,
) -> std::io::Result<Option<TempResponseFile>> {
    let estimated_len: usize = args.iter().map(|a| a.len() + 3).sum();
    if estimated_len < MAX_CMDLINE_LEN {
        return Ok(None);
    }

    let id = RSP_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let rsp_path =
        NormalizedPath::new(tmp_dir.join(format!("zccache_{}_{}.rsp", std::process::id(), id)));
    let Some(content) = format_rsp_content_if_safe(args) else {
        return Ok(None);
    };
    std::fs::write(&rsp_path, content)?;

    Ok(Some(TempResponseFile { path: rsp_path }))
}

/// No-op on non-Windows platforms (command-line length is not an issue).
#[cfg(not(windows))]
pub fn write_response_file_if_needed(
    _args: &[String],
    _tmp_dir: &Path,
) -> std::io::Result<Option<TempResponseFile>> {
    Ok(None)
}

/// RAII guard for a temporary response file. Deletes the file on drop.
pub struct TempResponseFile {
    pub path: NormalizedPath,
}

impl TempResponseFile {
    /// Returns the `@path` argument to pass to the compiler.
    pub fn at_arg(&self) -> String {
        format!("@{}", self.path.display())
    }
}

impl Drop for TempResponseFile {
    fn drop(&mut self) {
        let _ = std::fs::remove_file(&self.path);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    fn s(v: &[&str]) -> Vec<String> {
        v.iter().map(|x| x.to_string()).collect()
    }

    // ── parse_response_file_content tests ──

    #[test]
    fn parse_simple_whitespace_separated() {
        let result = parse_response_file_content("-c foo.cpp -o foo.o");
        assert_eq!(result, s(&["-c", "foo.cpp", "-o", "foo.o"]));
    }

    #[test]
    fn parse_newline_separated() {
        let result = parse_response_file_content("-c\nfoo.cpp\n-o\nfoo.o\n");
        assert_eq!(result, s(&["-c", "foo.cpp", "-o", "foo.o"]));
    }

    #[test]
    fn parse_mixed_whitespace() {
        let result = parse_response_file_content("  -c \t foo.cpp \n -O2  ");
        assert_eq!(result, s(&["-c", "foo.cpp", "-O2"]));
    }

    #[test]
    fn parse_double_quoted_string() {
        let result = parse_response_file_content(r#"-DMSG="hello world" -c foo.c"#);
        assert_eq!(result, s(&["-DMSG=hello world", "-c", "foo.c"]));
    }

    #[test]
    fn parse_single_quoted_string() {
        let result = parse_response_file_content("-DMSG='hello world' -c foo.c");
        assert_eq!(result, s(&["-DMSG=hello world", "-c", "foo.c"]));
    }

    #[test]
    fn parse_escaped_backslash_in_double_quotes() {
        let result = parse_response_file_content(r#""-I C:\\path\\to\\include""#);
        assert_eq!(result, s(&["-I C:\\path\\to\\include"]));
    }

    #[test]
    fn parse_escaped_quote_in_double_quotes() {
        let result = parse_response_file_content(r#""-DMSG=\"hi\"""#);
        assert_eq!(result, s(&[r#"-DMSG="hi""#]));
    }

    #[test]
    fn parse_escaped_newline_in_double_quotes() {
        let result = parse_response_file_content(r#""-DMSG=line1\nline2""#);
        assert_eq!(result, s(&["-DMSG=line1\nline2"]));
    }

    #[test]
    fn parse_single_quotes_no_escapes() {
        // Single quotes are literal — backslash is not special
        let result = parse_response_file_content(r"'-DMSG=a\nb'");
        assert_eq!(result, s(&[r"-DMSG=a\nb"]));
    }

    #[test]
    fn parse_unquoted_backslash_literal() {
        // Backslash is literal in unquoted context (important for Windows paths)
        let result = parse_response_file_content(r"-IC:\Users\include");
        assert_eq!(result, s(&[r"-IC:\Users\include"]));
    }

    #[test]
    fn parse_empty_content() {
        let result = parse_response_file_content("");
        assert!(result.is_empty());
    }

    #[test]
    fn parse_only_whitespace() {
        let result = parse_response_file_content("   \n\t\r\n  ");
        assert!(result.is_empty());
    }

    #[test]
    fn parse_empty_quoted_string() {
        let result = parse_response_file_content(r#""""#);
        assert_eq!(result, s(&[""]));
    }

    #[test]
    fn parse_adjacent_quoted_and_unquoted() {
        // -I"path with spaces" should produce -Ipath with spaces as one arg
        let result = parse_response_file_content(r#"-I"path with spaces""#);
        assert_eq!(result, s(&["-Ipath with spaces"]));
    }

    // ── expand_response_files tests ──

    #[test]
    fn expand_no_at_files() {
        let args = s(&["-c", "foo.cpp", "-o", "foo.o"]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(result, args);
    }

    #[test]
    fn expand_bare_at_passthrough() {
        // A bare `@` with no filename is not a response file reference
        let args = s(&["-c", "@", "foo.cpp"]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(result, args);
    }

    #[test]
    fn expand_single_response_file() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "-O2 -Wall -DNDEBUG").unwrap();

        let path = f.path().to_str().unwrap();
        let args = s(&["-c", "foo.cpp", &format!("@{path}")]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(result, s(&["-c", "foo.cpp", "-O2", "-Wall", "-DNDEBUG"]));
    }

    #[test]
    fn expand_response_file_with_quoted_args() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, r#"-DMSG="hello world" -I"/path/to/include""#).unwrap();

        let path = f.path().to_str().unwrap();
        let args = s(&["-c", "foo.cpp", &format!("@{path}")]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(
            result,
            s(&["-c", "foo.cpp", "-DMSG=hello world", "-I/path/to/include"])
        );
    }

    #[test]
    fn expand_multiple_response_files() {
        let mut f1 = NamedTempFile::new().unwrap();
        writeln!(f1, "-O2 -Wall").unwrap();
        let mut f2 = NamedTempFile::new().unwrap();
        writeln!(f2, "-DNDEBUG -std=c++17").unwrap();

        let p1 = f1.path().to_str().unwrap();
        let p2 = f2.path().to_str().unwrap();
        let args = s(&["-c", "foo.cpp", &format!("@{p1}"), &format!("@{p2}")]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(
            result,
            s(&["-c", "foo.cpp", "-O2", "-Wall", "-DNDEBUG", "-std=c++17"])
        );
    }

    #[test]
    fn expand_nested_response_files() {
        let mut inner = NamedTempFile::new().unwrap();
        writeln!(inner, "-DINNER=1").unwrap();

        let inner_path = inner.path().to_str().unwrap();
        let mut outer = NamedTempFile::new().unwrap();
        writeln!(outer, "-DOUTER=1 @{inner_path}").unwrap();

        let outer_path = outer.path().to_str().unwrap();
        let args = s(&["-c", "foo.cpp", &format!("@{outer_path}")]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(result, s(&["-c", "foo.cpp", "-DOUTER=1", "-DINNER=1"]));
    }

    #[test]
    fn expand_missing_file_errors() {
        let args = s(&["-c", "foo.cpp", "@/nonexistent/file.rsp"]);
        let result = expand_response_files(&args);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, ResponseFileError::ReadError { .. }));
    }

    #[test]
    fn expand_circular_reference_errors() {
        // Create two files that reference each other
        let dir = tempfile::tempdir().unwrap();
        let path_a = dir.path().join("a.rsp");
        let path_b = dir.path().join("b.rsp");

        std::fs::write(&path_a, format!("@{}", path_b.display())).unwrap();
        std::fs::write(&path_b, format!("@{}", path_a.display())).unwrap();

        let args = s(&["-c", "foo.cpp", &format!("@{}", path_a.display())]);
        let result = expand_response_files(&args);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            matches!(err, ResponseFileError::CircularReference { .. }),
            "expected CircularReference, got: {err}"
        );
    }

    #[test]
    fn expand_self_reference_errors() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("self.rsp");
        std::fs::write(&path, format!("-O2 @{}", path.display())).unwrap();

        let args = s(&[&format!("@{}", path.display())]);
        let result = expand_response_files(&args);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            matches!(err, ResponseFileError::CircularReference { .. }),
            "expected CircularReference, got: {err}"
        );
    }

    #[test]
    fn expand_preserves_arg_order() {
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "-B -C").unwrap();

        let path = f.path().to_str().unwrap();
        let args = s(&["-A", &format!("@{path}"), "-D"]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(result, s(&["-A", "-B", "-C", "-D"]));
    }

    #[test]
    fn expand_same_file_in_siblings_ok() {
        // The same file referenced twice at the same depth is OK
        // (circular = same file in ancestor chain)
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "-O2").unwrap();

        let path = f.path().to_str().unwrap();
        let args = s(&[&format!("@{path}"), &format!("@{path}")]);
        let result = expand_response_files(&args).unwrap();
        assert_eq!(result, s(&["-O2", "-O2"]));
    }

    #[test]
    fn expand_integration_with_parse_invocation() {
        // End-to-end: expand response files, then parse the invocation
        let mut f = NamedTempFile::new().unwrap();
        writeln!(f, "-O2 -Wall -DNDEBUG").unwrap();

        let path = f.path().to_str().unwrap();
        let args = s(&["-c", "foo.cpp", "-o", "foo.o", &format!("@{path}")]);
        let expanded = expand_response_files(&args).unwrap();
        let result = crate::parse_invocation("gcc", &expanded);
        match result {
            crate::ParsedInvocation::Cacheable(c) => {
                assert_eq!(c.source_file, NormalizedPath::new("foo.cpp"));
                assert_eq!(c.output_file, NormalizedPath::new("foo.o"));
                assert!(c.original_args.contains(&"-O2".to_string()));
                assert!(c.original_args.contains(&"-Wall".to_string()));
                assert!(c.original_args.contains(&"-DNDEBUG".to_string()));
            }
            _ => panic!("unexpected variant"),
        }
    }

    // ── expand_response_files_in tests ──

    #[test]
    fn expand_in_resolves_relative_against_base_dir() {
        let dir = tempfile::tempdir().unwrap();
        let rsp_path = dir.path().join("flags.rsp");
        std::fs::write(&rsp_path, "-O2 -Wall").unwrap();

        // Use relative name, resolve against dir
        let args = s(&["@flags.rsp", "-c", "foo.cpp"]);
        let result = expand_response_files_in(&args, dir.path()).unwrap();
        assert_eq!(result, s(&["-O2", "-Wall", "-c", "foo.cpp"]));
    }

    #[test]
    fn expand_in_absolute_path_ignores_base_dir() {
        let dir = tempfile::tempdir().unwrap();
        let rsp_path = dir.path().join("flags.rsp");
        std::fs::write(&rsp_path, "-O2").unwrap();

        let abs_ref = format!("@{}", rsp_path.display());
        let args = s(&[&abs_ref, "-c", "foo.cpp"]);
        // base_dir is irrelevant for absolute paths
        let other_dir = tempfile::tempdir().unwrap();
        let result = expand_response_files_in(&args, other_dir.path()).unwrap();
        assert_eq!(result, s(&["-O2", "-c", "foo.cpp"]));
    }

    #[test]
    fn expand_in_nested_resolves_against_parent_dir() {
        // outer/ contains outer.rsp which references @inner.rsp
        // inner/ (sibling) contains inner.rsp
        // But inner.rsp is in same dir as outer.rsp, so it resolves correctly.
        let dir = tempfile::tempdir().unwrap();
        let sub = dir.path().join("sub");
        std::fs::create_dir_all(&sub).unwrap();

        let inner_path = sub.join("inner.rsp");
        std::fs::write(&inner_path, "-DINNER=1").unwrap();

        let outer_path = sub.join("outer.rsp");
        std::fs::write(&outer_path, "-DOUTER=1 @inner.rsp").unwrap();

        // Resolve @sub/outer.rsp against base dir
        let args = s(&["-c", "foo.cpp", "@sub/outer.rsp"]);
        let result = expand_response_files_in(&args, dir.path()).unwrap();
        assert_eq!(result, s(&["-c", "foo.cpp", "-DOUTER=1", "-DINNER=1"]));
    }

    #[test]
    fn expand_in_nested_relative_cross_directory() {
        // base_dir/outer.rsp references @subdir/inner.rsp
        // subdir/inner.rsp exists relative to outer.rsp's directory
        let dir = tempfile::tempdir().unwrap();
        let subdir = dir.path().join("subdir");
        std::fs::create_dir_all(&subdir).unwrap();

        std::fs::write(subdir.join("inner.rsp"), "-DINNER=1").unwrap();
        std::fs::write(dir.path().join("outer.rsp"), "@subdir/inner.rsp -DOUTER=1").unwrap();

        let args = s(&["@outer.rsp"]);
        let result = expand_response_files_in(&args, dir.path()).unwrap();
        assert_eq!(result, s(&["-DINNER=1", "-DOUTER=1"]));
    }

    #[test]
    fn expand_in_dotdot_traversal() {
        // base_dir/sub/outer.rsp references @../sibling.rsp
        let dir = tempfile::tempdir().unwrap();
        let sub = dir.path().join("sub");
        std::fs::create_dir_all(&sub).unwrap();

        std::fs::write(dir.path().join("sibling.rsp"), "-DSIBLING=1").unwrap();
        std::fs::write(sub.join("outer.rsp"), "@../sibling.rsp -DOUTER=1").unwrap();

        let args = s(&["@sub/outer.rsp"]);
        let result = expand_response_files_in(&args, dir.path()).unwrap();
        assert_eq!(result, s(&["-DSIBLING=1", "-DOUTER=1"]));
    }

    #[test]
    fn expand_in_nested_absolute_inside_relative() {
        // @relative.rsp contains @/absolute/path.rsp — absolute ref ignores parent dir
        let dir = tempfile::tempdir().unwrap();
        let sub = dir.path().join("sub");
        std::fs::create_dir_all(&sub).unwrap();

        let abs_rsp = sub.join("abs.rsp");
        std::fs::write(&abs_rsp, "-DABS=1").unwrap();

        // outer.rsp in base_dir references abs.rsp by absolute path
        std::fs::write(
            dir.path().join("outer.rsp"),
            format!("@{} -DOUTER=1", abs_rsp.display()),
        )
        .unwrap();

        let args = s(&["@outer.rsp"]);
        let result = expand_response_files_in(&args, dir.path()).unwrap();
        assert_eq!(result, s(&["-DABS=1", "-DOUTER=1"]));
    }

    #[test]
    fn expand_in_three_level_relative_chain() {
        // a/1.rsp -> @b/2.rsp -> @c/3.rsp — each relative to its parent
        let dir = tempfile::tempdir().unwrap();
        let a = dir.path().join("a");
        let ab = a.join("b");
        let abc = ab.join("c");
        std::fs::create_dir_all(&abc).unwrap();

        std::fs::write(abc.join("3.rsp"), "-DLEVEL3=1").unwrap();
        std::fs::write(ab.join("2.rsp"), "@c/3.rsp -DLEVEL2=1").unwrap();
        std::fs::write(a.join("1.rsp"), "@b/2.rsp -DLEVEL1=1").unwrap();

        let args = s(&["@a/1.rsp"]);
        let result = expand_response_files_in(&args, dir.path()).unwrap();
        assert_eq!(result, s(&["-DLEVEL3=1", "-DLEVEL2=1", "-DLEVEL1=1"]));
    }

    #[test]
    fn expand_in_error_shows_resolved_path() {
        // @relative.rsp that doesn't exist — error should show resolved path, not raw
        let dir = tempfile::tempdir().unwrap();
        let args = s(&["@missing.rsp"]);
        let err = expand_response_files_in(&args, dir.path()).unwrap_err();
        match &err {
            ResponseFileError::ReadError { path, .. } => {
                // Error path should be the resolved path (base_dir/missing.rsp)
                assert!(
                    path.starts_with(dir.path()),
                    "error path {path:?} should be under {dir:?}",
                    dir = dir.path()
                );
            }
            other => panic!("expected ReadError, got: {other}"),
        }
    }

    #[test]
    fn expand_in_circular_in_custom_base_dir() {
        let dir = tempfile::tempdir().unwrap();
        let a = dir.path().join("a.rsp");
        let b = dir.path().join("b.rsp");
        std::fs::write(&a, format!("@{}", b.display())).unwrap();
        std::fs::write(&b, format!("@{}", a.display())).unwrap();

        let args = s(&["@a.rsp"]);
        let err = expand_response_files_in(&args, dir.path()).unwrap_err();
        assert!(
            matches!(err, ResponseFileError::CircularReference { .. }),
            "expected CircularReference, got: {err}"
        );
    }

    #[test]
    fn expand_in_depth_limit_with_custom_base_dir() {
        // Chain of 11 files (depth 0..10, exceeds MAX_DEPTH=10)
        let dir = tempfile::tempdir().unwrap();
        for i in (0..=MAX_DEPTH).rev() {
            let name = format!("{i}.rsp");
            let content = if i == MAX_DEPTH {
                "-DLEAF=1".to_string()
            } else {
                format!("@{}.rsp", i + 1)
            };
            std::fs::write(dir.path().join(name), content).unwrap();
        }

        let args = s(&["@0.rsp"]);
        let err = expand_response_files_in(&args, dir.path()).unwrap_err();
        assert!(
            matches!(err, ResponseFileError::TooDeep { .. }),
            "expected TooDeep, got: {err}"
        );
    }

    // ── format_rsp_content / write_response_file tests ──

    #[test]
    fn format_rsp_simple_args() {
        let args = s(&["-c", "foo.cpp", "-O2"]);
        let content = format_rsp_content(&args);
        assert_eq!(content, "-c\nfoo.cpp\n-O2\n");
    }

    #[test]
    fn format_rsp_quotes_spaces() {
        let args = s(&["-I/path with spaces/include", "-c"]);
        let content = format_rsp_content(&args);
        assert_eq!(content, "'-I/path with spaces/include'\n-c\n");
    }

    #[test]
    fn format_rsp_escapes_quotes() {
        let args = s(&[r#"-DMSG="hello""#]);
        let content = format_rsp_content(&args);
        assert_eq!(content, "'-DMSG=\"hello\"'\n");
    }

    #[test]
    fn format_rsp_escapes_backslash_in_quoted() {
        let args = s(&[r"-IC:\path with spaces\include"]);
        let content = format_rsp_content(&args);
        assert_eq!(content, "'-IC:\\path with spaces\\include'\n");
    }

    #[test]
    fn format_rsp_quotes_at_prefix() {
        // Args starting with @ must be quoted to prevent the compiler
        // from interpreting them as nested response file references.
        let args = s(&["@rpath/lib", "-c"]);
        let content = format_rsp_content(&args);
        assert_eq!(content, "'@rpath/lib'\n-c\n");
    }

    #[test]
    fn format_rsp_roundtrip() {
        // Write -> parse should recover the original args
        let args = s(&[
            "-c",
            "foo.cpp",
            "-I/path with spaces",
            r#"-DMSG="hello""#,
            "@rpath/lib",
            "-O2",
            r"-IC:\Users\include",
        ]);
        let content = format_rsp_content(&args);
        let parsed = parse_response_file_content(&content);
        assert_eq!(parsed, args);
    }

    #[test]
    fn format_rsp_declines_unsafe_single_quote_with_backslash() {
        let args = s(&[r"C:\path with spaces\o'hare"]);
        assert!(format_rsp_content_if_safe(&args).is_none());
    }

    #[test]
    fn format_rsp_prefers_single_quotes_for_windows_gcc_shapes() {
        let args = s(&[
            r#"-DVALUE="C:\Program Files\SDK\include""#,
            r"C:\work tree\main.c",
        ]);
        let content = format_rsp_content_if_safe(&args).unwrap();
        assert_eq!(
            content,
            "'-DVALUE=\"C:\\Program Files\\SDK\\include\"'\n'C:\\work tree\\main.c'\n"
        );
        assert_eq!(parse_response_file_content(&content), args);
    }

    #[test]
    fn temp_response_file_at_arg() {
        let path = NormalizedPath::new("/tmp/test.rsp");
        let rsp = TempResponseFile { path };
        assert_eq!(rsp.at_arg(), format!("@{}", rsp.path.display()));
        std::mem::forget(rsp);
    }

    #[test]
    fn temp_response_file_cleanup_on_drop() {
        let dir = tempfile::tempdir().unwrap();
        let rsp_path = dir.path().join("test.rsp");
        std::fs::write(&rsp_path, "test").unwrap();
        assert!(rsp_path.exists());

        let rsp = TempResponseFile {
            path: rsp_path.clone().into(),
        };
        drop(rsp);
        assert!(!rsp_path.exists());
    }
}