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
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
#![expect(
clippy::print_stdout,
clippy::print_stderr,
reason = "CLI binary produces intentional terminal output"
)]
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
mod api;
mod audit;
mod baseline;
mod check;
mod ci_template;
mod codeowners;
mod combined;
mod config;
mod coverage;
mod dupes;
mod error;
mod explain;
mod fix;
mod flags;
mod health;
mod health_types;
mod init;
mod license;
mod list;
mod migrate;
mod rayon_pool;
mod regression;
mod report;
mod runtime_support;
mod schema;
mod setup_hooks;
mod validate;
mod vital_signs;
mod watch;
use check::{CheckOptions, IssueFilters, TraceOptions};
use dupes::{DupesMode, DupesOptions};
use error::emit_error;
use health::{HealthOptions, SortBy};
use list::ListOptions;
pub use runtime_support::{AnalysisKind, GroupBy};
pub(crate) use runtime_support::{build_ownership_resolver, load_config, load_config_for_analysis};
// ── CLI definition ───────────────────────────────────────────────
#[derive(Parser)]
#[command(
name = "fallow",
about = "Codebase analyzer for TypeScript/JavaScript — unused code, circular dependencies, code duplication, complexity hotspots, and architecture boundary violations",
version,
after_help = "When no command is given, runs dead-code + dupes + health together.\nUse --only/--skip to select specific analyses."
)]
struct Cli {
#[command(subcommand)]
command: Option<Command>,
/// Project root directory
#[arg(short, long, global = true)]
root: Option<PathBuf>,
/// Path to config file (.fallowrc.json, .fallowrc.jsonc, or fallow.toml)
#[arg(short, long, global = true)]
config: Option<PathBuf>,
/// Output format (alias: --output)
#[arg(
short,
long,
visible_alias = "output",
global = true,
default_value = "human"
)]
format: Format,
/// Suppress progress output
#[arg(short, long, global = true)]
quiet: bool,
/// Disable incremental caching
#[arg(long, global = true)]
no_cache: bool,
/// Number of parser threads
#[arg(long, global = true)]
threads: Option<usize>,
/// Only report issues in files changed since this git ref (e.g., main, HEAD~5)
#[arg(long, visible_alias = "base", global = true)]
changed_since: Option<String>,
/// Compare against a previously saved baseline file
#[arg(long, global = true)]
baseline: Option<PathBuf>,
/// Save the current results as a baseline file
#[arg(long, global = true)]
save_baseline: Option<PathBuf>,
/// Production mode: exclude test/story/dev files, only start/build scripts,
/// report type-only dependencies
#[arg(long, global = true)]
production: bool,
/// Run dead-code analysis in production mode when using bare combined mode.
#[arg(long = "production-dead-code")]
production_dead_code: bool,
/// Run health analysis in production mode when using bare combined mode.
#[arg(long = "production-health")]
production_health: bool,
/// Run duplication analysis in production mode when using bare combined mode.
#[arg(long = "production-dupes")]
production_dupes: bool,
/// Scope output to one or more workspaces. Accepts exact package names, globs
/// (matched against the package name AND the workspace path relative to the repo
/// root), and `!`-prefixed negations. Values can be comma-separated or repeated.
/// The full cross-workspace graph is still built; only issues are filtered.
///
/// Examples:
/// -w web,admin
/// -w 'apps/*'
/// -w 'apps/*,!apps/legacy'
///
/// Use single quotes around patterns with `!` or glob chars. In bash,
/// unquoted `!` triggers history expansion and double quotes are not enough.
#[arg(short, long, global = true, value_delimiter = ',')]
workspace: Option<Vec<String>>,
/// Scope output to workspaces containing any file changed since the given git ref.
/// Auto-derives the set of touched packages in a monorepo so CI jobs don't have
/// to maintain a hand-written workspace list. Git is required; a missing ref or
/// non-git directory is a hard error, so failure is visible instead of quietly
/// widening back to the full monorepo. Mutually exclusive with --workspace.
///
/// Example:
/// fallow --changed-workspaces origin/main
#[arg(long, global = true, value_name = "REF")]
changed_workspaces: Option<String>,
/// Group output by owner (.github/CODEOWNERS) or by directory (no CODEOWNERS needed).
/// Partitions all issues into labeled sections for team-level triage and dashboards.
#[arg(long, global = true)]
group_by: Option<GroupBy>,
/// Show pipeline performance timing breakdown
#[arg(long, global = true)]
performance: bool,
/// Include metric definitions and rule descriptions in output.
/// JSON: adds a `_meta` object with docs URLs, metric ranges, and interpretations.
/// Always enabled for MCP server responses.
#[arg(long, global = true)]
explain: bool,
/// Show a per-pattern breakdown for default duplicates ignores.
/// Human and markdown output only; machine formats suppress the note.
#[arg(long, global = true)]
explain_skipped: bool,
/// Show only category counts without individual items
#[arg(long, global = true)]
summary: bool,
/// CI mode: equivalent to --format sarif --fail-on-issues --quiet
#[arg(long, global = true)]
ci: bool,
/// Exit with code 1 if issues are found
#[arg(long, global = true)]
fail_on_issues: bool,
/// Write SARIF output to a file (in addition to the primary --format output)
#[arg(long, global = true, value_name = "PATH")]
sarif_file: Option<PathBuf>,
/// Fail if issue count increased beyond tolerance compared to a regression baseline.
/// Use --save-regression-baseline to create a baseline first, then
/// --fail-on-regression on subsequent runs to detect regressions.
#[arg(long, global = true)]
fail_on_regression: bool,
/// Allowed issue count increase before a regression is flagged.
/// Use "N%" for percentage (e.g., "2%") or "N" for absolute count (e.g., "5").
/// Default: "0" (any increase fails). Only used with --fail-on-regression.
#[arg(long, global = true, value_name = "TOLERANCE", default_value = "0")]
tolerance: String,
/// Path to the regression baseline file for --fail-on-regression.
/// Default: .fallow/regression-baseline.json
#[arg(long, global = true, value_name = "PATH")]
regression_baseline: Option<PathBuf>,
/// Save the current issue counts as a regression baseline.
/// Without a path: writes into the config file (.fallowrc.json / .fallowrc.jsonc / fallow.toml).
/// With a path: writes a standalone JSON file.
#[expect(
clippy::option_option,
reason = "clap pattern: None=not passed, Some(None)=flag only (write to config), Some(Some(path))=write to file"
)]
#[arg(long, global = true, value_name = "PATH", num_args = 0..=1, default_missing_value = "")]
save_regression_baseline: Option<Option<String>>,
/// Run only specific analyses when no subcommand is given (comma-separated: dead-code,dupes,health)
#[arg(long, value_delimiter = ',')]
only: Vec<AnalysisKind>,
/// Skip specific analyses when no subcommand is given (comma-separated: dead-code,dupes,health)
#[arg(long, value_delimiter = ',')]
skip: Vec<AnalysisKind>,
/// Override duplication detection mode in combined mode.
#[arg(long = "dupes-mode", global = true)]
dupes_mode: Option<DupesMode>,
/// Override duplication threshold in combined mode.
#[arg(long = "dupes-threshold", global = true)]
dupes_threshold: Option<f64>,
/// Compute health score (0-100 with letter grade) in combined mode.
/// Use with `--trend` to show score deltas in PR comments.
#[arg(long)]
score: bool,
/// Compare current health metrics against the most recent saved snapshot
/// and show per-metric deltas. Implies --score.
#[arg(long)]
trend: bool,
/// Save a vital signs snapshot for trend tracking in combined mode.
/// Provide a path or omit for the default `.fallow/snapshots/` location.
#[expect(
clippy::option_option,
reason = "clap pattern: None=not passed, Some(None)=default path, Some(Some(path))=custom path"
)]
#[arg(long, value_name = "PATH", num_args = 0..=1, default_missing_value = "")]
save_snapshot: Option<Option<String>>,
/// Report unused exports in entry files instead of auto-marking them as used.
/// Catches typos in framework exports (e.g., `meatdata` instead of `metadata`).
/// Also configurable via `includeEntryExports: true` in the config file; the
/// CLI flag wins when set.
#[arg(long, global = true)]
include_entry_exports: bool,
}
#[derive(Subcommand)]
enum Command {
/// Analyze project for unused code and circular dependencies
#[command(name = "dead-code", alias = "check")]
Check {
/// Only report unused files
#[arg(long)]
unused_files: bool,
/// Only report unused exports
#[arg(long)]
unused_exports: bool,
/// Only report unused dependencies
#[arg(long)]
unused_deps: bool,
/// Only report unused type exports
#[arg(long)]
unused_types: bool,
/// Opt in to private type leak API hygiene findings and only report that issue type
#[arg(long)]
private_type_leaks: bool,
/// Only report unused enum members
#[arg(long)]
unused_enum_members: bool,
/// Only report unused class members
#[arg(long)]
unused_class_members: bool,
/// Only report unresolved imports
#[arg(long)]
unresolved_imports: bool,
/// Only report unlisted dependencies
#[arg(long)]
unlisted_deps: bool,
/// Only report duplicate exports
#[arg(long)]
duplicate_exports: bool,
/// Only report circular dependencies
#[arg(long)]
circular_deps: bool,
/// Only report boundary violations
#[arg(long)]
boundary_violations: bool,
/// Only report stale suppressions
#[arg(long)]
stale_suppressions: bool,
/// Also run duplication analysis and cross-reference with dead code
#[arg(long)]
include_dupes: bool,
/// Trace why an export is used/unused (format: `FILE:EXPORT_NAME`)
#[arg(long, value_name = "FILE:EXPORT")]
trace: Option<String>,
/// Trace all edges for a file (imports, exports, importers)
#[arg(long, value_name = "PATH")]
trace_file: Option<String>,
/// Trace where a dependency is used
#[arg(long, value_name = "PACKAGE")]
trace_dependency: Option<String>,
/// Show only the top N items per category
#[arg(long)]
top: Option<usize>,
/// Only report issues in the specified file(s). Accepts multiple values.
/// The full project graph is still built, but only issues in matching files
/// are reported. Useful for lint-staged pre-commit hooks.
#[arg(long, value_name = "PATH")]
file: Vec<std::path::PathBuf>,
},
/// Watch for changes and re-run analysis
Watch {
/// Don't clear the screen between re-analyses
#[arg(long)]
no_clear: bool,
},
/// Auto-fix issues (remove unused exports, dependencies, enum members)
Fix {
/// Dry run — show what would be changed without modifying files
#[arg(long)]
dry_run: bool,
/// Skip confirmation prompt (required in non-TTY environments like CI or AI agents)
#[arg(long, alias = "force")]
yes: bool,
},
/// Initialize a .fallowrc.json configuration file (optionally a git
/// pre-commit hook). Use `.fallowrc.jsonc` for editor-native JSON-with-comments
/// support; both extensions are auto-discovered.
///
/// `--hooks` scaffolds a shell-level Git pre-commit hook under
/// `.git/hooks/` that runs fallow on changed files. The clearer hook
/// namespace is `fallow hooks install --target git`; `init --hooks`
/// remains as a convenience during project initialization.
Init {
/// Generate TOML instead of JSONC
#[arg(long)]
toml: bool,
/// Scaffold a shell-level pre-commit git hook in `.git/hooks/` that
/// runs fallow on changed files. Alias for
/// `fallow hooks install --target git`.
#[arg(long)]
hooks: bool,
/// Fallback base branch/ref for the pre-commit hook when no upstream is set
#[arg(long, requires = "hooks")]
branch: Option<String>,
},
/// Install or remove fallow-managed Git and agent hooks.
///
/// Use `fallow hooks install --target git` for a shell-level Git
/// pre-commit hook. Use `fallow hooks install --target agent` for a
/// Claude Code / Codex gate that blocks agent `git commit` / `git push`
/// commands until `fallow audit` passes.
Hooks {
#[command(subcommand)]
subcommand: HooksCli,
},
/// Print the JSON Schema for fallow configuration files
ConfigSchema,
/// Print the JSON Schema for external plugin files
PluginSchema,
/// Show the resolved config and which config file was loaded
///
/// Walks up from the project root looking for `.fallowrc.json`,
/// `.fallowrc.jsonc`, `fallow.toml`, or `.fallow.toml`, resolves `extends`, and prints
/// the final config as JSON. Use `--path` to print only the config
/// file path (useful in shell scripts). Exit code 0 if a config was
/// found, 3 if only defaults are in effect.
Config {
/// Print only the config file path (one line, no JSON)
#[arg(long)]
path: bool,
},
/// List discovered entry points and files
List {
/// Show entry points
#[arg(long)]
entry_points: bool,
/// Show all discovered files
#[arg(long)]
files: bool,
/// Show active plugins
#[arg(long)]
plugins: bool,
/// Show architecture boundary zones, rules, and per-zone file counts
#[arg(long)]
boundaries: bool,
},
/// Find code duplication / clones across the project
Dupes {
/// Detection mode: strict, mild, weak, or semantic
#[arg(long, default_value = "mild")]
mode: DupesMode,
/// Minimum token count for a clone
#[arg(long, default_value = "50")]
min_tokens: usize,
/// Minimum line count for a clone
#[arg(long, default_value = "5")]
min_lines: usize,
/// Fail if duplication exceeds this percentage (0 = no limit)
#[arg(long, default_value = "0")]
threshold: f64,
/// Only report cross-directory duplicates
#[arg(long)]
skip_local: bool,
/// Enable cross-language detection (strip TS type annotations for TS↔JS matching)
#[arg(long)]
cross_language: bool,
/// Exclude import declarations from clone detection (reduces noise from sorted import blocks)
#[arg(long)]
ignore_imports: bool,
/// Show only the N largest clone groups
#[arg(long)]
top: Option<usize>,
/// Trace all clones at a specific location (format: `FILE:LINE`)
#[arg(long, value_name = "FILE:LINE")]
trace: Option<String>,
},
/// Analyze function complexity (cyclomatic + cognitive)
///
/// By default, shows all existing sections: health score, complexity findings,
/// file scores, hotspots, and refactoring targets. When any section flag is
/// specified, only those sections are shown.
Health {
/// Maximum cyclomatic complexity threshold (overrides config)
#[arg(long)]
max_cyclomatic: Option<u16>,
/// Maximum cognitive complexity threshold (overrides config)
#[arg(long)]
max_cognitive: Option<u16>,
/// Maximum CRAP score threshold (overrides config, default 30.0).
/// Functions meeting or exceeding this score are reported alongside
/// complexity findings. Pair with `--coverage` for accurate scoring.
#[arg(long)]
max_crap: Option<f64>,
/// Show only the N most complex functions
#[arg(long)]
top: Option<usize>,
/// Sort by: cyclomatic (default), cognitive, lines, or severity
#[arg(long, default_value = "cyclomatic")]
sort: SortBy,
/// Show only complexity findings (functions exceeding thresholds).
/// By default all sections are shown; use this to select only complexity.
#[arg(long)]
complexity: bool,
/// Show only per-file health scores (fan-in, fan-out, dead code ratio, maintainability index).
/// Requires full analysis pipeline (graph + dead code detection).
/// Sorted by maintainability index ascending (worst first). --sort and --baseline
/// apply to complexity findings only, not file scores.
#[arg(long)]
file_scores: bool,
/// Show only static test coverage gaps: runtime files and exports with no
/// dependency path from any discovered test root. Requires full analysis pipeline.
#[arg(long)]
coverage_gaps: bool,
/// Show only hotspots: files that are both complex and frequently changing.
/// Combines git churn history with complexity data. Requires a git repository.
#[arg(long)]
hotspots: bool,
/// Attach ownership signals to hotspot entries: bus factor, contributor
/// count, declared CODEOWNERS owner, and ownership drift. Implies
/// `--hotspots`. Requires a git repository.
#[arg(long)]
ownership: bool,
/// Privacy mode for author emails emitted with `--ownership`.
/// Defaults to `handle` (local-part only). Use `raw` for OSS repos
/// where authors are public, or `hash` to emit non-reversible
/// pseudonyms in regulated environments. Implies `--ownership`.
#[arg(long, value_name = "MODE", value_enum)]
ownership_emails: Option<EmailModeArg>,
/// Show only refactoring targets: ranked recommendations based on complexity,
/// coupling, churn, and dead code signals. Requires full analysis pipeline.
#[arg(long)]
targets: bool,
/// Filter refactoring targets by effort level (low, medium, high).
/// Implies --targets.
#[arg(long, value_enum)]
effort: Option<EffortFilter>,
/// Show only the project health score (0–100) with letter grade (A/B/C/D/F).
/// The score is included by default when no section flags are set.
#[arg(long)]
score: bool,
/// Fail if the health score is below this threshold (0–100).
/// Implies --score. Useful as a CI quality gate.
#[arg(long, value_name = "N")]
min_score: Option<f64>,
/// Only exit with error for findings at or above this severity.
/// Use --min-severity critical to ignore moderate/high findings in CI.
#[arg(long, value_name = "LEVEL", value_enum)]
min_severity: Option<crate::health_types::FindingSeverity>,
/// Git history window for hotspot analysis (default: 6m).
/// Accepts durations (6m, 90d, 1y, 2w) or ISO dates (2025-06-01).
#[arg(long, value_name = "DURATION")]
since: Option<String>,
/// Minimum number of commits for a file to be included in hotspot ranking (default: 3)
#[arg(long, value_name = "N")]
min_commits: Option<u32>,
/// Save a vital signs snapshot for trend tracking.
/// Defaults to `.fallow/snapshots/{timestamp}.json` if no path is given.
/// Forces file-scores, hotspot, and score computation for complete metrics.
#[expect(
clippy::option_option,
reason = "clap pattern: None=not passed, Some(None)=flag only, Some(Some(path))=with value"
)]
#[arg(long, value_name = "PATH", num_args = 0..=1, default_missing_value = "")]
save_snapshot: Option<Option<String>>,
/// Compare current metrics against the most recent saved snapshot.
/// Reads from `.fallow/snapshots/` and shows per-metric deltas with
/// directional indicators. Implies --score.
#[arg(long)]
trend: bool,
/// Path to coverage data (coverage-final.json) for exact per-function
/// CRAP scores. Generate with `jest --coverage`, `vitest run --coverage
/// --provider istanbul`, or any Istanbul-compatible tool. Requires
/// Istanbul format (not v8/c8 native format). Accepts a single
/// Istanbul coverage map JSON file or a directory containing
/// coverage-final.json. Use --coverage-root when the file was generated
/// in a different environment (CI runner, Docker). Affects CRAP scores
/// only, not --coverage-gaps. Also configurable via FALLOW_COVERAGE env var.
#[arg(long, value_name = "PATH")]
coverage: Option<PathBuf>,
/// Rebase file paths in coverage data by stripping this prefix and
/// prepending the project root. Use when coverage was generated in a
/// different environment (CI runner, Docker). Example: if coverage paths
/// start with /home/runner/work/myapp and the project root is ./,
/// pass --coverage-root /home/runner/work/myapp.
#[arg(long, value_name = "PATH")]
coverage_root: Option<PathBuf>,
/// File or directory containing runtime coverage input. Accepts a
/// V8 coverage directory, a single V8 JSON file, or a single
/// Istanbul coverage map JSON file (commonly coverage-final.json).
#[arg(long, value_name = "PATH")]
runtime_coverage: Option<PathBuf>,
/// Threshold for hot-path classification
#[arg(long, default_value_t = 100)]
min_invocations_hot: u64,
/// Minimum total trace volume before the sidecar allows high-confidence
/// `safe_to_delete` / `review_required` verdicts. Below this the
/// sidecar caps confidence at `medium` to protect against overconfident
/// verdicts on new or low-traffic services. Omit to use the sidecar's
/// spec default (5000).
#[arg(long, value_name = "N")]
min_observation_volume: Option<u32>,
/// Fraction of total trace count below which an invoked function is
/// classified as `low_traffic` rather than `active`. Expressed as a
/// decimal (e.g. `0.001` for 0.1%). Omit to use the sidecar's spec
/// default (0.001).
#[arg(long, value_name = "RATIO")]
low_traffic_threshold: Option<f64>,
},
/// Detect feature flag patterns in the codebase
///
/// Identifies environment variable flags (process.env.FEATURE_*),
/// SDK calls (LaunchDarkly, Statsig, Unleash, GrowthBook), and
/// config object patterns (opt-in). Reports flag locations, detection
/// confidence, and cross-reference with dead code findings.
Flags {
/// Show only the top N flags
#[arg(long)]
top: Option<usize>,
},
/// Explain one fallow issue type without running an analysis.
///
/// Prints the rule rationale, a worked example, fix guidance, and the
/// relevant docs URL. Accepts values like `unused-export`,
/// `fallow/unused-export`, `unused-exports`, and `code-duplication`.
Explain {
/// Issue type or rule id to explain
issue_type: String,
},
/// Audit changed files for dead code, complexity, and duplication.
///
/// Purpose-built for reviewing AI-generated code and PR quality gates.
/// Combines dead-code + complexity + duplication scoped to changed files
/// and returns a verdict (pass/warn/fail).
/// Auto-detects the base branch if --changed-since/--base is not set.
/// By default, only findings introduced by the changeset affect the verdict;
/// inherited findings are reported with new-vs-inherited attribution and
/// individual JSON findings include `introduced: true/false`. Use
/// `--gate all` or `[audit] gate = "all"` to fail on every finding in
/// changed files without running the extra base-snapshot attribution pass.
///
/// The global --baseline / --save-baseline flags are rejected on audit.
/// Use --dead-code-baseline, --health-baseline, and --dupes-baseline
/// (or their config equivalents) because each sub-analysis uses a
/// different baseline format.
Audit {
/// Run dead-code analysis in production mode for this audit.
#[arg(long = "production-dead-code")]
production_dead_code: bool,
/// Run health analysis in production mode for this audit.
#[arg(long = "production-health")]
production_health: bool,
/// Run duplication analysis in production mode for this audit.
#[arg(long = "production-dupes")]
production_dupes: bool,
/// Compare dead-code issues against a saved baseline
/// (produced by `fallow dead-code --save-baseline`).
#[arg(long)]
dead_code_baseline: Option<PathBuf>,
/// Compare health findings against a saved baseline
/// (produced by `fallow health --save-baseline`).
#[arg(long)]
health_baseline: Option<PathBuf>,
/// Compare duplication clone groups against a saved baseline
/// (produced by `fallow dupes --save-baseline`).
#[arg(long)]
dupes_baseline: Option<PathBuf>,
/// Maximum CRAP score threshold (overrides config, default 30.0).
/// Functions meeting or exceeding this score cause audit to fail.
/// Pair with `--coverage` for accurate scoring.
#[arg(long)]
max_crap: Option<f64>,
/// Which findings affect the audit verdict.
///
/// new-only (default): fail only on findings introduced by the current
/// changeset. all: fail on every finding in changed files and skip
/// base-snapshot attribution.
#[arg(long, value_enum)]
gate: Option<AuditGateArg>,
},
/// Dump the CLI interface as machine-readable JSON for agent introspection
Schema,
/// Print or vendor CI integration templates.
///
/// Use `fallow ci-template gitlab` to print the GitLab CI template, or
/// `fallow ci-template gitlab --vendor` to write the template plus the
/// jq/bash helper files that enable MR comments without downloading from
/// raw.githubusercontent.com at pipeline runtime.
CiTemplate {
#[command(subcommand)]
subcommand: CiTemplateCli,
},
/// Migrate configuration from knip or jscpd to fallow
Migrate {
/// Generate TOML instead of JSONC
#[arg(long)]
toml: bool,
/// Only preview the generated config without writing
#[arg(long)]
dry_run: bool,
/// Path to source config file (auto-detect if not specified)
#[arg(long, value_name = "PATH")]
from: Option<PathBuf>,
},
/// Manage the license for continuous/cloud runtime monitoring.
///
/// Verification is offline against an Ed25519 public key compiled into
/// the binary. The license file lives at `~/.fallow/license.jwt` (or
/// `$FALLOW_LICENSE_PATH`); `$FALLOW_LICENSE` env var takes precedence
/// and is the recommended path for shared CI runners.
License {
#[command(subcommand)]
subcommand: LicenseCli,
},
/// Runtime coverage workflow.
///
/// `setup` is the resumable single-entry-point first-run flow: license
/// check → sidecar install → coverage recipe → analysis. Spec:
/// `.internal/spec-runtime-coverage-phase-2.md` (private repo).
Coverage {
#[command(subcommand)]
subcommand: CoverageCli,
},
/// Install or remove a Claude Code PreToolUse hook that gates
/// `git commit` / `git push` on `fallow audit`, so the agent cleans
/// findings before the command runs.
///
/// This is the legacy AGENT-level enforcement command. Prefer
/// `fallow hooks install --target agent` for new setup. It writes into
/// `.claude/settings.json` + `.claude/hooks/fallow-gate.sh` (and
/// optionally an `AGENTS.md` managed block for Codex). For a
/// shell-level Git pre-commit hook in `.git/hooks/`, see
/// `fallow hooks install --target git` instead. Both targets can be used
/// together: git hooks catch human commits, agent hooks catch agent
/// commits.
///
/// See `/integrations/claude-hooks` in the docs for the full recipe.
SetupHooks {
/// Target a specific agent surface (default: auto-detect).
#[arg(long, value_enum)]
agent: Option<setup_hooks::HookAgentArg>,
/// Print what would be written or removed without touching the filesystem.
#[arg(long)]
dry_run: bool,
/// Overwrite a user-edited hook script, invalid settings.json, or
/// remove a user-edited script during uninstall.
#[arg(long)]
force: bool,
/// Write to the user's home directory instead of the project root.
#[arg(long)]
user: bool,
/// Append `.claude/` to the project's `.gitignore`.
#[arg(long)]
gitignore_claude: bool,
/// Remove the fallow-gate handler, hook script, and AGENTS.md
/// managed block instead of installing them. Idempotent: reports
/// "unchanged" when nothing to remove.
#[arg(long)]
uninstall: bool,
},
}
#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
enum HooksTargetArg {
/// Shell-level Git pre-commit hook under .git/hooks/ or .husky/.
Git,
/// Agent-level Claude Code / Codex gate.
Agent,
}
#[derive(clap::Subcommand)]
enum HooksCli {
/// Install a fallow-managed hook.
Install {
/// Hook surface to install.
#[arg(long, value_enum)]
target: HooksTargetArg,
/// Fallback base branch/ref for Git pre-commit hooks when no upstream is set.
#[arg(long)]
branch: Option<String>,
/// Target a specific agent surface when --target agent is used.
#[arg(long, value_enum)]
agent: Option<setup_hooks::HookAgentArg>,
/// Print what would be written without touching the filesystem.
#[arg(long)]
dry_run: bool,
/// Overwrite an existing managed or user-edited hook.
#[arg(long)]
force: bool,
/// Write agent hooks to the user's home directory instead of the project root.
#[arg(long)]
user: bool,
/// Append `.claude/` to the project's `.gitignore` for Claude agent hooks.
#[arg(long)]
gitignore_claude: bool,
},
/// Remove a fallow-managed hook.
Uninstall {
/// Hook surface to remove.
#[arg(long, value_enum)]
target: HooksTargetArg,
/// Target a specific agent surface when --target agent is used.
#[arg(long, value_enum)]
agent: Option<setup_hooks::HookAgentArg>,
/// Print what would be removed without touching the filesystem.
#[arg(long)]
dry_run: bool,
/// Remove a user-edited hook script or Git hook instead of preserving it.
#[arg(long)]
force: bool,
/// Remove agent hooks from the user's home directory instead of the project root.
#[arg(long)]
user: bool,
},
}
#[derive(clap::Subcommand)]
enum LicenseCli {
/// Activate a license JWT.
///
/// JWT input precedence: positional arg > `--from-file` > stdin (`-`).
/// All paths normalize whitespace before crypto verification.
Activate {
/// JWT as a positional argument.
#[arg(value_name = "JWT")]
jwt: Option<String>,
/// Path to a file containing the JWT.
#[arg(long, value_name = "PATH")]
from_file: Option<PathBuf>,
/// Read JWT from stdin.
#[arg(long, conflicts_with_all = ["jwt", "from_file"])]
stdin: bool,
/// Start a 30-day email-gated trial in one step.
///
/// The trial endpoint is rate-limited to 5 requests per hour per IP.
/// In CI or behind a shared NAT, start the trial from a developer
/// machine and set FALLOW_LICENSE (or FALLOW_LICENSE_PATH) on the
/// runner instead of re-running `activate --trial` per job.
#[arg(long, requires = "email")]
trial: bool,
/// Email address for the trial flow.
#[arg(long, value_name = "ADDR")]
email: Option<String>,
},
/// Show the active license tier, seats, features, and days remaining.
Status,
/// Fetch a fresh JWT from `api.fallow.cloud` (network-only).
Refresh,
/// Remove the local license file.
Deactivate,
}
#[derive(clap::Subcommand)]
enum CiTemplateCli {
/// Print or vendor the GitLab CI template and MR integration helpers.
Gitlab {
/// Write ci/ and action/ helper files under DIR instead of printing the template.
///
/// Passing --vendor without a DIR writes into the current directory.
#[arg(long, value_name = "DIR", num_args = 0..=1, default_missing_value = ".")]
vendor: Option<PathBuf>,
/// Overwrite existing files that differ from the bundled template.
#[arg(long)]
force: bool,
},
}
#[derive(clap::Subcommand)]
enum CoverageCli {
/// Resumable first-run setup: license + sidecar + recipe + analysis.
Setup {
/// Accept all prompts automatically.
#[arg(short = 'y', long)]
yes: bool,
/// Print instructions instead of prompting.
#[arg(long)]
non_interactive: bool,
/// Emit deterministic setup instructions as JSON. Implies --non-interactive.
#[arg(long)]
json: bool,
},
/// Analyze runtime coverage from a local artifact or explicit cloud source.
///
/// Cloud mode is opt-in only. `FALLOW_API_KEY` by itself never selects
/// cloud mode; pass `--cloud` / `--runtime-coverage-cloud`, or set
/// `FALLOW_RUNTIME_COVERAGE_SOURCE=cloud`.
Analyze {
/// File or directory containing local runtime coverage input.
#[arg(long, value_name = "PATH", conflicts_with = "cloud")]
runtime_coverage: Option<PathBuf>,
/// Fetch latest runtime facts from fallow cloud for the selected repo.
#[arg(long, visible_alias = "runtime-coverage-cloud")]
cloud: bool,
/// Fallow cloud API key. Precedence: this flag > $FALLOW_API_KEY.
#[arg(long, value_name = "KEY")]
api_key: Option<String>,
/// Override the fallow cloud base URL.
#[arg(long, value_name = "URL")]
api_endpoint: Option<String>,
/// Repository identifier, for example `owner/repo`.
///
/// Defaults to $FALLOW_REPO, then the parsed origin URL from
/// `git remote get-url origin`. Slashes are percent-encoded as one
/// URL segment when calling the cloud runtime-context endpoint.
#[arg(long, value_name = "OWNER/REPO")]
repo: Option<String>,
/// Optional monorepo/project disambiguator.
#[arg(long, value_name = "ID")]
project_id: Option<String>,
/// Runtime observation window to request from cloud (1..=90 days).
#[arg(long, value_name = "DAYS", default_value_t = 30)]
coverage_period: u16,
/// Optional runtime environment filter.
#[arg(long, value_name = "ENV")]
environment: Option<String>,
/// Optional commit SHA filter for cloud runtime facts.
#[arg(long, value_name = "SHA")]
commit_sha: Option<String>,
/// Analyze production code only.
#[arg(long)]
production: bool,
/// Threshold for hot-path classification.
#[arg(long, default_value_t = 100)]
min_invocations_hot: u64,
/// Minimum total trace volume before high-confidence verdicts.
#[arg(long, value_name = "N")]
min_observation_volume: Option<u32>,
/// Fraction of total trace count below which an invoked function is low traffic.
#[arg(long, value_name = "RATIO")]
low_traffic_threshold: Option<f64>,
/// Show only the top N runtime findings and hot paths.
#[arg(long)]
top: Option<usize>,
/// Show the first-class blast-radius section in human output.
#[arg(long)]
blast_radius: bool,
/// Show the first-class importance section in human output.
#[arg(long)]
importance: bool,
},
/// Upload a static function inventory to fallow cloud (Production
/// Coverage, paid). Unlocks the `untracked` filter on the dashboard by
/// pairing runtime coverage data with the AST view of "every function
/// that exists". See <https://docs.fallow.tools/analysis/runtime-coverage>.
///
/// This command makes network calls to fallow cloud. `fallow dead-code`
/// stays offline.
///
/// Exit codes: 0 ok · 7 network · 10 validation · 11 payload too large
/// · 12 auth rejected · 13 server error.
UploadInventory {
/// Fallow cloud API key (bearer token).
///
/// Precedence: this flag > $FALLOW_API_KEY. Generate at
/// <https://fallow.cloud/settings#api-keys>.
///
/// Security: prefer $FALLOW_API_KEY on shared CI runners. Passing a
/// secret on the command line may be visible to other processes via
/// `ps` and can leak into shell history or process audit logs.
#[arg(long, value_name = "KEY")]
api_key: Option<String>,
/// Override the fallow cloud base URL.
///
/// Useful for staging and on-premise deployments. Also respects
/// $FALLOW_API_URL when this flag is not set.
#[arg(long, value_name = "URL")]
api_endpoint: Option<String>,
/// Project identifier, for example `fallow-cloud-api` or `owner/repo`.
///
/// Defaults to $GITHUB_REPOSITORY, then $CI_PROJECT_PATH, then the
/// parsed origin URL from `git remote get-url origin`.
#[arg(long, value_name = "PROJECT_ID")]
project_id: Option<String>,
/// Explicit git SHA for this inventory.
///
/// Default: `git rev-parse HEAD`. The inventory is keyed on this
/// value; the cloud back-fills hourly buckets with a matching SHA.
#[arg(long, value_name = "SHA")]
git_sha: Option<String>,
/// Proceed even when the working tree has uncommitted changes.
///
/// Warning: the inventory is generated from the working copy, so it
/// may not match the uploaded git SHA. Commit or stash first if you
/// want a SHA-exact upload.
#[arg(long)]
allow_dirty: bool,
/// Additional glob patterns to exclude from the walk.
///
/// Applied after the existing fallow ignore rules. Repeatable.
#[arg(long, value_name = "GLOB", num_args = 0..)]
exclude_paths: Vec<String>,
/// Prefix prepended to every emitted filePath so the static
/// inventory joins with the runtime beacon for your deployment.
/// Required for containerized deployments where the deployed
/// WORKDIR rebases paths at runtime. Default: none (paths emit
/// repo-relative, matching local runs and non-container CI).
///
/// Common values: `/app` (typical Dockerfile), `/workspace`
/// (Buildpacks / Cloud Run), `/usr/src/app` (older Node images),
/// `/var/task` (Lambda), `/home/runner/work/<repo>/<repo>`
/// (GitHub Actions default checkout).
///
/// Must start with `/` and use POSIX separators.
#[arg(long, value_name = "PREFIX")]
path_prefix: Option<String>,
/// Print what would be uploaded and exit. No network call.
#[arg(long)]
dry_run: bool,
/// Treat transient upload failures as warnings instead of errors
/// (exit 0). Validation and auth errors still fail hard; this only
/// downgrades transport and server errors.
#[arg(long)]
ignore_upload_errors: bool,
},
/// Upload JavaScript source maps to fallow cloud for bundled runtime coverage.
///
/// Scans a build output directory for `.map` files and uploads them under
/// the selected repo + git SHA. The production beacon reports bundled
/// paths; the cloud resolver uses these maps to remap runtime coverage back
/// to original source files.
UploadSourceMaps {
/// Directory to scan recursively for source maps.
#[arg(long, value_name = "PATH", default_value = "dist")]
dir: PathBuf,
/// Glob pattern, relative to --dir, selecting maps to upload.
#[arg(long, value_name = "GLOB", default_value = "**/*.map")]
include: String,
/// Glob pattern, relative to --dir, selecting files to skip.
///
/// Repeatable. Defaults to `**/node_modules/**`.
#[arg(long, value_name = "GLOB", default_value = "**/node_modules/**")]
exclude: Vec<String>,
/// Repo name used in the API path.
///
/// Defaults to package.json repository.url, then `git remote get-url origin`.
#[arg(long, value_name = "NAME")]
repo: Option<String>,
/// Commit SHA to key uploads under.
///
/// Defaults to $GITHUB_SHA, $CI_COMMIT_SHA, $COMMIT_SHA, then
/// `git rev-parse HEAD`.
#[arg(long, value_name = "SHA")]
git_sha: Option<String>,
/// Override the fallow cloud base URL.
#[arg(long, value_name = "URL")]
endpoint: Option<String>,
/// Send only the basename as fileName by default.
///
/// Use `--strip-path=false` when your runtime coverage reports bundle
/// paths relative to the build directory, such as `assets/app.js`.
#[arg(long, value_name = "BOOL", default_value_t = true, action = clap::ArgAction::Set)]
strip_path: bool,
/// Print what would be uploaded and exit. No network call.
#[arg(long)]
dry_run: bool,
/// Parallel upload fanout.
#[arg(long, value_name = "N", default_value_t = 4)]
concurrency: usize,
/// Stop on first upload error.
#[arg(long)]
fail_fast: bool,
},
}
#[derive(Clone, Copy, clap::ValueEnum)]
enum Format {
Human,
Json,
Sarif,
Compact,
Markdown,
#[value(
name = "codeclimate",
alias = "gitlab-codequality",
alias = "gitlab-code-quality"
)]
CodeClimate,
Badge,
}
impl From<Format> for fallow_config::OutputFormat {
fn from(f: Format) -> Self {
match f {
Format::Human => Self::Human,
Format::Json => Self::Json,
Format::Sarif => Self::Sarif,
Format::Compact => Self::Compact,
Format::Markdown => Self::Markdown,
Format::CodeClimate => Self::CodeClimate,
Format::Badge => Self::Badge,
}
}
}
/// Filter refactoring targets by effort level.
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum EffortFilter {
Low,
Medium,
High,
}
impl EffortFilter {
/// Convert to the corresponding `EffortEstimate` for comparison.
const fn to_estimate(self) -> health_types::EffortEstimate {
match self {
Self::Low => health_types::EffortEstimate::Low,
Self::Medium => health_types::EffortEstimate::Medium,
Self::High => health_types::EffortEstimate::High,
}
}
}
/// Privacy mode for author emails emitted by `--ownership`.
///
/// CLI mirror of [`fallow_config::EmailMode`]. Kept as a separate enum so
/// the help text controls rendering and we don't leak config-internal
/// schema details into clap.
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum EmailModeArg {
/// Show full email addresses as recorded in git history.
Raw,
/// Show local-part only (default). Unwraps GitHub-style noreply prefixes.
Handle,
/// Show stable non-cryptographic pseudonyms (`xxh3:<hex>`).
Hash,
}
impl EmailModeArg {
/// Convert to the equivalent config-level mode.
const fn to_config(self) -> fallow_config::EmailMode {
match self {
Self::Raw => fallow_config::EmailMode::Raw,
Self::Handle => fallow_config::EmailMode::Handle,
Self::Hash => fallow_config::EmailMode::Hash,
}
}
}
/// CLI mirror of [`fallow_config::AuditGate`].
#[derive(Clone, Copy, Debug, PartialEq, Eq, clap::ValueEnum)]
pub enum AuditGateArg {
/// Only findings introduced by the current changeset affect the verdict.
NewOnly,
/// All findings in changed files affect the verdict.
All,
}
impl From<AuditGateArg> for fallow_config::AuditGate {
fn from(value: AuditGateArg) -> Self {
match value {
AuditGateArg::NewOnly => Self::NewOnly,
AuditGateArg::All => Self::All,
}
}
}
// See `error.rs` — `emit_error` is re-exported via `use error::emit_error`.
// ── Environment variable helpers ─────────────────────────────────
/// Read `FALLOW_FORMAT` env var and parse it into a Format value.
fn format_from_env() -> Option<Format> {
let val = std::env::var("FALLOW_FORMAT").ok()?;
match val.to_lowercase().as_str() {
"json" => Some(Format::Json),
"human" => Some(Format::Human),
"sarif" => Some(Format::Sarif),
"compact" => Some(Format::Compact),
"markdown" | "md" => Some(Format::Markdown),
"codeclimate" | "gitlab-codequality" | "gitlab-code-quality" => Some(Format::CodeClimate),
"badge" => Some(Format::Badge),
_ => None,
}
}
/// Read `FALLOW_QUIET` env var: "1" or "true" (case-insensitive) means quiet.
fn quiet_from_env() -> bool {
std::env::var("FALLOW_QUIET").is_ok_and(|v| v == "1" || v.eq_ignore_ascii_case("true"))
}
fn bool_from_env(name: &str) -> Option<bool> {
let value = std::env::var(name).ok()?;
match value.trim().to_ascii_lowercase().as_str() {
"1" | "true" | "yes" | "on" => Some(true),
"0" | "false" | "no" | "off" => Some(false),
_ => None,
}
}
/// Resolve an audit baseline path using CLI > config precedence.
///
/// Both sources resolve relative paths against the project root. This keeps
/// behavior consistent in CI scripts where `--root $REPO_ROOT` differs from
/// the process CWD.
fn resolve_audit_baseline_path(
root: &std::path::Path,
cli: Option<&std::path::Path>,
config: Option<&str>,
) -> Option<PathBuf> {
let path = cli.map(std::path::Path::to_path_buf).or_else(|| {
config.map(|p| {
let path = PathBuf::from(p);
if path.is_absolute() {
path
} else {
root.join(path)
}
})
})?;
if path.is_absolute() {
Some(path)
} else {
Some(root.join(path))
}
}
// ── Format resolution ─────────────────────────────────────────────
struct FormatConfig {
output: fallow_config::OutputFormat,
quiet: bool,
cli_format_was_explicit: bool,
}
fn resolve_format(cli: &Cli) -> FormatConfig {
// Resolve output format: CLI flag > FALLOW_FORMAT env var > default ("human").
// clap sets the default to "human", so we only override with the env var
// when the user did NOT explicitly pass --format on the CLI.
let cli_format_was_explicit = std::env::args()
.any(|a| a == "--format" || a == "--output" || a.starts_with("--format=") || a == "-f");
let format: Format = if cli_format_was_explicit {
cli.format
} else {
format_from_env().unwrap_or(cli.format)
};
// Resolve quiet: CLI --quiet flag > FALLOW_QUIET env var > false
let quiet = cli.quiet || quiet_from_env();
FormatConfig {
output: format.into(),
quiet,
cli_format_was_explicit,
}
}
// ── Tracing setup ─────────────────────────────────────────────────
/// Build the tracing filter for the CLI.
///
/// Human output should stay clean by default, even when stderr is redirected to a
/// file or captured by an agent. Internal INFO-level tracing is therefore opt-in
/// via `RUST_LOG`, while warnings remain visible. An explicitly empty `RUST_LOG`
/// disables tracing entirely, which keeps the test harness deterministic.
fn build_tracing_filter(rust_log: Option<&str>) -> tracing_subscriber::EnvFilter {
use tracing_subscriber::filter::LevelFilter;
let builder = tracing_subscriber::EnvFilter::builder();
match rust_log.map(str::trim) {
Some("") => builder
.with_default_directive(LevelFilter::OFF.into())
.parse_lossy("off"),
Some(value) => builder
.with_default_directive(LevelFilter::OFF.into())
.parse_lossy(value),
None => builder
.with_default_directive(LevelFilter::WARN.into())
.parse_lossy(""),
}
}
/// Set up tracing for the CLI.
fn setup_tracing() {
let rust_log = std::env::var("RUST_LOG").ok();
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_env_filter(build_tracing_filter(rust_log.as_deref()))
.with_target(false)
.with_timer(tracing_subscriber::fmt::time::uptime())
.init();
}
// ── Input validation ──────────────────────────────────────────────
fn validate_inputs(
cli: &Cli,
output: fallow_config::OutputFormat,
) -> Result<(PathBuf, usize), ExitCode> {
// Validate control characters in key string inputs
if let Some(ref config_path) = cli.config
&& let Some(s) = config_path.to_str()
&& let Err(e) = validate::validate_no_control_chars(s, "--config")
{
return Err(emit_error(&e, 2, output));
}
if let Some(ref ws_patterns) = cli.workspace {
for ws in ws_patterns {
if let Err(e) = validate::validate_no_control_chars(ws, "--workspace") {
return Err(emit_error(&e, 2, output));
}
}
}
if let Some(ref git_ref) = cli.changed_since
&& let Err(e) = validate::validate_no_control_chars(git_ref, "--changed-since")
{
return Err(emit_error(&e, 2, output));
}
if let Some(ref git_ref) = cli.changed_workspaces
&& let Err(e) = validate::validate_no_control_chars(git_ref, "--changed-workspaces")
{
return Err(emit_error(&e, 2, output));
}
// --workspace and --changed-workspaces are mutually exclusive: one is an
// explicit list of packages, the other is git-derived. Mixing them has no
// coherent intersection semantics, so reject early with a targeted message.
if cli.workspace.is_some() && cli.changed_workspaces.is_some() {
return Err(emit_error(
"--workspace and --changed-workspaces are mutually exclusive. \
Pick one: --workspace for explicit package names/globs, \
--changed-workspaces for git-derived monorepo CI scoping.",
2,
output,
));
}
// Validate and resolve root
let raw_root = cli
.root
.clone()
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current directory"));
let root = match validate::validate_root(&raw_root) {
Ok(r) => r,
Err(e) => {
return Err(emit_error(&e, 2, output));
}
};
// Validate --changed-since early
if let Some(ref git_ref) = cli.changed_since
&& let Err(e) = validate::validate_git_ref(git_ref)
{
return Err(emit_error(
&format!("invalid --changed-since: {e}"),
2,
output,
));
}
if let Some(ref git_ref) = cli.changed_workspaces
&& let Err(e) = validate::validate_git_ref(git_ref)
{
return Err(emit_error(
&format!("invalid --changed-workspaces: {e}"),
2,
output,
));
}
let threads = cli
.threads
.unwrap_or_else(|| std::thread::available_parallelism().map_or(4, std::num::NonZero::get));
rayon_pool::configure_global_pool(threads);
Ok((root, threads))
}
/// Apply CI defaults: if `--ci` is set, override format to SARIF (unless explicit),
/// enable fail-on-issues, and set quiet. Returns (output, quiet, `fail_on_issues`).
fn apply_ci_defaults(
ci: bool,
mut fail_on_issues: bool,
output: fallow_config::OutputFormat,
quiet: bool,
cli_format_was_explicit: bool,
) -> (fallow_config::OutputFormat, bool, bool) {
if ci {
let ci_output = if !cli_format_was_explicit && format_from_env().is_none() {
fallow_config::OutputFormat::Sarif
} else {
output
};
fail_on_issues = true;
(ci_output, true, fail_on_issues)
} else {
(output, quiet, fail_on_issues)
}
}
// ── Helpers ──────────────────────────────────────────────────────
fn build_regression_opts<'a>(
fail_on_regression: bool,
tolerance: regression::Tolerance,
regression_baseline: Option<&'a std::path::Path>,
save_regression_file: Option<&'a std::path::PathBuf>,
save_to_config: bool,
scoped: bool,
quiet: bool,
) -> regression::RegressionOpts<'a> {
regression::RegressionOpts {
fail_on_regression,
tolerance,
regression_baseline_file: regression_baseline,
save_target: if let Some(path) = save_regression_file {
regression::SaveRegressionTarget::File(path)
} else if save_to_config {
regression::SaveRegressionTarget::Config
} else {
regression::SaveRegressionTarget::None
},
scoped,
quiet,
}
}
struct DispatchContext<'a> {
cli: &'a Cli,
root: &'a std::path::Path,
output: fallow_config::OutputFormat,
quiet: bool,
cli_format_was_explicit: bool,
threads: usize,
tolerance: regression::Tolerance,
save_regression_file: Option<&'a std::path::PathBuf>,
save_to_config: bool,
}
impl DispatchContext<'_> {
fn ci_defaults(&self) -> (fallow_config::OutputFormat, bool, bool) {
apply_ci_defaults(
self.cli.ci,
self.cli.fail_on_issues,
self.output,
self.quiet,
self.cli_format_was_explicit,
)
}
fn production_modes(
&self,
dead_code: bool,
health: bool,
dupes: bool,
) -> Result<ProductionModes, ExitCode> {
resolve_production_modes(self.cli, self.root, self.output, dead_code, health, dupes)
}
fn production_for(
&self,
analysis: fallow_config::ProductionAnalysis,
) -> Result<bool, ExitCode> {
self.production_modes(false, false, false)
.map(|modes| modes.for_analysis(analysis))
}
fn regression_opts(&self, scoped: bool) -> regression::RegressionOpts<'_> {
build_regression_opts(
self.cli.fail_on_regression,
self.tolerance,
self.cli.regression_baseline.as_deref(),
self.save_regression_file,
self.save_to_config,
scoped,
self.quiet,
)
}
}
#[derive(Clone, Copy)]
struct ProductionModes {
dead_code: bool,
health: bool,
dupes: bool,
}
impl ProductionModes {
const fn for_analysis(self, analysis: fallow_config::ProductionAnalysis) -> bool {
match analysis {
fallow_config::ProductionAnalysis::DeadCode => self.dead_code,
fallow_config::ProductionAnalysis::Health => self.health,
fallow_config::ProductionAnalysis::Dupes => self.dupes,
}
}
}
fn load_config_production(
root: &std::path::Path,
config_path: Option<&PathBuf>,
output: fallow_config::OutputFormat,
) -> Result<fallow_config::ProductionConfig, ExitCode> {
let loaded = if let Some(path) = config_path {
fallow_config::FallowConfig::load(path)
.map(Some)
.map_err(|e| {
emit_error(
&format!("failed to load config '{}': {e}", path.display()),
2,
output,
)
})?
} else {
fallow_config::FallowConfig::find_and_load(root)
.map(|found| found.map(|(config, _)| config))
.map_err(|e| emit_error(&e, 2, output))?
};
Ok(match loaded {
Some(config) => config.production,
None => fallow_config::ProductionConfig::default(),
})
}
fn resolve_production_modes(
cli: &Cli,
root: &std::path::Path,
output: fallow_config::OutputFormat,
production_dead_code: bool,
production_health: bool,
production_dupes: bool,
) -> Result<ProductionModes, ExitCode> {
let config = load_config_production(root, cli.config.as_ref(), output)?;
let env_global = bool_from_env("FALLOW_PRODUCTION");
let resolve_one =
|analysis: fallow_config::ProductionAnalysis, cli_specific: bool, env_name: &str| {
if cli.production || cli_specific {
true
} else if let Some(value) = bool_from_env(env_name) {
value
} else if let Some(value) = env_global {
value
} else {
config.for_analysis(analysis)
}
};
Ok(ProductionModes {
dead_code: resolve_one(
fallow_config::ProductionAnalysis::DeadCode,
production_dead_code,
"FALLOW_PRODUCTION_DEAD_CODE",
),
health: resolve_one(
fallow_config::ProductionAnalysis::Health,
production_health,
"FALLOW_PRODUCTION_HEALTH",
),
dupes: resolve_one(
fallow_config::ProductionAnalysis::Dupes,
production_dupes,
"FALLOW_PRODUCTION_DUPES",
),
})
}
// ── Main ─────────────────────────────────────────────────────────
fn main() -> ExitCode {
let mut cli = Cli::parse();
// Handle schema commands before tracing setup (no side effects)
if matches!(cli.command, Some(Command::Schema)) {
return schema::run_schema();
}
if matches!(cli.command, Some(Command::ConfigSchema)) {
return init::run_config_schema();
}
if matches!(cli.command, Some(Command::PluginSchema)) {
return init::run_plugin_schema();
}
let fmt = resolve_format(&cli);
setup_tracing();
let (root, threads) = match validate_inputs(&cli, fmt.output) {
Ok(v) => v,
Err(code) => return code,
};
let FormatConfig {
output,
quiet,
cli_format_was_explicit,
} = fmt;
// Validate --ci/--fail-on-issues/--sarif-file are not used with irrelevant commands
if (cli.ci || cli.fail_on_issues || cli.sarif_file.is_some())
&& matches!(
cli.command,
Some(
Command::Init { .. }
| Command::ConfigSchema
| Command::PluginSchema
| Command::Schema
| Command::Explain { .. }
| Command::CiTemplate { .. }
| Command::Config { .. }
| Command::List { .. }
| Command::Flags { .. }
| Command::Migrate { .. }
| Command::License { .. }
| Command::Coverage { .. }
| Command::Hooks { .. }
| Command::SetupHooks { .. }
)
)
{
return emit_error(
"--ci, --fail-on-issues, and --sarif-file are only valid with dead-code, dupes, health, or bare invocation",
2,
output,
);
}
// Validate --only/--skip are not used with a subcommand
if (!cli.only.is_empty() || !cli.skip.is_empty()) && cli.command.is_some() {
return emit_error(
"--only and --skip can only be used without a subcommand",
2,
output,
);
}
if (cli.production_dead_code || cli.production_health || cli.production_dupes)
&& cli.command.is_some()
{
return emit_error(
"--production-dead-code, --production-health, and --production-dupes can only be used without a subcommand. For audit, pass them after `audit`",
2,
output,
);
}
if !cli.only.is_empty() && !cli.skip.is_empty() {
return emit_error("--only and --skip are mutually exclusive", 2, output);
}
// Parse regression tolerance
let tolerance = match regression::Tolerance::parse(&cli.tolerance) {
Ok(t) => t,
Err(e) => return emit_error(&format!("invalid --tolerance: {e}"), 2, output),
};
// Resolve save-regression-baseline target
let save_regression_file: Option<std::path::PathBuf> =
cli.save_regression_baseline.as_ref().and_then(|opt| {
opt.as_ref()
.filter(|s| !s.is_empty())
.map(std::path::PathBuf::from)
});
let save_to_config = cli.save_regression_baseline.is_some() && save_regression_file.is_none();
let command = cli.command.take();
let dispatch = DispatchContext {
cli: &cli,
root: &root,
output,
quiet,
cli_format_was_explicit,
threads,
tolerance,
save_regression_file: save_regression_file.as_ref(),
save_to_config,
};
match command {
None => dispatch_bare_command(&dispatch),
Some(cmd) => dispatch_subcommand(cmd, &dispatch),
}
}
fn dispatch_bare_command(dispatch: &DispatchContext<'_>) -> ExitCode {
let cli = dispatch.cli;
let (output, quiet, fail_on_issues) = dispatch.ci_defaults();
let (run_check, run_dupes, run_health) = combined::resolve_analyses(&cli.only, &cli.skip);
let production = match dispatch.production_modes(
cli.production_dead_code,
cli.production_health,
cli.production_dupes,
) {
Ok(production) => production,
Err(code) => return code,
};
combined::run_combined(&combined::CombinedOptions {
root: dispatch.root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads: dispatch.threads,
quiet,
fail_on_issues,
sarif_file: cli.sarif_file.as_deref(),
changed_since: cli.changed_since.as_deref(),
baseline: cli.baseline.as_deref(),
save_baseline: cli.save_baseline.as_deref(),
production: cli.production,
production_dead_code: Some(production.dead_code),
production_health: Some(production.health),
production_dupes: Some(production.dupes),
workspace: cli.workspace.as_deref(),
changed_workspaces: cli.changed_workspaces.as_deref(),
group_by: cli.group_by,
explain: cli.explain,
explain_skipped: cli.explain_skipped,
performance: cli.performance,
summary: cli.summary,
run_check,
run_dupes,
run_health,
dupes_mode: cli.dupes_mode,
dupes_threshold: cli.dupes_threshold,
score: cli.score || cli.trend,
trend: cli.trend,
save_snapshot: cli.save_snapshot.as_ref(),
include_entry_exports: cli.include_entry_exports,
regression_opts: dispatch.regression_opts(
cli.changed_since.is_some()
|| cli.workspace.is_some()
|| cli.changed_workspaces.is_some(),
),
})
}
#[expect(
clippy::too_many_lines,
reason = "CLI dispatch handles all subcommands"
)]
fn dispatch_subcommand(command: Command, dispatch: &DispatchContext<'_>) -> ExitCode {
let cli = dispatch.cli;
let root = dispatch.root;
let output = dispatch.output;
let quiet = dispatch.quiet;
let threads = dispatch.threads;
match command {
Command::Check {
unused_files,
unused_exports,
unused_deps,
unused_types,
private_type_leaks,
unused_enum_members,
unused_class_members,
unresolved_imports,
unlisted_deps,
duplicate_exports,
circular_deps,
boundary_violations,
stale_suppressions,
include_dupes,
trace,
trace_file,
trace_dependency,
top,
file,
} => dispatch_check(
dispatch,
&CheckDispatchArgs {
filters: IssueFilters {
unused_files,
unused_exports,
unused_deps,
unused_types,
private_type_leaks,
unused_enum_members,
unused_class_members,
unresolved_imports,
unlisted_deps,
duplicate_exports,
circular_deps,
boundary_violations,
stale_suppressions,
},
trace_opts: TraceOptions {
trace_export: trace,
trace_file,
trace_dependency,
performance: cli.performance,
},
include_dupes,
top,
file,
},
),
Command::Watch { no_clear } => {
let production = match resolve_production_modes(cli, root, output, false, false, false)
{
Ok(modes) => modes.for_analysis(fallow_config::ProductionAnalysis::DeadCode),
Err(code) => return code,
};
watch::run_watch(&watch::WatchOptions {
root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads,
quiet,
production,
clear_screen: !no_clear,
explain: cli.explain,
include_entry_exports: cli.include_entry_exports,
})
}
Command::Fix { dry_run, yes } => {
let production = match resolve_production_modes(cli, root, output, false, false, false)
{
Ok(modes) => modes.for_analysis(fallow_config::ProductionAnalysis::DeadCode),
Err(code) => return code,
};
fix::run_fix(&fix::FixOptions {
root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads,
quiet,
dry_run,
yes,
production,
})
}
Command::Init {
toml,
hooks,
branch,
} => init::run_init(&init::InitOptions {
root,
use_toml: toml,
hooks,
branch: branch.as_deref(),
}),
Command::Hooks { subcommand } => run_hooks_command(root, subcommand, output),
Command::ConfigSchema => init::run_config_schema(),
Command::PluginSchema => init::run_plugin_schema(),
Command::CiTemplate { subcommand } => match subcommand {
CiTemplateCli::Gitlab { vendor, force } => {
ci_template::run_gitlab_template(&ci_template::GitlabTemplateOptions {
vendor_dir: vendor,
force,
})
}
},
Command::Config { path } => config::run_config(root, cli.config.as_deref(), path),
Command::List {
entry_points,
files,
plugins,
boundaries,
} => {
let production = match resolve_production_modes(cli, root, output, false, false, false)
{
Ok(modes) => modes.for_analysis(fallow_config::ProductionAnalysis::DeadCode),
Err(code) => return code,
};
list::run_list(&ListOptions {
root,
config_path: &cli.config,
output,
threads,
no_cache: cli.no_cache,
entry_points,
files,
plugins,
boundaries,
production,
})
}
Command::Dupes {
mode,
min_tokens,
min_lines,
threshold,
skip_local,
cross_language,
ignore_imports,
top,
trace,
} => dispatch_dupes(
dispatch,
&DupesDispatchArgs {
mode,
min_tokens,
min_lines,
threshold,
skip_local,
cross_language,
ignore_imports,
top,
trace,
},
),
Command::Health {
max_cyclomatic,
max_cognitive,
max_crap,
top,
sort,
complexity,
file_scores,
coverage_gaps,
hotspots,
ownership,
ownership_emails,
targets,
effort,
score,
min_score,
min_severity,
since,
min_commits,
save_snapshot,
trend,
coverage,
coverage_root,
runtime_coverage,
min_invocations_hot,
min_observation_volume,
low_traffic_threshold,
} => {
// Resolve coverage: CLI flag > FALLOW_COVERAGE env var
let coverage =
coverage.or_else(|| std::env::var("FALLOW_COVERAGE").ok().map(PathBuf::from));
// --ownership-emails implies --ownership; --ownership implies --hotspots
let ownership = ownership || ownership_emails.is_some();
let hotspots = hotspots || ownership;
dispatch_health(
dispatch,
HealthDispatchArgs {
max_cyclomatic,
max_cognitive,
max_crap,
top,
sort,
complexity,
file_scores,
coverage_gaps,
hotspots,
ownership,
ownership_emails: ownership_emails.map(EmailModeArg::to_config),
targets,
effort,
score,
min_score,
min_severity,
since: since.as_deref(),
min_commits,
save_snapshot: save_snapshot.as_ref(),
trend,
coverage: coverage.as_deref(),
coverage_root: coverage_root.as_deref(),
runtime_coverage: runtime_coverage.as_deref(),
min_invocations_hot,
min_observation_volume,
low_traffic_threshold,
},
)
}
Command::Flags { top } => {
let production = match resolve_production_modes(cli, root, output, false, false, false)
{
Ok(modes) => modes.for_analysis(fallow_config::ProductionAnalysis::DeadCode),
Err(code) => return code,
};
flags::run_flags(&flags::FlagsOptions {
root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads,
quiet,
production,
workspace: cli.workspace.as_deref(),
changed_workspaces: cli.changed_workspaces.as_deref(),
changed_since: cli.changed_since.as_deref(),
explain: cli.explain,
top,
})
}
Command::Explain { issue_type } => explain::run_explain(&issue_type, output),
Command::Audit {
production_dead_code,
production_health,
production_dupes,
dead_code_baseline,
health_baseline,
dupes_baseline,
max_crap,
gate,
} => {
if cli.baseline.is_some() || cli.save_baseline.is_some() {
return emit_error(
"audit uses per-analysis baselines. Use --dead-code-baseline, --health-baseline, or --dupes-baseline (or save them with `fallow dead-code|health|dupes --save-baseline <file>`)",
2,
output,
);
}
let audit_cfg = match load_config(
root,
&cli.config,
output,
cli.no_cache,
threads,
cli.production,
quiet,
) {
Ok(c) => c.audit,
Err(code) => return code,
};
let production = match resolve_production_modes(
cli,
root,
output,
production_dead_code,
production_health,
production_dupes,
) {
Ok(production) => production,
Err(code) => return code,
};
let resolved_dead_code_baseline = resolve_audit_baseline_path(
root,
dead_code_baseline.as_deref(),
audit_cfg.dead_code_baseline.as_deref(),
);
let resolved_health_baseline = resolve_audit_baseline_path(
root,
health_baseline.as_deref(),
audit_cfg.health_baseline.as_deref(),
);
let resolved_dupes_baseline = resolve_audit_baseline_path(
root,
dupes_baseline.as_deref(),
audit_cfg.dupes_baseline.as_deref(),
);
audit::run_audit(&audit::AuditOptions {
root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads,
quiet,
changed_since: cli.changed_since.as_deref(),
production: cli.production,
production_dead_code: Some(production.dead_code),
production_health: Some(production.health),
production_dupes: Some(production.dupes),
workspace: cli.workspace.as_deref(),
changed_workspaces: cli.changed_workspaces.as_deref(),
explain: cli.explain,
explain_skipped: cli.explain_skipped,
performance: cli.performance,
group_by: cli.group_by,
dead_code_baseline: resolved_dead_code_baseline.as_deref(),
health_baseline: resolved_health_baseline.as_deref(),
dupes_baseline: resolved_dupes_baseline.as_deref(),
max_crap,
gate: gate.map_or(audit_cfg.gate, Into::into),
include_entry_exports: cli.include_entry_exports,
})
}
Command::Schema => unreachable!("handled above"),
Command::Migrate {
toml,
dry_run,
from,
} => migrate::run_migrate(root, toml, dry_run, from.as_deref()),
Command::License { subcommand } => license::run(&map_license_subcommand(subcommand)),
Command::Coverage { subcommand } => coverage::run(
map_coverage_subcommand(&subcommand, cli.explain),
&coverage::RunContext {
root,
config_path: &cli.config,
output,
quiet,
no_cache: cli.no_cache,
threads,
explain: cli.explain,
},
),
Command::SetupHooks {
agent,
dry_run,
force,
user,
gitignore_claude,
uninstall,
} => setup_hooks::run_setup_hooks(&setup_hooks::SetupHooksOptions {
root,
agent,
dry_run,
force,
user,
gitignore_claude,
uninstall,
}),
}
}
fn run_hooks_command(
root: &std::path::Path,
subcommand: HooksCli,
output: fallow_config::OutputFormat,
) -> ExitCode {
match subcommand {
HooksCli::Install {
target: HooksTargetArg::Git,
branch,
agent,
dry_run,
force,
user,
gitignore_claude,
} => {
if agent.is_some() || user || gitignore_claude {
return emit_error(
"--agent, --user, and --gitignore-claude are only valid with `fallow hooks install --target agent`",
2,
output,
);
}
init::run_git_hooks_install(&init::GitHooksInstallOptions {
root,
branch: branch.as_deref(),
dry_run,
force,
})
}
HooksCli::Install {
target: HooksTargetArg::Agent,
branch,
agent,
dry_run,
force,
user,
gitignore_claude,
} => {
if branch.is_some() {
return emit_error(
"--branch is only valid with `fallow hooks install --target git`",
2,
output,
);
}
setup_hooks::run_setup_hooks_with_label(
&setup_hooks::SetupHooksOptions {
root,
agent,
dry_run,
force,
user,
gitignore_claude,
uninstall: false,
},
"fallow hooks install --target agent",
)
}
HooksCli::Uninstall {
target: HooksTargetArg::Git,
agent,
dry_run,
force,
user,
} => {
if agent.is_some() || user {
return emit_error(
"--agent and --user are only valid with `fallow hooks uninstall --target agent`",
2,
output,
);
}
init::run_git_hooks_uninstall(&init::GitHooksUninstallOptions {
root,
dry_run,
force,
})
}
HooksCli::Uninstall {
target: HooksTargetArg::Agent,
agent,
dry_run,
force,
user,
} => setup_hooks::run_setup_hooks_with_label(
&setup_hooks::SetupHooksOptions {
root,
agent,
dry_run,
force,
user,
gitignore_claude: false,
uninstall: true,
},
"fallow hooks uninstall --target agent",
),
}
}
fn map_license_subcommand(sub: LicenseCli) -> license::LicenseSubcommand {
match sub {
LicenseCli::Activate {
jwt,
from_file,
stdin,
trial,
email,
} => license::LicenseSubcommand::Activate(license::ActivateArgs {
raw_jwt: jwt,
from_file,
from_stdin: stdin,
trial,
email,
}),
LicenseCli::Status => license::LicenseSubcommand::Status,
LicenseCli::Refresh => license::LicenseSubcommand::Refresh,
LicenseCli::Deactivate => license::LicenseSubcommand::Deactivate,
}
}
fn map_coverage_subcommand(sub: &CoverageCli, explain: bool) -> coverage::CoverageSubcommand {
match sub {
CoverageCli::Setup {
yes,
non_interactive,
json,
} => coverage::CoverageSubcommand::Setup(coverage::SetupArgs {
yes: *yes,
non_interactive: *non_interactive || *json,
json: *json,
explain,
}),
CoverageCli::Analyze {
runtime_coverage,
cloud,
api_key,
api_endpoint,
repo,
project_id,
coverage_period,
environment,
commit_sha,
production,
min_invocations_hot,
min_observation_volume,
low_traffic_threshold,
top,
blast_radius,
importance,
} => coverage::CoverageSubcommand::Analyze(coverage::AnalyzeArgs {
runtime_coverage: runtime_coverage.clone(),
cloud: *cloud,
api_key: api_key.clone(),
api_endpoint: api_endpoint.clone(),
repo: repo.clone(),
project_id: project_id.clone(),
coverage_period: *coverage_period,
environment: environment.clone(),
commit_sha: commit_sha.clone(),
production: *production,
min_invocations_hot: *min_invocations_hot,
min_observation_volume: *min_observation_volume,
low_traffic_threshold: *low_traffic_threshold,
top: *top,
blast_radius: *blast_radius,
importance: *importance,
}),
CoverageCli::UploadInventory {
api_key,
api_endpoint,
project_id,
git_sha,
allow_dirty,
exclude_paths,
path_prefix,
dry_run,
ignore_upload_errors,
} => coverage::CoverageSubcommand::UploadInventory(coverage::UploadInventoryArgs {
api_key: api_key.clone(),
api_endpoint: api_endpoint.clone(),
project_id: project_id.clone(),
git_sha: git_sha.clone(),
allow_dirty: *allow_dirty,
exclude_paths: exclude_paths.clone(),
path_prefix: path_prefix.clone(),
dry_run: *dry_run,
ignore_upload_errors: *ignore_upload_errors,
}),
CoverageCli::UploadSourceMaps {
dir,
include,
exclude,
repo,
git_sha,
endpoint,
strip_path,
dry_run,
concurrency,
fail_fast,
} => coverage::CoverageSubcommand::UploadSourceMaps(coverage::UploadSourceMapsArgs {
dir: dir.clone(),
include: include.clone(),
exclude: exclude.clone(),
repo: repo.clone(),
git_sha: git_sha.clone(),
endpoint: endpoint.clone(),
strip_path: *strip_path,
dry_run: *dry_run,
concurrency: *concurrency,
fail_fast: *fail_fast,
}),
}
}
struct CheckDispatchArgs {
filters: IssueFilters,
trace_opts: TraceOptions,
include_dupes: bool,
top: Option<usize>,
file: Vec<std::path::PathBuf>,
}
fn dispatch_check(dispatch: &DispatchContext<'_>, args: &CheckDispatchArgs) -> ExitCode {
let cli = dispatch.cli;
let (output, quiet, fail_on_issues) = dispatch.ci_defaults();
let production = match dispatch.production_for(fallow_config::ProductionAnalysis::DeadCode) {
Ok(production) => production,
Err(code) => return code,
};
check::run_check(&CheckOptions {
root: dispatch.root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads: dispatch.threads,
quiet,
fail_on_issues,
filters: &args.filters,
changed_since: cli.changed_since.as_deref(),
baseline: cli.baseline.as_deref(),
save_baseline: cli.save_baseline.as_deref(),
sarif_file: cli.sarif_file.as_deref(),
production,
production_override: Some(production),
workspace: cli.workspace.as_deref(),
changed_workspaces: cli.changed_workspaces.as_deref(),
group_by: cli.group_by,
include_dupes: args.include_dupes,
trace_opts: &args.trace_opts,
explain: cli.explain,
top: args.top,
file: &args.file,
include_entry_exports: cli.include_entry_exports,
summary: cli.summary,
regression_opts: dispatch.regression_opts(
cli.changed_since.is_some()
|| cli.workspace.is_some()
|| cli.changed_workspaces.is_some()
|| !args.file.is_empty(),
),
retain_modules_for_health: false,
defer_performance: false,
})
}
struct DupesDispatchArgs {
mode: DupesMode,
min_tokens: usize,
min_lines: usize,
threshold: f64,
skip_local: bool,
cross_language: bool,
ignore_imports: bool,
top: Option<usize>,
trace: Option<String>,
}
fn dispatch_dupes(dispatch: &DispatchContext<'_>, args: &DupesDispatchArgs) -> ExitCode {
let cli = dispatch.cli;
let (output, quiet, _fail_on_issues) = dispatch.ci_defaults();
let production = match dispatch.production_for(fallow_config::ProductionAnalysis::Dupes) {
Ok(production) => production,
Err(code) => return code,
};
dupes::run_dupes(&DupesOptions {
root: dispatch.root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads: dispatch.threads,
quiet,
mode: args.mode,
min_tokens: args.min_tokens,
min_lines: args.min_lines,
threshold: args.threshold,
skip_local: args.skip_local,
cross_language: args.cross_language,
ignore_imports: args.ignore_imports,
top: args.top,
baseline_path: cli.baseline.as_deref(),
save_baseline_path: cli.save_baseline.as_deref(),
production,
production_override: Some(production),
trace: args.trace.as_deref(),
changed_since: cli.changed_since.as_deref(),
changed_files: None,
workspace: cli.workspace.as_deref(),
changed_workspaces: cli.changed_workspaces.as_deref(),
explain: cli.explain,
explain_skipped: cli.explain_skipped,
summary: cli.summary,
group_by: cli.group_by,
})
}
struct HealthDispatchArgs<'a> {
max_cyclomatic: Option<u16>,
max_cognitive: Option<u16>,
max_crap: Option<f64>,
top: Option<usize>,
sort: health::SortBy,
complexity: bool,
file_scores: bool,
coverage_gaps: bool,
hotspots: bool,
ownership: bool,
ownership_emails: Option<fallow_config::EmailMode>,
targets: bool,
effort: Option<EffortFilter>,
score: bool,
min_score: Option<f64>,
min_severity: Option<health_types::FindingSeverity>,
since: Option<&'a str>,
min_commits: Option<u32>,
save_snapshot: Option<&'a Option<String>>,
trend: bool,
coverage: Option<&'a std::path::Path>,
coverage_root: Option<&'a std::path::Path>,
runtime_coverage: Option<&'a std::path::Path>,
min_invocations_hot: u64,
min_observation_volume: Option<u32>,
low_traffic_threshold: Option<f64>,
}
fn dispatch_health(dispatch: &DispatchContext<'_>, args: HealthDispatchArgs<'_>) -> ExitCode {
let cli = dispatch.cli;
let root = dispatch.root;
let threads = dispatch.threads;
let (output, quiet, _fail_on_issues) = dispatch.ci_defaults();
let HealthDispatchArgs {
max_cyclomatic,
max_cognitive,
max_crap,
top,
sort,
complexity,
file_scores,
coverage_gaps,
hotspots,
ownership,
ownership_emails,
targets,
effort,
score,
min_score,
min_severity,
since,
min_commits,
save_snapshot,
trend,
coverage,
coverage_root,
runtime_coverage,
min_invocations_hot,
min_observation_volume,
low_traffic_threshold,
} = args;
// --effort implies --targets
let targets = targets || effort.is_some();
// --min-score, --save-snapshot, --trend, and --format badge imply --score
let badge_format = matches!(output, fallow_config::OutputFormat::Badge);
let score = score || min_score.is_some() || trend || badge_format;
let snapshot_requested = save_snapshot.is_some();
// No section flags = show all (including score). Any flag set = show only those.
// --save-snapshot and --trend are orthogonal (not section flags) but force score.
let any_section = complexity || file_scores || coverage_gaps || hotspots || targets || score;
let eff_score = if any_section { score } else { true } || snapshot_requested;
// Score needs dead-code/file-score inputs and duplication for accuracy.
// Plain --score keeps churn-backed hotspot penalties tied to --hotspots/--targets,
// but snapshots and trend comparisons need complete vital signs.
let force_full = snapshot_requested || eff_score;
let needs_hotspot_vitals = snapshot_requested || trend;
let score_only_output =
score && !complexity && !file_scores && !coverage_gaps && !hotspots && !targets && !trend;
let eff_file_scores = if any_section { file_scores } else { true } || force_full;
let eff_coverage_gaps = if any_section { coverage_gaps } else { false };
let eff_hotspots = if any_section { hotspots } else { true } || needs_hotspot_vitals;
let eff_complexity = if any_section { complexity } else { true };
let eff_targets = if any_section { targets } else { true };
let runtime_coverage = if let Some(path) = runtime_coverage {
match health::coverage::prepare_options(
path,
min_invocations_hot,
min_observation_volume,
low_traffic_threshold,
output,
) {
Ok(options) => Some(options),
Err(code) => return code,
}
} else {
None
};
let production = match resolve_production_modes(cli, root, output, false, false, false) {
Ok(modes) => modes.for_analysis(fallow_config::ProductionAnalysis::Health),
Err(code) => return code,
};
health::run_health(&HealthOptions {
root,
config_path: &cli.config,
output,
no_cache: cli.no_cache,
threads,
quiet,
max_cyclomatic,
max_cognitive,
max_crap,
top,
sort,
production,
production_override: Some(production),
changed_since: cli.changed_since.as_deref(),
workspace: cli.workspace.as_deref(),
changed_workspaces: cli.changed_workspaces.as_deref(),
baseline: cli.baseline.as_deref(),
save_baseline: cli.save_baseline.as_deref(),
complexity: eff_complexity,
file_scores: eff_file_scores,
coverage_gaps: eff_coverage_gaps,
config_activates_coverage_gaps: !any_section,
hotspots: eff_hotspots,
ownership: ownership && eff_hotspots,
ownership_emails,
targets: eff_targets,
force_full,
score_only_output,
enforce_coverage_gap_gate: true,
effort: effort.map(EffortFilter::to_estimate),
score: eff_score,
min_score,
min_severity,
since,
min_commits,
explain: cli.explain,
summary: cli.summary,
save_snapshot: save_snapshot.map(|opt| PathBuf::from(opt.as_deref().unwrap_or_default())),
trend,
group_by: cli.group_by,
coverage,
coverage_root,
performance: cli.performance,
runtime_coverage,
})
}
#[cfg(test)]
mod tests {
use super::*;
// ── CLI definition validity ─────────────────────────────────────
/// Validates that the CLI definition has no flag name collisions, missing
/// fields, or other structural errors. Catches issues like a global alias
/// `--base` colliding with a subcommand's `--base` flag.
#[test]
fn cli_definition_has_no_flag_collisions() {
use clap::CommandFactory;
Cli::command().debug_assert();
}
/// Guard against deferred-work wording leaking into clap-rendered help.
/// `stub`, `placeholder`, and `not yet` framings tell users the feature
/// is broken or pending; they belong in tracked issues, not in `--help`.
/// Walk every (sub)command and assert each rendered long-help is clean.
#[test]
fn cli_help_text_contains_no_implementation_status_wording() {
use clap::CommandFactory;
let mut root = Cli::command();
let mut violations: Vec<(String, String)> = Vec::new();
visit_help(&mut root, "fallow", &mut violations);
assert!(
violations.is_empty(),
"found implementation-status wording in --help output:\n{}",
violations
.iter()
.map(|(cmd, line)| format!(" {cmd}: {line}"))
.collect::<Vec<_>>()
.join("\n")
);
}
fn visit_help(cmd: &mut clap::Command, path: &str, violations: &mut Vec<(String, String)>) {
let help = cmd.render_long_help().to_string();
for line in scan_forbidden(&help) {
violations.push((path.to_owned(), line));
}
let names: Vec<String> = cmd
.get_subcommands()
.map(|sub| sub.get_name().to_owned())
.collect();
for name in names {
// Skip the synthetic `help` subcommand clap injects automatically.
if name == "help" {
continue;
}
if let Some(sub) = cmd.find_subcommand_mut(&name) {
let sub_path = format!("{path} {name}");
visit_help(sub, &sub_path, violations);
}
}
}
fn scan_forbidden(s: &str) -> Vec<String> {
let lower = s.to_ascii_lowercase();
let mut out = Vec::new();
for word in ["stub", "placeholder"] {
if let Some(idx) = find_whole_word(&lower, word) {
out.push(extract_line(s, idx));
}
}
if let Some(idx) = lower.find("not yet") {
out.push(extract_line(s, idx));
}
out
}
fn find_whole_word(haystack: &str, word: &str) -> Option<usize> {
let bytes = haystack.as_bytes();
let mut start = 0;
while let Some(rel) = haystack[start..].find(word) {
let abs = start + rel;
let before_ok = abs == 0 || !bytes[abs - 1].is_ascii_alphanumeric();
let after_idx = abs + word.len();
let after_ok = after_idx >= bytes.len() || !bytes[after_idx].is_ascii_alphanumeric();
if before_ok && after_ok {
return Some(abs);
}
start = abs + word.len();
}
None
}
fn extract_line(s: &str, byte_idx: usize) -> String {
let line_start = s[..byte_idx].rfind('\n').map_or(0, |i| i + 1);
let line_end = s[byte_idx..].find('\n').map_or(s.len(), |i| byte_idx + i);
s[line_start..line_end].trim().to_owned()
}
// ── emit_error ──────────────────────────────────────────────────
#[test]
fn emit_error_returns_given_exit_code() {
let code = emit_error("test error", 2, fallow_config::OutputFormat::Human);
assert_eq!(code, ExitCode::from(2));
}
// ── format/quiet parsing logic ─────────────────────────────────
// Note: format_from_env() and quiet_from_env() read process-global
// env vars, so we test the underlying parsing logic directly to
// avoid unsafe set_var/remove_var and parallel test interference.
#[test]
fn format_parsing_covers_all_variants() {
// The format_from_env function lowercases then matches.
// Test the same logic inline.
let parse = |s: &str| -> Option<Format> {
match s.to_lowercase().as_str() {
"json" => Some(Format::Json),
"human" => Some(Format::Human),
"sarif" => Some(Format::Sarif),
"compact" => Some(Format::Compact),
"markdown" | "md" => Some(Format::Markdown),
"codeclimate" | "gitlab-codequality" | "gitlab-code-quality" => {
Some(Format::CodeClimate)
}
"badge" => Some(Format::Badge),
_ => None,
}
};
assert!(matches!(parse("json"), Some(Format::Json)));
assert!(matches!(parse("JSON"), Some(Format::Json)));
assert!(matches!(parse("human"), Some(Format::Human)));
assert!(matches!(parse("sarif"), Some(Format::Sarif)));
assert!(matches!(parse("compact"), Some(Format::Compact)));
assert!(matches!(parse("markdown"), Some(Format::Markdown)));
assert!(matches!(parse("md"), Some(Format::Markdown)));
assert!(matches!(parse("codeclimate"), Some(Format::CodeClimate)));
assert!(matches!(
parse("gitlab-codequality"),
Some(Format::CodeClimate)
));
assert!(matches!(
parse("gitlab-code-quality"),
Some(Format::CodeClimate)
));
assert!(matches!(parse("badge"), Some(Format::Badge)));
assert!(parse("xml").is_none());
assert!(parse("").is_none());
}
#[test]
fn quiet_parsing_logic() {
let parse = |s: &str| -> bool { s == "1" || s.eq_ignore_ascii_case("true") };
assert!(parse("1"));
assert!(parse("true"));
assert!(parse("TRUE"));
assert!(parse("True"));
assert!(!parse("0"));
assert!(!parse("false"));
assert!(!parse("yes"));
}
#[test]
fn tracing_filter_defaults_to_warn_without_env() {
assert_eq!(build_tracing_filter(None).to_string(), "warn");
}
#[test]
fn tracing_filter_respects_explicit_env_directives() {
assert_eq!(build_tracing_filter(Some("info")).to_string(), "info");
}
#[test]
fn tracing_filter_treats_empty_env_as_off() {
assert_eq!(build_tracing_filter(Some("")).to_string(), "off");
assert_eq!(build_tracing_filter(Some(" ")).to_string(), "off");
}
}