runner-run 0.11.0

Universal project task runner
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
//! Turborepo — monorepo build system.

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

use anyhow::Context as _;
use serde::Deserialize;

use crate::tool::files;

/// Directories produced by Turborepo.
pub(crate) const CLEAN_DIRS: &[&str] = &[".turbo"];

/// Supported Turborepo config filenames (priority order). Turborepo v2
/// accepts both `turbo.json` and `turbo.jsonc` natively.
pub(crate) const FILENAMES: &[&str] = &["turbo.json", "turbo.jsonc"];

/// Resolve the active Turborepo config in `dir`, if any.
pub(crate) fn find_config(dir: &Path) -> Option<PathBuf> {
    files::find_first(dir, FILENAMES).filter(|path| path.is_file())
}

/// Detected via `turbo.json` or `turbo.jsonc`.
pub(crate) fn detect(dir: &Path) -> bool {
    find_config(dir).is_some()
}

/// Parse task names from `turbo.json` / `turbo.jsonc`.
///
/// Supports both v2 (`"tasks"`) and v1 (`"pipeline"`) schemas. Workspace-
/// scoped entries like `"my-app#build"` are filtered out, while Root Task
/// entries (`"//#lint"`) are surfaced as their bare name (`"lint"`) — both
/// are invoked the same way as a plain task. JSONC syntax (line comments,
/// block comments, trailing commas) is accepted under either filename,
/// matching Turborepo's own parser.
pub(crate) fn extract_tasks(dir: &Path) -> anyhow::Result<Vec<String>> {
    #[derive(Deserialize)]
    struct Partial {
        tasks: Option<HashMap<String, serde_json::Value>>,
        pipeline: Option<HashMap<String, serde_json::Value>>,
    }
    let Some(path) = find_config(dir) else {
        return Ok(vec![]);
    };
    let content = std::fs::read_to_string(&path)
        .with_context(|| format!("failed to read {}", path.display()))?;
    let p = json5::from_str::<Partial>(&content)
        .with_context(|| format!("{} is not valid JSON/JSONC", path.display()))?;
    let Some(tasks) = p.tasks.or(p.pipeline) else {
        return Ok(vec![]);
    };
    Ok(tasks
        .into_keys()
        .filter_map(classify_task_key)
        .collect::<HashSet<_>>()
        .into_iter()
        .collect())
}

/// Map a raw `tasks`/`pipeline` key to the runnable task name, or drop it.
///
/// - `"//#lint"` → `Some("lint")` (Root Task — surface bare name).
/// - `"web#build"` → `None` (workspace-scoped — invoked as `web#build`,
///   not as a top-level task).
/// - `"build"` → `Some("build")` (plain task).
/// - `"//#"` or `"//#a#b"` → `None` (malformed).
fn classify_task_key(name: String) -> Option<String> {
    if let Some(rest) = name.strip_prefix("//#") {
        return (!rest.is_empty() && !rest.contains('#')).then(|| rest.to_string());
    }
    (!name.contains('#')).then_some(name)
}

/// `turbo run <task> [-- args...]`
pub(crate) fn run_cmd(task: &str, args: &[String]) -> Command {
    let mut c = super::program::command("turbo");
    c.arg("run").arg(task);
    if !args.is_empty() {
        c.arg("--").args(args);
    }
    c
}

/// Returns `true` if `command` is a thin invocation of `turbo` that targets
/// the same task name — i.e. `turbo run <name>` or the shorthand
/// `turbo <name>`, optionally followed by flag tokens (e.g. `--filter web`,
/// `--concurrency=4`).
///
/// The tail after the target name must consist solely of flag tokens
/// (`-x`, `--long`, `--key=value`), values immediately following a
/// non-`=` flag, or — after a bare `--` end-of-options separator —
/// args forwarded to the underlying task. The `--` separator itself
/// is recognized as a marker (POSIX/getopt convention), not as a
/// flag; turbo's own argument-forwarding pattern is `turbo run <task>
/// -- <args...>`. Any of these reject the match — they mean the
/// script does more than just dispatch to turbo:
/// - shell control operators: `&&`, `||`, `;`, `;;`, `;&`, `;;&`,
///   `|`, `|&`, `&`, `!`, `{`, `}`, `(`, `)`
/// - redirect operators: bare `>`/`<`/`>>`/`<<`/`<<<`, combined-fd
///   `&>`/`&>>`/`>&`, fd-prefixed `2>`, `1>`, composite `2>&1`,
///   `1>&2`, `2>/dev/null`, `&>file.log`
/// - shell expansion tokens (anything containing `$` or backtick):
///   parameter expansion (`$X`, `${X}`, `${X:-def}`, `${X//a/b}`,
///   `${!X}`, `${#X}`, `${X[@]}`), special vars (`$@`, `$*`, `$#`,
///   `$?`), command substitution (`$(cmd)`, `` `cmd` ``), arithmetic
///   (`$((expr))`), and quoted forms with embedded expansion
///   (`"${X}"`)
/// - any other bare positional that isn't consuming a flag's value
///
/// This is purely a textual heuristic on the script body. Indirect
/// invocations (`npx turbo run build`, `pnpm exec turbo run build`) are
/// intentionally not matched: a wrapper that goes through a package-manager
/// shim is a step removed from the canonical Turborepo pattern, and matching
/// it would risk false positives for unrelated `npx`/`pnpm exec` scripts.
///
/// Known limitations (deferred — rare in turbo dispatch scripts and
/// stricter detection would over-reject legitimate patterns):
/// - unquoted globs (`*`, `?`) following a flag are accepted because
///   `*` is legitimate in turbo filters like `@scope/*`;
/// - tilde expansion (`~/cache`) following a flag is accepted because
///   `~` is legitimate in path values like `--cache-dir`;
/// - brace expansion (`{a,b,c}`) following a flag is accepted because
///   distinguishing it from quoted JSON values is fragile;
/// - quoted multi-word arguments (`--filter "my app"`) are split
///   incorrectly by `split_whitespace` and reject via the positional
///   rule — false negative, safe direction (script stays visible);
/// - single-quoted literals containing `$` or backtick (e.g. `'$X'`)
///   are rejected even though shell-literal — extremely rare in turbo
///   scripts, false negative, safe direction.
pub(crate) fn is_self_passthrough(name: &str, command: &str) -> bool {
    let mut tokens = command.split_whitespace();
    if tokens.next() != Some("turbo") {
        return false;
    }
    let Some(second) = tokens.next() else {
        return false;
    };
    let target = if second == "run" {
        let Some(third) = tokens.next() else {
            return false;
        };
        third
    } else {
        second
    };
    if target != name {
        return false;
    }

    // Tail must be flags-only (with optional space-separated values),
    // or a `--` end-of-options marker followed by forwarded args.
    // Any bare positional, shell metachar, redirect operator, or
    // shell-expansion token means the script is doing extra work
    // and is not a thin passthrough.
    //
    // Order matters: control / redirect / expansion checks all run
    // BEFORE flag-value consumption so that tokens like `2>&1`,
    // `|&`, or `$X` positioned after a value-expecting flag are
    // rejected, not silently swallowed as the flag's value.
    //
    // `--` (POSIX end-of-options separator): turbo forwards everything
    // after `--` to the underlying task. Once seen, remaining tokens
    // are accepted as forwarded args (still rejecting shell metachars,
    // redirects, and expansions which would mean the script body does
    // real work beyond dispatching to turbo).
    let mut expects_flag_value = false;
    let mut after_double_dash = false;
    for token in tokens {
        if is_shell_control_token(token) {
            return false;
        }
        if looks_like_redirect(token) {
            return false;
        }
        if looks_like_shell_expansion(token) {
            return false;
        }
        if after_double_dash {
            continue;
        }
        if token == "--" {
            after_double_dash = true;
            continue;
        }
        if token.starts_with('-') {
            expects_flag_value = !token.contains('=');
            continue;
        }
        if expects_flag_value {
            expects_flag_value = false;
            continue;
        }
        return false;
    }
    true
}

/// Detects standalone bash control operators that introduce extra
/// behavior beyond a thin turbo dispatch:
/// - list operators (`&&`, `||`)
/// - command separators (`;`, `;;`, `;&`, `;;&`)
/// - pipes (`|`, `|&`)
/// - background (`&`)
/// - pipeline negation (`!`)
/// - group/subshell delimiters (`{`, `}`, `(`, `)`)
fn is_shell_control_token(token: &str) -> bool {
    matches!(
        token,
        "&&" | "||" | ";" | ";;" | ";&" | ";;&" | "|" | "|&" | "&" | "!" | "{" | "}" | "(" | ")"
    )
}

/// Detects bash redirect operators in any of these forms:
/// - bare: `>`, `>>`, `<`, `<<`, `<<<`
/// - combined-fd: `&>`, `&>>`, `>&`
/// - fd-prefixed: `2>`, `1>`, `3<`, …
/// - composite: `2>&1`, `1>&2`, `2>/dev/null`, `&>file.log`, `>file`
///
/// The strategy: strip optional leading file-descriptor digits, then
/// optional leading `&` (for combined-fd forms), then check whether
/// what remains starts with `>` or `<`.
fn looks_like_redirect(token: &str) -> bool {
    let rest = token.trim_start_matches(|c: char| c.is_ascii_digit());
    let rest = rest.strip_prefix('&').unwrap_or(rest);
    rest.starts_with('>') || rest.starts_with('<')
}

/// Detects tokens that perform shell expansion: parameter expansion,
/// command substitution, or arithmetic. Any of these mean the script's
/// effective behavior depends on shell state at run time, so it isn't
/// a thin turbo dispatch.
///
/// Catches the full bash expansion family in one rule: presence of
/// `$` covers `$X`, `${X}`, `${X:-def}`, `${X//a/b}`, `${!X}`,
/// `${#X}`, `${X[@]}`, special vars (`$@`, `$*`, `$#`, `$?`, `$$`,
/// `$!`, `$_`, `$0`-`$9`, `${10}+`), `$(cmd)`, `$((expr))`, and any
/// double-quoted form with embedded expansion (`"${X}"`). Backtick
/// covers the legacy `` `cmd` `` substitution form.
fn looks_like_shell_expansion(token: &str) -> bool {
    token.contains('$') || token.contains('`')
}

#[cfg(test)]
#[allow(
    clippy::literal_string_with_formatting_args,
    reason = "test fixtures embed bash parameter-expansion strings like \
              `${X:-default}` and `${X:+alt}` as input to is_self_passthrough; \
              the lint mistakes them for Rust format args."
)]
mod tests {
    use std::fs;

    use super::{detect, extract_tasks};
    use crate::tool::test_support::TempDir;

    #[test]
    fn extract_tasks_returns_empty_when_turbo_json_is_missing() {
        let dir = TempDir::new("turbo-missing");

        assert!(
            extract_tasks(dir.path())
                .expect("missing turbo.json should be ok")
                .is_empty()
        );
    }

    #[test]
    fn extract_tasks_errors_on_malformed_json() {
        let dir = TempDir::new("turbo-malformed");
        fs::write(dir.path().join("turbo.json"), "{").expect("turbo.json should be written");

        assert!(extract_tasks(dir.path()).is_err());
    }

    #[test]
    fn extract_tasks_returns_empty_when_no_task_table_exists() {
        let dir = TempDir::new("turbo-empty");
        fs::write(dir.path().join("turbo.json"), "{}").expect("turbo.json should be written");

        assert!(
            extract_tasks(dir.path())
                .expect("empty turbo config should parse")
                .is_empty()
        );
    }

    #[test]
    fn extract_tasks_reads_v2_tasks_schema() {
        let dir = TempDir::new("turbo-v2");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"tasks":{"build":{},"lint":{},"web#build":{}}}"#,
        )
        .expect("turbo.json should be written");

        let mut tasks = extract_tasks(dir.path()).expect("v2 turbo config should parse");
        tasks.sort_unstable();

        assert_eq!(tasks, ["build", "lint"]);
    }

    #[test]
    fn extract_tasks_reads_v1_pipeline_schema() {
        let dir = TempDir::new("turbo-v1");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"pipeline":{"test":{},"typecheck":{},"pkg#build":{}}}"#,
        )
        .expect("turbo.json should be written");

        let mut tasks = extract_tasks(dir.path()).expect("v1 turbo config should parse");
        tasks.sort_unstable();

        assert_eq!(tasks, ["test", "typecheck"]);
    }

    #[test]
    fn detect_finds_turbo_jsonc_filename() {
        let dir = TempDir::new("turbo-detect-jsonc");
        fs::write(dir.path().join("turbo.jsonc"), r#"{"tasks":{"build":{}}}"#)
            .expect("turbo.jsonc should be written");

        assert!(detect(dir.path()));
    }

    #[test]
    fn extract_tasks_reads_turbo_jsonc_filename() {
        let dir = TempDir::new("turbo-jsonc-filename");
        fs::write(
            dir.path().join("turbo.jsonc"),
            r#"{"tasks":{"build":{},"lint":{}}}"#,
        )
        .expect("turbo.jsonc should be written");

        let mut tasks = extract_tasks(dir.path()).expect("turbo.jsonc should parse");
        tasks.sort_unstable();

        assert_eq!(tasks, ["build", "lint"]);
    }

    #[test]
    fn extract_tasks_accepts_trailing_commas_in_turbo_json() {
        let dir = TempDir::new("turbo-trailing-comma");
        fs::write(dir.path().join("turbo.json"), r#"{"tasks":{"build":{},}}"#)
            .expect("turbo.json should be written");

        let tasks = extract_tasks(dir.path()).expect("trailing comma should parse");

        assert_eq!(tasks, ["build"]);
    }

    #[test]
    fn extract_tasks_accepts_jsonc_comments_under_either_filename() {
        let dir = TempDir::new("turbo-jsonc-comments");
        fs::write(
            dir.path().join("turbo.jsonc"),
            r#"{
  // line comment
  "tasks": {
    /* block comment */
    "build": {},
    "test": {},
  },
}
"#,
        )
        .expect("turbo.jsonc should be written");

        let mut tasks = extract_tasks(dir.path()).expect("jsonc should parse");
        tasks.sort_unstable();

        assert_eq!(tasks, ["build", "test"]);
    }

    #[test]
    fn extract_tasks_surfaces_root_tasks_with_bare_name() {
        let dir = TempDir::new("turbo-root-tasks");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"tasks":{"//#lint":{},"//#format":{"cache":false}}}"#,
        )
        .expect("turbo.json should be written");

        let mut tasks = extract_tasks(dir.path()).expect("root tasks should parse");
        tasks.sort_unstable();

        assert_eq!(tasks, ["format", "lint"]);
    }

    #[test]
    fn extract_tasks_mixes_root_plain_and_workspace_scoped_entries() {
        // The bug-report repro: root tasks must surface, plain tasks pass
        // through, workspace-scoped (`web#build`) stays filtered.
        let dir = TempDir::new("turbo-mixed-keys");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"tasks":{"build":{},"//#lint":{},"//#format":{"cache":false},"web#build":{}}}"#,
        )
        .expect("turbo.json should be written");

        let mut tasks = extract_tasks(dir.path()).expect("mixed keys should parse");
        tasks.sort_unstable();

        assert_eq!(tasks, ["build", "format", "lint"]);
    }

    #[test]
    fn extract_tasks_drops_malformed_root_task_keys() {
        // `//#` with no name and `//#a#b` (extra `#`) are not valid root
        // tasks — drop them rather than surface a confusing entry.
        let dir = TempDir::new("turbo-malformed-root");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"tasks":{"//#":{},"//#a#b":{},"//#ok":{}}}"#,
        )
        .expect("turbo.json should be written");

        let tasks = extract_tasks(dir.path()).expect("malformed root keys should parse");

        assert_eq!(tasks, ["ok"]);
    }

    #[test]
    fn extract_tasks_dedupes_root_task_and_plain_task_collision() {
        // Both `lint` and `//#lint` resolve to the same `turbo run lint`
        // invocation, so listing both would render a duplicate row.
        let dir = TempDir::new("turbo-root-collision");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"tasks":{"lint":{},"//#lint":{}}}"#,
        )
        .expect("turbo.json should be written");

        let tasks = extract_tasks(dir.path()).expect("colliding keys should parse");

        assert_eq!(tasks, ["lint"]);
    }

    #[test]
    fn extract_tasks_prefers_turbo_json_when_both_filenames_exist() {
        let dir = TempDir::new("turbo-priority");
        fs::write(
            dir.path().join("turbo.json"),
            r#"{"tasks":{"from-json":{}}}"#,
        )
        .expect("turbo.json should be written");
        fs::write(
            dir.path().join("turbo.jsonc"),
            r#"{"tasks":{"from-jsonc":{}}}"#,
        )
        .expect("turbo.jsonc should be written");

        let tasks = extract_tasks(dir.path()).expect("turbo.json should parse");

        assert_eq!(tasks, ["from-json"]);
    }

    use super::is_self_passthrough;

    #[test]
    fn is_self_passthrough_matches_canonical_run_form() {
        assert!(is_self_passthrough("build", "turbo run build"));
    }

    #[test]
    fn is_self_passthrough_matches_with_trailing_flags() {
        assert!(is_self_passthrough(
            "build",
            "turbo run build --filter=web --concurrency=4"
        ));
    }

    #[test]
    fn is_self_passthrough_matches_shorthand_form() {
        assert!(is_self_passthrough("build", "turbo build"));
    }

    #[test]
    fn is_self_passthrough_tolerates_irregular_whitespace() {
        assert!(is_self_passthrough("build", "  turbo   run    build  "));
    }

    #[test]
    fn is_self_passthrough_rejects_real_script() {
        assert!(!is_self_passthrough("build", "vite build"));
    }

    #[test]
    fn is_self_passthrough_rejects_passthrough_to_different_target() {
        assert!(!is_self_passthrough("build", "turbo run lint"));
    }

    #[test]
    fn is_self_passthrough_rejects_indirect_invocation_via_pm() {
        // npx/pnpm exec wrappers are intentionally not matched.
        assert!(!is_self_passthrough("build", "npx turbo run build"));
        assert!(!is_self_passthrough("build", "pnpm exec turbo run build"));
    }

    #[test]
    fn is_self_passthrough_rejects_empty_or_partial_command() {
        assert!(!is_self_passthrough("build", ""));
        assert!(!is_self_passthrough("build", "turbo"));
        assert!(!is_self_passthrough("build", "turbo run"));
    }

    #[test]
    fn is_self_passthrough_rejects_shell_chain_and() {
        // `turbo run build && echo done` does extra work — not a thin
        // passthrough; swallowing it would hide the trailing command.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build && echo done"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_shell_chain_or() {
        assert!(!is_self_passthrough("build", "turbo run build || exit 1"));
    }

    #[test]
    fn is_self_passthrough_rejects_shell_pipe() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build | tee log.txt"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_shell_redirect() {
        // `>` is a bare positional under split_whitespace, which falls into
        // the "non-flag, no flag-value expected" branch and rejects.
        assert!(!is_self_passthrough("build", "turbo run build > out.log"));
    }

    #[test]
    fn is_self_passthrough_rejects_shell_background() {
        assert!(!is_self_passthrough("build", "turbo run build &"));
    }

    #[test]
    fn is_self_passthrough_rejects_extra_positional_target() {
        // `turbo run build lint` runs both `build` and `lint` — invoking
        // through runner would silently drop `lint`, so don't classify
        // this as a passthrough.
        assert!(!is_self_passthrough("build", "turbo run build lint"));
    }

    #[test]
    fn is_self_passthrough_accepts_space_separated_flag_value() {
        assert!(is_self_passthrough("build", "turbo run build --filter web"));
    }

    #[test]
    fn is_self_passthrough_accepts_trailing_bool_flag() {
        // `--no-cache` takes no value; end-of-tokens exits the loop cleanly.
        assert!(is_self_passthrough("build", "turbo run build --no-cache"));
    }

    #[test]
    fn is_self_passthrough_rejects_stderr_to_stdout_after_flag() {
        // The bug CodeRabbit-reviewer #2 caught: `2>&1` was consumed as
        // `--no-cache`'s value. The redirect-detection pass now rejects.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache 2>&1"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_stdout_to_stderr_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache 1>&2"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_dev_null_redirect_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache 2>/dev/null"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_combined_fd_redirect_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache &>output.log"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_append_redirect_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache >>build.log"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_dup_fd_redirect_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache >&2"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_pipe_with_stderr_after_flag() {
        // `|&` (bash 4+ pipe both stdout and stderr) was previously
        // consumed as `--no-cache`'s value — now caught by the
        // expanded shell-control-token list.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache |& tee log"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_case_terminator_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache ;;"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_case_fallthrough_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache ;&"
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache ;;&"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_negation_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache !"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_group_delimiters_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache {"
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache }"
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache ("
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --no-cache )"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_var_expansion_after_flag() {
        // `--filter $X` was previously consumed: the value of `$X` is
        // resolved at run time, so the script's effective filter
        // depends on shell state — not a thin passthrough.
        assert!(!is_self_passthrough("build", "turbo run build --filter $X"));
    }

    #[test]
    fn is_self_passthrough_rejects_braced_var_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --filter ${X}"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_default_var_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --filter ${X:-web}"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_pattern_substitution_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --filter ${X//foo/bar}"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_command_substitution_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --filter $(get_filter)"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_backtick_substitution_after_flag() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --filter `get_filter`"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_arithmetic_expansion_after_flag() {
        // Whitespace in `$((CORES * 2))` splits into multiple tokens,
        // but the first one (`$((CORES`) still trips the `$` check.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --concurrency $((CORES * 2))"
        ));
        // Same form without internal spaces.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --concurrency $((CORES*2))"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_special_var_after_flag() {
        assert!(!is_self_passthrough("build", "turbo run build --filter $@"));
        assert!(!is_self_passthrough("build", "turbo run build --filter $*"));
    }

    #[test]
    fn is_self_passthrough_rejects_quoted_expansion_after_flag() {
        // The exact form from the user's bug report:
        // `"build": "turbo run build \"${X}\""` decodes to
        // `turbo run build "${X}"` — the quoted form must reject too.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build --filter \"${X}\""
        ));
        // Standalone positional case (already rejected via the
        // positional rule, but pin behavior under the new rule too).
        assert!(!is_self_passthrough("build", "turbo run build \"${X}\""));
    }

    #[test]
    fn is_self_passthrough_rejects_bare_var_positional() {
        // Standalone `$X` after the target — already rejected by the
        // positional rule; under the new rule it now rejects via the
        // explicit shell-expansion check, which is clearer.
        assert!(!is_self_passthrough("build", "turbo run build $X"));
    }

    use super::{is_shell_control_token, looks_like_redirect, looks_like_shell_expansion};

    #[test]
    fn is_shell_control_token_matches_full_bash_set() {
        for op in [
            "&&", "||", ";", ";;", ";&", ";;&", "|", "|&", "&", "!", "{", "}", "(", ")",
        ] {
            assert!(
                is_shell_control_token(op),
                "expected `{op}` to be classified as shell control"
            );
        }
    }

    #[test]
    fn is_shell_control_token_rejects_flags_values_and_redirects() {
        // Anything that isn't an exact-match control op must not classify.
        for non_op in [
            "--filter", "web", "4", "@scope/*", "$(date)", ">", "2>&1", "&>",
        ] {
            assert!(
                !is_shell_control_token(non_op),
                "`{non_op}` must not classify as shell control"
            );
        }
    }

    #[test]
    fn looks_like_shell_expansion_matches_full_dollar_family() {
        for form in [
            // Parameter expansion variants.
            "$X",
            "${X}",
            "${X:-default}",
            "${X:=default}",
            "${X:?msg}",
            "${X:+alt}",
            "${X#prefix}",
            "${X##prefix}",
            "${X%suffix}",
            "${X%%suffix}",
            "${X/foo/bar}",
            "${X//foo/bar}",
            "${X^^}",
            "${X,,}",
            "${#X}",
            "${!X}",
            "${X[@]}",
            "${X[0]}",
            // Special vars.
            "$@",
            "$*",
            "$#",
            "$?",
            "$$",
            "$!",
            "$_",
            "$0",
            "$9",
            "${10}",
            // Command substitution.
            "$(cmd)",
            "$(cmd --flag)",
            // Arithmetic expansion.
            "$((1+1))",
            "$((CORES*2))",
            // Quoted forms with embedded expansion.
            "\"${X}\"",
            "\"$X\"",
            "\"prefix-${X}\"",
        ] {
            assert!(
                looks_like_shell_expansion(form),
                "expected `{form}` to be detected as shell expansion"
            );
        }
    }

    #[test]
    fn looks_like_shell_expansion_matches_backtick_substitution() {
        assert!(looks_like_shell_expansion("`cmd`"));
        assert!(looks_like_shell_expansion("`cmd --flag`"));
        assert!(looks_like_shell_expansion("prefix-`cmd`-suffix"));
    }

    #[test]
    fn looks_like_shell_expansion_rejects_plain_values_and_flags() {
        for plain in [
            "--filter",
            "--concurrency=4",
            "web",
            "4",
            "@scope/*",
            "./packages/web",
            ">",
            "2>&1",
            "&>",
            "{a,b,c}",
            "~/cache",
        ] {
            assert!(
                !looks_like_shell_expansion(plain),
                "`{plain}` must not classify as shell expansion"
            );
        }
    }

    #[test]
    fn looks_like_redirect_matches_bare_operators() {
        assert!(looks_like_redirect(">"));
        assert!(looks_like_redirect(">>"));
        assert!(looks_like_redirect("<"));
        assert!(looks_like_redirect("<<"));
        assert!(looks_like_redirect("<<<"));
    }

    #[test]
    fn looks_like_redirect_matches_combined_fd_forms() {
        assert!(looks_like_redirect("&>"));
        assert!(looks_like_redirect("&>>"));
        assert!(looks_like_redirect(">&"));
    }

    #[test]
    fn looks_like_redirect_matches_fd_prefixed_forms() {
        assert!(looks_like_redirect("2>"));
        assert!(looks_like_redirect("1>"));
        assert!(looks_like_redirect("3<"));
    }

    #[test]
    fn looks_like_redirect_matches_composite_forms() {
        assert!(looks_like_redirect("2>&1"));
        assert!(looks_like_redirect("1>&2"));
        assert!(looks_like_redirect("2>/dev/null"));
        assert!(looks_like_redirect("&>file.log"));
        assert!(looks_like_redirect(">file"));
        assert!(looks_like_redirect(">>append"));
    }

    #[test]
    fn looks_like_redirect_rejects_flags_and_values() {
        // Flags and ordinary values must not be misclassified as redirects.
        assert!(!looks_like_redirect("--filter"));
        assert!(!looks_like_redirect("--concurrency=4"));
        assert!(!looks_like_redirect("web"));
        assert!(!looks_like_redirect("4"));
        assert!(!looks_like_redirect("@scope/*"));
        assert!(!looks_like_redirect("$(date)"));
        // Quoted patterns: leading quote/letter, no `>` or `<` after fd-strip.
        assert!(!looks_like_redirect("'<pkg>'"));
        // Bare `&` is a metachar handled separately, not a redirect.
        assert!(!looks_like_redirect("&"));
    }

    #[test]
    fn is_self_passthrough_accepts_double_dash_with_forwarded_flag() {
        // POSIX `--` end-of-options separator: turbo forwards everything
        // after `--` to the underlying task.
        assert!(is_self_passthrough("build", "turbo run build -- --watch"));
        assert!(is_self_passthrough(
            "test",
            "turbo run test --filter web -- --reporter=verbose"
        ));
    }

    #[test]
    fn is_self_passthrough_accepts_double_dash_with_multiple_positionals() {
        // Multi-positional forwarding via `--` was the concrete bug:
        // pre-fix, `arg2` rejected as a bare positional because `--`
        // had been treated as a value-expecting flag.
        assert!(is_self_passthrough("build", "turbo run build -- arg1 arg2"));
        assert!(is_self_passthrough(
            "build",
            "turbo run build -- arg1 arg2 arg3 arg4"
        ));
    }

    #[test]
    fn is_self_passthrough_accepts_double_dash_with_no_following_args() {
        assert!(is_self_passthrough("build", "turbo run build --"));
    }

    #[test]
    fn is_self_passthrough_rejects_shell_chain_after_double_dash() {
        // Shell metacharacters after `--` still mean the script does
        // real work; the separator is not a free pass.
        assert!(!is_self_passthrough(
            "build",
            "turbo run build -- --watch && echo done"
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build -- arg1 ; cleanup"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_redirect_after_double_dash() {
        assert!(!is_self_passthrough(
            "build",
            "turbo run build -- > build.log"
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build -- --watch 2>&1"
        ));
    }

    #[test]
    fn is_self_passthrough_rejects_expansion_after_double_dash() {
        assert!(!is_self_passthrough("build", "turbo run build -- $TARGET"));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build -- --filter ${SCOPE}"
        ));
        assert!(!is_self_passthrough(
            "build",
            "turbo run build -- $(date +%s)"
        ));
    }
}