runner-run 0.19.1

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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
//! Command-line interface definition via [`clap`].

use std::path::{Path, PathBuf};
use std::sync::LazyLock;

use clap::builder::styling::{AnsiColor, Color, Style, Styles};
use clap::{Args, Parser, Subcommand};
use clap_complete::aot::Shell;
use clap_complete::engine::{ArgValueCandidates, CompletionCandidate, SubcommandCandidates};

use crate::types::{PackageManager, TaskRunner};

/// Color palette for help output. clap auto-disables when stdout isn't a
/// TTY or `NO_COLOR` is set, so the same constant works for piped output
/// and color-averse users without extra plumbing.
const HELP_STYLES: Styles = Styles::styled()
    .header(
        Style::new()
            .fg_color(Some(Color::Ansi(AnsiColor::Yellow)))
            .bold()
            .underline(),
    )
    .usage(
        Style::new()
            .fg_color(Some(Color::Ansi(AnsiColor::Yellow)))
            .bold()
            .underline(),
    )
    .literal(
        Style::new()
            .fg_color(Some(Color::Ansi(AnsiColor::Cyan)))
            .bold(),
    )
    .placeholder(Style::new().fg_color(Some(Color::Ansi(AnsiColor::Cyan))))
    .valid(
        Style::new()
            .fg_color(Some(Color::Ansi(AnsiColor::Green)))
            .bold(),
    )
    .invalid(
        Style::new()
            .fg_color(Some(Color::Ansi(AnsiColor::Red)))
            .bold(),
    )
    .error(
        Style::new()
            .fg_color(Some(Color::Ansi(AnsiColor::Red)))
            .bold(),
    );

/// ANSI cyan wrapper used for inline literals embedded in flag-help prose
/// (PM names, env-var names, etc.). The `HELP_STYLES` `Styles::literal` /
/// `Styles::placeholder` slots only style structural pieces (flag names,
/// value placeholders); for tokens inside the description body we emit
/// ANSI directly. clap routes its output through `anstream`, which strips
/// ANSI when stdout isn't a TTY or `NO_COLOR` is set, so these inline
/// escapes are dropped automatically for piped output.
macro_rules! cyan {
    ($s:literal) => {
        concat!("\x1b[36m", $s, "\x1b[0m")
    };
}

/// Wrap a runtime string in the same cyan ANSI escape pair the [`cyan!`] macro
/// emits for compile-time literals. clap routes help through `anstream`, which
/// strips ANSI on non-TTY / `NO_COLOR` output.
fn cyan_str(s: &str) -> String {
    format!("\x1b[36m{s}\x1b[0m")
}

/// Compact env-var suffix matching clap's `[env: VAR=]` help style.
fn env_suffix(var: &str) -> String {
    format!("[env: {}]", cyan_str(var))
}

/// Cyan-styled env-var suffix for `--dir`. Kept alongside [`PM_HELP`] /
/// [`RUNNER_HELP`] rather than as a plain `format!` inline in the doc
/// comment so the `--dir` flag renders identically to every other flag's
/// `[env: VAR]` suffix — clap's own `env = "..."` attribute would style
/// it differently and also print the variable's *current* value (e.g.
/// `[env: RUNNER_DIR=]` when set-but-empty), which the other flags never
/// do since their env fallback lives at the call site, not in clap.
static DIR_HELP: LazyLock<String> = LazyLock::new(|| {
    format!(
        "Project directory (default: cwd) {}",
        env_suffix("RUNNER_DIR")
    )
});

/// Comma-joined, cyan-styled list of every [`PackageManager`] label.
/// Built once at first help-text access via [`LazyLock`]; rebuilding the
/// list on every `--help` invocation would waste work for a value that is
/// fully determined by the [`PackageManager::all`] enumeration.
static PM_HELP: LazyLock<String> = LazyLock::new(|| {
    let joined = PackageManager::all()
        .iter()
        .map(|pm| pm.label())
        .collect::<Vec<_>>()
        .join(", ");
    format!("Force PM ({joined}) {}", env_suffix("RUNNER_PM"))
});

/// Comma-joined, cyan-styled list of every [`TaskRunner`] label.
/// Lazy-built for the same reason as [`PM_HELP`].
static RUNNER_HELP: LazyLock<String> = LazyLock::new(|| {
    let joined = TaskRunner::all()
        .iter()
        .map(|r| r.label())
        .collect::<Vec<_>>()
        .join(", ");
    format!(
        "Force task runner ({joined}) {}",
        env_suffix("RUNNER_RUNNER")
    )
});

/// Sort aliases after all real recipes in completion candidates by offsetting
/// their display order beyond any realistic [`TaskSource::display_order`] value.
const ALIAS_DISPLAY_ORDER_OFFSET: usize = 100;

/// Help-text ordering bands. Flattened [`GlobalOpts`] and per-command flag
/// structs register args in interleaved parse order; without explicit bands
/// `-k`/`--pm`/`-K`/`--runner` shuffle together in `--help`.
mod help_order {
    pub(super) const DIR: usize = 10;
    pub(super) const COMMAND: usize = 20;
    pub(super) const CHAIN_MODE: usize = 30;
    pub(super) const CHAIN_FAILURE: usize = 40;
    pub(super) const PM: usize = 100;
    pub(super) const RUNNER: usize = 101;
    pub(super) const FALLBACK: usize = 102;
    pub(super) const ON_MISMATCH: usize = 103;
    pub(super) const EXPLAIN: usize = 200;
    pub(super) const NO_WARNINGS: usize = 201;
    pub(super) const QUIET: usize = 202;
    pub(super) const SCHEMA_VERSION: usize = 203;
}
/// Produce [`CompletionCandidate`]s for every detected task in the current
/// directory. Called lazily by clap's runtime completion engine — only runs
/// when the shell is actually requesting completions, never during normal
/// execution.
fn task_candidates() -> Vec<CompletionCandidate> {
    let Ok(dir) = completion_dir() else {
        return vec![];
    };
    let ctx = crate::detect::detect(&dir);
    task_candidates_from(&ctx.tasks)
}

fn completion_dir() -> std::io::Result<PathBuf> {
    let cwd = std::env::current_dir()?;
    let argv: Vec<std::ffi::OsString> = std::env::args_os().collect();
    Ok(resolve_completion_dir(
        &cwd,
        cli_dir_from_argv(&argv).as_deref(),
        std::env::var_os("RUNNER_DIR").as_deref(),
    ))
}

/// Mirror clap's `--dir` precedence at completion time.
///
/// Precedence (highest first) — same as the resolver at runtime so
/// the completion list matches the directory the user is about to
/// dispatch against:
/// 1. `--dir` parsed from the in-flight argv (the user is typing
///    `runner --dir /other/repo <TAB>`).
/// 2. `RUNNER_DIR` env var.
/// 3. The shell's working directory.
fn resolve_completion_dir(
    cwd: &Path,
    cli_dir: Option<&std::ffi::OsStr>,
    env_dir: Option<&std::ffi::OsStr>,
) -> PathBuf {
    let raw = cli_dir.or(env_dir);
    match raw.map(PathBuf::from) {
        Some(path) if path.is_absolute() => path,
        Some(path) => cwd.join(path),
        None => cwd.to_path_buf(),
    }
}

/// Scan the argv (as the shell passed it to the binary during
/// completion) for `--dir <value>` / `--dir=<value>`. Returns the last
/// occurrence so repeated flags behave the same way clap does at parse
/// time.
///
/// `clap_complete`'s bash registration invokes the binary as
/// `completer -- "${words[@]}"`, so the user-typed words live *after*
/// the first `--` separator. We seek past that separator first, then
/// scan; if no separator exists (binary invoked directly without
/// `clap_complete`'s harness, e.g. in tests), the entire tail is
/// scanned.
fn cli_dir_from_argv(argv: &[std::ffi::OsString]) -> Option<std::ffi::OsString> {
    use std::ffi::OsString;

    // Find the `--` separator clap_complete inserts between the
    // completer path and the user's word list. Skip past it; otherwise
    // start at index 1 (after argv[0]).
    let start = argv.iter().position(|a| a == "--").map_or(1, |idx| idx + 1);
    if start >= argv.len() {
        return None;
    }

    let mut found: Option<OsString> = None;
    let mut iter = argv[start..].iter();
    while let Some(arg) = iter.next() {
        if arg == "--dir" {
            if let Some(next) = iter.next() {
                found = Some(next.clone());
            }
            continue;
        }
        if let Some(rest) = arg
            .to_str()
            .and_then(|s| s.strip_prefix("--dir="))
            .map(OsString::from)
        {
            found = Some(rest);
        }
    }
    found
}

/// The `--long`/`-s` spellings of every root-level (flattened
/// [`GlobalOpts`]) flag that consumes a following value, read from the
/// actual clap definition. Used by [`chain_flag_precedes_first_task`] to
/// skip flag values while scanning for the first task word.
fn global_value_flags() -> Vec<String> {
    use clap::CommandFactory as _;
    Cli::command()
        .get_arguments()
        .filter(|arg| !arg.is_positional() && arg.get_action().takes_values())
        .flat_map(|arg| {
            arg.get_long()
                .map(|long| format!("--{long}"))
                .into_iter()
                .chain(arg.get_short().map(|short| format!("-{short}")))
        })
        .collect()
}

/// Candidates for the trailing `args` positional of `run`. In chain mode
/// (`-s`/`-p` typed before the first task) the trailing words are extra
/// task names, so complete tasks; otherwise they are arguments forwarded
/// verbatim to the task, where suggesting task names would be noise —
/// complete nothing.
fn chain_args_candidates() -> Vec<CompletionCandidate> {
    let argv: Vec<std::ffi::OsString> = std::env::args_os().collect();
    if !chain_flag_precedes_first_task(&argv) {
        return vec![];
    }
    task_candidates()
}

/// Scan the in-flight completion argv for a chain-mode flag (`-s`/`-p`,
/// long forms, or a short cluster like `-sk`) *before* the first task
/// word — mirroring dispatch, where `trailing_var_arg` means chain flags
/// must precede task names and a later `-s`/`-p` is forwarded to the
/// task instead. Same argv shape as [`cli_dir_from_argv`]: user words
/// follow the first `--`, and the word after that is the binary name.
/// Value-carrying global flags are skipped with their values so
/// `--dir /some/path -s build` still detects the chain flag.
fn chain_flag_precedes_first_task(argv: &[std::ffi::OsString]) -> bool {
    // Flags whose value arrives as the *next* word (the `=` form needs no
    // special casing — it stays one word). Derived from the real clap
    // definition so a new value-taking global can't silently drift out of
    // sync with this scanner and get its value mistaken for the task.
    let value_flags = global_value_flags();

    // Skip past clap_complete's `--` separator, then past the binary
    // name itself (`runner` or the `run` alias).
    let start = argv.iter().position(|a| a == "--").map_or(1, |idx| idx + 1);
    let mut iter = argv.get(start + 1..).unwrap_or(&[]).iter();
    let mut subcommand_seen = false;
    while let Some(arg) = iter.next() {
        let Some(word) = arg.to_str() else {
            continue;
        };
        match word {
            "-s" | "--sequential" | "-p" | "--parallel" => return true,
            // The `run` subcommand token (`runner run …`); the alias
            // binary has no subcommand. Only the first bare word can be
            // it — a task literally named `run` still terminates the
            // scan below on any later occurrence.
            "run" | "r" if !subcommand_seen => subcommand_seen = true,
            _ if value_flags.iter().any(|flag| flag == word) => {
                iter.next();
            }
            // Short cluster (`-sk`, `-pK`): clap accepts combined
            // shorts, so a chain flag can hide inside one.
            _ if word.starts_with('-') && !word.starts_with("--") => {
                if word.chars().skip(1).any(|c| c == 's' || c == 'p') {
                    return true;
                }
            }
            _ if word.starts_with('-') => {}
            // First bare word is the task; anything after it belongs to
            // the task, not the chain.
            _ => return false,
        }
    }
    false
}

/// Build [`CompletionCandidate`]s from a task list.
///
/// When a task name appears in more than one source, both the bare name *and*
/// a `source:name` qualified form are emitted for each occurrence, enabling
/// disambiguation via tab-completion. The bare candidate's help/label is taken
/// from the source the runtime selector would pick (default tier; see the
/// `bare_winner` computation below), not from detection order, so it names the
/// source `runner <name>` actually dispatches to.
///
/// Exception: a `package.json` script whose body is a literal turbo
/// passthrough wrapper (`"build": "turbo run build"`, the canonical
/// Turborepo pattern) is dropped from completion candidates *iff* a
/// same-named `turbo.json` task also exists. The passthrough flag is set
/// during detection by inspecting the actual script body
/// ([`crate::tool::turbo::is_self_passthrough`]), so a real script like
/// `"build": "vite build"` keeps its qualified form even when a
/// `turbo.json` `build` task is present. `runner list` still surfaces both
/// sources for transparency, and `runner build` already dispatches through
/// turbo per the source-priority order in `cmd::run::source_priority`.
fn task_candidates_from(tasks: &[crate::types::Task]) -> Vec<CompletionCandidate> {
    use std::collections::{HashMap, HashSet};

    use crate::types::TaskSource;

    let mut sources_for_name: HashMap<&str, HashSet<TaskSource>> = HashMap::new();
    for task in tasks {
        sources_for_name
            .entry(&task.name)
            .or_default()
            .insert(task.source);
    }

    // A `package.json` script is only swallowed when it (a) declared itself a
    // passthrough wrapper at detection time *and* (b) the project actually
    // has a same-named task from that runner's source to absorb it. Without
    // (b), suppressing would leave the user with no completion for the
    // script at all.
    let is_self_passthrough = |task: &crate::types::Task| -> bool {
        let Some(runner) = task.passthrough_to else {
            return false;
        };
        let Some(peer_source) = runner.task_source() else {
            return false;
        };
        task.source == TaskSource::PackageJson
            && sources_for_name
                .get(task.name.as_str())
                .is_some_and(|set| set.contains(&peer_source))
    };

    let mut effective_count: HashMap<&str, usize> = HashMap::new();
    for task in tasks {
        if !is_self_passthrough(task) {
            *effective_count.entry(task.name.as_str()).or_default() += 1;
        }
    }

    // Pick which source supplies each name's bare candidate by mirroring the
    // runtime selector's default tier (`Turbo > Package > others`, then
    // `display_order`, then recipes-before-aliases — see
    // `cmd::run::select::select_task_entry`). Previously the bare label came
    // from whichever source appeared first in detection order, which could
    // name a different source than the one `runner <name>` actually
    // dispatches to. The selector's `[task_runner].prefer` and nearest-config
    // (`source_depth`) tiebreaks need config plus a `ProjectContext` the
    // completion callback doesn't have, so the bare label aligns with the
    // *default* tier only — still strictly better than detection order, and
    // the qualified `source:name` forms remain for exact disambiguation.
    let no_overrides = crate::resolver::ResolutionOverrides::default();
    let bare_rank = |task: &crate::types::Task| {
        (
            crate::cmd::run::source_priority(&no_overrides, task.source),
            task.source.display_order(),
            task.alias_of.is_some(),
        )
    };
    let mut bare_winner: HashMap<&str, usize> = HashMap::new();
    for (idx, task) in tasks.iter().enumerate() {
        if is_self_passthrough(task) {
            continue;
        }
        let is_better = match bare_winner.get(task.name.as_str()) {
            Some(&best) => bare_rank(task) < bare_rank(&tasks[best]),
            None => true,
        };
        if is_better {
            bare_winner.insert(task.name.as_str(), idx);
        }
    }

    let mut candidates = Vec::new();
    for (idx, task) in tasks.iter().enumerate() {
        if is_self_passthrough(task) {
            continue;
        }

        let source_label = task.source.label();
        // Separate tag group keeps aliases under their own zsh section instead
        // of interleaving with real recipes.
        let (help, tag, order) = task.alias_of.as_deref().map_or_else(
            || {
                let help = task.description.as_ref().map_or_else(
                    || source_label.to_string(),
                    |desc| format!("{source_label}: {desc}"),
                );
                (
                    help,
                    source_label.to_string(),
                    usize::from(task.source.display_order()),
                )
            },
            |target| {
                let help = format!("{target}");
                let tag = format!("{source_label} (aliases)");
                let order = usize::from(task.source.display_order()) + ALIAS_DISPLAY_ORDER_OFFSET;
                (help, tag, order)
            },
        );
        let is_duplicate = effective_count
            .get(task.name.as_str())
            .copied()
            .unwrap_or(0)
            > 1;

        // The rank-winning source supplies the bare candidate (emitted once
        // per name); its label names the source `runner <name>` dispatches to.
        if bare_winner.get(task.name.as_str()) == Some(&idx) {
            candidates.push(
                CompletionCandidate::new(&task.name)
                    .help(Some(help.clone().into()))
                    .tag(Some(tag.clone().into()))
                    .display_order(Some(order)),
            );
        }

        // For duplicate names, also emit "source:name" qualified form
        if is_duplicate {
            let qualified = format!("{source_label}:{}", task.name);
            candidates.push(
                CompletionCandidate::new(qualified)
                    .help(Some(help.into()))
                    .tag(Some(tag.into()))
                    .display_order(Some(order)),
            );
        }
    }
    candidates
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;
    use std::path::{Path, PathBuf};

    use std::ffi::OsString;

    use clap::{CommandFactory, Parser};

    use super::{
        ChainFailureFlags, Cli, Command, RunAliasCli, chain_flag_precedes_first_task,
        cli_dir_from_argv, resolve_completion_dir, task_candidates_from,
    };

    fn osv(words: &[&str]) -> Vec<OsString> {
        words.iter().map(OsString::from).collect()
    }

    #[test]
    fn global_value_flags_reflect_clap_definition() {
        // The chain-flag scanner skips these flags' values; the list is
        // derived from clap metadata precisely so it can't drift. Pin the
        // known members so a regression in the derivation itself (e.g. a
        // filter change dropping everything) is caught.
        let flags = super::global_value_flags();
        for expected in ["--dir", "--pm", "--runner", "--fallback", "--on-mismatch"] {
            assert!(
                flags.iter().any(|f| f == expected),
                "expected {expected} in derived value flags: {flags:?}",
            );
        }
        assert!(
            !flags.iter().any(|f| f == "--quiet"),
            "boolean flags take no value and must not be skipped-with-value: {flags:?}",
        );
    }

    #[test]
    fn chain_flag_detected_before_first_task() {
        // `runner run -s build <TAB>` — chain mode, trailing words are tasks.
        assert!(chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "runner",
            "run",
            "-s",
            "build",
            ""
        ])));
        // Long form + value-carrying global flag before it.
        assert!(chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "runner",
            "--dir",
            "/repo",
            "run",
            "--parallel",
            "build",
            ""
        ])));
        // `run` alias binary, no subcommand token.
        assert!(chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "run",
            "-p",
            "build",
            ""
        ])));
        // Chain flag hidden in a short cluster.
        assert!(chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "run",
            "-sk",
            "build",
            ""
        ])));
    }

    #[test]
    fn chain_flag_after_first_task_is_forwarded_not_chain() {
        // `run build -p 3000 <TAB>` — `-p` lands after the task, so
        // trailing_var_arg forwards it to the task; not chain mode.
        assert!(!chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "run",
            "build",
            "-p",
            "3000",
            ""
        ])));
        // Plain single-task run.
        assert!(!chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "runner",
            "run",
            "build",
            ""
        ])));
        // No chain flag at all.
        assert!(!chain_flag_precedes_first_task(&osv(&[
            "completer",
            "--",
            "runner",
            "run",
            ""
        ])));
    }
    use crate::types::{Task, TaskSource};

    fn task(name: &str, source: TaskSource) -> Task {
        Task {
            name: name.into(),
            source,
            run_target: None,
            description: None,
            alias_of: None,
            passthrough_to: None,
        }
    }

    fn turbo_passthrough(name: &str) -> Task {
        Task {
            passthrough_to: Some(crate::types::TaskRunner::Turbo),
            ..task(name, TaskSource::PackageJson)
        }
    }

    #[test]
    fn qualified_candidates_emitted_for_duplicates() {
        let tasks = vec![
            task("test", TaskSource::PackageJson),
            task("test", TaskSource::Makefile),
            task("build", TaskSource::PackageJson),
        ];
        let candidates = task_candidates_from(&tasks);
        let values: Vec<String> = candidates
            .iter()
            .map(|c| c.get_value().to_string_lossy().into_owned())
            .collect();
        // "test" appears as bare (once) + both qualified forms; "build" is bare only
        assert_eq!(
            values.iter().filter(|v| *v == "test").count(),
            1,
            "bare 'test' should appear exactly once"
        );
        assert!(values.contains(&"package.json:test".to_string()));
        assert!(values.contains(&"make:test".to_string()));
        assert!(values.contains(&"build".to_string()));
        assert!(!values.contains(&"package.json:build".to_string()));
    }

    #[test]
    fn package_json_passthrough_to_turbo_collapses_to_bare_name() {
        let tasks = vec![
            turbo_passthrough("build"),
            task("build", TaskSource::TurboJson),
            task("fmt", TaskSource::PackageJson),
        ];
        let candidates = task_candidates_from(&tasks);
        let values: Vec<String> = candidates
            .iter()
            .map(|c| c.get_value().to_string_lossy().into_owned())
            .collect();

        assert_eq!(
            values.iter().filter(|v| *v == "build").count(),
            1,
            "bare 'build' should appear exactly once"
        );
        assert!(
            !values.contains(&"package.json:build".to_string()),
            "the package.json passthrough should not surface a qualified form"
        );
        assert!(
            !values.contains(&"turbo.json:build".to_string()),
            "with the package.json source swallowed, no qualified form is needed"
        );
        assert!(values.contains(&"fmt".to_string()));
    }

    #[test]
    fn passthrough_swallow_keeps_unrelated_runner_qualified_forms() {
        let tasks = vec![
            turbo_passthrough("build"),
            task("build", TaskSource::Makefile),
            task("build", TaskSource::TurboJson),
        ];
        let candidates = task_candidates_from(&tasks);
        let values: Vec<String> = candidates
            .iter()
            .map(|c| c.get_value().to_string_lossy().into_owned())
            .collect();

        assert!(values.contains(&"build".to_string()));
        assert!(
            !values.contains(&"package.json:build".to_string()),
            "package.json must remain swallowed even when other runners share the name"
        );
        assert!(
            values.contains(&"make:build".to_string()),
            "Makefile is a real definition, not a passthrough — keep its qualified form"
        );
        assert!(
            values.contains(&"turbo:build".to_string()),
            "turbo.json must keep a qualified form to disambiguate from Makefile"
        );
    }

    #[test]
    fn real_package_json_script_keeps_qualified_form_alongside_turbo() {
        // Regression guard: a real `"build": "vite build"` script that
        // happens to share its name with a `turbo.json` task must NOT be
        // swallowed — the passthrough flag is set per-script-body during
        // detection, not inferred from name collisions alone.
        let tasks = vec![
            // Same name, but `passthrough_to_turbo: false` because the
            // command body is `vite build`, not `turbo run build`.
            task("build", TaskSource::PackageJson),
            task("build", TaskSource::TurboJson),
        ];
        let candidates = task_candidates_from(&tasks);
        let values: Vec<String> = candidates
            .iter()
            .map(|c| c.get_value().to_string_lossy().into_owned())
            .collect();

        assert!(values.contains(&"build".to_string()));
        assert!(
            values.contains(&"package.json:build".to_string()),
            "a real package.json script must surface its qualified form for disambiguation"
        );
        assert!(
            values.contains(&"turbo:build".to_string()),
            "the turbo.json source must surface its qualified form when a real twin exists"
        );
    }

    #[test]
    fn bare_label_follows_dispatch_priority_not_detection_order() {
        // `package.json` is detected first, but `runner build` dispatches to
        // turbo (Turbo > Package in the default selector tier). The bare
        // candidate's label must therefore name turbo, not the detection-order
        // first source — otherwise the completion menu misreports what runs.
        let tasks = vec![
            task("build", TaskSource::PackageJson),
            task("build", TaskSource::TurboJson),
        ];
        let candidates = task_candidates_from(&tasks);
        let bare = candidates
            .iter()
            .find(|c| c.get_value().to_string_lossy() == "build")
            .expect("bare 'build' candidate must exist");
        assert_eq!(
            bare.get_tag().map(ToString::to_string).as_deref(),
            Some("turbo"),
            "bare label must name the dispatch-winning source (turbo), not the detection-order \
             first source (package.json)"
        );

        let values: Vec<String> = candidates
            .iter()
            .map(|c| c.get_value().to_string_lossy().into_owned())
            .collect();
        assert_eq!(
            values.iter().filter(|v| *v == "build").count(),
            1,
            "bare 'build' should appear exactly once"
        );
        assert!(values.contains(&"package.json:build".to_string()));
        assert!(values.contains(&"turbo:build".to_string()));
    }

    #[test]
    fn bare_label_skips_suppressed_passthrough_and_ranks_real_sources() {
        // A suppressed package.json turbo-passthrough plus two real sources.
        // The passthrough must not supply the bare label, and among the real
        // sources turbo outranks make, so the bare label names turbo.
        let tasks = vec![
            turbo_passthrough("build"),
            task("build", TaskSource::Makefile),
            task("build", TaskSource::TurboJson),
        ];
        let candidates = task_candidates_from(&tasks);
        let bare = candidates
            .iter()
            .find(|c| c.get_value().to_string_lossy() == "build")
            .expect("bare 'build' candidate must exist");
        assert_eq!(
            bare.get_tag().map(ToString::to_string).as_deref(),
            Some("turbo"),
            "bare label must name the rank-winning real source, not the suppressed passthrough \
             source"
        );

        let values: Vec<String> = candidates
            .iter()
            .map(|c| c.get_value().to_string_lossy().into_owned())
            .collect();
        assert!(
            !values.contains(&"package.json:build".to_string()),
            "the suppressed passthrough must not surface a qualified form"
        );
        assert!(values.contains(&"make:build".to_string()));
        assert!(values.contains(&"turbo:build".to_string()));
    }

    #[test]
    fn passthrough_without_turbo_twin_stays_visible() {
        // Misconfigured project: `"build": "turbo run build"` but no
        // `turbo.json` to back it. Suppressing here would leave the user
        // with no completion at all, so the passthrough must remain.
        let tasks = vec![turbo_passthrough("build")];
        let candidates = task_candidates_from(&tasks);

        assert!(
            candidates
                .iter()
                .any(|c| c.get_value().to_string_lossy() == "build"),
            "without a turbo.json twin, the passthrough is the only source — keep it"
        );
    }

    #[test]
    fn alias_candidate_uses_arrow_help_and_dedicated_tag() {
        let tasks = vec![
            Task {
                description: Some("Build the project".into()),
                ..task("build", TaskSource::Justfile)
            },
            Task {
                alias_of: Some("build".into()),
                ..task("b", TaskSource::Justfile)
            },
        ];
        let candidates = task_candidates_from(&tasks);
        let alias = candidates
            .iter()
            .find(|c| c.get_value() == "b")
            .expect("alias candidate b should be emitted");
        let help = alias
            .get_help()
            .expect("alias candidate should carry help text")
            .to_string();
        assert_eq!(help, "→ build");
        let tag = alias
            .get_tag()
            .expect("alias candidate should carry a tag")
            .to_string();
        assert_eq!(tag, "just (aliases)");

        let recipe = candidates
            .iter()
            .find(|c| c.get_value() == "build")
            .expect("recipe candidate build should be emitted");
        let recipe_tag = recipe
            .get_tag()
            .expect("recipe candidate should carry a tag")
            .to_string();
        assert_eq!(recipe_tag, "just");
    }

    #[test]
    fn resolve_completion_dir_uses_absolute_runner_dir_env() {
        let dir = resolve_completion_dir(
            Path::new("/tmp/workspace"),
            None,
            Some(OsStr::new("/tmp/runner-target")),
        );

        assert_eq!(dir, PathBuf::from("/tmp/runner-target"));
    }

    #[test]
    fn resolve_completion_dir_prefers_cli_over_env() {
        // `runner --dir /cli-target <TAB>` with `RUNNER_DIR=/env-target`
        // set in the environment — completion should reflect the CLI
        // flag, matching clap's runtime precedence.
        let dir = resolve_completion_dir(
            Path::new("/tmp/workspace"),
            Some(OsStr::new("/cli-target")),
            Some(OsStr::new("/env-target")),
        );

        assert_eq!(dir, PathBuf::from("/cli-target"));
    }

    #[test]
    fn cli_dir_from_argv_parses_space_separated_form_with_clap_complete_harness() {
        // Bash completion invokes the binary as
        // `completer -- "${words[@]}"`, so the user-typed words live
        // *after* the first `--`. The helper has to seek past it and
        // then scan for `--dir`.
        let argv = vec![
            OsString::from("/path/to/runner"),
            OsString::from("--"),
            OsString::from("runner"),
            OsString::from("--dir"),
            OsString::from("/repo"),
            OsString::from("build"),
            OsString::from(""),
        ];

        assert_eq!(
            cli_dir_from_argv(&argv).as_deref(),
            Some(OsStr::new("/repo"))
        );
    }

    #[test]
    fn cli_dir_from_argv_parses_space_separated_form_without_separator() {
        // Direct invocation (no clap_complete harness, e.g. tests):
        // scan the full tail starting at argv[1].
        let argv = vec![
            OsString::from("runner"),
            OsString::from("--dir"),
            OsString::from("/repo"),
            OsString::from("build"),
        ];

        assert_eq!(
            cli_dir_from_argv(&argv).as_deref(),
            Some(OsStr::new("/repo"))
        );
    }

    #[test]
    fn cli_dir_from_argv_parses_equals_form() {
        let argv = vec![
            OsString::from("runner"),
            OsString::from("--dir=/repo"),
            OsString::from("build"),
        ];

        assert_eq!(
            cli_dir_from_argv(&argv).as_deref(),
            Some(OsStr::new("/repo"))
        );
    }

    #[test]
    fn cli_dir_from_argv_last_occurrence_wins() {
        // Match clap's behavior for repeated flags: last value wins.
        let argv = vec![
            OsString::from("runner"),
            OsString::from("--dir"),
            OsString::from("/first"),
            OsString::from("--dir=/second"),
        ];

        assert_eq!(
            cli_dir_from_argv(&argv).as_deref(),
            Some(OsStr::new("/second"))
        );
    }

    #[test]
    fn cli_dir_from_argv_returns_none_without_flag() {
        let argv = vec![OsString::from("runner"), OsString::from("build")];

        assert_eq!(cli_dir_from_argv(&argv), None);
    }

    #[test]
    fn run_accepts_sequential_chain_flag() {
        let cli = Cli::try_parse_from(["runner", "run", "-s", "build", "test"]).expect("parses");
        let Some(Command::Run {
            task, args, mode, ..
        }) = cli.command
        else {
            panic!("expected Run subcommand");
        };
        assert!(mode.sequential, "-s should set sequential");
        assert!(!mode.parallel, "-p should not be set");
        assert_eq!(task.as_deref(), Some("build"));
        assert_eq!(args, vec!["test".to_string()]);
    }

    #[test]
    fn run_rejects_sequential_and_parallel_together() {
        let err =
            Cli::try_parse_from(["runner", "run", "-s", "-p", "build"]).expect_err("conflict");
        let msg = format!("{err}");
        assert!(msg.contains("--parallel") || msg.contains("--sequential"));
    }

    #[test]
    fn run_rejects_keep_going_and_kill_on_fail_together() {
        let err = Cli::try_parse_from(["runner", "run", "-s", "-k", "-K", "build", "test"])
            .expect_err("conflict");
        let msg = format!("{err}");
        assert!(msg.contains("--keep-going") || msg.contains("--kill-on-fail"));
    }

    #[test]
    fn run_parses_kill_on_fail_short_flag() {
        let cli =
            Cli::try_parse_from(["runner", "run", "-p", "-K", "build", "test"]).expect("parses");
        match cli.command {
            Some(Command::Run {
                failure:
                    ChainFailureFlags {
                        kill_on_fail: true, ..
                    },
                ..
            }) => {}
            other => panic!("expected Run with kill_on_fail=true, got {other:?}"),
        }
    }

    #[test]
    fn list_rejects_conflicting_output_modes() {
        let err = Cli::try_parse_from(["runner", "list", "--raw", "--json"])
            .expect_err("list output modes must conflict");
        assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
    }

    #[test]
    fn info_subcommand_still_parses_but_is_hidden() {
        // Deprecated alias — must keep parsing (with and without --json)
        // so existing `runner info` invocations don't break …
        Cli::try_parse_from(["runner", "info"]).expect("`runner info` still parses");
        Cli::try_parse_from(["runner", "info", "--json"])
            .expect("`runner info --json` still parses");

        // … but it must not advertise itself in help output.
        let help = Cli::command().render_long_help().to_string();
        assert!(
            !help.contains("\n  info"),
            "hidden `info` subcommand must not appear in --help, got:\n{help}",
        );
    }

    #[test]
    fn schema_version_rejects_out_of_range_values() {
        let err = Cli::try_parse_from(["runner", "--schema-version", "99", "info"])
            .expect_err("schema version should be bounded by clap");

        assert_eq!(err.kind(), clap::error::ErrorKind::ValueValidation);
    }

    #[test]
    fn run_alias_parses_chain_flags_too() {
        let cli = RunAliasCli::try_parse_from(["run", "-p", "lint", "test"]).expect("parses");
        assert!(cli.mode.parallel);
        assert!(!cli.mode.sequential);
        assert_eq!(cli.task.as_deref(), Some("lint"));
        assert_eq!(cli.args, vec!["test".to_string()]);
    }

    #[test]
    fn install_accepts_task_list() {
        let cli = Cli::try_parse_from(["runner", "install", "build", "test"]).expect("parses");
        let Some(Command::Install {
            tasks,
            frozen,
            failure,
            ..
        }) = cli.command
        else {
            panic!("expected Install subcommand");
        };
        assert!(!frozen);
        assert!(!failure.keep_going);
        assert_eq!(tasks, vec!["build".to_string(), "test".to_string()]);
    }

    #[test]
    fn install_accepts_no_scripts_flag() {
        let cli = Cli::try_parse_from(["runner", "install", "--no-scripts"]).expect("parses");
        let Some(Command::Install {
            no_scripts,
            scripts,
            ..
        }) = cli.command
        else {
            panic!("expected Install subcommand");
        };
        assert!(no_scripts, "--no-scripts should set the flag");
        assert!(!scripts, "--scripts stays off when only --no-scripts given");
    }

    #[test]
    fn install_accepts_scripts_flag() {
        let cli = Cli::try_parse_from(["runner", "install", "--scripts"]).expect("parses");
        let Some(Command::Install {
            no_scripts,
            scripts,
            ..
        }) = cli.command
        else {
            panic!("expected Install subcommand");
        };
        assert!(scripts, "--scripts should set the flag");
        assert!(
            !no_scripts,
            "--no-scripts stays off when only --scripts given"
        );
    }

    #[test]
    fn install_scripts_and_no_scripts_are_mutually_exclusive() {
        let err = Cli::try_parse_from(["runner", "install", "--scripts", "--no-scripts"])
            .expect_err("--scripts and --no-scripts must conflict");
        assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict);
    }

    #[test]
    fn install_defaults_both_script_flags_to_false() {
        let cli = Cli::try_parse_from(["runner", "install"]).expect("parses");
        let Some(Command::Install {
            no_scripts,
            scripts,
            ..
        }) = cli.command
        else {
            panic!("expected Install subcommand");
        };
        assert!(!no_scripts, "--no-scripts should default off");
        assert!(!scripts, "--scripts should default off");
    }

    #[test]
    fn install_accepts_keep_going_flag() {
        let cli = Cli::try_parse_from(["runner", "install", "-k", "build"]).expect("parses");
        let Some(Command::Install { tasks, failure, .. }) = cli.command else {
            panic!("expected Install subcommand");
        };
        assert!(failure.keep_going);
        assert_eq!(tasks, vec!["build".to_string()]);
    }

    #[test]
    fn install_accepts_parallel_flag() {
        // `-p` after the task list still parses as a flag (plain positional,
        // not trailing_var_arg) and selects parallel post-install execution.
        let cli =
            Cli::try_parse_from(["runner", "install", "build", "test", "-p"]).expect("parses");
        let Some(Command::Install { tasks, mode, .. }) = cli.command else {
            panic!("expected Install subcommand");
        };
        assert!(mode.parallel, "-p should set parallel");
        assert!(!mode.sequential);
        assert_eq!(tasks, vec!["build".to_string(), "test".to_string()]);
    }

    #[test]
    fn install_rejects_sequential_and_parallel_together() {
        let err =
            Cli::try_parse_from(["runner", "install", "-s", "-p", "build"]).expect_err("conflict");
        let msg = format!("{err}");
        assert!(msg.contains("--parallel") || msg.contains("--sequential"));
    }
}

/// Universal project task runner.
#[derive(Debug, Parser)]
#[command(
    name = "runner",
    about = clap::crate_description!(),
    help_template = "{about-with-newline}{before-help}{usage-heading} {usage}\n\n{all-args}{after-help}",
    version,
    styles = HELP_STYLES,
    arg_required_else_help = false,
    add = SubcommandCandidates::new(task_candidates)
)]
pub(crate) struct Cli {
    /// Global options shared with [`RunAliasCli`].
    #[command(flatten)]
    pub global: GlobalOpts,

    /// Subcommand to execute. Defaults to [`Command::Info`] when absent.
    #[command(subcommand)]
    pub command: Option<Command>,
}

/// Flags shared by both `runner` and `run`. Carried inline via
/// `#[command(flatten)]` so each binary's `--help` lists them at the
/// same level as subcommand-specific arguments — clap unrolls them as
/// if they were defined on the parent struct.
#[derive(Debug, Args)]
pub(crate) struct GlobalOpts {
    /// Project directory (default: cwd). `$RUNNER_DIR` is also consulted
    /// independently when this flag is omitted (env reads live at the
    /// dispatch call site via `configured_project_dir`, not clap).
    #[arg(
        long = "dir",
        global = true,
        value_name = "PATH",
        value_hint = clap::ValueHint::DirPath,
        value_parser = clap::value_parser!(PathBuf),
        help = DIR_HELP.as_str(),
        display_order = help_order::DIR,
    )]
    pub project_dir: Option<PathBuf>,

    /// Override the detected package manager (e.g. `pnpm`, `bun`, `yarn`).
    /// The resolver also consults `$RUNNER_PM` independently when this
    /// flag is omitted (env reads live in `crate::resolver`, not clap).
    #[arg(
        long = "pm",
        global = true,
        value_name = "NAME",
        help = PM_HELP.as_str(),
        display_order = help_order::PM,
    )]
    pub pm_override: Option<String>,

    /// Override the detected task runner (e.g. `just`, `turbo`, `make`).
    /// The resolver also consults `$RUNNER_RUNNER` independently when
    /// this flag is omitted (env reads live in `crate::resolver`, not
    /// clap).
    #[arg(
        long = "runner",
        global = true,
        value_name = "NAME",
        help = RUNNER_HELP.as_str(),
        display_order = help_order::RUNNER,
    )]
    pub runner_override: Option<String>,

    /// What to do when no detection signal matches: `probe` (default,
    /// PATH probe), `npm` (legacy silent fallback), `error` (refuse).
    /// The resolver also consults `$RUNNER_FALLBACK` independently when
    /// this flag is omitted (env reads live in `crate::resolver`, not
    /// clap).
    #[arg(
        long = "fallback",
        global = true,
        value_name = "POLICY",
        display_order = help_order::FALLBACK,
        help = concat!(
            "No detection match: ",
            cyan!("probe"), " (default), ",
            cyan!("npm"), ", ",
            cyan!("error"), " ",
            "[env: ", cyan!("RUNNER_FALLBACK"), "]"
        ),
    )]
    pub fallback: Option<String>,

    /// What to do when the manifest declaration (packageManager / devEngines)
    /// disagrees with the detected lockfile: `warn` (default), `error`
    /// (refuse, exit 2), `ignore` (silent). The resolver also consults
    /// `$RUNNER_ON_MISMATCH` independently when this flag is omitted.
    #[arg(
        long = "on-mismatch",
        global = true,
        value_name = "POLICY",
        display_order = help_order::ON_MISMATCH,
        help = concat!(
            "Manifest vs lockfile: ",
            cyan!("warn"), " (default), ",
            cyan!("error"), " (exit 2), ",
            cyan!("ignore"), " ",
            "[env: ", cyan!("RUNNER_ON_MISMATCH"), "]"
        ),
    )]
    pub on_mismatch: Option<String>,

    /// Print a one-line trace describing how the package manager was
    /// resolved. The resolver also enables this when `$RUNNER_EXPLAIN`
    /// is set to a truthy value (env reads live in `crate::resolver`,
    /// not clap).
    #[arg(
        long = "explain",
        global = true,
        display_order = help_order::EXPLAIN,
        help = concat!(
            "PM resolution trace ",
            "[env: ", cyan!("RUNNER_EXPLAIN"), "]"
        ),
    )]
    pub explain: bool,

    /// Suppress all non-fatal warnings on stderr. Errors still surface;
    /// only `DetectionWarning` output is silenced. Also enabled when
    /// `$RUNNER_NO_WARNINGS` is set to a truthy value.
    #[arg(
        long = "no-warnings",
        global = true,
        display_order = help_order::NO_WARNINGS,
        help = concat!(
            "Hide non-fatal warnings ",
            "[env: ", cyan!("RUNNER_NO_WARNINGS"), "]"
        ),
    )]
    pub no_warnings: bool,

    /// Suppress the dispatch arrow (`→ <source> <task>`) on stderr. Also
    /// silences the `--explain` trace at dispatch time. Enabled when
    /// `$RUNNER_QUIET` is set to a truthy value.
    #[arg(
        short = 'q',
        long = "quiet",
        global = true,
        display_order = help_order::QUIET,
        help = concat!(
            "Hide dispatch line + ", cyan!("--explain"), " trace ",
            "[env: ", cyan!("RUNNER_QUIET"), "]"
        ),
    )]
    pub quiet: bool,

    /// Pin the `--json` output schema version. Currently always `1` —
    /// kept for scripts that already pass it explicitly; any other value
    /// is rejected.
    #[arg(
        long = "schema-version",
        global = true,
        value_parser = clap::value_parser!(u32).range(1..=1),
        value_name = "N",
        display_order = help_order::SCHEMA_VERSION,
        help = concat!("Pin ", cyan!("--json"), " schema (currently always ", cyan!("1"), ")"),
    )]
    pub schema_version: Option<u32>,
}

/// Available subcommands.
#[derive(Debug, Subcommand)]
pub(crate) enum Command {
    /// Run or exec a task; `-s`/`-p` chain multiple
    #[command(
        alias = "r",
        about = concat!("Run or exec a task; ", cyan!("-s"), "/", cyan!("-p"), " chain multiple"),
    )]
    Run {
        /// Task name or command to execute. In chain mode, the first task in the chain.
        #[arg(add = ArgValueCandidates::new(task_candidates))]
        task: Option<String>,
        /// Arguments forwarded to the task, or extra task names in chain mode.
        // In chain mode, chain-failure flags (`-k`) must precede task names —
        // `trailing_var_arg` consumes everything after the first positional.
        #[arg(
            trailing_var_arg = true,
            allow_hyphen_values = true,
            add = ArgValueCandidates::new(chain_args_candidates),
        )]
        args: Vec<String>,
        /// Chain mode flags: `-s` / `-p`.
        #[command(flatten)]
        mode: ChainModeFlags,
        /// Chain failure-policy flags: `-k` / `-K`.
        #[command(flatten)]
        failure: ChainFailureFlags,
    },

    /// List tasks from detected sources
    #[command(alias = "ls")]
    List {
        /// Print bare task names, one per line (for scripting / completions)
        #[arg(long, conflicts_with_all = ["json"])]
        raw: bool,
        /// Emit JSON instead of human-readable output.
        #[arg(long, conflicts_with_all = ["raw"])]
        json: bool,
        /// Restrict output to a single source (e.g. `package.json`,
        /// `Makefile`, `justfile`).
        #[arg(long, value_name = "SOURCE")]
        source: Option<String>,
    },

    /// Install deps; may chain tasks after; `-s`/`-p` pick the post-install mode
    #[command(
        alias = "i",
        about = concat!("Install deps; may chain tasks after; ", cyan!("-s"), "/", cyan!("-p"), " pick the post-install mode"),
    )]
    Install {
        /// Reproducible install from lockfile (npm ci, --frozen-lockfile, etc.)
        #[arg(short = 'f', long, display_order = help_order::COMMAND)]
        frozen: bool,
        /// Skip install lifecycle scripts where the PM supports it
        /// (npm/yarn/pnpm/bun/composer; deno already denies)
        #[arg(long = "no-scripts", display_order = help_order::COMMAND + 1)]
        no_scripts: bool,
        /// Force install lifecycle scripts on where the PM can express it
        /// (npm/yarn-berry/deno; bun/pnpm need a manifest allowlist)
        #[arg(
            long = "scripts",
            conflicts_with = "no_scripts",
            display_order = help_order::COMMAND + 2
        )]
        scripts: bool,
        /// Optional task names to run after install completes. Sequential by
        /// default; `-p` runs them concurrently once install finishes (install
        /// itself always runs first, never as a parallel sibling). Plain
        /// positional (no `trailing_var_arg`) so chain flags placed after the
        /// task list still parse as flags, not task names.
        #[arg(add = ArgValueCandidates::new(task_candidates))]
        tasks: Vec<String>,
        /// Chain mode flags `-s`/`-p` — govern the post-install tasks only.
        #[command(flatten)]
        mode: ChainModeFlags,
        /// Chain failure-policy flags `-k`/`-K`. `-K` (kill siblings) only
        /// bites with `-p`; under `-s` it degrades to fail-fast.
        #[command(flatten)]
        failure: ChainFailureFlags,
    },

    /// Remove caches and build artifacts
    Clean {
        /// Skip confirmation prompt
        #[arg(short, long)]
        yes: bool,
        /// Include framework-specific Node build dirs like `.next`
        #[arg(long)]
        include_framework: bool,
    },

    /// Deprecated alias for `list` — hidden, prints a warning, then
    /// renders the task list. Bare `runner` still shows the project
    /// dashboard; only the explicit `info` verb is deprecated.
    #[command(hide = true)]
    Info {
        /// Emit JSON instead of human-readable output.
        #[arg(long)]
        json: bool,
    },

    /// How a task would dispatch
    Why {
        /// Task name to analyze.
        task: String,
        /// Emit JSON instead of human-readable output.
        #[arg(long)]
        json: bool,
    },

    /// Resolver signals for this directory
    Doctor {
        /// Emit JSON instead of human-readable output.
        #[arg(long)]
        json: bool,
    },

    /// Manage the project `runner.toml`
    Config {
        /// Config action: `init`, `show`, `validate`, `path`.
        #[command(subcommand)]
        action: ConfigAction,
    },

    /// Generate shell completions
    Completions {
        /// Target shell — bare name (`zsh`) or full path (`/usr/bin/zsh`).
        /// Defaults to `$SHELL`.
        #[arg(value_parser = crate::cmd::parse_shell_arg)]
        shell: Option<Shell>,

        /// Write the completion script to <PATH> instead of stdout. Any
        /// existing file is overwritten.
        #[arg(
            short = 'o',
            long = "output",
            value_name = "PATH",
            value_hint = clap::ValueHint::FilePath,
            value_parser = clap::value_parser!(PathBuf),
        )]
        output: Option<PathBuf>,
    },

    /// Render roff man pages (build: --features man)
    #[cfg(feature = "man")]
    #[command(hide = true)]
    Man {
        /// Write every page into this dir instead of the `runner` page to stdout.
        #[arg(
            short = 'o',
            long = "output",
            value_name = "DIR",
            value_hint = clap::ValueHint::DirPath,
            value_parser = clap::value_parser!(PathBuf),
        )]
        output: Option<PathBuf>,
    },

    /// Emit JSON Schemas. Only compiled in with the `schema` cargo feature.
    #[cfg(feature = "schema")]
    #[command(about = "Emit JSON Schemas")]
    Schema {
        /// Emit every committed schema into the output directory.
        #[arg(long)]
        all: bool,
        /// Write the schema to this file, or all schemas to this directory with --all.
        #[arg(
            short = 'o',
            long = "output",
            value_name = "PATH",
            value_hint = clap::ValueHint::FilePath,
            value_parser = clap::value_parser!(PathBuf),
        )]
        output: Option<PathBuf>,
    },

    /// Run the editor language server for runner.toml over stdio.
    /// Only compiled in with the `lsp` cargo feature.
    #[cfg(feature = "lsp")]
    #[command(about = "Run the runner.toml language server (LSP) over stdio")]
    Lsp,

    /// Catch-all: treat unknown subcommands as task names.
    #[command(external_subcommand)]
    External(Vec<String>),
}

/// Actions under `runner config`. All are `Copy` so [`crate::lib`] can peek
/// the variant before the resolver setup (which `config` deliberately
/// bypasses) without moving out of the parsed [`Cli`].
#[derive(Debug, Clone, Copy, Subcommand)]
pub(crate) enum ConfigAction {
    /// Write a starter runner.toml to the project root
    Init {
        /// Overwrite an existing runner.toml instead of refusing.
        #[arg(short, long)]
        force: bool,
    },
    /// Print the effective config and where it loaded from
    Show {
        /// Emit JSON instead of TOML.
        #[arg(long)]
        json: bool,
    },
    /// Parse and validate runner.toml; exit 2 on error
    Validate,
    /// Print the resolved runner.toml path
    Path,
}

/// CLI used by the `run` alias binary. Behaves as a shortcut for
/// `runner run <task>`: the first positional is the task or command,
/// any remaining positionals are forwarded as its arguments, and
/// built-in subcommand names are never parsed specially (so
/// `run foo bar` runs `foo` with `bar`, not two separate targets).
#[derive(Debug, Parser)]
#[command(
    name = "run",
    about = "Run or exec a task via the detected package manager",
    help_template = "{about-with-newline}{before-help}{usage-heading} {usage}\n\n{all-args}{after-help}",
    // `-h`/`--help`/`-V`/`--version` are no longer clap args (see the
    // disable note below), so document them here instead of in the options
    // list, and flag the forwarding rule that distinguishes this binary
    // from `runner run`.
    after_help = concat!(
        "\nUse ", cyan!("-h"), "/", cyan!("--help"), " or ", cyan!("-V"), "/", cyan!("--version"),
        " before a task for this binary's own help and version.\n",
        "After a task name they are forwarded to the task instead (use ", cyan!("--"), " to force forwarding).",
    ),
    styles = HELP_STYLES,
    arg_required_else_help = false,
    // clap's built-in `--help`/`--version` short-circuit parsing wherever
    // they appear, so `run <task> --help` printed *our* help instead of the
    // task's. We disable them and leave `--help`/`--version` undefined: a
    // *defined* flag would be consumed by clap even after the task (like
    // `-k`), but an *undefined* hyphen token after the first positional is
    // swallowed by `args` (`trailing_var_arg`) and forwarded to the task.
    // A leading `--help`/`--version` (before any task) instead surfaces as
    // an `UnknownArgument` error — `task` takes no hyphen valueswhich
    // `run_alias_in_dir` recognises as this binary's own help/version
    // request. `run <task> -- --help` keeps forwarding literally.
    disable_help_flag = true,
    disable_version_flag = true,
)]
pub(crate) struct RunAliasCli {
    /// Global options shared with [`Cli`].
    #[command(flatten)]
    pub global: GlobalOpts,

    /// Task name or command. When omitted, prints project info.
    #[arg(add = ArgValueCandidates::new(task_candidates))]
    pub task: Option<String>,

    /// Arguments forwarded to the task, or extra task names in chain mode.
    // In chain mode, chain-failure flags (`-k`) must precede task names —
    // `trailing_var_arg` consumes everything after the first positional.
    // That same rule forwards a *trailing* `--help`/`--version` to the task
    // rather than treating it as this binary's own.
    #[arg(
        trailing_var_arg = true,
        allow_hyphen_values = true,
        add = ArgValueCandidates::new(chain_args_candidates),
    )]
    pub args: Vec<String>,

    /// Chain mode flags: `-s` / `-p`.
    #[command(flatten)]
    pub mode: ChainModeFlags,

    /// Chain failure-policy flags: `-k` / `-K`.
    #[command(flatten)]
    pub failure: ChainFailureFlags,
}

/// Chain-mode flags shared across `Cli::Run` and `RunAliasCli`. Grouped
/// so neither subcommand exceeds clippy's `struct_excessive_bools` cap
/// of three.
#[derive(Debug, Args, Default, Clone, Copy)]
pub(crate) struct ChainModeFlags {
    /// Chain tasks in order
    #[arg(
        short = 's',
        long,
        conflicts_with = "parallel",
        display_order = help_order::CHAIN_MODE,
    )]
    pub sequential: bool,
    /// Chain tasks concurrently
    #[arg(short = 'p', long, display_order = help_order::CHAIN_MODE + 1)]
    pub parallel: bool,
}

/// Chain failure-policy flags shared across `Cli::Run`, `Cli::Install`,
/// and `RunAliasCli`. Mutually exclusive (`-k` vs `-K`) enforced at the
/// clap layer.
#[derive(Debug, Args, Default, Clone, Copy)]
pub(crate) struct ChainFailureFlags {
    /// Finish chain despite failures. The resolver also consults
    /// `$RUNNER_KEEP_GOING` independently when this flag is omitted (env
    /// reads live in `crate::resolver`, not clap).
    #[arg(
        short = 'k',
        long,
        conflicts_with = "kill_on_fail",
        display_order = help_order::CHAIN_FAILURE,
        help = concat!(
            "Finish chain despite failures ",
            "[env: ", cyan!("RUNNER_KEEP_GOING"), "]"
        ),
    )]
    pub keep_going: bool,
    /// Parallel: kill siblings on first failure. The resolver also
    /// consults `$RUNNER_KILL_ON_FAIL` independently when this flag is
    /// omitted (env reads live in `crate::resolver`, not clap).
    #[arg(
        short = 'K',
        long,
        conflicts_with = "keep_going",
        display_order = help_order::CHAIN_FAILURE + 1,
        help = concat!(
            "Parallel: kill siblings on first failure ",
            "[env: ", cyan!("RUNNER_KILL_ON_FAIL"), "]"
        ),
    )]
    pub kill_on_fail: bool,
}