fallow-core 3.21.0

Internal detector backend for fallow-engine and fallow-api
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
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
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};

use fallow_config::{ResolvedConfig, WorkspaceDiagnostic, WorkspaceDiagnosticKind};
use fallow_types::discover::{DiscoveredFile, FileId};
use ignore::WalkBuilder;
use rustc_hash::FxHashSet;

use super::{ALLOWED_HIDDEN_DIRS, SCRIPT_SCOPE_DENYLIST};

/// Process-wide dedupe of the size-skip / largest-files stderr notes, keyed by a
/// content-derived string, so combined-mode (`fallow` runs check + dupes +
/// health, each of which can trigger a source walk) emits each note at most once
/// per distinct content. Mirrors the workspace-diagnostics `should_emit`
/// pattern (issue #1086).
fn should_emit_note_once(key: String) -> bool {
    static EMITTED: OnceLock<Mutex<FxHashSet<String>>> = OnceLock::new();
    EMITTED
        .get_or_init(|| Mutex::new(FxHashSet::default()))
        .lock()
        .map_or(true, |mut set| set.insert(key))
}

/// A discovered file path paired with its on-disk size in bytes, as collected
/// by the parallel walker before [`DiscoveredFile`] ids are assigned.
type SizedFile = (PathBuf, u64);

/// Dot-prefixed directories the walk dropped, collected inside the parallel
/// walker's `filter_entry` predicate.
///
/// `Arc<Mutex<..>>` and not a borrow: `WalkBuilder::filter_entry` requires
/// `Fn(&DirEntry) -> bool + Send + Sync + 'static`, so the closure can neither
/// borrow a local nor mutate captured state directly, and the predicate runs
/// on every walker thread.
type SkippedDotdirSink = Arc<Mutex<Vec<PathBuf>>>;

/// Number of example file paths named in the aggregated skipped-large-file and
/// largest-files stderr notes before the tail collapses to "and N more". Keeps
/// the notes to one bounded line on a monorepo that skips many files.
const NOTE_EXAMPLE_CAP: usize = 5;

/// Directory levels below a skipped dotdir the bounded scan descends. The
/// dotdir itself is level 0. Two levels reach the conventional
/// `<dotdir>/<group>/<file>` layout (`.claude/hooks/probe.mjs`) with one level
/// of headroom, and stop well above a vendored toolchain tree.
const DOTDIR_SCAN_MAX_DEPTH: usize = 2;

/// Directory entries the bounded scan reads across all levels of ONE skipped
/// dotdir. The ceiling this buys is a SYSCALL count, not a wall-clock figure:
/// a directory-heavy dotdir spends budget on subdirectories that each cost an
/// opendir of their own, so 256 entries can still mean 257 directory reads and
/// several milliseconds. State the bound in syscalls, never in milliseconds.
const DOTDIR_SCAN_MAX_ENTRIES: usize = 256;

/// Directory entries the bounded scan reads across ALL skipped dotdirs in one
/// walk. [`DOTDIR_SCAN_MAX_ENTRIES`] bounds a single directory and nothing
/// bounded the sum, so the added cost was linear in the candidate count: a
/// synthetic tree of 1000 directory-heavy dotdirs turned a 191 ms run into
/// 6.6 s. Candidates are scanned in sorted order and share this budget, so
/// exhausting it drops the advisory for the remaining candidates
/// deterministically instead of paying an unbounded cost. With this ceiling
/// and [`DOTDIR_SCAN_MAX_CANDIDATES`] the same synthetic trees measure about
/// 30 ms of added work whether they hold 300 or 1000 candidates, and a real
/// repository stays inside run-to-run noise.
const DOTDIR_SCAN_TOTAL_ENTRIES: usize = 1024;

/// Skipped dotdirs the bounded scan OPENS in one walk. The entry budget does
/// not bound the per-candidate setup cost, since each scan builds its own
/// gitignore matcher chain (about 0.2 ms) before it reads a single entry, so
/// the candidate count needs a ceiling of its own. Counted after the name
/// checks, so a monorepo full of `.turbo` and `.next` directories cannot spend
/// the ceiling on directories that were never going to be scanned.
const DOTDIR_SCAN_MAX_CANDIDATES: usize = 64;

/// File extensions that put a file in the module graph the advisory talks
/// about. Narrower than [`SOURCE_EXTENSIONS`] on purpose: the message states
/// that the directory's imports and exports are not analyzed, and that is only
/// true of code. A dotdir holding nothing but a generated Lighthouse
/// `report.html`, a Sanity runtime page, a `schema.graphql`, or a stylesheet
/// has no imports or exports to lose, so it does not earn the advisory.
const DOTDIR_MODULE_EXTENSIONS: &[&str] = &[
    "ts", "tsx", "mts", "cts", "gts", "js", "jsx", "mjs", "cjs", "gjs", "vue", "svelte", "astro",
    "mdx",
];

/// Discovered-file-count threshold above which the pre-parse largest-files note
/// fires, so an out-of-memory hang at the parse stage has a visible suspect
/// list (issue #1086).
const LARGE_SET_THRESHOLD: usize = 20_000;

/// Single-file byte threshold above which the pre-parse largest-files note
/// fires even on a small project. Set just under the default 5 MB skip so the
/// note fires for kept files that are approaching the skip limit (the genuine
/// out-of-memory suspects), not for ordinary large-but-benign files.
const LARGE_FILE_NOTE_BYTES: u64 = 4 * 1024 * 1024;

/// Minimum size for a file to appear in the largest-files note. Filters out the
/// `0.0 MB` entries that would otherwise pad the list once it fires, keeping the
/// named files to plausible memory contributors.
const NOTE_FILE_FLOOR_BYTES: u64 = 256 * 1024;

/// Minimum size for content-shape based minified-bundle skipping. Smaller
/// one-line files can be hand-written utilities, while multi-MB one-line JS is
/// generated output in practice.
const MINIFIED_FILE_SKIP_BYTES: u64 = 1024 * 1024;

/// Number of bytes inspected when deciding whether a large JS file is minified.
const MINIFIED_SAMPLE_BYTES: usize = 256 * 1024;

/// A single line this long in a multi-MB JS file is treated as generated
/// minified output. This avoids parsing assets that can expand to huge ASTs.
const MINIFIED_LONG_LINE_BYTES: usize = 128 * 1024;

/// Whether a path is a TypeScript declaration file (`.d.ts`/`.d.mts`/`.d.cts`).
/// Declaration files are exempt from the per-file size skip because they are
/// reachability roots for global types: skipping a large `auto-imports.d.ts`
/// would false-flag the files whose types it provides.
fn is_declaration_file(path: &Path) -> bool {
    let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
    name.ends_with(".d.ts") || name.ends_with(".d.mts") || name.ends_with(".d.cts")
}

fn is_plain_js_file(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|ext| ext.to_str()),
        Some("js" | "mjs" | "cjs")
    )
}

fn has_minified_line_shape(path: &Path) -> bool {
    use std::io::Read;

    let Ok(mut file) = std::fs::File::open(path) else {
        return false;
    };
    let mut sample = vec![0; MINIFIED_SAMPLE_BYTES];
    let Ok(len) = file.read(&mut sample) else {
        return false;
    };
    sample.truncate(len);
    if sample.is_empty() {
        return false;
    }

    let mut current_line = 0usize;
    for byte in sample {
        if byte == b'\n' || byte == b'\r' {
            current_line = 0;
            continue;
        }
        current_line += 1;
        if current_line >= MINIFIED_LONG_LINE_BYTES {
            return true;
        }
    }
    false
}

fn is_probably_minified_generated_js(path: &Path, size_bytes: u64) -> bool {
    size_bytes >= MINIFIED_FILE_SKIP_BYTES
        && is_plain_js_file(path)
        && !is_declaration_file(path)
        && has_minified_line_shape(path)
}

/// Render a byte count as a megabyte figure with one decimal place.
fn format_size_mb(bytes: u64) -> String {
    #[expect(
        clippy::cast_precision_loss,
        reason = "display-only size figure; precision loss past 2^53 bytes is irrelevant"
    )]
    let mb = bytes as f64 / (1024.0 * 1024.0);
    format!("{mb:.1} MB")
}

/// Join up to [`NOTE_EXAMPLE_CAP`] `path (size)` examples (already ordered) into
/// one comma-separated string, collapsing the tail to "and N more".
fn summarize_examples(root: &Path, examples: &[SizedFile]) -> String {
    let shown: Vec<String> = examples
        .iter()
        .take(NOTE_EXAMPLE_CAP)
        .map(|(path, size)| {
            let display = display_relative_path(root, path);
            format!("{display} ({})", format_size_mb(*size))
        })
        .collect();
    let remaining = examples.len().saturating_sub(NOTE_EXAMPLE_CAP);
    if remaining > 0 {
        format!("{}, and {remaining} more", shown.join(", "))
    } else {
        shown.join(", ")
    }
}

/// Split discovered `(path, size)` pairs into the kept set and the set skipped
/// for exceeding `max_file_size_bytes`. Declaration files are never skipped.
fn partition_by_size(
    raw: Vec<SizedFile>,
    max_file_size_bytes: Option<u64>,
) -> (Vec<SizedFile>, Vec<SizedFile>) {
    let Some(limit) = max_file_size_bytes else {
        return (raw, Vec::new());
    };
    raw.into_iter()
        .partition(|(path, size)| *size <= limit || is_declaration_file(path))
}

/// Split discovered `(path, size)` pairs into files kept for parsing and files
/// skipped because they look like generated minified JavaScript.
fn partition_minified_generated_js(
    raw: Vec<SizedFile>,
    max_file_size_bytes: Option<u64>,
) -> (Vec<SizedFile>, Vec<SizedFile>) {
    if max_file_size_bytes.is_none() {
        return (raw, Vec::new());
    }
    raw.into_iter()
        .partition(|(path, size)| !is_probably_minified_generated_js(path, *size))
}

/// Build the typed diagnostics for the over-limit files this walk dropped and
/// emit one aggregated `tracing::warn!` so a human running `fallow` sees what
/// was dropped. Mirrors the JSON-plus-gated-warn pattern used for undeclared
/// workspaces. The caller writes the returned list to the registry.
fn report_skipped_large_files(
    config: &ResolvedConfig,
    skipped: &[SizedFile],
) -> Vec<WorkspaceDiagnostic> {
    if skipped.is_empty() {
        return Vec::new();
    }
    let diagnostics: Vec<WorkspaceDiagnostic> = skipped
        .iter()
        .map(|(path, size_bytes)| {
            WorkspaceDiagnostic::new(
                &config.root,
                path.clone(),
                WorkspaceDiagnosticKind::SkippedLargeFile {
                    size_bytes: *size_bytes,
                },
            )
        })
        .collect();

    let mut sorted: Vec<SizedFile> = skipped.to_vec();
    sorted.sort_unstable_by_key(|f| std::cmp::Reverse(f.1));
    let count = skipped.len();
    if !config.quiet
        && should_emit_note_once(format!(
            "skip::{}::{count}::{}",
            config.root.display(),
            sorted.first().map_or(0, |f| f.1)
        ))
    {
        let examples = summarize_examples(&config.root, &sorted);
        let noun = if count == 1 { "file" } else { "files" };
        tracing::warn!(
            "fallow: skipped {count} {noun} over the max file size limit ({examples}). \
             Raise the limit with --max-file-size <MB> (or FALLOW_MAX_FILE_SIZE), or add them to ignorePatterns."
        );
    }
    diagnostics
}

/// Build the typed diagnostics for generated minified JS files skipped before
/// parsing. The caller writes the returned list to the registry.
fn report_skipped_minified_files(
    config: &ResolvedConfig,
    skipped: &[SizedFile],
) -> Vec<WorkspaceDiagnostic> {
    if skipped.is_empty() {
        return Vec::new();
    }
    let diagnostics: Vec<WorkspaceDiagnostic> = skipped
        .iter()
        .map(|(path, size_bytes)| {
            WorkspaceDiagnostic::new(
                &config.root,
                path.clone(),
                WorkspaceDiagnosticKind::SkippedMinifiedFile {
                    size_bytes: *size_bytes,
                },
            )
        })
        .collect();

    let mut sorted: Vec<SizedFile> = skipped.to_vec();
    sorted.sort_unstable_by_key(|f| std::cmp::Reverse(f.1));
    let count = skipped.len();
    if !config.quiet
        && should_emit_note_once(format!(
            "minified::{}::{count}::{}",
            config.root.display(),
            sorted.first().map_or(0, |f| f.1)
        ))
    {
        let examples = summarize_examples(&config.root, &sorted);
        let noun = if count == 1 { "file" } else { "files" };
        let pronoun = if count == 1 { "it" } else { "them" };
        tracing::warn!(
            "fallow: skipped {count} minified generated JS {noun} ({examples}). \
             Add {pronoun} to ignorePatterns, rename {pronoun} with a .min.js suffix, or use --max-file-size 0 to analyze {pronoun}."
        );
    }
    diagnostics
}

/// Join up to [`NOTE_EXAMPLE_CAP`] root-relative paths (already ordered) into
/// one comma-separated string, collapsing the tail to "and N more". The
/// size-bearing sibling is [`summarize_examples`].
fn summarize_paths(root: &Path, examples: &[&PathBuf]) -> String {
    let shown: Vec<String> = examples
        .iter()
        .take(NOTE_EXAMPLE_CAP)
        .map(|path| display_relative_path(root, path))
        .collect();
    let remaining = examples.len().saturating_sub(NOTE_EXAMPLE_CAP);
    if remaining > 0 {
        format!("{}, and {remaining} more", shown.join(", "))
    } else {
        shown.join(", ")
    }
}

/// Like [`summarize_paths`], but for a list already known to be incomplete: the
/// tail reads "and more" rather than naming a count the run cannot vouch for.
fn summarize_paths_open_ended(root: &Path, examples: &[&PathBuf]) -> String {
    let shown: Vec<String> = examples
        .iter()
        .take(NOTE_EXAMPLE_CAP)
        .map(|path| display_relative_path(root, path))
        .collect();
    if examples.len() > NOTE_EXAMPLE_CAP {
        format!("{}, and more", shown.join(", "))
    } else {
        shown.join(", ")
    }
}

/// Render `path` relative to `root` with forward slashes. Cross-platform
/// output stability depends on the slash normalisation.
fn display_relative_path(root: &Path, path: &Path) -> String {
    path.strip_prefix(root)
        .unwrap_or(path)
        .display()
        .to_string()
        .replace('\\', "/")
}

/// Whether a candidate file inside a skipped dotdir is one this run had
/// already excluded from analysis, matching what [`FileVisitor`] applies to
/// every discovered file: the compiled `ignorePatterns` set (user entries plus
/// the built-in defaults) against the ROOT-RELATIVE path, plus the production
/// excludes when the run is a `--production` run.
///
/// The root-relative path is what matters. Matching the directory path instead
/// would miss the pattern a user actually writes, because `.claude/**` does not
/// match `.claude`.
///
/// Production excludes ARE applied, even though the skip the diagnostic reports
/// is a traversal decision that `--production` does not change. A `--production`
/// run that named a dotdir holding only `thing.test.ts` would print a remedy
/// (`fallow --root .qa --production`) that returns nothing, so the advisory has
/// to agree with the file set the run would actually analyze. Combined mode's
/// two walks can therefore disagree, which the documented union semantics of
/// the combined root already cover.
fn is_excluded_from_analysis(
    config: &ResolvedConfig,
    production_excludes: Option<&globset::GlobSet>,
    path: &Path,
) -> bool {
    let relative = path.strip_prefix(&config.root).unwrap_or(path);
    config.ignore_patterns.is_match(relative)
        || production_excludes.is_some_and(|excludes| excludes.is_match(relative))
}

/// True when `path` carries an extension that puts it in the module graph the
/// advisory describes. See [`DOTDIR_MODULE_EXTENSIONS`] for why this is
/// narrower than [`has_source_extension`].
fn has_module_extension(path: &Path) -> bool {
    path.extension()
        .and_then(OsStr::to_str)
        .is_some_and(|ext| DOTDIR_MODULE_EXTENSIONS.contains(&ext))
}

/// Depth- and entry-capped search for one reportable source file, stopping at
/// the first hit. Never a recursive walk of an unbounded tree: the ceiling is
/// `1 + DOTDIR_SCAN_MAX_ENTRIES` directory reads for one dotdir, and
/// [`DOTDIR_SCAN_TOTAL_ENTRIES`] across the whole walk.
///
/// Runs on `ignore::WalkBuilder` with the same git settings as the source walk
/// rather than a bare `read_dir`, because "the project has not excluded it" has
/// to mean what git means. Only the DIRECTORY form of a gitignore rule
/// (`.build-tools/`) prunes a dotdir before the walk's own filter sees it: the
/// `dir/**`, `dir/*`, `**/dir/**` and file-level (`*.ts`) forms all leave the
/// directory reaching this scan with every file inside it ignored, and a cache
/// directory that ignores itself through its own nested `.gitignore` does the
/// same. Reporting those would advertise two remedies that both do nothing,
/// since a re-rooted `fallow --root <dir>` still reads the parent repository's
/// gitignore and would find no files either.
///
/// Accepted tradeoff: for a dotdir with more than [`DOTDIR_SCAN_MAX_ENTRIES`]
/// entries whose only source file sits past the budget, the verdict is
/// readdir-order dependent, so the advisory can flap between runs. The
/// alternative is unbounded I/O, and the consequence of a flap is a missing
/// advisory line, never a changed analysis. No test may depend on that
/// boundary.
fn scan_for_reportable_source(
    config: &ResolvedConfig,
    production_excludes: Option<&globset::GlobSet>,
    dir: &Path,
    budget: &mut usize,
) -> bool {
    let mut builder = WalkBuilder::new(dir);
    builder
        .hidden(false)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .follow_links(false)
        .max_depth(Some(DOTDIR_SCAN_MAX_DEPTH + 1))
        .threads(1);
    builder.filter_entry(|entry| {
        if entry.depth() == 0 || !entry.file_type().is_some_and(|ft| ft.is_dir()) {
            return true;
        }
        entry
            .file_name()
            .to_str()
            .is_none_or(|name| !SCRIPT_SCOPE_DENYLIST.contains(&name) && name != "node_modules")
    });

    let mut per_dotdir = DOTDIR_SCAN_MAX_ENTRIES;
    for entry in builder.build() {
        if per_dotdir == 0 || *budget == 0 {
            return false;
        }
        per_dotdir -= 1;
        *budget -= 1;
        let Ok(entry) = entry else {
            continue;
        };
        // Regular files only. A symlink is never followed out of the scan, and
        // a fifo or a socket named `pipe.ts` is not source either.
        #[expect(
            clippy::filetype_is_file,
            reason = "regular files only is the point: !is_dir() would readmit fifos and sockets"
        )]
        let is_regular_file = entry
            .file_type()
            .is_some_and(|file_type| file_type.is_file());
        if !is_regular_file {
            continue;
        }
        if has_module_extension(entry.path())
            && !is_excluded_from_analysis(config, production_excludes, entry.path())
        {
            return true;
        }
    }
    false
}

/// Path components whose subtrees never earn the skipped-source-dotdir
/// advisory. A hidden directory under one of these is test scaffolding rather
/// than first-party source the project meant to analyze.
const DOTDIR_NOISE_PATH_COMPONENTS: &[&str] = &[
    "__fixtures__",
    "__mocks__",
    "__tests__",
    "e2e",
    "fixture",
    "fixtures",
    "playground",
    "playgrounds",
    "spec",
    "test",
    "tests",
];

/// Whether a dropped dotdir is worth opening at all. Decided from the PATH
/// alone, so it costs no I/O and runs before the scan budget is touched:
/// `.git` in a large repository, a `.jj` object store, and `node_modules/.pnpm`
/// are never opened. `ALLOWED_HIDDEN_DIRS` and every plugin- or
/// script-contributed scope are already excluded by construction, since a
/// directory they admit is never dropped and so never reaches this list.
fn dotdir_is_scan_candidate(config: &ResolvedConfig, dir: &Path) -> bool {
    let Some(name) = dir.file_name().and_then(OsStr::to_str) else {
        return false;
    };
    if SCRIPT_SCOPE_DENYLIST.contains(&name) {
        return false;
    }
    let relative = dir.strip_prefix(&config.root).unwrap_or(dir);
    // Pure cost saving, not a further condition: the built-in `**/node_modules/**`
    // ignore default makes every file under a `node_modules` component ignored,
    // so the scan could only ever return false.
    if relative
        .components()
        .any(|component| component.as_os_str() == OsStr::new("node_modules"))
    {
        return false;
    }
    // Precision, and the one place this check is deliberately less complete
    // than it could be. A hidden directory under a test, fixture, or playground
    // tree is usually there BECAUSE it is hidden: some of these exist purely to
    // exercise hidden-directory handling, so an advisory about them is wrong
    // about the project every time it fires. Measured on a ten-repository
    // corpus, this component filter removes every false positive one framework
    // contributed and half of another's while keeping the true positives, which
    // sit at a repository root rather than under a test tree.
    !relative.components().any(|component| {
        DOTDIR_NOISE_PATH_COMPONENTS.contains(&component.as_os_str().to_string_lossy().as_ref())
    })
}

/// Build the typed diagnostics for the dot-prefixed directories this walk
/// dropped that hold source files the project has not excluded, and emit one
/// aggregated `tracing::warn!` so the otherwise silent skip is visible on
/// stderr too (issue #461). The caller writes the returned list to the
/// registry.
fn report_skipped_source_dotdirs(
    config: &ResolvedConfig,
    production_excludes: Option<&globset::GlobSet>,
    candidates: &[PathBuf],
) -> Vec<WorkspaceDiagnostic> {
    if candidates.is_empty() {
        return Vec::new();
    }
    // The caller sorted and deduped, so both caps truncate deterministically:
    // the same tree reports the same prefix on every run.
    let mut budget = DOTDIR_SCAN_TOTAL_ENTRIES;
    let scannable: Vec<&PathBuf> = candidates
        .iter()
        .filter(|dir| dotdir_is_scan_candidate(config, dir))
        .collect();
    let reportable: Vec<&PathBuf> = scannable
        .iter()
        .copied()
        .take(DOTDIR_SCAN_MAX_CANDIDATES)
        .filter(|dir| scan_for_reportable_source(config, production_excludes, dir, &mut budget))
        .collect();
    // Either ceiling can stop the scan with candidates left unexamined, so the
    // count is a floor rather than a total whenever one of them binds.
    let truncated = scannable.len() > DOTDIR_SCAN_MAX_CANDIDATES || budget == 0;
    if reportable.is_empty() {
        return Vec::new();
    }

    let diagnostics: Vec<WorkspaceDiagnostic> = reportable
        .iter()
        .map(|dir| {
            WorkspaceDiagnostic::new(
                &config.root,
                (*dir).clone(),
                WorkspaceDiagnosticKind::SkippedSourceDotdir,
            )
        })
        .collect();

    let count = reportable.len();
    if !config.quiet
        && should_emit_note_once(format!(
            "dotdir::{}::{count}::{}",
            config.root.display(),
            reportable
                .first()
                .map_or_else(String::new, |dir| display_relative_path(&config.root, dir))
        ))
    {
        tracing::warn!(
            "{}",
            build_skipped_dotdirs_note(&config.root, &reportable, truncated)
        );
    }
    diagnostics
}

/// Build the skipped-source-dotdir note. Pure so the singular and plural forms,
/// the truncated prefix, and the single-directory remedy substitution are
/// unit-testable without a tracing subscriber, mirroring
/// [`build_largest_files_note`].
///
/// With exactly one directory the remedy names it instead of printing a `<dir>`
/// placeholder: the path is already known and was printed a few words earlier,
/// so a placeholder would make the one case a user can act on directly the one
/// case they have to retype.
fn build_skipped_dotdirs_note(root: &Path, reportable: &[&PathBuf], truncated: bool) -> String {
    let count = reportable.len();
    // An exact remainder inside an explicitly inexact total reads as a
    // contradiction ("at least 64 ... and 59 more"), so a truncated run drops
    // the tail count.
    let examples = if truncated {
        summarize_paths_open_ended(root, reportable)
    } else {
        summarize_paths(root, reportable)
    };
    let noun = if count == 1 {
        "directory"
    } else {
        "directories"
    };
    let verb = if count == 1 { "contains" } else { "contain" };
    let at_least = if truncated { "at least " } else { "" };
    let (target, pronoun) = match reportable {
        [only] => (display_relative_path(root, only), "it"),
        _ => ("<dir>".to_owned(), "one"),
    };
    format!(
        "fallow: skipped {at_least}{count} hidden {noun} that {verb} source files ({examples}). \
         Hidden directories are not traversed and no config field adds one: analyze {pronoun} \
         with fallow --root {target} if it holds first-party source, or add '{target}/**' to \
         ignorePatterns to silence this."
    )
}

/// Build the pre-parse largest-files note, or `None` when the discovered set is
/// neither unusually large nor contains an unusually large file. Pure so the
/// pluralization, floor filtering, and count-only fallback are unit-testable
/// without a tracing subscriber. See issue #1086.
fn build_largest_files_note(root: &Path, files: &[DiscoveredFile]) -> Option<String> {
    if files.is_empty() {
        return None;
    }
    let largest = files.iter().map(|f| f.size_bytes).max().unwrap_or(0);
    if files.len() <= LARGE_SET_THRESHOLD && largest < LARGE_FILE_NOTE_BYTES {
        return None;
    }
    let count = files.len();
    let noun = if count == 1 { "file" } else { "files" };
    let mut by_size: Vec<SizedFile> = files
        .iter()
        .filter(|f| f.size_bytes >= NOTE_FILE_FLOOR_BYTES)
        .map(|f| (f.path.clone(), f.size_bytes))
        .collect();
    by_size.sort_unstable_by_key(|f| std::cmp::Reverse(f.1));
    if by_size.is_empty() {
        // Large file SET with no individually large file: report the count only,
        // omitting a "largest:" list that would otherwise be all sub-floor noise.
        return Some(format!(
            "fallow: discovered {count} {noun}. If analysis stalls or runs out of memory, \
             exclude large generated files via ignorePatterns or --max-file-size."
        ));
    }
    let examples = summarize_examples(root, &by_size);
    Some(format!(
        "fallow: discovered {count} {noun}; largest: {examples}. If analysis stalls or runs out of memory, \
         exclude large generated files via ignorePatterns or --max-file-size."
    ))
}

/// Emit a pre-parse note listing the largest kept files when the discovered set
/// is unusually large or contains an unusually large file, so an out-of-memory
/// hang at the parse stage is diagnosable (issue #1086). Visible before the
/// expensive parse begins, so it survives a subsequent crash.
fn note_largest_files(config: &ResolvedConfig, files: &[DiscoveredFile]) {
    if config.quiet {
        return;
    }
    if let Some(message) = build_largest_files_note(&config.root, files)
        && should_emit_note_once(format!("note::{}::{}", config.root.display(), files.len()))
    {
        tracing::warn!("{message}");
    }
}

/// How a [`HiddenDirScope`] matches a hidden directory during the walk.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HiddenDirMatch {
    /// Match by directory NAME at any depth beneath the scope root.
    ///
    /// Framework plugins declare bundle-boundary conventions like `.client`
    /// and `.server` that a project may place under any route directory, so
    /// the name is the whole rule and the depth is not knowable in advance.
    AnyDepth,
    /// Match the exact root-relative directory PATH.
    ///
    /// A `package.json` script naming `.a/.b/build.mjs` states where the file
    /// it needs actually lives, so the scope admits `<root>/.a` and
    /// `<root>/.a/.b` and nothing else. An unrelated `packages/x/.b` stays
    /// untraversed (issue #461).
    ExactPath,
}

/// Package-scoped hidden directories that source discovery should traverse.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HiddenDirScope {
    root: PathBuf,
    dirs: Vec<String>,
    match_mode: HiddenDirMatch,
}

impl HiddenDirScope {
    /// Build a scope rooted at a package directory that admits the given
    /// hidden directory names at any depth beneath it.
    ///
    /// This is the plugin-contributed shape. For a scope inferred from a
    /// concrete path, use [`HiddenDirScope::new_exact_paths`], which does not
    /// admit the same name elsewhere in the tree.
    #[must_use]
    pub fn new(root: PathBuf, dirs: Vec<String>) -> Self {
        Self {
            root,
            dirs,
            match_mode: HiddenDirMatch::AnyDepth,
        }
    }

    /// Build a scope rooted at a package directory that admits exactly the
    /// given root-relative directory paths.
    #[must_use]
    pub fn new_exact_paths(root: PathBuf, dirs: Vec<String>) -> Self {
        Self {
            root,
            dirs,
            match_mode: HiddenDirMatch::ExactPath,
        }
    }

    /// Rebuild a scope with an explicit match mode.
    ///
    /// Used when a scope crosses a crate boundary and must arrive with the
    /// same semantics it left with.
    #[must_use]
    pub fn with_match_mode(root: PathBuf, dirs: Vec<String>, match_mode: HiddenDirMatch) -> Self {
        Self {
            root,
            dirs,
            match_mode,
        }
    }

    #[must_use]
    pub fn root(&self) -> &Path {
        &self.root
    }

    #[must_use]
    pub fn dirs(&self) -> &[String] {
        &self.dirs
    }

    #[must_use]
    pub fn match_mode(&self) -> HiddenDirMatch {
        self.match_mode
    }

    fn allows(&self, path: &Path, name: &OsStr) -> bool {
        match self.match_mode {
            HiddenDirMatch::AnyDepth => {
                path.starts_with(&self.root) && self.dirs.iter().any(|dir| OsStr::new(dir) == name)
            }
            HiddenDirMatch::ExactPath => {
                // `Path` compares component-wise, so a `/`-separated entry
                // from a script string matches on every platform.
                let Ok(relative) = path.strip_prefix(&self.root) else {
                    return false;
                };
                self.dirs.iter().any(|dir| Path::new(dir) == relative)
            }
        }
    }
}

/// Per-thread file collector for the parallel walker.
///
/// Source files (by extension) flow to `shared`; when `config_shared` is set,
/// non-source files admitted by the config-candidate type group flow to it
/// instead. The two channels are disjoint and the source channel is byte-for-byte
/// identical to the config-capture-disabled walk.
struct FileVisitor<'a> {
    root: &'a Path,
    canonical_root: Option<&'a Path>,
    ignore_patterns: &'a globset::GlobSet,
    production_excludes: &'a Option<globset::GlobSet>,
    shared: &'a Mutex<Vec<(std::path::PathBuf, u64)>>,
    config_shared: Option<&'a Mutex<Vec<std::path::PathBuf>>>,
    local: Vec<(std::path::PathBuf, u64)>,
    config_local: Vec<std::path::PathBuf>,
}

impl ignore::ParallelVisitor for FileVisitor<'_> {
    fn visit(&mut self, result: Result<ignore::DirEntry, ignore::Error>) -> ignore::WalkState {
        let Ok(entry) = result else {
            return ignore::WalkState::Continue;
        };
        if entry.file_type().is_some_and(|ft| ft.is_dir()) {
            return ignore::WalkState::Continue;
        }
        let relative = entry
            .path()
            .strip_prefix(self.root)
            .unwrap_or_else(|_| entry.path());
        if self.ignore_patterns.is_match(relative) {
            return ignore::WalkState::Continue;
        }
        if self
            .production_excludes
            .as_ref()
            .is_some_and(|excludes| excludes.is_match(relative))
        {
            return ignore::WalkState::Continue;
        }
        let symlink_size = if entry.file_type().is_some_and(|ft| ft.is_symlink()) {
            let Some(size) = contained_symlink_file_size(entry.path(), self.canonical_root) else {
                tracing::debug!(
                    path = %entry.path().display(),
                    "skipping source symlink with a broken, non-file, or outside-root target"
                );
                return ignore::WalkState::Continue;
            };
            Some(size)
        } else {
            None
        };
        if has_source_extension(entry.path()) {
            let size_bytes =
                symlink_size.unwrap_or_else(|| entry.metadata().map_or(0, |m| m.len()));
            self.local.push((entry.into_path(), size_bytes));
        } else if self.config_shared.is_some() {
            // A non-source file admitted by the config-candidate type group. No
            // size metadata is needed; these are pattern-matched, never parsed.
            self.config_local.push(entry.into_path());
        }
        ignore::WalkState::Continue
    }
}

fn contained_symlink_file_size(path: &Path, canonical_root: Option<&Path>) -> Option<u64> {
    let root = canonical_root?;
    let target = path.canonicalize().ok()?;
    if !target.starts_with(root) {
        return None;
    }
    let metadata = target.metadata().ok()?;
    metadata.is_file().then_some(metadata.len())
}

impl Drop for FileVisitor<'_> {
    #[expect(
        clippy::expect_used,
        reason = "poisoned walk collector lock means worker state is unrecoverable"
    )]
    fn drop(&mut self) {
        if !self.local.is_empty() {
            self.shared
                .lock()
                .expect("walk collector lock poisoned")
                .append(&mut self.local);
        }
        if let Some(config_shared) = self.config_shared
            && !self.config_local.is_empty()
        {
            config_shared
                .lock()
                .expect("walk config collector lock poisoned")
                .append(&mut self.config_local);
        }
    }
}

/// Builder that creates per-thread `FileVisitor` instances for the parallel walker.
struct FileVisitorBuilder<'a> {
    root: &'a Path,
    canonical_root: Option<&'a Path>,
    ignore_patterns: &'a globset::GlobSet,
    production_excludes: &'a Option<globset::GlobSet>,
    shared: &'a Mutex<Vec<(std::path::PathBuf, u64)>>,
    config_shared: Option<&'a Mutex<Vec<std::path::PathBuf>>>,
}

impl<'s> ignore::ParallelVisitorBuilder<'s> for FileVisitorBuilder<'s> {
    fn build(&mut self) -> Box<dyn ignore::ParallelVisitor + 's> {
        Box::new(FileVisitor {
            root: self.root,
            canonical_root: self.canonical_root,
            ignore_patterns: self.ignore_patterns,
            production_excludes: self.production_excludes,
            shared: self.shared,
            config_shared: self.config_shared,
            local: Vec::new(),
            config_local: Vec::new(),
        })
    }
}

/// File extensions discovery treats as analyzable source files.
pub const SOURCE_EXTENSIONS: &[&str] = &[
    "ts", "tsx", "mts", "cts", "gts", "js", "jsx", "mjs", "cjs", "gjs", "vue", "svelte", "astro",
    "mdx", "css", "scss", "sass", "less", "html", "graphql", "gql",
];

/// Glob patterns for test/dev/story files excluded in production mode.
pub const PRODUCTION_EXCLUDE_PATTERNS: &[&str] = &[
    "**/*.test.*",
    "**/*.spec.*",
    "**/*.e2e.*",
    "**/*.e2e-spec.*",
    "**/*.bench.*",
    "**/*.fixture.*",
    "**/*.stories.*",
    "**/*.story.*",
    "**/__tests__/**",
    "**/__mocks__/**",
    "**/__snapshots__/**",
    "**/__fixtures__/**",
    "**/test/**",
    "**/tests/**",
    "*.config.*",
    "**/.*.js",
    "**/.*.ts",
    "**/.*.mjs",
    "**/.*.cjs",
];

/// Check if a hidden directory name is on the allowlist.
pub fn is_allowed_hidden_dir(name: &OsStr) -> bool {
    ALLOWED_HIDDEN_DIRS.iter().any(|&d| OsStr::new(d) == name)
}

fn is_allowed_scoped_hidden_dir(
    name: &OsStr,
    path: &Path,
    additional_hidden_dir_scopes: &[HiddenDirScope],
) -> bool {
    additional_hidden_dir_scopes
        .iter()
        .any(|scope| scope.allows(path, name))
}

/// Files Yarn Plug'n'Play writes at the workspace root. They carry source
/// extensions (`.pnp.cjs` is the multi-megabyte generated loader with the
/// install state inlined, `.pnp.loader.mjs` its ESM shim) but are install
/// artifacts, not project source, so the walker drops them by name.
const YARN_PNP_GENERATED_FILES: &[&str] = &[".pnp.cjs", ".pnp.loader.mjs"];

fn is_yarn_pnp_generated_file(name: &OsStr) -> bool {
    YARN_PNP_GENERATED_FILES
        .iter()
        .any(|&f| OsStr::new(f) == name)
}

/// Check if a hidden directory entry should be allowed through the filter.
///
/// Returns `true` if the entry is not hidden or is on the allowlist.
/// Hidden files (not directories) are allowed through since the type filter
/// handles them, except for the generated Yarn PnP files.
fn is_allowed_hidden_with_scopes(
    entry: &ignore::DirEntry,
    additional_hidden_dir_scopes: &[HiddenDirScope],
) -> bool {
    let name = entry.file_name();
    let name_str = name.to_string_lossy();

    if !name_str.starts_with('.') {
        return true;
    }

    if entry.file_type().is_some_and(|ft| !ft.is_dir()) {
        return !is_yarn_pnp_generated_file(name);
    }

    is_allowed_hidden_dir(name)
        || is_allowed_scoped_hidden_dir(name, entry.path(), additional_hidden_dir_scopes)
}

/// Discover all source files in the project.
///
/// # Panics
///
/// Panics if the file type glob or progress template is invalid (compile-time constants).
pub fn discover_files(config: &ResolvedConfig) -> Vec<DiscoveredFile> {
    discover_files_with_additional_hidden_dirs(config, &[])
}

/// The set of config-file basenames (last path component of every built-in
/// plugin `config_patterns()` entry, brace forms preserved) that the walk should
/// additionally admit so non-source configs (`tsconfig.json`, `bunfig.toml`,
/// `.eslintrc.json`, ...) can be captured in one traversal instead of being
/// re-discovered by a filesystem re-walk in `discover_config_files`.
///
/// Derived live from the built-in plugin list, so it can never drift behind a
/// new plugin's config patterns. Source-extension config basenames
/// (`vite.config.{ts,js}`) are admitted too, but the walk visitor routes them
/// back to the source channel by extension, so the config channel only ever
/// collects genuinely non-source files.
fn config_candidate_basename_globs() -> &'static [String] {
    static GLOBS: OnceLock<Vec<String>> = OnceLock::new();
    GLOBS.get_or_init(|| {
        let mut set: FxHashSet<String> = FxHashSet::default();
        for plugin in crate::plugins::registry::builtin::create_builtin_plugins() {
            for pattern in plugin.config_patterns() {
                let basename = pattern.rsplit('/').next().unwrap_or(pattern);
                set.insert(basename.to_string());
            }
        }
        let mut globs: Vec<String> = set.into_iter().collect();
        globs.sort_unstable();
        globs
    })
}

/// True when `path`'s extension is one of the known source extensions, i.e. the
/// file belongs in the source channel rather than the config-candidate channel.
fn has_source_extension(path: &Path) -> bool {
    path.extension()
        .and_then(OsStr::to_str)
        .is_some_and(|ext| SOURCE_EXTENSIONS.contains(&ext))
}

/// Build the file-type filter. Always selects known source extensions; when
/// `capture_config` is set, also selects config-candidate basenames so the
/// walker yields them for the second collection channel.
#[expect(
    clippy::expect_used,
    reason = "source file globs are hard-coded compile-time constants"
)]
fn build_walk_types(capture_config: bool) -> ignore::types::Types {
    static SOURCE_TYPES: OnceLock<ignore::types::Types> = OnceLock::new();
    static SOURCE_AND_CONFIG_TYPES: OnceLock<ignore::types::Types> = OnceLock::new();

    let cache = if capture_config {
        &SOURCE_AND_CONFIG_TYPES
    } else {
        &SOURCE_TYPES
    };
    cache
        .get_or_init(|| {
            let mut types_builder = ignore::types::TypesBuilder::new();
            let source_glob = format!("*.{{{}}}", SOURCE_EXTENSIONS.join(","));
            types_builder
                .add("source", &source_glob)
                .expect("valid glob");
            types_builder.select("source");
            if capture_config {
                for glob in config_candidate_basename_globs() {
                    // Ignore individually-invalid plugin patterns rather than panicking;
                    // a malformed pattern simply fails to admit its config file (the
                    // pre-existing filesystem fallback still covers production mode).
                    let _ = types_builder.add("config", glob);
                }
                types_builder.select("config");
            }
            types_builder.build().expect("valid types")
        })
        .clone()
}

/// Construct the parallel walker, applying the appropriate hidden-dir filter.
/// When `capture_config` is set the walk also yields config-candidate files for
/// the secondary collection channel.
fn build_source_walk_builder(
    config: &ResolvedConfig,
    additional_hidden_dir_scopes: &[HiddenDirScope],
    capture_config: bool,
    skipped_dotdirs: &SkippedDotdirSink,
) -> WalkBuilder {
    let mut walk_builder = WalkBuilder::new(&config.root);
    walk_builder
        .hidden(false)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .types(build_walk_types(capture_config))
        .threads(config.threads);
    // One filter, not two: `filter_entry` replaces rather than chains, and the
    // dropped-dotdir record has to happen on the same false path that decides
    // the skip so the allowlist and every plugin- or script-contributed scope
    // are excluded by construction (issue #461).
    let scopes = additional_hidden_dir_scopes.to_vec();
    let sink = Arc::clone(skipped_dotdirs);
    walk_builder.filter_entry(move |entry| {
        if is_allowed_hidden_with_scopes(entry, &scopes) {
            return true;
        }
        if entry.file_type().is_some_and(|ft| ft.is_dir())
            && let Ok(mut collected) = sink.lock()
        {
            collected.push(entry.path().to_path_buf());
        }
        false
    });
    walk_builder
}

/// Compile the production-mode exclude glob set, or `None` outside production mode.
fn build_production_excludes(config: &ResolvedConfig) -> Option<globset::GlobSet> {
    if !config.production {
        return None;
    }
    let mut builder = globset::GlobSetBuilder::new();
    for pattern in PRODUCTION_EXCLUDE_PATTERNS {
        if let Ok(glob) = globset::GlobBuilder::new(pattern)
            .literal_separator(true)
            .build()
        {
            builder.add(glob);
        }
    }
    builder.build().ok()
}

/// Discover all source files in the project, with package-scoped hidden dirs.
///
/// # Panics
///
/// Panics if the file type glob or progress template is invalid (compile-time constants).
pub fn discover_files_with_additional_hidden_dirs(
    config: &ResolvedConfig,
    additional_hidden_dir_scopes: &[HiddenDirScope],
) -> Vec<DiscoveredFile> {
    discover_files_and_config_candidates(config, additional_hidden_dir_scopes).0
}

/// Discover source files AND, in one traversal, the non-source config-candidate
/// files (`tsconfig.json`, `bunfig.toml`, `.eslintrc.json`, ...) used by
/// `discover_config_files` to resolve plugin config patterns in-memory instead of
/// re-walking the filesystem.
///
/// The returned `Vec<DiscoveredFile>` is byte-for-byte identical to the
/// config-capture-disabled walk: config candidates are routed to the second
/// return value by extension and never enter the source channel. Config capture
/// is skipped in production mode (where the walk applies `PRODUCTION_EXCLUDE_PATTERNS`
/// and `discover_config_files` keeps its filesystem path), so the second vector is
/// empty there.
///
/// # Panics
///
/// Panics if the file type glob or progress template is invalid (compile-time constants).
pub fn discover_files_and_config_candidates(
    config: &ResolvedConfig,
    additional_hidden_dir_scopes: &[HiddenDirScope],
) -> (Vec<DiscoveredFile>, Vec<PathBuf>) {
    let discovered =
        discover_files_config_candidates_and_diagnostics(config, additional_hidden_dir_scopes);
    (discovered.files, discovered.config_candidates)
}

/// Source files, config candidates, and the source-discovery diagnostics one
/// walk produced.
///
/// `diagnostics` is the walk's OWN skip list, not a read of the process-wide
/// registry: combined mode can run two walks on the same root concurrently, and
/// each walk replaces the registry's source-discovery entries, so only the
/// by-value list is a stable answer to "what did THIS analysis skip" (issue
/// #2366).
pub struct DiscoveredSources {
    /// Source files with stable path-sorted [`FileId`]s.
    pub files: Vec<DiscoveredFile>,
    /// Non-source config-candidate paths captured in the same traversal.
    pub config_candidates: Vec<PathBuf>,
    /// Skipped-large-file, skipped-minified-file, and skipped-source-dotdir
    /// diagnostics from this walk.
    pub diagnostics: Vec<WorkspaceDiagnostic>,
}

/// [`discover_files_and_config_candidates`] plus the source-discovery
/// diagnostics this walk recorded, for callers that must carry a per-analysis
/// snapshot instead of reading the shared registry back (issue #2366).
///
/// # Panics
///
/// Panics if the file type glob or progress template is invalid (compile-time constants).
#[expect(
    clippy::cast_possible_truncation,
    reason = "file count is bounded by project size, well under u32::MAX"
)]
#[expect(clippy::expect_used, reason = "the collector lock must remain usable")]
pub fn discover_files_config_candidates_and_diagnostics(
    config: &ResolvedConfig,
    additional_hidden_dir_scopes: &[HiddenDirScope],
) -> DiscoveredSources {
    let _span = tracing::info_span!("discover_files").entered();

    let capture_config = !config.production;
    let skipped_dotdirs: SkippedDotdirSink = Arc::new(Mutex::new(Vec::new()));
    let walk_builder = build_source_walk_builder(
        config,
        additional_hidden_dir_scopes,
        capture_config,
        &skipped_dotdirs,
    );
    let production_excludes = build_production_excludes(config);
    let canonical_root = config.root.canonicalize().ok();

    let collected: Mutex<Vec<(std::path::PathBuf, u64)>> = Mutex::new(Vec::new());
    let config_collected: Mutex<Vec<std::path::PathBuf>> = Mutex::new(Vec::new());
    let mut visitor_builder = FileVisitorBuilder {
        root: &config.root,
        canonical_root: canonical_root.as_deref(),
        ignore_patterns: &config.ignore_patterns,
        production_excludes: &production_excludes,
        shared: &collected,
        config_shared: capture_config.then_some(&config_collected),
    };
    walk_builder.build_parallel().visit(&mut visitor_builder);

    let mut raw = collected
        .into_inner()
        .expect("walk collector lock poisoned");
    // ADR-004 (path-sorted FileIds): the parallel walk visits files in
    // nondeterministic order, so we sort by absolute path BEFORE the
    // `.enumerate()` FileId assignment below. This is the stable-cross-run
    // identity invariant the persisted graph cache depends on: an identical
    // file set yields identical FileIds, so a cache hit (same paths +
    // fingerprints) can trust graph data persisted by FileId. Do not replace
    // this with insertion-order assignment.
    raw.sort_unstable_by(|a, b| a.0.cmp(&b.0));

    let mut config_candidates = config_collected
        .into_inner()
        .expect("walk config collector lock poisoned");
    config_candidates.sort_unstable();

    // The parallel walk records dotdirs in nondeterministic thread order, and
    // the diagnostic array order is part of the JSON contract, so sort and
    // dedupe before the predicate runs (issue #2366).
    let mut dotdir_candidates = skipped_dotdirs
        .lock()
        .map_or_else(|_| Vec::new(), |mut guard| std::mem::take(&mut *guard));
    dotdir_candidates.sort_unstable();
    dotdir_candidates.dedup();

    let (kept, skipped) = partition_by_size(raw, config.max_file_size_bytes);
    let (kept, skipped_minified) =
        partition_minified_generated_js(kept, config.max_file_size_bytes);
    // One registry write replaces this root's whole source-discovery set, so a
    // stale entry from a previous pass drops out (issue #1086) without a window
    // in which a concurrent walk on the same root can observe or clobber a
    // half-written set (issue #2366).
    let diagnostics = fallow_config::replace_source_discovery_diagnostics(
        &config.root,
        report_skipped_large_files(config, &skipped)
            .into_iter()
            .chain(report_skipped_minified_files(config, &skipped_minified))
            .chain(report_skipped_source_dotdirs(
                config,
                production_excludes.as_ref(),
                &dotdir_candidates,
            ))
            .collect(),
    );

    let files: Vec<DiscoveredFile> = kept
        .into_iter()
        .enumerate()
        .map(|(idx, (path, size_bytes))| DiscoveredFile {
            id: FileId(idx as u32),
            path,
            size_bytes,
        })
        .collect();

    note_largest_files(config, &files);

    DiscoveredSources {
        files,
        config_candidates,
        diagnostics,
    }
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;
    use std::path::MAIN_SEPARATOR;

    use super::*;

    #[test]
    fn skipped_dotdirs_note_names_the_directory_when_there_is_one() {
        let root = Path::new("/repo");
        let only = PathBuf::from("/repo/.tooling");
        let note = build_skipped_dotdirs_note(root, &[&only], false);
        assert!(note.contains("skipped 1 hidden directory that contains source files"));
        assert!(note.contains("analyze it with fallow --root .tooling"));
        assert!(note.contains("add '.tooling/**' to"));
        assert!(
            !note.contains("<dir>"),
            "the single-directory remedy must be copy-pasteable: {note}"
        );
    }

    #[test]
    fn skipped_dotdirs_note_pluralizes_and_keeps_the_placeholder() {
        let root = Path::new("/repo");
        let a = PathBuf::from("/repo/.a");
        let b = PathBuf::from("/repo/.b");
        let note = build_skipped_dotdirs_note(root, &[&a, &b], false);
        assert!(note.contains("skipped 2 hidden directories that contain source files"));
        assert!(note.contains("analyze one with fallow --root <dir>"));
    }

    #[test]
    fn skipped_dotdirs_note_drops_the_tail_count_when_truncated() {
        let root = Path::new("/repo");
        let owned: Vec<PathBuf> = (0..8)
            .map(|i| PathBuf::from(format!("/repo/.d{i}")))
            .collect();
        let reportable: Vec<&PathBuf> = owned.iter().collect();

        let bounded = build_skipped_dotdirs_note(root, &reportable, true);
        assert!(bounded.contains("skipped at least 8 hidden directories"));
        assert!(
            bounded.contains("and more") && !bounded.contains("and 3 more"),
            "an inexact total must not carry an exact remainder: {bounded}"
        );

        let complete = build_skipped_dotdirs_note(root, &reportable, false);
        assert!(!complete.contains("at least"));
        assert!(complete.contains("and 3 more"));
    }

    #[test]
    fn dotdir_noise_path_components_stay_sorted_and_lowercase() {
        let mut sorted = DOTDIR_NOISE_PATH_COMPONENTS.to_vec();
        sorted.sort_unstable();
        assert_eq!(sorted, DOTDIR_NOISE_PATH_COMPONENTS);
        for component in DOTDIR_NOISE_PATH_COMPONENTS {
            assert!(!component.starts_with('.'), "'{component}' is not hidden");
            assert_eq!(
                *component,
                component.to_lowercase(),
                "'{component}' is matched verbatim against a path component"
            );
        }
    }

    #[test]
    fn script_scope_denylist_stays_disjoint_and_sorted() {
        let mut sorted = SCRIPT_SCOPE_DENYLIST.to_vec();
        sorted.sort_unstable();
        assert_eq!(
            sorted, SCRIPT_SCOPE_DENYLIST,
            "keep the list sorted so additions stay reviewable"
        );
        for dir in SCRIPT_SCOPE_DENYLIST {
            assert!(dir.starts_with('.'), "'{dir}' is not a hidden directory");
            assert!(
                !ALLOWED_HIDDEN_DIRS.contains(dir),
                "'{dir}' is traversed, so it can never be a skipped candidate"
            );
        }
    }

    #[test]
    fn dotdir_module_extensions_are_a_subset_of_source_extensions() {
        for ext in DOTDIR_MODULE_EXTENSIONS {
            assert!(
                SOURCE_EXTENSIONS.contains(ext),
                "'{ext}' is not discovered as source, so it cannot be a trigger"
            );
        }
        for ext in ["css", "scss", "sass", "less", "html", "graphql", "gql"] {
            assert!(
                !DOTDIR_MODULE_EXTENSIONS.contains(&ext),
                "'{ext}' carries no imports or exports for the message to be about"
            );
        }
    }

    /// Reproduce the FileId-assignment rule used by `walk_source_files`: sort by
    /// absolute path, then assign `FileId(idx)` in that order.
    fn assign_file_ids(mut raw: Vec<(std::path::PathBuf, u64)>) -> Vec<DiscoveredFile> {
        raw.sort_unstable_by(|a, b| a.0.cmp(&b.0));
        raw.into_iter()
            .enumerate()
            .map(|(idx, (path, size_bytes))| DiscoveredFile {
                id: FileId(idx as u32),
                path,
                size_bytes,
            })
            .collect()
    }

    /// ADR-004: an identical file set must yield identical FileIds regardless of
    /// the (nondeterministic, parallel) discovery order. The persisted graph
    /// cache keys persisted graph data by FileId, so a cache HIT (same paths +
    /// fingerprints) must reproduce the exact same FileId-to-path mapping the
    /// graph was built against. This guards the cache's soundness prerequisite.
    #[test]
    fn file_id_assignment_is_deterministic_for_identical_file_set() {
        let paths = [
            "/project/src/z.ts",
            "/project/src/a.ts",
            "/project/src/components/Button.tsx",
            "/project/src/components/Button.module.css",
            "/project/index.ts",
        ];

        // Two independent walks that observe the same paths in DIFFERENT orders.
        let walk_one: Vec<(std::path::PathBuf, u64)> = paths
            .iter()
            .map(|p| (std::path::PathBuf::from(p), 10))
            .collect();
        let mut walk_two = walk_one.clone();
        walk_two.reverse();

        let files_one = assign_file_ids(walk_one);
        let files_two = assign_file_ids(walk_two);

        // Identical (FileId -> path) mapping despite the different walk orders.
        assert_eq!(files_one.len(), files_two.len());
        for (a, b) in files_one.iter().zip(files_two.iter()) {
            assert_eq!(a.id, b.id);
            assert_eq!(a.path, b.path);
        }

        // The mapping is the path-sorted order, and each FileId equals its index
        // (the density invariant `project.rs` asserts and the graph relies on).
        for (idx, file) in files_one.iter().enumerate() {
            assert_eq!(file.id, FileId(idx as u32));
        }
        assert_eq!(
            files_one[0].path,
            std::path::PathBuf::from("/project/index.ts")
        );
    }

    #[test]
    fn file_id_assignment_recomputes_after_rename_or_delete() {
        let before = assign_file_ids(vec![
            (std::path::PathBuf::from("/project/src/a.ts"), 10),
            (std::path::PathBuf::from("/project/src/b.ts"), 10),
            (std::path::PathBuf::from("/project/src/c.ts"), 10),
        ]);
        let after_delete = assign_file_ids(vec![
            (std::path::PathBuf::from("/project/src/a.ts"), 10),
            (std::path::PathBuf::from("/project/src/c.ts"), 10),
        ]);
        let after_rename = assign_file_ids(vec![
            (std::path::PathBuf::from("/project/src/a.ts"), 10),
            (std::path::PathBuf::from("/project/src/c.ts"), 10),
            (std::path::PathBuf::from("/project/src/d.ts"), 10),
        ]);

        assert_eq!(before[0].id, FileId(0));
        assert_eq!(before[1].id, FileId(1));
        assert_eq!(before[2].id, FileId(2));
        assert_eq!(after_delete[0].id, FileId(0));
        assert_eq!(after_delete[1].id, FileId(1));
        assert_eq!(
            after_delete[1].path,
            std::path::PathBuf::from("/project/src/c.ts")
        );
        assert_eq!(after_rename[0].id, FileId(0));
        assert_eq!(after_rename[1].id, FileId(1));
        assert_eq!(
            after_rename[1].path,
            std::path::PathBuf::from("/project/src/c.ts")
        );
        assert_eq!(after_rename[2].id, FileId(2));
        assert_eq!(
            after_rename[2].path,
            std::path::PathBuf::from("/project/src/d.ts")
        );
    }

    #[test]
    fn allowed_hidden_dirs() {
        assert!(is_allowed_hidden_dir(OsStr::new(".storybook")));
        assert!(is_allowed_hidden_dir(OsStr::new(".vitepress")));
        assert!(is_allowed_hidden_dir(OsStr::new(".well-known")));
        assert!(is_allowed_hidden_dir(OsStr::new(".changeset")));
        assert!(is_allowed_hidden_dir(OsStr::new(".github")));
    }

    #[test]
    fn disallowed_hidden_dirs() {
        assert!(!is_allowed_hidden_dir(OsStr::new(".git")));
        assert!(!is_allowed_hidden_dir(OsStr::new(".cache")));
        assert!(!is_allowed_hidden_dir(OsStr::new(".vscode")));
        assert!(!is_allowed_hidden_dir(OsStr::new(".fallow")));
        assert!(!is_allowed_hidden_dir(OsStr::new(".next")));
    }

    #[test]
    fn non_hidden_dirs_not_in_allowlist() {
        assert!(!is_allowed_hidden_dir(OsStr::new("src")));
        assert!(!is_allowed_hidden_dir(OsStr::new("node_modules")));
    }

    #[test]
    fn walk_types_match_every_supported_source_extension() {
        for capture_config in [false, true] {
            let types = build_walk_types(capture_config);
            for extension in SOURCE_EXTENSIONS {
                let path = format!("packages/ui/src/nested/component.{extension}");
                assert!(
                    types.matched(&path, false).is_whitelist(),
                    "expected source match for {path} with capture_config={capture_config}"
                );
            }
        }
    }

    #[test]
    fn walk_types_match_typescript_declaration_files() {
        let types = build_walk_types(true);
        for path in [
            "src/env.d.ts",
            "packages/app/types/generated.d.mts",
            "packages/app/types/compat.d.cts",
        ] {
            assert!(
                types.matched(path, false).is_whitelist(),
                "expected declaration source match for {path}"
            );
        }
    }

    #[test]
    fn walk_types_reject_source_extension_near_misses() {
        for capture_config in [false, true] {
            let types = build_walk_types(capture_config);
            for path in [
                "src/component.tsx.bak",
                "src/component.tsxmap",
                "src/component.TS",
                "src/component.gqlx",
                "src/component.htm",
                "src/component",
                "assets/component.png",
            ] {
                assert!(
                    types.matched(path, false).is_ignore(),
                    "expected non-source rejection for {path} with capture_config={capture_config}"
                );
            }
        }
    }

    #[test]
    fn walk_types_keep_config_candidate_selection_separate() {
        assert!(
            build_walk_types(true)
                .matched("packages/app/tsconfig.json", false)
                .is_whitelist()
        );
        assert!(
            build_walk_types(false)
                .matched("packages/app/tsconfig.json", false)
                .is_ignore()
        );
    }

    #[test]
    fn source_extensions_include_typescript() {
        assert!(SOURCE_EXTENSIONS.contains(&"ts"));
        assert!(SOURCE_EXTENSIONS.contains(&"tsx"));
        assert!(SOURCE_EXTENSIONS.contains(&"mts"));
        assert!(SOURCE_EXTENSIONS.contains(&"cts"));
        assert!(SOURCE_EXTENSIONS.contains(&"gts"));
    }

    #[test]
    fn source_extensions_include_javascript() {
        assert!(SOURCE_EXTENSIONS.contains(&"js"));
        assert!(SOURCE_EXTENSIONS.contains(&"jsx"));
        assert!(SOURCE_EXTENSIONS.contains(&"mjs"));
        assert!(SOURCE_EXTENSIONS.contains(&"cjs"));
        assert!(SOURCE_EXTENSIONS.contains(&"gjs"));
    }

    #[test]
    fn source_extensions_include_sfc_formats() {
        assert!(SOURCE_EXTENSIONS.contains(&"vue"));
        assert!(SOURCE_EXTENSIONS.contains(&"svelte"));
        assert!(SOURCE_EXTENSIONS.contains(&"astro"));
    }

    #[test]
    fn source_extensions_include_styles() {
        assert!(SOURCE_EXTENSIONS.contains(&"css"));
        assert!(SOURCE_EXTENSIONS.contains(&"scss"));
        assert!(SOURCE_EXTENSIONS.contains(&"sass"));
        assert!(SOURCE_EXTENSIONS.contains(&"less"));
    }

    #[test]
    fn source_extensions_exclude_non_source() {
        assert!(!SOURCE_EXTENSIONS.contains(&"json"));
        assert!(!SOURCE_EXTENSIONS.contains(&"yaml"));
        assert!(!SOURCE_EXTENSIONS.contains(&"md"));
        assert!(!SOURCE_EXTENSIONS.contains(&"png"));
        assert!(!SOURCE_EXTENSIONS.contains(&"htm"));
    }

    #[test]
    fn source_extensions_include_html() {
        assert!(SOURCE_EXTENSIONS.contains(&"html"));
    }

    #[test]
    fn source_extensions_include_graphql_documents() {
        assert!(SOURCE_EXTENSIONS.contains(&"graphql"));
        assert!(SOURCE_EXTENSIONS.contains(&"gql"));
    }

    fn build_production_glob_set() -> globset::GlobSet {
        let mut builder = globset::GlobSetBuilder::new();
        for pattern in PRODUCTION_EXCLUDE_PATTERNS {
            builder.add(
                globset::GlobBuilder::new(pattern)
                    .literal_separator(true)
                    .build()
                    .expect("valid glob pattern"),
            );
        }
        builder.build().expect("valid glob set")
    }

    #[test]
    fn production_excludes_test_files() {
        let set = build_production_glob_set();
        assert!(set.is_match("src/Button.test.ts"));
        assert!(set.is_match("src/utils.spec.tsx"));
        assert!(set.is_match("src/__tests__/helper.ts"));
        assert!(!set.is_match("src/Button.ts"));
        assert!(!set.is_match("src/utils.tsx"));
    }

    #[test]
    fn production_excludes_story_files() {
        let set = build_production_glob_set();
        assert!(set.is_match("src/Button.stories.tsx"));
        assert!(set.is_match("src/Card.story.ts"));
        assert!(!set.is_match("src/Button.tsx"));
    }

    #[test]
    fn production_excludes_config_files_at_root_only() {
        let set = build_production_glob_set();
        assert!(set.is_match("vitest.config.ts"));
        assert!(set.is_match("jest.config.js"));
        assert!(!set.is_match("src/app/app.config.ts"));
        assert!(!set.is_match("src/app/app.config.server.ts"));
        assert!(!set.is_match("packages/foo/vitest.config.ts"));
        assert!(!set.is_match("src/config.ts"));
    }

    #[test]
    fn production_patterns_are_valid_globs() {
        let _ = build_production_glob_set();
    }

    #[test]
    fn disallowed_hidden_dirs_idea() {
        assert!(!is_allowed_hidden_dir(OsStr::new(".idea")));
    }

    #[test]
    fn source_extensions_include_mdx() {
        assert!(SOURCE_EXTENSIONS.contains(&"mdx"));
    }

    #[test]
    fn source_extensions_exclude_image_and_data_formats() {
        assert!(!SOURCE_EXTENSIONS.contains(&"png"));
        assert!(!SOURCE_EXTENSIONS.contains(&"jpg"));
        assert!(!SOURCE_EXTENSIONS.contains(&"svg"));
        assert!(!SOURCE_EXTENSIONS.contains(&"txt"));
        assert!(!SOURCE_EXTENSIONS.contains(&"csv"));
        assert!(!SOURCE_EXTENSIONS.contains(&"wasm"));
    }

    #[test]
    fn is_declaration_file_matches_dts_variants() {
        assert!(is_declaration_file(Path::new("env.d.ts")));
        assert!(is_declaration_file(Path::new("src/auto-imports.d.ts")));
        assert!(is_declaration_file(Path::new("mod.d.mts")));
        assert!(is_declaration_file(Path::new("compat.d.cts")));
        assert!(!is_declaration_file(Path::new("index.ts")));
        assert!(!is_declaration_file(Path::new("component.tsx")));
        assert!(!is_declaration_file(Path::new("notes.d.txt")));
    }

    #[test]
    fn format_size_mb_renders_one_decimal() {
        assert_eq!(format_size_mb(5 * 1024 * 1024), "5.0 MB");
        assert_eq!(format_size_mb(1024 * 1024 + 512 * 1024), "1.5 MB");
        assert_eq!(format_size_mb(0), "0.0 MB");
    }

    #[test]
    fn partition_by_size_no_limit_keeps_all() {
        let raw = vec![(PathBuf::from("a.ts"), 10), (PathBuf::from("b.ts"), 10_000)];
        let (kept, skipped) = partition_by_size(raw, None);
        assert_eq!(kept.len(), 2);
        assert!(skipped.is_empty());
    }

    #[test]
    fn partition_by_size_skips_strictly_over_limit() {
        let raw = vec![
            (PathBuf::from("under.ts"), 99),
            (PathBuf::from("exact.ts"), 100),
            (PathBuf::from("over.ts"), 101),
        ];
        let (kept, skipped) = partition_by_size(raw, Some(100));
        let kept_has = |name: &str| kept.iter().any(|(p, _)| p.as_path() == Path::new(name));
        assert!(kept_has("under.ts"));
        assert!(
            kept_has("exact.ts"),
            "a file exactly at the limit is kept (skip is strictly-greater)"
        );
        assert_eq!(skipped.len(), 1);
        assert_eq!(skipped[0].0, PathBuf::from("over.ts"));
    }

    #[test]
    fn partition_by_size_exempts_declaration_files() {
        let raw = vec![
            (PathBuf::from("huge.ts"), 10_000),
            (PathBuf::from("auto-imports.d.ts"), 10_000),
        ];
        let (kept, skipped) = partition_by_size(raw, Some(100));
        assert!(
            kept.iter()
                .any(|(p, _)| p.as_path() == Path::new("auto-imports.d.ts")),
            "declaration files are exempt from the size skip regardless of size"
        );
        assert_eq!(skipped.len(), 1);
        assert_eq!(skipped[0].0, PathBuf::from("huge.ts"));
    }

    fn disco(path: &str, size_bytes: u64) -> DiscoveredFile {
        DiscoveredFile {
            id: FileId(0),
            path: PathBuf::from(path),
            size_bytes,
        }
    }

    #[test]
    fn largest_files_note_below_threshold_is_none() {
        let files = [disco("a.ts", 100), disco("b.ts", 200)];
        assert!(build_largest_files_note(Path::new("/p"), &files).is_none());
    }

    #[test]
    fn largest_files_note_single_file_uses_singular() {
        let files = [disco("big.ts", 5 * 1024 * 1024)];
        let note = build_largest_files_note(Path::new("/p"), &files).expect("note fires");
        assert!(
            note.contains("discovered 1 file;"),
            "singular noun on the single-big-file path (issue #1086 regression): {note}"
        );
        assert!(!note.contains("discovered 1 files"));
        assert!(note.contains("big.ts (5.0 MB)"));
    }

    #[test]
    fn largest_files_note_filters_sub_floor_files() {
        let files = [disco("big.ts", 5 * 1024 * 1024), disco("tiny.ts", 10)];
        let note = build_largest_files_note(Path::new("/p"), &files).expect("note fires");
        assert!(note.contains("discovered 2 files;"));
        assert!(note.contains("big.ts (5.0 MB)"));
        assert!(
            !note.contains("tiny.ts"),
            "sub-floor files are not listed as `0.0 MB` chaff: {note}"
        );
    }

    #[test]
    fn largest_files_note_large_set_no_big_file_omits_list() {
        let files: Vec<DiscoveredFile> = (0..=LARGE_SET_THRESHOLD)
            .map(|i| disco(&format!("f{i}.ts"), 100))
            .collect();
        let note = build_largest_files_note(Path::new("/p"), &files).expect("large set fires");
        assert!(note.contains(&format!("discovered {} files", LARGE_SET_THRESHOLD + 1)));
        assert!(
            !note.contains("largest:"),
            "no sub-floor `largest:` list when no file clears the floor: {note}"
        );
    }

    mod discover_files_integration {
        use std::path::PathBuf;

        use fallow_config::{
            DuplicatesConfig, FallowConfig, FlagsConfig, HealthConfig, OutputFormat, ResolveConfig,
            RulesConfig,
        };

        use super::*;

        /// Create a minimal ResolvedConfig pointing at the given root directory.
        fn make_config(root: PathBuf, production: bool) -> ResolvedConfig {
            FallowConfig {
                production: production.into(),
                ..Default::default()
            }
            .resolve(root, OutputFormat::Human, 1, true, true, None)
        }

        /// Helper to collect discovered file names (relative to root) for assertions.
        /// Normalizes path separators to `/` for cross-platform test consistency.
        fn file_names(files: &[DiscoveredFile], root: &std::path::Path) -> Vec<String> {
            files
                .iter()
                .map(|f| {
                    f.path
                        .strip_prefix(root)
                        .unwrap_or(&f.path)
                        .to_string_lossy()
                        .replace('\\', "/")
                })
                .collect()
        }

        #[cfg(unix)]
        fn symlink_file(target: &Path, link: &Path) {
            std::os::unix::fs::symlink(target, link).expect("create file symlink");
        }

        #[cfg(windows)]
        fn symlink_file(target: &Path, link: &Path) {
            std::os::windows::fs::symlink_file(target, link).expect("create file symlink");
        }

        #[cfg(unix)]
        fn symlink_dir(target: &Path, link: &Path) {
            std::os::unix::fs::symlink(target, link).expect("create directory symlink");
        }

        #[cfg(windows)]
        fn symlink_dir(target: &Path, link: &Path) {
            std::os::windows::fs::symlink_dir(target, link).expect("create directory symlink");
        }

        #[test]
        fn source_symlinks_must_target_regular_files_inside_root() {
            let dir = tempfile::tempdir().expect("create project");
            let outside = tempfile::tempdir().expect("create outside dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("regular.ts"), "export const regular = 1;").unwrap();
            std::fs::write(src.join("inside-target.ts"), "export const inside = 1;").unwrap();
            std::fs::write(
                outside.path().join("outside-target.ts"),
                "export const outside = 1;",
            )
            .unwrap();
            std::fs::create_dir_all(src.join("directory-target")).unwrap();

            symlink_file(&src.join("inside-target.ts"), &src.join("inside-link.ts"));
            symlink_file(
                &outside.path().join("outside-target.ts"),
                &src.join("outside-link.ts"),
            );
            symlink_file(&src.join("missing-target.ts"), &src.join("broken-link.ts"));
            symlink_dir(
                &src.join("directory-target"),
                &src.join("directory-link.ts"),
            );

            let config = make_config(dir.path().to_path_buf(), false);
            let names = file_names(&discover_files(&config), dir.path());

            assert!(names.contains(&"src/regular.ts".to_string()));
            assert!(names.contains(&"src/inside-target.ts".to_string()));
            assert!(names.contains(&"src/inside-link.ts".to_string()));
            assert!(!names.contains(&"src/outside-link.ts".to_string()));
            assert!(!names.contains(&"src/broken-link.ts".to_string()));
            assert!(!names.contains(&"src/directory-link.ts".to_string()));
        }

        /// Yarn PnP writes `.pnp.cjs` and `.pnp.loader.mjs` at the workspace
        /// root. They match the source extension filter but are generated
        /// install state, not code to analyze.
        #[test]
        fn skips_yarn_pnp_generated_files() {
            let dir = tempfile::tempdir().expect("create temp dir");
            std::fs::write(dir.path().join(".pnp.cjs"), "module.exports = {};").unwrap();
            std::fs::write(dir.path().join(".pnp.loader.mjs"), "export {};").unwrap();
            std::fs::write(dir.path().join("index.ts"), "export const a = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let names = file_names(&discover_files(&config), dir.path());

            assert_eq!(names, vec!["index.ts".to_string()]);
        }

        #[test]
        fn discovers_source_files_with_valid_extensions() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();

            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
            std::fs::write(src.join("component.tsx"), "export default () => {};").unwrap();
            std::fs::write(src.join("utils.js"), "module.exports = {};").unwrap();
            std::fs::write(src.join("helper.jsx"), "export const h = 1;").unwrap();
            std::fs::write(src.join("config.mjs"), "export default {};").unwrap();
            std::fs::write(src.join("legacy.cjs"), "module.exports = {};").unwrap();
            std::fs::write(src.join("types.mts"), "export type T = string;").unwrap();
            std::fs::write(src.join("compat.cts"), "module.exports = {};").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"src/app.ts".to_string()));
            assert!(names.contains(&"src/component.tsx".to_string()));
            assert!(names.contains(&"src/utils.js".to_string()));
            assert!(names.contains(&"src/helper.jsx".to_string()));
            assert!(names.contains(&"src/config.mjs".to_string()));
            assert!(names.contains(&"src/legacy.cjs".to_string()));
            assert!(names.contains(&"src/types.mts".to_string()));
            assert!(names.contains(&"src/compat.cts".to_string()));
        }

        #[test]
        fn compact_source_glob_preserves_discovered_file_inventory() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let nested = dir.path().join("packages/ui/src/nested");
            std::fs::create_dir_all(&nested).unwrap();

            let mut expected = Vec::new();
            for (index, extension) in SOURCE_EXTENSIONS.iter().enumerate() {
                let relative = format!("packages/ui/src/nested/source-{index}.{extension}");
                std::fs::write(dir.path().join(&relative), "export const value = 1;").unwrap();
                expected.push(relative);
            }
            for relative in [
                "packages/ui/src/nested/env.d.ts",
                "packages/ui/src/nested/generated.d.mts",
                "packages/ui/src/nested/compat.d.cts",
            ] {
                std::fs::write(dir.path().join(relative), "export type Value = string;").unwrap();
                expected.push(relative.to_string());
            }
            let rejected = [
                "packages/ui/src/nested/component.tsx.bak",
                "packages/ui/src/nested/component.tsxmap",
                "packages/ui/src/nested/component.TS",
                "packages/ui/src/nested/component.gqlx",
                "packages/ui/src/nested/component.htm",
                "packages/ui/src/nested/component",
                "packages/ui/src/nested/component.png",
            ];
            for relative in rejected {
                std::fs::write(dir.path().join(relative), "not source").unwrap();
            }

            let config = make_config(dir.path().to_path_buf(), false);
            let names = file_names(&discover_files(&config), dir.path());

            for relative in expected {
                assert!(
                    names.contains(&relative),
                    "missing supported source {relative}"
                );
            }
            for relative in rejected {
                assert!(
                    !names.iter().any(|name| name == relative),
                    "unexpected near-miss source {relative}"
                );
            }
        }

        #[test]
        fn excludes_non_source_extensions() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();

            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();

            std::fs::write(src.join("data.json"), "{}").unwrap();
            std::fs::write(src.join("readme.md"), "# Hello").unwrap();
            std::fs::write(src.join("notes.txt"), "notes").unwrap();
            std::fs::write(src.join("logo.png"), [0u8; 8]).unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(names.len(), 1, "only the .ts file should be discovered");
            assert!(names.contains(&"src/app.ts".to_string()));
        }

        #[test]
        fn excludes_disallowed_hidden_directories() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let git_dir = dir.path().join(".git");
            std::fs::create_dir_all(&git_dir).unwrap();
            std::fs::write(git_dir.join("hooks.ts"), "// git hook").unwrap();

            let idea_dir = dir.path().join(".idea");
            std::fs::create_dir_all(&idea_dir).unwrap();
            std::fs::write(idea_dir.join("workspace.ts"), "// idea").unwrap();

            let cache_dir = dir.path().join(".cache");
            std::fs::create_dir_all(&cache_dir).unwrap();
            std::fs::write(cache_dir.join("cached.js"), "// cached").unwrap();

            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(names.len(), 1, "only src/app.ts should be discovered");
            assert!(names.contains(&"src/app.ts".to_string()));
        }

        #[test]
        fn includes_allowed_hidden_directories() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let storybook = dir.path().join(".storybook");
            std::fs::create_dir_all(&storybook).unwrap();
            std::fs::write(storybook.join("main.ts"), "export default {};").unwrap();

            let github = dir.path().join(".github");
            std::fs::create_dir_all(&github).unwrap();
            std::fs::write(github.join("actions.js"), "module.exports = {};").unwrap();

            let changeset = dir.path().join(".changeset");
            std::fs::create_dir_all(&changeset).unwrap();
            std::fs::write(changeset.join("config.js"), "module.exports = {};").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&".storybook/main.ts".to_string()),
                "files in .storybook should be discovered"
            );
            assert!(
                names.contains(&".github/actions.js".to_string()),
                "files in .github should be discovered"
            );
            assert!(
                names.contains(&".changeset/config.js".to_string()),
                "files in .changeset should be discovered"
            );
        }

        #[test]
        fn default_discovery_excludes_client_and_server_hidden_directories() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let app = dir.path().join("app");
            std::fs::create_dir_all(app.join(".client")).unwrap();
            std::fs::create_dir_all(app.join(".server")).unwrap();
            std::fs::write(app.join(".client/analytics.ts"), "export const a = 1;").unwrap();
            std::fs::write(app.join(".server/db.ts"), "export const db = {};").unwrap();
            std::fs::write(app.join("root.tsx"), "export default function Root() {}").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"app/root.tsx".to_string()));
            assert!(!names.contains(&"app/.client/analytics.ts".to_string()));
            assert!(!names.contains(&"app/.server/db.ts".to_string()));
        }

        #[test]
        fn scoped_hidden_dirs_include_client_and_server_under_package_root() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let package = dir.path().join("packages/app");
            std::fs::create_dir_all(package.join("app/.client")).unwrap();
            std::fs::create_dir_all(package.join("app/.server")).unwrap();
            std::fs::write(
                package.join("app/.client/analytics.ts"),
                "export const track = () => {};",
            )
            .unwrap();
            std::fs::write(package.join("app/.server/db.ts"), "export const db = {};").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let scopes = [HiddenDirScope::new(
                package,
                vec![".client".to_string(), ".server".to_string()],
            )];
            let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"packages/app/app/.client/analytics.ts".to_string()));
            assert!(names.contains(&"packages/app/app/.server/db.ts".to_string()));
        }

        #[test]
        fn scoped_hidden_dirs_do_not_include_unscoped_packages() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let active = dir.path().join("packages/active");
            let inactive = dir.path().join("packages/inactive");
            std::fs::create_dir_all(active.join("app/.server")).unwrap();
            std::fs::create_dir_all(inactive.join("app/.server")).unwrap();
            std::fs::write(active.join("app/.server/db.ts"), "export const db = {};").unwrap();
            std::fs::write(inactive.join("app/.server/db.ts"), "export const db = {};").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let scopes = [HiddenDirScope::new(active, vec![".server".to_string()])];
            let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"packages/active/app/.server/db.ts".to_string()));
            assert!(!names.contains(&"packages/inactive/app/.server/db.ts".to_string()));
        }

        #[test]
        fn exact_path_scope_does_not_admit_the_same_name_elsewhere() {
            // A script naming `.a/.b/deep.mjs` says where the file it needs
            // lives. Before issue #461 the scope stored the bare names, so an
            // unrelated `elsewhere/.b` and `unrelated/.a` were pulled in too.
            let dir = tempfile::tempdir().expect("create temp dir");
            std::fs::create_dir_all(dir.path().join(".a/.b")).unwrap();
            std::fs::create_dir_all(dir.path().join("elsewhere/.b")).unwrap();
            std::fs::create_dir_all(dir.path().join("unrelated/.a")).unwrap();
            std::fs::write(dir.path().join(".a/.b/deep.mjs"), "export const a = 1;").unwrap();
            std::fs::write(dir.path().join("elsewhere/.b/y.mjs"), "export const b = 1;").unwrap();
            std::fs::write(dir.path().join("unrelated/.a/u.mjs"), "export const c = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let scopes = [HiddenDirScope::new_exact_paths(
                dir.path().to_path_buf(),
                vec![".a".to_string(), format!(".a{MAIN_SEPARATOR}.b")],
            )];
            let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&".a/.b/deep.mjs".to_string()));
            assert!(!names.contains(&"elsewhere/.b/y.mjs".to_string()));
            assert!(!names.contains(&"unrelated/.a/u.mjs".to_string()));
        }

        #[test]
        fn exact_path_scope_admits_a_hidden_dir_under_a_visible_parent() {
            let dir = tempfile::tempdir().expect("create temp dir");
            std::fs::create_dir_all(dir.path().join("tools/.config")).unwrap();
            std::fs::create_dir_all(dir.path().join("other/.config")).unwrap();
            std::fs::write(
                dir.path().join("tools/.config/eslint.config.js"),
                "export default [];",
            )
            .unwrap();
            std::fs::write(
                dir.path().join("other/.config/eslint.config.js"),
                "export default [];",
            )
            .unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let scopes = [HiddenDirScope::new_exact_paths(
                dir.path().to_path_buf(),
                vec![format!("tools{MAIN_SEPARATOR}.config")],
            )];
            let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"tools/.config/eslint.config.js".to_string()));
            assert!(!names.contains(&"other/.config/eslint.config.js".to_string()));
        }

        #[test]
        fn any_depth_scope_keeps_matching_by_name_for_plugins() {
            // Framework plugins declare `.client` / `.server` conventions that
            // may sit under any route directory, so the plugin shape must keep
            // matching at any depth.
            let dir = tempfile::tempdir().expect("create temp dir");
            std::fs::create_dir_all(dir.path().join("app/routes/deep/.server")).unwrap();
            std::fs::write(
                dir.path().join("app/routes/deep/.server/db.ts"),
                "export const db = {};",
            )
            .unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let scopes = [HiddenDirScope::new(
                dir.path().to_path_buf(),
                vec![".server".to_string()],
            )];
            let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"app/routes/deep/.server/db.ts".to_string()));
        }

        #[test]
        fn excludes_root_build_directory() {
            let dir = tempfile::tempdir().expect("create temp dir");

            std::fs::write(dir.path().join(".ignore"), "/build/\n").unwrap();

            let build_dir = dir.path().join("build");
            std::fs::create_dir_all(&build_dir).unwrap();
            std::fs::write(build_dir.join("output.js"), "// build output").unwrap();

            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(names.len(), 1, "root build/ should be excluded via .ignore");
            assert!(names.contains(&"src/app.ts".to_string()));
        }

        #[test]
        fn includes_nested_build_directory() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let nested_build = dir.path().join("src").join("build");
            std::fs::create_dir_all(&nested_build).unwrap();
            std::fs::write(nested_build.join("helper.ts"), "export const h = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&"src/build/helper.ts".to_string()),
                "nested build/ directories should be included"
            );
        }

        #[test]
        #[expect(
            clippy::cast_possible_truncation,
            reason = "test file counts are trivially small"
        )]
        fn file_ids_are_sequential_after_sorting() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();

            std::fs::write(src.join("z_last.ts"), "export const z = 1;").unwrap();
            std::fs::write(src.join("a_first.ts"), "export const a = 1;").unwrap();
            std::fs::write(src.join("m_middle.ts"), "export const m = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);

            for (idx, file) in files.iter().enumerate() {
                assert_eq!(file.id, FileId(idx as u32), "FileId should be sequential");
            }

            for pair in files.windows(2) {
                assert!(
                    pair[0].path < pair[1].path,
                    "files should be sorted by path"
                );
            }
        }

        #[test]
        fn production_mode_excludes_test_files() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();

            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
            std::fs::write(src.join("app.test.ts"), "test('a', () => {});").unwrap();
            std::fs::write(src.join("app.spec.ts"), "describe('a', () => {});").unwrap();
            std::fs::write(src.join("app.stories.tsx"), "export default {};").unwrap();

            let config = make_config(dir.path().to_path_buf(), true);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&"src/app.ts".to_string()),
                "source files should be included in production mode"
            );
            assert!(
                !names.contains(&"src/app.test.ts".to_string()),
                "test files should be excluded in production mode"
            );
            assert!(
                !names.contains(&"src/app.spec.ts".to_string()),
                "spec files should be excluded in production mode"
            );
            assert!(
                !names.contains(&"src/app.stories.tsx".to_string()),
                "story files should be excluded in production mode"
            );
        }

        #[test]
        fn non_production_mode_includes_test_files() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();

            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();
            std::fs::write(src.join("app.test.ts"), "test('a', () => {});").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"src/app.ts".to_string()));
            assert!(
                names.contains(&"src/app.test.ts".to_string()),
                "test files should be included in non-production mode"
            );
        }

        #[test]
        fn empty_directory_returns_no_files() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            assert!(files.is_empty(), "empty project should discover no files");
        }

        #[test]
        fn hidden_files_not_discovered_as_source() {
            let dir = tempfile::tempdir().expect("create temp dir");

            std::fs::write(dir.path().join(".env"), "SECRET=abc").unwrap();
            std::fs::write(dir.path().join(".gitignore"), "node_modules").unwrap();
            std::fs::write(dir.path().join(".eslintrc.js"), "module.exports = {};").unwrap();

            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("app.ts"), "export const a = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                !names.contains(&".env".to_string()),
                ".env should not be discovered"
            );
            assert!(
                !names.contains(&".gitignore".to_string()),
                ".gitignore should not be discovered"
            );
        }

        /// Create a config with custom ignore patterns.
        fn make_config_with_ignores(root: PathBuf, ignores: Vec<String>) -> ResolvedConfig {
            FallowConfig {
                type_aware: fallow_config::TypeAwareConfig::default(),
                schema: None,
                extends: vec![],
                entry: vec![],
                ignore_patterns: ignores,
                ignore_findings: vec![],
                framework: vec![],
                workspaces: None,
                ignore_dependencies: vec![],
                ignore_unresolved_imports: vec![],
                ignore_exports: vec![],
                ignore_catalog_references: vec![],
                ignore_dependency_overrides: vec![],
                ignore_exports_used_in_file: fallow_config::IgnoreExportsUsedInFileConfig::default(
                ),
                used_class_members: vec![],
                ignore_decorators: vec![],
                unused_component_props: fallow_config::UnusedComponentPropsConfig::default(),
                duplicates: DuplicatesConfig::default(),
                similar_code: fallow_config::SimilarCodeConfig::default(),
                health: HealthConfig::default(),
                rules: RulesConfig::default(),
                boundaries: fallow_config::BoundaryConfig::default(),
                production: false.into(),
                plugins: vec![],
                rule_packs: vec![],
                dynamically_loaded: vec![],
                overrides: vec![],
                regression: None,
                audit: fallow_config::AuditConfig::default(),
                codeowners: None,
                public_packages: vec![],
                flags: FlagsConfig::default(),
                security: fallow_config::SecurityConfig::default(),
                fix: fallow_config::FixConfig::default(),
                resolve: ResolveConfig::default(),
                sealed: false,
                include_entry_exports: false,
                auto_imports: false,
                cache: fallow_config::CacheConfig::default(),
            }
            .resolve(root, OutputFormat::Human, 1, true, true, None)
        }

        #[test]
        fn custom_ignore_patterns_exclude_matching_files() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let generated = dir.path().join("src").join("api").join("generated");
            std::fs::create_dir_all(&generated).unwrap();
            std::fs::write(generated.join("client.ts"), "export const api = {};").unwrap();

            let client = dir.path().join("src").join("api").join("client");
            std::fs::create_dir_all(&client).unwrap();
            std::fs::write(client.join("fetch.ts"), "export const fetch = {};").unwrap();

            let src = dir.path().join("src");
            std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();

            let config = make_config_with_ignores(
                dir.path().to_path_buf(),
                vec![
                    "src/api/generated/**".to_string(),
                    "src/api/client/**".to_string(),
                ],
            );
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(names.len(), 1, "only non-ignored files: {names:?}");
            assert!(names.contains(&"src/index.ts".to_string()));
        }

        #[test]
        fn leading_dot_ignore_patterns_exclude_matching_files() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let generated = dir.path().join("src").join("generated");
            std::fs::create_dir_all(&generated).unwrap();
            std::fs::write(generated.join("client.ts"), "export const api = {};").unwrap();

            let src = dir.path().join("src");
            std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();

            let config = make_config_with_ignores(
                dir.path().to_path_buf(),
                vec!["./src/generated/**".to_string()],
            );
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(names, vec!["src/index.ts"]);
        }

        #[test]
        fn default_ignore_patterns_exclude_node_modules_and_dist() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let nm = dir.path().join("node_modules").join("lodash");
            std::fs::create_dir_all(&nm).unwrap();
            std::fs::write(nm.join("lodash.js"), "module.exports = {};").unwrap();

            let dist = dir.path().join("dist");
            std::fs::create_dir_all(&dist).unwrap();
            std::fs::write(dist.join("bundle.js"), "// bundled").unwrap();

            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(names.len(), 1);
            assert!(names.contains(&"src/index.ts".to_string()));
        }

        #[test]
        fn default_ignore_patterns_exclude_root_build() {
            let dir = tempfile::tempdir().expect("create temp dir");

            let build = dir.path().join("build");
            std::fs::create_dir_all(&build).unwrap();
            std::fs::write(build.join("output.js"), "// built").unwrap();

            let nested_build = dir.path().join("src").join("build");
            std::fs::create_dir_all(&nested_build).unwrap();
            std::fs::write(nested_build.join("helper.ts"), "export const h = 1;").unwrap();

            let src = dir.path().join("src");
            std::fs::write(src.join("index.ts"), "export const x = 1;").unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert_eq!(
                names.len(),
                2,
                "root build/ excluded, nested kept: {names:?}"
            );
            assert!(names.contains(&"src/index.ts".to_string()));
            assert!(names.contains(&"src/build/helper.ts".to_string()));
        }

        /// Resolve a config then override the per-file size limit in bytes.
        fn make_config_with_max_file_size(
            root: PathBuf,
            max_file_size_bytes: Option<u64>,
        ) -> ResolvedConfig {
            let mut config = make_config(root, false);
            config.max_file_size_bytes = max_file_size_bytes;
            config
        }

        #[test]
        fn skips_files_over_max_file_size() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("small.ts"), "export const a = 1;").unwrap();
            std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();

            let config = make_config_with_max_file_size(dir.path().to_path_buf(), Some(1_000));
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(names.contains(&"src/small.ts".to_string()));
            assert!(
                !names.contains(&"src/huge.ts".to_string()),
                "a file over the size limit must not be discovered"
            );
        }

        #[test]
        fn declaration_files_exempt_from_size_skip() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("auto-imports.d.ts"), "x".repeat(5_000)).unwrap();
            std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();

            let config = make_config_with_max_file_size(dir.path().to_path_buf(), Some(1_000));
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&"src/auto-imports.d.ts".to_string()),
                "a large .d.ts is exempt from the skip (reachability root for global types)"
            );
            assert!(!names.contains(&"src/huge.ts".to_string()));
        }

        #[test]
        fn unlimited_size_keeps_large_files() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();

            let config = make_config_with_max_file_size(dir.path().to_path_buf(), None);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&"src/huge.ts".to_string()),
                "no limit keeps every file"
            );
        }

        #[test]
        fn skipped_file_recorded_in_workspace_diagnostics() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            std::fs::write(src.join("huge.ts"), "x".repeat(5_000)).unwrap();

            let config = make_config_with_max_file_size(dir.path().to_path_buf(), Some(1_000));
            let _ = discover_files(&config);

            let diagnostics = fallow_config::workspace_diagnostics_for(dir.path());
            let skipped: Vec<_> = diagnostics
                .iter()
                .filter(|d| {
                    matches!(
                        d.kind,
                        fallow_config::WorkspaceDiagnosticKind::SkippedLargeFile { .. }
                    )
                })
                .collect();
            assert_eq!(
                skipped.len(),
                1,
                "the skipped file is recorded in workspace diagnostics for JSON output"
            );
            assert!(skipped[0].path.ends_with("src/huge.ts"));
            assert!(
                matches!(
                    skipped[0].kind,
                    fallow_config::WorkspaceDiagnosticKind::SkippedLargeFile { size_bytes }
                        if size_bytes == 5_000
                ),
                "the recorded diagnostic carries the on-disk byte size"
            );
        }

        /// The skipped-source-dotdir entries the last walk on `root` recorded.
        fn dotdir_diagnostics(root: &Path) -> Vec<fallow_config::WorkspaceDiagnostic> {
            fallow_config::workspace_diagnostics_for(root)
                .into_iter()
                .filter(|d| {
                    matches!(
                        d.kind,
                        fallow_config::WorkspaceDiagnosticKind::SkippedSourceDotdir
                    )
                })
                .collect()
        }

        fn write_at(root: &Path, relative: &str, contents: &str) {
            let path = root.join(relative);
            std::fs::create_dir_all(path.parent().expect("has a parent")).unwrap();
            std::fs::write(path, contents).unwrap();
        }

        #[test]
        fn skipped_source_dotdir_recorded_in_workspace_diagnostics() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(
                dir.path(),
                ".claude/hooks/probe.mjs",
                "export const a = 1;\n",
            );
            write_at(dir.path(), "src/app.ts", "export const b = 2;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            let reported = dotdir_diagnostics(dir.path());
            assert_eq!(reported.len(), 1, "one skipped dotdir holds source files");
            assert!(reported[0].path.ends_with(".claude"));
            assert_eq!(reported[0].kind.id(), "skipped-source-dotdir");
            assert!(
                reported[0].message.contains("--root"),
                "message names the real remedy: {}",
                reported[0].message
            );
            assert!(
                names.contains(&"src/app.ts".to_string()),
                "traversal is unchanged for ordinary directories"
            );
            assert!(
                !names.contains(&".claude/hooks/probe.mjs".to_string()),
                "the diagnostic reports the skip, it does not change traversal"
            );
        }

        #[test]
        fn allowlisted_dotdir_is_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".storybook/main.ts", "export const a = 1;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(dotdir_diagnostics(dir.path()).is_empty());
            assert!(
                names.contains(&".storybook/main.ts".to_string()),
                "an allowlisted dotdir is still traversed"
            );
        }

        #[test]
        fn denylisted_dotdir_is_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".idea/workspace.ts", "export const a = 1;\n");
            write_at(dir.path(), ".husky/hook.js", "export const b = 2;\n");
            write_at(dir.path(), ".next/page.js", "export const c = 3;\n");
            write_at(dir.path(), ".pnpm/x.js", "export const d = 4;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "build caches, VCS and package-manager state never advise"
            );
        }

        #[test]
        fn scoped_dotdir_is_traversed_and_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(
                dir.path(),
                ".claude/hooks/probe.mjs",
                "export const a = 1;\n",
            );

            let config = make_config(dir.path().to_path_buf(), false);
            let scopes = [HiddenDirScope::new(
                dir.path().to_path_buf(),
                vec![".claude".to_owned()],
            )];
            let files = discover_files_with_additional_hidden_dirs(&config, &scopes);
            let names = file_names(&files, dir.path());

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "a plugin- or script-contributed scope is admitted, so nothing was skipped"
            );
            assert!(names.contains(&".claude/hooks/probe.mjs".to_string()));
        }

        #[test]
        fn ignore_patterns_silence_the_skipped_source_dotdir() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(
                dir.path(),
                ".claude/hooks/probe.mjs",
                "export const a = 1;\n",
            );

            let config =
                make_config_with_ignores(dir.path().to_path_buf(), vec![".claude/**".to_owned()]);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "the documented silencing route works"
            );
        }

        #[test]
        fn dotdir_without_source_files_is_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".claude/settings.json", "{}\n");
            write_at(dir.path(), ".claude/README.md", "# notes\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert!(dotdir_diagnostics(dir.path()).is_empty());
        }

        #[test]
        fn dotdir_source_at_scan_depth_limit_is_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".claude/a/b/deep.ts", "export const a = 1;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert_eq!(dotdir_diagnostics(dir.path()).len(), 1);
        }

        #[test]
        fn dotdir_source_below_scan_depth_limit_is_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(
                dir.path(),
                ".claude/a/b/c/deeper.ts",
                "export const a = 1;\n",
            );

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "the depth cap is real, so widening it stays a deliberate act"
            );
        }

        /// Mark `root` as a git worktree so the `ignore` crate applies the
        /// gitignore files below it. `require_git` is on by default, and it
        /// tests for the presence of `.git`, not for a valid object store.
        fn mark_as_git_repo(root: &Path) {
            std::fs::create_dir_all(root.join(".git")).expect("create .git marker");
        }

        #[test]
        fn gitignored_dotdir_contents_are_not_reported() {
            // The directory FORM (`.tooling/`) prunes the dotdir upstream of the
            // predicate, so these are the forms that reach it with every file
            // inside already ignored.
            for pattern in [".tooling/**", ".tooling/*", "**/.tooling/**", "*.ts"] {
                let dir = tempfile::tempdir().expect("create temp dir");
                mark_as_git_repo(dir.path());
                write_at(dir.path(), ".gitignore", &format!("{pattern}\n"));
                write_at(dir.path(), ".tooling/mod.ts", "export const a = 1;\n");

                let config = make_config(dir.path().to_path_buf(), false);
                let _ = discover_files(&config);

                assert!(
                    dotdir_diagnostics(dir.path()).is_empty(),
                    "gitignore pattern '{pattern}' excludes the contents, so neither \
                     advertised remedy would find anything there"
                );
            }
        }

        #[test]
        fn self_ignoring_dotdir_is_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            mark_as_git_repo(dir.path());
            write_at(dir.path(), ".toolcache/.gitignore", "*\n");
            write_at(dir.path(), ".toolcache/mod.ts", "export const a = 1;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "a cache directory that ignores itself has excluded its own contents"
            );
        }

        #[test]
        fn ungitignored_dotdir_in_a_git_repo_is_still_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            mark_as_git_repo(dir.path());
            write_at(dir.path(), ".gitignore", "dist/\n");
            write_at(dir.path(), ".tooling/mod.ts", "export const a = 1;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert_eq!(
                dotdir_diagnostics(dir.path()).len(),
                1,
                "the gitignore check must not swallow the case the diagnostic exists for"
            );
        }

        #[test]
        fn production_run_does_not_report_a_test_only_dotdir() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".qa/thing.test.ts", "export const a = 1;\n");
            write_at(dir.path(), ".qa/thing.stories.tsx", "export const b = 2;\n");

            let config = make_config(dir.path().to_path_buf(), true);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "a --production run would analyze none of those files, so the \
                 --root remedy would return nothing"
            );
        }

        #[test]
        fn production_run_still_reports_a_dotdir_with_production_source() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".qa/thing.test.ts", "export const a = 1;\n");
            write_at(dir.path(), ".qa/helper.ts", "export const b = 2;\n");

            let config = make_config(dir.path().to_path_buf(), true);
            let _ = discover_files(&config);

            assert_eq!(dotdir_diagnostics(dir.path()).len(), 1);
        }

        #[test]
        fn dotdir_with_only_generated_markup_is_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".lighthouseci/lhr-1.html", "<html></html>\n");
            write_at(dir.path(), ".styles/theme.css", ":root { color: red; }\n");
            write_at(dir.path(), ".gql/schema.graphql", "type Query { a: Int }\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "the message claims imports and exports are lost, and these have none"
            );
        }

        #[test]
        fn generated_tool_and_foreign_vcs_dotdirs_are_not_reported() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(dir.path(), ".astro/types.d.ts", "export {};\n");
            write_at(dir.path(), ".wxt/types/imports.d.ts", "export {};\n");
            write_at(dir.path(), ".yalc/pkg/index.js", "export const a = 1;\n");
            write_at(dir.path(), ".jj/repo/config.js", "export const b = 2;\n");
            write_at(dir.path(), ".svn/pristine/y.js", "export const c = 3;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            assert!(
                dotdir_diagnostics(dir.path()).is_empty(),
                "generated output and foreign VCS metadata are not first-party source"
            );
        }

        #[test]
        fn denylisted_dotdirs_do_not_consume_the_candidate_ceiling() {
            let dir = tempfile::tempdir().expect("create temp dir");
            // Sorted before the real candidate, and more of them than the
            // ceiling, so a cap applied before the name checks would hide it.
            for index in 0..(DOTDIR_SCAN_MAX_CANDIDATES + 8) {
                write_at(
                    dir.path(),
                    &format!("packages/pkg{index:03}/.turbo/blob.js"),
                    "export const a = 1;\n",
                );
            }
            write_at(dir.path(), "zz/.tooling/mod.ts", "export const b = 2;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            let reported = dotdir_diagnostics(dir.path());
            assert_eq!(reported.len(), 1, "{reported:?}");
            assert!(reported[0].path.ends_with(".tooling"));
        }

        #[test]
        fn one_pathological_dotdir_cannot_starve_the_rest() {
            let dir = tempfile::tempdir().expect("create temp dir");
            // Wide and shallow, no source: exhausts this candidate's own budget.
            for index in 0..(DOTDIR_SCAN_MAX_ENTRIES * 2) {
                write_at(dir.path(), &format!(".aaa-noise/f{index}.bin"), "x");
            }
            write_at(dir.path(), ".zzz-real/mod.ts", "export const a = 1;\n");

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);

            let reported = dotdir_diagnostics(dir.path());
            assert_eq!(reported.len(), 1, "{reported:?}");
            assert!(reported[0].path.ends_with(".zzz-real"));
        }

        #[test]
        fn repeat_walks_do_not_stack_skipped_source_dotdirs() {
            let dir = tempfile::tempdir().expect("create temp dir");
            write_at(
                dir.path(),
                ".claude/hooks/probe.mjs",
                "export const a = 1;\n",
            );

            let config = make_config(dir.path().to_path_buf(), false);
            let _ = discover_files(&config);
            let _ = discover_files(&config);

            assert_eq!(
                dotdir_diagnostics(dir.path()).len(),
                1,
                "each walk replaces its own root's source-discovery set"
            );
        }

        #[test]
        fn skips_large_one_line_js_as_minified_generated_output() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            let asset = src.join("index-abc123.js");
            std::fs::write(&asset, "x".repeat(MINIFIED_FILE_SKIP_BYTES as usize + 1)).unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                !names.contains(&"src/index-abc123.js".to_string()),
                "large one-line JS assets should be skipped before parsing"
            );

            let diagnostics = fallow_config::workspace_diagnostics_for(dir.path());
            assert!(
                diagnostics.iter().any(|diag| {
                    diag.path.ends_with("src/index-abc123.js")
                        && matches!(
                            diag.kind,
                            fallow_config::WorkspaceDiagnosticKind::SkippedMinifiedFile { .. }
                        )
                }),
                "the skipped minified asset is recorded for JSON output: {diagnostics:?}"
            );
        }

        #[test]
        fn unlimited_size_keeps_large_one_line_js() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            let asset = src.join("index-abc123.js");
            std::fs::write(&asset, "x".repeat(MINIFIED_FILE_SKIP_BYTES as usize + 1)).unwrap();

            let config = make_config_with_max_file_size(dir.path().to_path_buf(), None);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&"src/index-abc123.js".to_string()),
                "--max-file-size 0 should opt out of generated JS skipping"
            );
        }

        #[test]
        fn keeps_large_multiline_js() {
            let dir = tempfile::tempdir().expect("create temp dir");
            let src = dir.path().join("src");
            std::fs::create_dir_all(&src).unwrap();
            let asset = src.join("handwritten.js");
            let mut content = String::new();
            while content.len() <= MINIFIED_FILE_SKIP_BYTES as usize + 1 {
                content.push_str("export const value = 1;\n");
            }
            std::fs::write(&asset, content).unwrap();

            let config = make_config(dir.path().to_path_buf(), false);
            let files = discover_files(&config);
            let names = file_names(&files, dir.path());

            assert!(
                names.contains(&"src/handwritten.js".to_string()),
                "large multiline JS should not be treated as a generated minified asset"
            );
        }
    }
}