loctree 0.8.16

Structural code intelligence for AI agents. Scan once, query everything.
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
use std::collections::{HashMap, HashSet};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::thread;

use globset::GlobSet;
use serde_json::json;

use crate::args::ParsedArgs;
use crate::fs_utils::{GitIgnoreChecker, gather_files};
use crate::snapshot::Snapshot;
use crate::types::{
    ColorMode, ExportIndex, FileAnalysis, ImportKind, Options, OutputMode, PayloadMap,
};

use super::classify::is_dev_file;
use super::resolvers::{
    ExtractedResolverConfig, TsPathResolver, resolve_js_relative, resolve_python_absolute,
    resolve_python_relative,
};
use super::scan::{
    analyze_file, matches_focus, resolve_event_constants_across_files, strip_excluded,
};
use super::{DupLocation, DupSeverity, RankedDup, coverage::CommandUsage, twins};

pub struct ScanConfig<'a> {
    pub roots: &'a [PathBuf],
    pub parsed: &'a ParsedArgs,
    pub extensions: Option<HashSet<String>>,
    pub focus_set: &'a Option<GlobSet>,
    pub exclude_set: &'a Option<GlobSet>,
    pub ignore_exact: HashSet<String>,
    pub ignore_prefixes: Vec<String>,
    pub py_stdlib: &'a HashSet<String>,
    /// Cached file analyses from previous snapshot for incremental scanning
    pub cached_analyses: Option<&'a HashMap<String, crate::types::FileAnalysis>>,
    /// Force collection of graph edges (for Init mode snapshots)
    pub collect_edges: bool,
    /// Custom Tauri command macros from .loctree/config.toml
    pub custom_command_macros: &'a [String],
    /// Command detection exclusions (DOM/invoke/invalid) from config
    pub command_detection: crate::analyzer::ast_js::CommandDetectionConfig,
}

#[derive(Clone)]
pub struct RootContext {
    pub root_path: PathBuf,
    pub options: Options,
    pub analyses: Vec<FileAnalysis>,
    pub export_index: ExportIndex,
    pub dynamic_summary: Vec<(String, Vec<String>)>,
    pub cascades: Vec<(String, String)>,
    pub filtered_ranked: Vec<RankedDup>,
    pub graph_edges: Vec<(String, String, String)>,
    pub loc_map: HashMap<String, usize>,
    pub languages: HashSet<String>,
    pub tsconfig_summary: serde_json::Value,
    pub calls_with_generics: Vec<serde_json::Value>,
    pub renamed_handlers: Vec<serde_json::Value>,
    pub barrels: Vec<BarrelInfo>,
}

impl Default for RootContext {
    fn default() -> Self {
        Self {
            root_path: PathBuf::new(),
            options: Options::default(),
            analyses: Vec::new(),
            export_index: HashMap::new(),
            dynamic_summary: Vec::new(),
            cascades: Vec::new(),
            filtered_ranked: Vec::new(),
            graph_edges: Vec::new(),
            loc_map: HashMap::new(),
            languages: HashSet::new(),
            tsconfig_summary: serde_json::json!({}),
            calls_with_generics: Vec::new(),
            renamed_handlers: Vec::new(),
            barrels: Vec::new(),
        }
    }
}

#[derive(Clone)]
pub struct BarrelInfo {
    pub path: String,
    pub module_id: String,
    pub reexport_count: usize,
    pub target_count: usize,
    pub mixed: bool,
    pub targets: Vec<String>,
}

#[derive(Clone)]
pub struct ScanResults {
    pub contexts: Vec<RootContext>,
    pub global_fe_commands: CommandUsage,
    pub global_be_commands: CommandUsage,
    pub global_fe_payloads: PayloadMap,
    pub global_be_payloads: PayloadMap,
    pub global_analyses: Vec<FileAnalysis>,
    /// Extracted TypeScript resolver configuration for caching in snapshot
    pub ts_resolver_config: Option<ExtractedResolverConfig>,
    /// Python root paths for caching in snapshot
    pub py_roots: Vec<String>,
}

/// Result from scanning a single root (used for parallel processing)
struct SingleRootResult {
    context: RootContext,
    fe_commands: CommandUsage,
    be_commands: CommandUsage,
    fe_payloads: PayloadMap,
    be_payloads: PayloadMap,
    analyses: Vec<FileAnalysis>,
    /// Extracted TS resolver config (if tsconfig found in this root)
    ts_resolver_config: Option<ExtractedResolverConfig>,
    /// Python roots used for this root scan
    py_roots: Vec<String>,
}

/// Maximum number of roots to scan in parallel (bounded parallelism)
const MAX_PARALLEL_ROOTS: usize = 4;

pub fn scan_roots(cfg: ScanConfig<'_>) -> io::Result<ScanResults> {
    // For single root, skip parallelization overhead
    if cfg.roots.len() <= 1 {
        return scan_roots_sequential(cfg);
    }

    // Parallel scanning with bounded concurrency
    let results: Mutex<Vec<SingleRootResult>> = Mutex::new(Vec::new());
    let errors: Mutex<Vec<(PathBuf, io::Error)>> = Mutex::new(Vec::new());

    thread::scope(|s| {
        // Process roots in chunks for bounded parallelism
        for chunk in cfg.roots.chunks(MAX_PARALLEL_ROOTS) {
            let handles: Vec<_> = chunk
                .iter()
                .map(|root_path| s.spawn(|| scan_single_root(root_path, &cfg)))
                .collect();

            for (handle, root_path) in handles.into_iter().zip(chunk.iter()) {
                match handle.join() {
                    Ok(Ok(result)) => {
                        let mut guard = match results.lock() {
                            Ok(g) => g,
                            Err(poisoned) => {
                                eprintln!(
                                    "[loctree][warn] result mutex poisoned; salvaging partial data"
                                );
                                poisoned.into_inner()
                            }
                        };
                        guard.push(result);
                    }
                    Ok(Err(e)) => {
                        let mut guard = match errors.lock() {
                            Ok(g) => g,
                            Err(poisoned) => {
                                eprintln!(
                                    "[loctree][warn] error mutex poisoned; salvaging partial errors"
                                );
                                poisoned.into_inner()
                            }
                        };
                        guard.push((root_path.clone(), e));
                    }
                    Err(_) => {
                        let mut guard = match errors.lock() {
                            Ok(g) => g,
                            Err(poisoned) => {
                                eprintln!(
                                    "[loctree][warn] error mutex poisoned; salvaging partial errors"
                                );
                                poisoned.into_inner()
                            }
                        };
                        guard.push((root_path.clone(), io::Error::other("thread panic")));
                    }
                }
            }
        }
    });

    // Check for errors
    let errors = match errors.into_inner() {
        Ok(v) => v,
        Err(poisoned) => {
            eprintln!(
                "[loctree][warn] error mutex poisoned during unwrap; returning partial errors"
            );
            poisoned.into_inner()
        }
    };
    if !errors.is_empty() {
        let first_err = errors.into_iter().next().unwrap();
        return Err(io::Error::new(
            first_err.1.kind(),
            format!("Error scanning {}: {}", first_err.0.display(), first_err.1),
        ));
    }

    // Merge results from all roots
    let all_results = match results.into_inner() {
        Ok(v) => v,
        Err(poisoned) => {
            eprintln!(
                "[loctree][warn] result mutex poisoned during unwrap; returning partial results"
            );
            poisoned.into_inner()
        }
    };
    let mut contexts: Vec<RootContext> = Vec::new();
    let mut global_fe_commands: CommandUsage = HashMap::new();
    let mut global_be_commands: CommandUsage = HashMap::new();
    let mut global_fe_payloads: PayloadMap = HashMap::new();
    let mut global_be_payloads: PayloadMap = HashMap::new();
    let mut global_analyses: Vec<FileAnalysis> = Vec::new();
    let mut ts_resolver_config: Option<ExtractedResolverConfig> = None;
    let mut all_py_roots: Vec<String> = Vec::new();

    for result in all_results {
        contexts.push(result.context);
        for (name, entries) in result.fe_commands {
            global_fe_commands.entry(name).or_default().extend(entries);
        }
        for (name, entries) in result.be_commands {
            global_be_commands.entry(name).or_default().extend(entries);
        }
        for (name, entries) in result.fe_payloads {
            global_fe_payloads.entry(name).or_default().extend(entries);
        }
        for (name, entries) in result.be_payloads {
            global_be_payloads.entry(name).or_default().extend(entries);
        }
        global_analyses.extend(result.analyses);

        // Use first non-None ts_resolver_config (typically from project root)
        if ts_resolver_config.is_none() && result.ts_resolver_config.is_some() {
            ts_resolver_config = result.ts_resolver_config;
        }
        all_py_roots.extend(result.py_roots);
    }

    // Deduplicate py_roots
    all_py_roots.sort();
    all_py_roots.dedup();

    Ok(ScanResults {
        contexts,
        global_fe_commands,
        global_be_commands,
        global_fe_payloads,
        global_be_payloads,
        global_analyses,
        ts_resolver_config,
        py_roots: all_py_roots,
    })
}

/// Sequential scanning (original implementation, used for single root)
fn scan_roots_sequential(cfg: ScanConfig<'_>) -> io::Result<ScanResults> {
    let mut contexts: Vec<RootContext> = Vec::new();
    let mut global_fe_commands: CommandUsage = HashMap::new();
    let mut global_be_commands: CommandUsage = HashMap::new();
    let mut global_fe_payloads: PayloadMap = HashMap::new();
    let mut global_be_payloads: PayloadMap = HashMap::new();
    let mut global_analyses: Vec<FileAnalysis> = Vec::new();
    let mut ts_resolver_config: Option<ExtractedResolverConfig> = None;
    let mut all_py_roots: Vec<String> = Vec::new();

    for root_path in cfg.roots.iter() {
        let result = scan_single_root(root_path, &cfg)?;

        contexts.push(result.context);
        for (name, entries) in result.fe_commands {
            global_fe_commands.entry(name).or_default().extend(entries);
        }
        for (name, entries) in result.be_commands {
            global_be_commands.entry(name).or_default().extend(entries);
        }
        for (name, entries) in result.fe_payloads {
            global_fe_payloads.entry(name).or_default().extend(entries);
        }
        for (name, entries) in result.be_payloads {
            global_be_payloads.entry(name).or_default().extend(entries);
        }
        global_analyses.extend(result.analyses);

        // Use first non-None ts_resolver_config (typically from project root)
        if ts_resolver_config.is_none() && result.ts_resolver_config.is_some() {
            ts_resolver_config = result.ts_resolver_config;
        }
        all_py_roots.extend(result.py_roots);
    }

    // Deduplicate py_roots
    all_py_roots.sort();
    all_py_roots.dedup();

    Ok(ScanResults {
        contexts,
        global_fe_commands,
        global_be_commands,
        global_fe_payloads,
        global_be_payloads,
        global_analyses,
        ts_resolver_config,
        py_roots: all_py_roots,
    })
}

/// Scan a single root directory and return results
fn scan_single_root(
    root_path: &std::path::Path,
    cfg: &ScanConfig<'_>,
) -> io::Result<SingleRootResult> {
    let ignore_matchers =
        crate::fs_utils::build_ignore_matchers(&cfg.parsed.ignore_patterns, root_path);
    let root_canon = root_path
        .canonicalize()
        .unwrap_or_else(|_| root_path.to_path_buf());

    // Ensure core language extensions are always included even if user provided a custom list
    let extensions = cfg.extensions.clone().map(|mut set| {
        for lang_ext in ["go", "dart"] {
            set.insert(lang_ext.to_string());
        }
        set
    });

    let options = Options {
        extensions,
        ignore_paths: ignore_matchers.ignore_paths,
        ignore_globs: ignore_matchers.ignore_globs,
        // --scan-all should ignore gitignore and include everything
        use_gitignore: cfg.parsed.use_gitignore && !cfg.parsed.scan_all,
        max_depth: cfg.parsed.max_depth,
        color: cfg.parsed.color,
        output: cfg.parsed.output,
        summary: cfg.parsed.summary,
        summary_limit: cfg.parsed.summary_limit,
        summary_only: cfg.parsed.summary_only,
        show_hidden: cfg.parsed.show_hidden,
        show_ignored: false, // Only used in tree mode
        loc_threshold: cfg.parsed.loc_threshold,
        analyze_limit: cfg.parsed.analyze_limit,
        report_path: cfg.parsed.report_path.clone(),
        serve: cfg.parsed.serve,
        editor_cmd: cfg.parsed.editor_cmd.clone(),
        max_graph_nodes: cfg.parsed.max_graph_nodes,
        max_graph_edges: cfg.parsed.max_graph_edges,
        verbose: cfg.parsed.verbose,
        scan_all: cfg.parsed.scan_all,
        symbol: cfg.parsed.symbol.clone(),
        impact: cfg.parsed.impact.clone(),
        find_artifacts: false, // Only used in tree mode
    };

    if options.verbose {
        eprintln!("[loctree][debug] analyzing root {}", root_path.display());
    }

    let git_checker = if options.use_gitignore {
        GitIgnoreChecker::new(root_path)
    } else {
        None
    };

    let ts_resolver = TsPathResolver::from_tsconfig(&root_canon);
    let py_roots: Vec<PathBuf> = build_py_roots(&root_canon, &cfg.parsed.py_roots);

    // Extract resolver config for caching in snapshot
    let extracted_ts_config = ts_resolver.as_ref().map(|r| r.extract_config());
    let py_roots_strings: Vec<String> = py_roots
        .iter()
        .map(|p| p.to_string_lossy().to_string())
        .collect();

    let mut files = Vec::new();
    let mut visited = HashSet::new();
    gather_files(
        root_path,
        &options,
        0,
        git_checker.as_ref(),
        &mut visited,
        &mut files,
    )?;

    if let (Some(focus), Some(exclude)) = (cfg.focus_set, cfg.exclude_set) {
        let mut overlapping = Vec::new();
        for path in &files {
            let canon = path.canonicalize().unwrap_or_else(|_| path.clone());
            if focus.is_match(&canon) && exclude.is_match(&canon) {
                overlapping.push(canon.display().to_string());
                if overlapping.len() >= 5 {
                    break;
                }
            }
        }
        if !overlapping.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                format!(
                    "--focus and --exclude-report overlap on: {}",
                    overlapping.join(", ")
                ),
            ));
        }
    }

    let mut analyses = Vec::new();
    let mut export_index: ExportIndex = HashMap::new();
    let mut reexport_edges: Vec<(String, Option<String>)> = Vec::new();
    let mut dynamic_summary: Vec<(String, Vec<String>)> = Vec::new();
    let mut fe_commands: CommandUsage = HashMap::new();
    let mut be_commands: CommandUsage = HashMap::new();
    let mut fe_payloads: PayloadMap = HashMap::new();
    let mut be_payloads: PayloadMap = HashMap::new();
    let mut graph_edges: Vec<(String, String, String)> = Vec::new();
    let mut loc_map: HashMap<String, usize> = HashMap::new();
    let mut languages: HashSet<String> = HashSet::new();
    let mut barrels: Vec<BarrelInfo> = Vec::new();

    let mut cached_hits = 0usize;
    let mut fresh_scans = 0usize;

    for file in files {
        // Get current file mtime and size for incremental scanning
        // Using both mtime + size avoids FP on fast edits (sub-second granularity)
        let metadata = std::fs::metadata(&file).ok();
        let current_mtime = metadata
            .as_ref()
            .and_then(|m| m.modified().ok())
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let current_size = metadata.as_ref().map(|m| m.len()).unwrap_or(0);

        // Compute relative path for cache lookup
        let rel_path = file
            .strip_prefix(&root_canon)
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| file.to_string_lossy().to_string())
            .replace('\\', "/");

        // Check if we can use cached analysis (mtime + size must both match)
        let analysis = if let Some(cache) = cfg.cached_analyses {
            if let Some(cached) = cache.get(&rel_path) {
                let mtime_matches = cached.mtime > 0 && cached.mtime == current_mtime;
                let size_matches = cached.size == current_size;
                if mtime_matches && size_matches {
                    // File unchanged - reuse cached analysis
                    cached_hits += 1;
                    cached.clone()
                } else {
                    // File changed - re-analyze
                    fresh_scans += 1;
                    match analyze_file(
                        &file,
                        &root_canon,
                        options.extensions.as_ref(),
                        ts_resolver.as_ref(),
                        &py_roots,
                        cfg.py_stdlib,
                        options.symbol.as_deref(),
                        cfg.custom_command_macros,
                        &cfg.command_detection,
                    ) {
                        Ok(mut a) => {
                            a.mtime = current_mtime;
                            a.size = current_size;
                            a
                        }
                        Err(e) if e.kind() == io::ErrorKind::InvalidData => {
                            // Skip binary files or invalid UTF-8
                            continue;
                        }
                        Err(e) => return Err(e),
                    }
                }
            } else {
                // New file - analyze
                fresh_scans += 1;
                match analyze_file(
                    &file,
                    &root_canon,
                    options.extensions.as_ref(),
                    ts_resolver.as_ref(),
                    &py_roots,
                    cfg.py_stdlib,
                    options.symbol.as_deref(),
                    cfg.custom_command_macros,
                    &cfg.command_detection,
                ) {
                    Ok(mut a) => {
                        a.mtime = current_mtime;
                        a.size = current_size;
                        a
                    }
                    Err(e) if e.kind() == io::ErrorKind::InvalidData => {
                        // Skip binary files or invalid UTF-8
                        continue;
                    }
                    Err(e) => return Err(e),
                }
            }
        } else {
            // No cache - fresh scan
            fresh_scans += 1;
            match analyze_file(
                &file,
                &root_canon,
                options.extensions.as_ref(),
                ts_resolver.as_ref(),
                &py_roots,
                cfg.py_stdlib,
                options.symbol.as_deref(),
                cfg.custom_command_macros,
                &cfg.command_detection,
            ) {
                Ok(mut a) => {
                    a.mtime = current_mtime;
                    a.size = current_size;
                    a
                }
                Err(e) if e.kind() == io::ErrorKind::InvalidData => {
                    // Skip binary files or invalid UTF-8
                    continue;
                }
                Err(e) => return Err(e),
            }
        };
        let abs_for_match = root_canon.join(&analysis.path);
        let is_excluded_for_commands = cfg
            .exclude_set
            .as_ref()
            .map(|set| {
                let canon = abs_for_match
                    .canonicalize()
                    .unwrap_or_else(|_| abs_for_match.clone());
                set.is_match(&canon)
            })
            .unwrap_or(false);

        loc_map.insert(analysis.path.clone(), analysis.loc);
        // Exclude test fixtures from export index to avoid false positive duplicates
        let is_test_fixture = super::classify::should_exclude_from_reports(&analysis.path);
        if !is_test_fixture {
            for exp in &analysis.exports {
                if exp.kind == "reexport" {
                    continue;
                }
                if exp.export_type == "default" {
                    continue;
                }
                let name_lc = exp.name.to_lowercase();
                let is_decl = [".d.ts", ".d.tsx", ".d.mts", ".d.cts"]
                    .iter()
                    .any(|ext| analysis.path.ends_with(ext));
                if is_decl && name_lc == "default" {
                    continue;
                }
                let ignored = cfg.ignore_exact.contains(&name_lc)
                    || cfg.ignore_prefixes.iter().any(|p| name_lc.starts_with(p));
                if ignored {
                    continue;
                }
                export_index
                    .entry(exp.name.clone())
                    .or_default()
                    .push(analysis.path.clone());
            }
        }
        for re in &analysis.reexports {
            // Re-export edges are useful for cycle detection and barrel awareness.
            // If the parser didn't resolve the target, try to resolve it here using the same
            // resolution logic as imports (relative or tsconfig alias).
            let mut resolved_target = re.resolved.clone();
            if resolved_target.is_none() {
                let spec = &re.source;
                let ext = file
                    .extension()
                    .and_then(|e| e.to_str())
                    .map(|s| s.to_lowercase())
                    .unwrap_or_default();
                resolved_target = if spec.starts_with('.') {
                    resolve_js_relative(&file, root_path, spec, options.extensions.as_ref())
                } else if matches!(
                    ext.as_str(),
                    "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" | "svelte" | "vue"
                ) {
                    ts_resolver
                        .as_ref()
                        .and_then(|r| r.resolve(spec, options.extensions.as_ref()))
                } else {
                    None
                };
            }

            reexport_edges.push((analysis.path.clone(), resolved_target.clone()));
            let collect_edges = cfg.collect_edges
                || (cfg.parsed.graph && options.report_path.is_some())
                || options.impact.is_some();
            if collect_edges && let Some(target) = &resolved_target {
                graph_edges.push((
                    analysis.path.clone(),
                    target.clone(),
                    "reexport".to_string(),
                ));
            }
        }
        if !analysis.dynamic_imports.is_empty() {
            dynamic_summary.push((analysis.path.clone(), analysis.dynamic_imports.clone()));
        }
        let should_collect_edges = cfg.collect_edges
            || (cfg.parsed.graph && options.report_path.is_some())
            || options.impact.is_some();
        if should_collect_edges {
            let ext = file
                .extension()
                .and_then(|e| e.to_str())
                .map(|s| s.to_lowercase())
                .unwrap_or_default();
            for imp in &analysis.imports {
                let resolved = imp.resolved_path.clone().or_else(|| match ext.as_str() {
                    "py" => {
                        if imp.source.starts_with('.') {
                            resolve_python_relative(
                                &imp.source,
                                &file,
                                root_path,
                                options.extensions.as_ref(),
                            )
                        } else {
                            resolve_python_absolute(
                                &imp.source,
                                &py_roots,
                                root_path,
                                options.extensions.as_ref(),
                            )
                        }
                    }
                    "ts" | "tsx" | "js" | "jsx" | "mjs" | "cjs" | "css" | "svelte" | "vue" => {
                        if imp.source.starts_with('.') {
                            resolve_js_relative(
                                &file,
                                root_path,
                                &imp.source,
                                options.extensions.as_ref(),
                            )
                        } else {
                            ts_resolver
                                .as_ref()
                                .and_then(|r| r.resolve(&imp.source, options.extensions.as_ref()))
                        }
                    }
                    _ => None,
                });
                if let Some(target) = resolved {
                    let label = if imp.is_mod_declaration {
                        // Rust `mod foo;` declarations create parent->child module relationships
                        // These are NOT import edges and should not contribute to cycle detection
                        "mod"
                    } else if imp.is_type_checking {
                        "type_import"
                    } else if imp.is_lazy {
                        "lazy_import"
                    } else {
                        match imp.kind {
                            ImportKind::Static | ImportKind::Type | ImportKind::SideEffect => {
                                "import"
                            }
                            ImportKind::Dynamic => "dynamic_import",
                        }
                    };
                    if label == "type_import" {
                        continue;
                    }
                    // Use full paths for edges (slice module needs exact paths)
                    // Note: normalize_module_id strips /index suffix which breaks slice lookups
                    graph_edges.push((analysis.path.clone(), target, label.to_string()));
                }
            }
        }
        // Exclude test fixtures from command analysis to avoid false positives
        let is_test_fixture = super::classify::should_exclude_from_reports(&analysis.path);
        if !is_excluded_for_commands && !is_test_fixture {
            for call in &analysis.command_calls {
                if call.name.contains('.') {
                    // VSCode-style command IDs (e.g. "loctree.analyzeImpact") are not Tauri invokes.
                    continue;
                }
                let mut key = call.name.clone();
                if let Some(stripped) = key.strip_suffix("_command") {
                    key = stripped.to_string();
                } else if let Some(stripped) = key.strip_suffix("_cmd") {
                    key = stripped.to_string();
                }
                fe_commands.entry(key.clone()).or_default().push((
                    analysis.path.clone(),
                    call.line,
                    call.name.clone(),
                ));
                fe_payloads.entry(key).or_default().push((
                    analysis.path.clone(),
                    call.line,
                    call.payload.clone(),
                ));
            }
            for handler in &analysis.command_handlers {
                let mut key = handler
                    .exposed_name
                    .as_ref()
                    .unwrap_or(&handler.name)
                    .clone();
                if let Some(stripped) = key.strip_suffix("_command") {
                    key = stripped.to_string();
                } else if let Some(stripped) = key.strip_suffix("_cmd") {
                    key = stripped.to_string();
                }
                be_payloads.entry(key.clone()).or_default().push((
                    analysis.path.clone(),
                    handler.line,
                    handler.payload.clone(),
                ));
                be_commands.entry(key).or_default().push((
                    analysis.path.clone(),
                    handler.line,
                    handler.name.clone(),
                ));
            }
        }
        languages.insert(analysis.language.clone());
        analyses.push(analysis);
    }

    let mut barrel_map: HashMap<String, BarrelInfo> = HashMap::new();
    for analysis in &analyses {
        if analysis.reexports.is_empty() {
            continue;
        }
        if !is_index_like(&analysis.path) {
            continue;
        }

        let mut targets: Vec<String> = analysis
            .reexports
            .iter()
            .filter_map(|r| r.resolved.clone().or_else(|| Some(r.source.clone())))
            .map(|t| normalize_module_id(&t).as_key())
            .collect();
        targets.sort();
        targets.dedup();

        let module_id = normalize_module_id(&analysis.path).as_key();
        let entry = barrel_map.entry(module_id.clone()).or_insert(BarrelInfo {
            path: analysis.path.clone(),
            module_id,
            reexport_count: 0,
            target_count: 0,
            mixed: false,
            targets: Vec::new(),
        });

        entry.reexport_count += analysis.reexports.len();
        let has_own_defs = analysis.exports.iter().any(|e| e.kind != "reexport");
        entry.mixed |= has_own_defs;
        entry.targets.extend(targets);
    }
    for (_, mut info) in barrel_map {
        info.targets.sort();
        info.targets.dedup();
        info.target_count = info.targets.len();
        barrels.push(info);
    }
    barrels.sort_by(|a, b| a.path.cmp(&b.path));

    let duplicate_exports: Vec<_> = export_index
        .iter()
        .filter(|(_, files)| files.len() > 1)
        .map(|(name, files)| (name.clone(), files.clone()))
        .collect();

    let reexport_files: HashSet<String> = analyses
        .iter()
        .filter(|a| !a.reexports.is_empty())
        .map(|a| a.path.clone())
        .collect();

    resolve_event_constants_across_files(&mut analyses);

    let mut cascades = Vec::new();
    for (from, resolved) in &reexport_edges {
        if let Some(target) = resolved
            && reexport_files.contains(target)
        {
            cascades.push((from.clone(), target.clone()));
        }
    }

    // Build lookup map for export line numbers: (file_path, export_name) -> line
    let export_lines: std::collections::HashMap<(String, String), Option<usize>> = analyses
        .iter()
        .flat_map(|a| {
            a.exports
                .iter()
                .map(|e| ((a.path.clone(), e.name.clone()), e.line))
        })
        .collect();

    let mut ranked_dups: Vec<RankedDup> = Vec::new();
    for (name, files) in &duplicate_exports {
        // Skip generic method names that are expected to be duplicated across different types
        if is_generic_method(name) {
            continue;
        }

        let all_rs = files.iter().all(|f| f.ends_with(".rs"));
        let all_d_ts = files.iter().all(|f| {
            f.ends_with(".d.ts")
                || f.ends_with(".d.tsx")
                || f.ends_with(".d.mts")
                || f.ends_with(".d.cts")
        });
        // Keep the old filter for backward compatibility (though now redundant)
        if (name == "new" && all_rs) || (name == "default" && all_d_ts) {
            continue;
        }
        let dev_count = files.iter().filter(|f| is_dev_file(f)).count();
        let prod_count = files.len().saturating_sub(dev_count);
        let score = prod_count * 2 + dev_count;
        let canonical = files
            .iter()
            .find(|f| !is_dev_file(f))
            .cloned()
            .unwrap_or_else(|| files[0].clone());
        let canonical_line = export_lines
            .get(&(canonical.clone(), name.clone()))
            .copied()
            .flatten();
        let locations: Vec<DupLocation> = files
            .iter()
            .map(|f| DupLocation {
                file: f.clone(),
                line: export_lines
                    .get(&(f.clone(), name.clone()))
                    .copied()
                    .flatten(),
            })
            .collect();
        let mut refactors: Vec<String> =
            files.iter().filter(|f| *f != &canonical).cloned().collect();
        refactors.sort();

        // Extract packages for severity
        let packages: Vec<String> = files
            .iter()
            .map(|f| extract_package(f))
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();
        let cross_lang = is_cross_lang(files);
        let (severity, reason) = compute_severity(name, files, &packages);

        ranked_dups.push(RankedDup {
            name: name.clone(),
            files: files.clone(),
            locations,
            score,
            prod_count,
            dev_count,
            canonical,
            canonical_line,
            refactors,
            severity,
            is_cross_lang: cross_lang,
            packages,
            reason,
        });
    }
    ranked_dups.sort_by(|a, b| {
        b.score
            .cmp(&a.score)
            .then(b.files.len().cmp(&a.files.len()))
    });

    let mut filtered_ranked: Vec<RankedDup> = Vec::new();
    for dup in ranked_dups.into_iter() {
        let kept_files = strip_excluded(&dup.files, cfg.exclude_set);
        if kept_files.len() <= 1 {
            continue;
        }
        if !matches_focus(&kept_files, cfg.focus_set) {
            continue;
        }
        let canonical = if kept_files.contains(&dup.canonical) {
            dup.canonical.clone()
        } else {
            kept_files
                .iter()
                .find(|f| !is_dev_file(f))
                .cloned()
                .unwrap_or_else(|| kept_files[0].clone())
        };
        let dev_count = kept_files.iter().filter(|f| is_dev_file(f)).count();
        let prod_count = kept_files.len().saturating_sub(dev_count);
        let score = prod_count * 2 + dev_count;
        // Filter locations to only kept files
        let locations: Vec<DupLocation> = dup
            .locations
            .into_iter()
            .filter(|loc| kept_files.contains(&loc.file))
            .collect();
        let canonical_line = locations
            .iter()
            .find(|loc| loc.file == canonical)
            .and_then(|loc| loc.line);
        let mut refactors: Vec<String> = kept_files
            .iter()
            .filter(|f| *f != &canonical)
            .cloned()
            .collect();
        refactors.sort();

        // Recompute severity for filtered files
        let packages: Vec<String> = kept_files
            .iter()
            .map(|f| extract_package(f))
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();
        let cross_lang = is_cross_lang(&kept_files);
        let (severity, reason) = compute_severity(&dup.name, &kept_files, &packages);

        filtered_ranked.push(RankedDup {
            name: dup.name,
            files: kept_files,
            locations,
            score,
            prod_count,
            dev_count,
            canonical,
            canonical_line,
            refactors,
            severity,
            is_cross_lang: cross_lang,
            packages,
            reason,
        });
    }
    // Sort by severity (descending), then by score
    filtered_ranked.sort_by(|a, b| {
        let sev_cmp = (b.severity as u8).cmp(&(a.severity as u8));
        if sev_cmp != std::cmp::Ordering::Equal {
            sev_cmp
        } else {
            b.score
                .cmp(&a.score)
                .then(b.files.len().cmp(&a.files.len()))
        }
    });

    let tsconfig_summary = super::tsconfig::summarize_tsconfig(root_path, &analyses);

    // Log incremental scan stats if verbose
    if options.verbose && cfg.cached_analyses.is_some() {
        let total = cached_hits + fresh_scans;
        eprintln!(
            "[loctree][incremental] {} cached, {} fresh ({} total files)",
            cached_hits, fresh_scans, total
        );
    }

    let mut calls_with_generics = Vec::new();
    for analysis in &analyses {
        for call in &analysis.command_calls {
            if let Some(gt) = &call.generic_type {
                calls_with_generics.push(json!({
                    "name": call.name,
                    "path": analysis.path,
                    "line": call.line,
                    "genericType": gt,
                }));
            }
        }
    }

    let mut renamed_handlers = Vec::new();
    for analysis in &analyses {
        for handler in &analysis.command_handlers {
            if let Some(exposed) = &handler.exposed_name
                && exposed != &handler.name
            {
                renamed_handlers.push(json!({
                    "path": analysis.path,
                    "line": handler.line,
                    "name": handler.name,
                    "exposedName": exposed,
                }));
            }
        }
    }

    let context = RootContext {
        root_path: root_path.to_path_buf(),
        options,
        analyses: analyses.clone(),
        export_index,
        dynamic_summary,
        cascades,
        filtered_ranked,
        graph_edges,
        loc_map,
        languages,
        tsconfig_summary,
        calls_with_generics,
        renamed_handlers,
        barrels,
    };

    Ok(SingleRootResult {
        context,
        fe_commands,
        be_commands,
        fe_payloads,
        be_payloads,
        analyses,
        ts_resolver_config: extracted_ts_config,
        py_roots: py_roots_strings,
    })
}

fn is_index_like(path: &str) -> bool {
    let lowered = path.to_lowercase();
    lowered.ends_with("/index.ts")
        || lowered.ends_with("/index.tsx")
        || lowered.ends_with("/index.js")
        || lowered.ends_with("/index.jsx")
        || lowered.ends_with("/index.mjs")
        || lowered.ends_with("/index.cjs")
        || lowered.ends_with("/index.rs")
        || lowered.ends_with("/index.svelte")
        || lowered.ends_with("/index.vue")
}

/// Normalized module identifier with language context
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NormalizedModule {
    /// Path without extension and /index suffix
    pub path: String,
    /// Language/extension identifier (ts, tsx, js, jsx, mjs, cjs, rs, py, css)
    pub lang: String,
}

impl NormalizedModule {
    /// Format as string for use as map key: "{path}:{lang}"
    pub fn as_key(&self) -> String {
        format!("{}:{}", self.path, self.lang)
    }

    /// Parse from key string created by as_key()
    pub fn from_key(key: &str) -> Option<Self> {
        let parts: Vec<&str> = key.rsplitn(2, ':').collect();
        if parts.len() == 2 {
            Some(NormalizedModule {
                path: parts[1].to_string(),
                lang: parts[0].to_string(),
            })
        } else {
            None
        }
    }
}

/// Normalize module identifier preserving language context
///
/// - TS/JS family (`ts`, `tsx`, `js`, `jsx`, `mjs`, `cjs`) collapses to `ts`
///   to reduce FP noise across extensions and barrels.
/// - Cross-language collisions (e.g., `.rs` vs `.ts`) are still prevented.
/// - `/index` suffix is stripped so `foo/index.ts` -> `foo:ts`.
///
/// Returns a NormalizedModule with separate path and language fields.
pub(crate) fn normalize_module_id(path: &str) -> NormalizedModule {
    let mut p = path.replace('\\', "/");
    let mut lang = String::new();

    // Extract language family from extension (collapse TS/JS variants)
    for ext in [
        ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".rs", ".py", ".css", ".svelte", ".vue",
    ] {
        if let Some(stripped) = p.strip_suffix(ext) {
            p = stripped.to_string();
            lang = match ext {
                ".ts" | ".tsx" | ".js" | ".jsx" | ".mjs" | ".cjs" | ".svelte" | ".vue" => {
                    "ts".to_string()
                }
                ".rs" => "rs".to_string(),
                ".py" => "py".to_string(),
                ".css" => "css".to_string(),
                _ => ext.trim_start_matches('.').to_string(),
            };
            break;
        }
    }

    // Strip /index suffix
    if let Some(stripped) = p.strip_suffix("/index") {
        p = stripped.to_string();
    }

    NormalizedModule { path: p, lang }
}

/// Build ScanResults from a loaded Snapshot (scan once, analyze many)
pub fn scan_results_from_snapshot(snapshot: &Snapshot) -> ScanResults {
    let mut global_fe_commands: CommandUsage = HashMap::new();
    let mut global_be_commands: CommandUsage = HashMap::new();
    let mut global_fe_payloads: PayloadMap = HashMap::new();
    let mut global_be_payloads: PayloadMap = HashMap::new();

    // Build command maps from FileAnalysis
    for analysis in &snapshot.files {
        // Frontend commands (invoke calls)
        for call in &analysis.command_calls {
            let mut key = call.name.clone();
            if let Some(stripped) = key.strip_suffix("_command") {
                key = stripped.to_string();
            }
            global_fe_commands.entry(key.clone()).or_default().push((
                analysis.path.clone(),
                call.line,
                call.name.clone(),
            ));

            // Track payload types if present
            if let Some(payload) = &call.payload {
                global_fe_payloads.entry(key).or_default().push((
                    analysis.path.clone(),
                    call.line,
                    Some(payload.clone()),
                ));
            }
        }

        // Backend handlers (#[tauri::command])
        for handler in &analysis.command_handlers {
            let mut key = handler
                .exposed_name
                .as_ref()
                .unwrap_or(&handler.name)
                .clone();
            if let Some(stripped) = key.strip_suffix("_command") {
                key = stripped.to_string();
            }
            global_be_commands.entry(key.clone()).or_default().push((
                analysis.path.clone(),
                handler.line,
                handler.name.clone(),
            ));

            // Track return types if present
            if let Some(ret) = &handler.payload {
                global_be_payloads.entry(key).or_default().push((
                    analysis.path.clone(),
                    handler.line,
                    Some(ret.clone()),
                ));
            }
        }
    }

    // Build minimal RootContext for each root in snapshot
    let mut contexts = Vec::new();

    // If only one root, all files belong to it (paths are relative in snapshot)
    let single_root = snapshot.metadata.roots.len() == 1;

    for root_str in &snapshot.metadata.roots {
        let root_path = PathBuf::from(root_str);

        // Filter analyses for this root
        // Note: snapshot paths are relative, so for single root we take all files
        let root_analyses: Vec<FileAnalysis> = if single_root {
            snapshot.files.clone()
        } else {
            snapshot
                .files
                .iter()
                .filter(|a| a.path.starts_with(root_str) || root_str == ".")
                .cloned()
                .collect()
        };

        // Build export index
        let mut export_index: ExportIndex = HashMap::new();
        for analysis in &root_analyses {
            for export in &analysis.exports {
                // Skip re-exports - they're not duplicate definitions
                if export.kind == "reexport" {
                    continue;
                }
                export_index
                    .entry(export.name.clone())
                    .or_default()
                    .push(analysis.path.clone());
            }
        }

        // Build LOC map
        let loc_map: HashMap<String, usize> = root_analyses
            .iter()
            .map(|a| (a.path.clone(), a.loc))
            .collect();

        // Build dynamic summary
        let dynamic_summary: Vec<(String, Vec<String>)> = root_analyses
            .iter()
            .filter(|a| !a.dynamic_imports.is_empty())
            .map(|a| (a.path.clone(), a.dynamic_imports.clone()))
            .collect();

        // Build graph edges from snapshot
        // Note: For single root, all edges belong to it (paths in snapshot are relative)
        let graph_edges: Vec<(String, String, String)> = if single_root {
            snapshot
                .edges
                .iter()
                .map(|e| (e.from.clone(), e.to.clone(), e.label.clone()))
                .collect()
        } else {
            snapshot
                .edges
                .iter()
                .filter(|e| e.from.starts_with(root_str) || root_str == ".")
                .map(|e| (e.from.clone(), e.to.clone(), e.label.clone()))
                .collect()
        };

        // Detect languages
        let languages: HashSet<String> = root_analyses.iter().map(|a| a.language.clone()).collect();

        // Calculate ranked duplicates
        let ignore_exact: HashSet<String> = twins::GENERIC_METHOD_NAMES
            .iter()
            .map(|s| s.to_string())
            .collect();
        let filtered_ranked = rank_duplicates(&export_index, &root_analyses, &ignore_exact, &[]);

        // Build cascades (re-export chains)
        let cascades = build_cascades(&root_analyses);

        // Build barrel info
        let barrels: Vec<BarrelInfo> = snapshot
            .barrels
            .iter()
            .filter(|b| b.path.starts_with(root_str) || root_str == ".")
            .map(|b| BarrelInfo {
                path: b.path.clone(),
                module_id: b.module_id.clone(),
                reexport_count: b.reexport_count,
                target_count: b.targets.len(),
                mixed: false, // Can't determine from snapshot
                targets: b.targets.clone(),
            })
            .collect();

        contexts.push(RootContext {
            root_path,
            options: Options {
                extensions: None,
                ignore_paths: vec![],
                ignore_globs: None,
                use_gitignore: false,
                max_depth: None,
                color: ColorMode::Auto,
                output: OutputMode::Human,
                summary: false,
                summary_limit: 5,
                summary_only: false,
                show_hidden: false,
                show_ignored: false,
                loc_threshold: 1000,
                analyze_limit: 0,
                report_path: None,
                serve: false,
                editor_cmd: None,
                max_graph_nodes: None,
                max_graph_edges: None,
                verbose: false,
                scan_all: false,
                symbol: None,
                impact: None,
                find_artifacts: false,
            },
            analyses: root_analyses,
            export_index,
            dynamic_summary,
            cascades,
            filtered_ranked,
            graph_edges,
            loc_map,
            languages,
            tsconfig_summary: json!({}),
            calls_with_generics: vec![],
            renamed_handlers: vec![],
            barrels,
        });
    }

    // Extract cached resolver config from snapshot metadata
    let ts_resolver_config =
        snapshot
            .metadata
            .resolver_config
            .as_ref()
            .map(|rc| ExtractedResolverConfig {
                ts_paths: rc.ts_paths.clone(),
                ts_base_url: rc.ts_base_url.clone(),
            });

    let py_roots = snapshot
        .metadata
        .resolver_config
        .as_ref()
        .map(|rc| rc.py_roots.clone())
        .unwrap_or_default();

    ScanResults {
        contexts,
        global_fe_commands,
        global_be_commands,
        global_fe_payloads,
        global_be_payloads,
        global_analyses: snapshot.files.clone(),
        ts_resolver_config,
        py_roots,
    }
}

// ============================================================================
// Duplicate severity detection helpers
// ============================================================================

/// Known DTO type names that are expected to be duplicated across Rust↔TS
const DTO_WHITELIST: &[&str] = &[
    "visit",
    "task",
    "chatmessage",
    "attachment",
    "patient",
    "message",
    "user",
    "config",
    "settings",
    "response",
    "request",
    "error",
    "result",
    "event",
    "state",
    "status",
    "payload",
    "data",
];

/// System constants expected across languages
const SYSTEM_CONSTANTS: &[&str] = &["touch_id", "keychain", "app_name", "version", "default"];

/// Generic method/function names that should be excluded from duplicate detection.
/// These are common patterns across different types/structs and not actual duplicates.
const GENERIC_METHOD_NAMES: &[&str] = &[
    "new",     // Constructor pattern (Rust, Python __new__, etc.)
    "default", // Default trait implementation
    "from",    // From trait / factory pattern
    "into",    // Into trait / conversion
    "clone",   // Clone trait
    "process", // Too generic - processing logic
    "load",    // Too generic - loading data
    "save",    // Too generic - saving data
    "run",     // Too generic - execution
    "init",    // Too generic - initialization
    "build",   // Builder pattern
    "execute", // Too generic - execution
    "handle",  // Too generic - event handling
    "create",  // Too generic - factory pattern
    "update",  // Too generic - mutation
    "delete",  // Too generic - removal
    "get",     // Too generic - getter
    "set",     // Too generic - setter
];

/// Check if a name is a generic method that should be excluded from duplicate detection
fn is_generic_method(name: &str) -> bool {
    GENERIC_METHOD_NAMES.contains(&name)
}

/// Detect language family from file path
fn detect_language(path: &str) -> &'static str {
    let ext = path.rsplit('.').next().unwrap_or("");
    match ext {
        "rs" => "rust",
        "ts" | "tsx" | "js" | "jsx" | "mjs" | "mts" => "typescript",
        "py" | "pyi" => "python",
        "css" | "scss" | "less" => "css",
        _ => "unknown",
    }
}

/// Extract package/directory context from path
fn extract_package(path: &str) -> String {
    // Extract first meaningful directory after src/ or src-tauri/
    let parts: Vec<&str> = path.split('/').collect();

    // Find src or src-tauri, take next directory
    for (i, part) in parts.iter().enumerate() {
        if (*part == "src" || *part == "src-tauri") && i + 1 < parts.len() {
            return parts[i + 1].to_string();
        }
    }

    // Fallback: use parent directory
    parts
        .iter()
        .rev()
        .nth(1)
        .map(|s| s.to_string())
        .unwrap_or_else(|| "root".to_string())
}

/// Extract crate/npm package root from path
/// Returns the crate name or package directory (e.g., "rmcp_mux", "rmcp_memex", "loctree_rs")
fn extract_crate_root(path: &str) -> String {
    let parts: Vec<&str> = path.split('/').collect();

    // For monorepo structures like:
    // - "rmcp_mux/src/..." -> "rmcp_mux"
    // - "loctree_rs/src/..." -> "loctree_rs"
    // - "packages/frontend/src/..." -> "packages/frontend"

    // Take first meaningful directory component
    if parts.is_empty() {
        return "root".to_string();
    }

    // Skip leading . or empty parts
    for part in &parts {
        if !part.is_empty() && *part != "." && *part != ".." {
            return part.to_string();
        }
    }

    "root".to_string()
}

/// Check if symbol name matches DTO whitelist
fn is_whitelisted_dto(name: &str) -> bool {
    let lc = name.to_lowercase();
    DTO_WHITELIST.iter().any(|dto| lc.contains(dto))
        || SYSTEM_CONSTANTS.iter().any(|c| lc.contains(c))
}

/// Check if files span multiple languages
fn is_cross_lang(files: &[String]) -> bool {
    let langs: HashSet<_> = files.iter().map(|f| detect_language(f)).collect();
    langs.len() > 1
}

/// Check if path is in src-tauri (backend)
fn is_backend_path(path: &str) -> bool {
    path.contains("src-tauri") || path.contains("src-rs") || path.ends_with(".rs")
}

/// Config file patterns that export "default" or common utilities
const CONFIG_PATTERNS: &[&str] = &[
    "eslint",
    "vite.config",
    "vitest.config",
    "playwright.config",
    "postcss.config",
    "tailwind.config",
    "jest.config",
    "webpack.config",
    "rollup.config",
    "babel.config",
    "tsconfig",
    ".d.ts",
];

/// Check if file is a config file (expected to have default exports)
fn is_config_file(path: &str) -> bool {
    let lower = path.to_lowercase();
    CONFIG_PATTERNS.iter().any(|p| lower.contains(p))
}

/// Compute severity for a duplicate export
fn compute_severity(name: &str, files: &[String], packages: &[String]) -> (DupSeverity, String) {
    let cross_lang = is_cross_lang(files);
    let whitelisted = is_whitelisted_dto(name);
    let is_generic = is_generic_method(name);

    // Count config files
    let config_count = files.iter().filter(|f| is_config_file(f)).count();
    let all_config = config_count == files.len();
    let mostly_config = files.len() > 1 && config_count as f64 / files.len() as f64 > 0.5;

    // Severity 0: Config file exports (default, new, etc.)
    // These are expected to be duplicated across config files
    if all_config || (mostly_config && (name == "default" || is_generic)) {
        return (
            DupSeverity::CrossLangExpected,
            "Config file export".to_string(),
        );
    }

    // Severity 1: Generic methods (new, from, clone, etc.) - usually OK
    // These are common patterns and not real duplicates
    if is_generic {
        return (
            DupSeverity::ReExportOrGeneric,
            format!("Generic method/pattern: {}", name),
        );
    }

    // Check for src-tauri vs src split (expected for Tauri apps)
    let has_backend = files.iter().any(|f| is_backend_path(f));
    let has_frontend = files.iter().any(|f| !is_backend_path(f));
    let fe_be_split = has_backend && has_frontend;

    // Severity 0: Cross-language expected duplicates
    if cross_lang && (whitelisted || fe_be_split) {
        return (
            DupSeverity::CrossLangExpected,
            format!(
                "Cross-lang DTO ({})",
                if whitelisted {
                    "whitelisted"
                } else {
                    "FE↔BE"
                }
            ),
        );
    }

    // HIGHEST SEVERITY 4: Cross-crate duplicates (REAL ISSUES!)
    // Same symbol name in DIFFERENT crates/packages
    // Example: HostKind enum defined in both rmcp_mux and rmcp_memex
    let crate_roots: HashSet<String> = files.iter().map(|f| extract_crate_root(f)).collect();
    let cross_crate = crate_roots.len() > 1;

    if cross_crate {
        let crate_list: Vec<String> = crate_roots.iter().cloned().collect();
        return (
            DupSeverity::CrossCrate,
            format!("Cross-crate duplicate in: {}", crate_list.join(", ")),
        );
    }

    // Severity 3: Cross-module duplicates (same crate, different modules)
    // e.g., Attachment in icons/ vs hooks/
    if packages.len() >= 3 {
        return (
            DupSeverity::CrossModule,
            format!("Symbol in {} different modules", packages.len()),
        );
    }

    // Check for potentially conflicting contexts
    let contexts: Vec<_> = packages.iter().map(|p| p.to_lowercase()).collect();
    let has_ui = contexts
        .iter()
        .any(|c| c.contains("icon") || c.contains("component") || c.contains("ui"));
    let has_logic = contexts.iter().any(|c| {
        c.contains("hook")
            || c.contains("service")
            || c.contains("util")
            || c.contains("context")
            || c.contains("store")
    });
    if has_ui && has_logic && !whitelisted {
        return (
            DupSeverity::CrossModule,
            "UI vs logic context mismatch".to_string(),
        );
    }

    // Severity 1: Same-package or expected structure
    (
        DupSeverity::SamePackage,
        if cross_lang {
            "Cross-lang (not whitelisted)".to_string()
        } else {
            "Same language duplicate".to_string()
        },
    )
}

/// Rank duplicates by severity (helper for snapshot conversion)
fn rank_duplicates(
    export_index: &ExportIndex,
    analyses: &[FileAnalysis],
    ignore_exact: &HashSet<String>,
    ignore_prefixes: &[String],
) -> Vec<RankedDup> {
    use super::classify::is_dev_file;

    // Build lookup map for export line numbers
    let export_lines: std::collections::HashMap<(String, String), Option<usize>> = analyses
        .iter()
        .flat_map(|a| {
            a.exports
                .iter()
                .map(|e| ((a.path.clone(), e.name.clone()), e.line))
        })
        .collect();

    let mut ranked = Vec::new();
    for (name, files) in export_index {
        if files.len() <= 1 {
            continue;
        }
        let lc = name.to_lowercase();
        if ignore_exact.contains(&lc) {
            continue;
        }
        if ignore_prefixes.iter().any(|p| lc.starts_with(p)) {
            continue;
        }

        let mut prod_count = 0;
        let mut dev_count = 0;
        for f in files {
            if is_dev_file(f) {
                dev_count += 1;
            } else {
                prod_count += 1;
            }
        }

        // Skip if all are dev files
        if prod_count == 0 {
            continue;
        }

        let score = files.len() + prod_count;
        let canonical = files.first().cloned().unwrap_or_default();
        let canonical_line = export_lines
            .get(&(canonical.clone(), name.clone()))
            .copied()
            .flatten();

        // Build locations with line numbers
        let locations: Vec<DupLocation> = files
            .iter()
            .map(|f| DupLocation {
                file: f.clone(),
                line: export_lines
                    .get(&(f.clone(), name.clone()))
                    .copied()
                    .flatten(),
            })
            .collect();

        // Refactor targets = all files except canonical
        let refactors: Vec<String> = files.iter().filter(|f| *f != &canonical).cloned().collect();

        // Extract unique packages for severity calculation
        let packages: Vec<String> = files
            .iter()
            .map(|f| extract_package(f))
            .collect::<HashSet<_>>()
            .into_iter()
            .collect();

        // Compute severity and reason
        let cross_lang = is_cross_lang(files);
        let (severity, reason) = compute_severity(name, files, &packages);

        ranked.push(RankedDup {
            name: name.clone(),
            score,
            files: files.clone(),
            locations,
            prod_count,
            dev_count,
            canonical,
            canonical_line,
            refactors,
            severity,
            is_cross_lang: cross_lang,
            packages,
            reason,
        });
    }

    // Sort by severity (descending), then by score
    ranked.sort_by(|a, b| {
        let sev_cmp = (b.severity as u8).cmp(&(a.severity as u8));
        if sev_cmp != std::cmp::Ordering::Equal {
            sev_cmp
        } else {
            b.score.cmp(&a.score)
        }
    });
    ranked
}

pub(crate) fn build_py_roots(root_canon: &Path, extra_roots: &[PathBuf]) -> Vec<PathBuf> {
    let mut roots: Vec<PathBuf> = Vec::new();

    // Always include project root
    let root_canon = root_canon
        .canonicalize()
        .unwrap_or_else(|_| root_canon.to_path_buf());
    roots.push(root_canon.clone());

    // Common src/ layout
    let src = root_canon.join("src");
    if src.is_dir() {
        roots.push(src.canonicalize().unwrap_or(src));
    }

    // Discover from pyproject.toml (poetry/setuptools)
    let pyproject = root_canon.join("pyproject.toml");
    if pyproject.exists()
        && let Ok(text) = std::fs::read_to_string(&pyproject)
        && let Ok(val) = text.parse::<toml::Table>()
    {
        // tool.poetry.packages = [{ include = "...", from = "src" }]
        if let Some(packages) = val
            .get("tool")
            .and_then(|t| t.get("poetry"))
            .and_then(|p| p.get("packages"))
            .and_then(|p| p.as_array())
        {
            for pkg in packages {
                if let Some(include) = pkg.get("include").and_then(|i| i.as_str()) {
                    let from = pkg.get("from").and_then(|f| f.as_str());
                    let base = from
                        .map(|f| root_canon.join(f))
                        .unwrap_or_else(|| root_canon.clone());
                    let candidate = base.join(include);
                    if candidate.exists() {
                        roots.push(candidate.canonicalize().unwrap_or(candidate));
                    }
                }
            }
        }

        // tool.setuptools.packages.find.where = ["src", ...]
        if let Some(where_arr) = val
            .get("tool")
            .and_then(|t| t.get("setuptools"))
            .and_then(|s| s.get("packages"))
            .and_then(|p| p.get("find"))
            .and_then(|f| f.get("where"))
            .and_then(|w| w.as_array())
        {
            for entry in where_arr {
                if let Some(path_str) = entry.as_str() {
                    let candidate = root_canon.join(path_str);
                    if candidate.exists() {
                        roots.push(candidate.canonicalize().unwrap_or(candidate));
                    }
                }
            }
        }
    }

    // User-provided overrides
    for extra in extra_roots {
        let candidate = if extra.is_absolute() {
            extra.clone()
        } else {
            root_canon.join(extra)
        };
        if candidate.exists() {
            roots.push(candidate.canonicalize().unwrap_or(candidate));
        } else {
            eprintln!(
                "[loctree][warn] --py-root '{}' not found under {}; skipping",
                extra.display(),
                root_canon.display()
            );
        }
    }

    roots.sort();
    roots.dedup();
    roots
}

/// Build cascades (re-export chains) from analyses
fn build_cascades(analyses: &[FileAnalysis]) -> Vec<(String, String)> {
    let mut cascades = Vec::new();
    for analysis in analyses {
        for reexport in &analysis.reexports {
            if !reexport.source.is_empty() {
                cascades.push((analysis.path.clone(), reexport.source.clone()));
            }
        }
    }
    cascades
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_normalize_module_id_preserves_language() {
        // Test that different languages with same base path get different normalized IDs (except TS/JS family)
        let rust_module = normalize_module_id("src/utils.rs");
        let ts_module = normalize_module_id("src/utils.ts");
        let tsx_module = normalize_module_id("src/utils.tsx");

        assert_eq!(rust_module.path, "src/utils");
        assert_eq!(rust_module.lang, "rs");

        assert_eq!(ts_module.path, "src/utils");
        assert_eq!(ts_module.lang, "ts");

        assert_eq!(tsx_module.path, "src/utils");
        assert_eq!(tsx_module.lang, "ts");

        // Keys should be different across languages, but TS/JS family collapses
        assert_ne!(rust_module.as_key(), ts_module.as_key());
        assert_ne!(rust_module.as_key(), tsx_module.as_key());
        assert_eq!(ts_module.as_key(), tsx_module.as_key());
    }

    #[test]
    fn detects_common_python_roots_and_pyproject() {
        let dir = tempdir().expect("tempdir");
        let root = dir.path();
        let root_canon = root.canonicalize().unwrap();

        // src layout
        std::fs::create_dir_all(root.join("src/app")).unwrap();
        // setuptools style
        std::fs::create_dir_all(root.join("services")).unwrap();
        // poetry package from "src"
        let pyproject = r#"
[tool.poetry]
name = "example"
version = "0.1.0"
packages = [
    { include = "app", from = "src" }
]

[tool.setuptools.packages.find]
where = ["services"]
"#;
        std::fs::write(root.join("pyproject.toml"), pyproject).unwrap();
        // extra user provided
        let extra_dir = root.join("custom");
        std::fs::create_dir_all(&extra_dir).unwrap();
        let extra_dir_canon = extra_dir.canonicalize().unwrap();

        let roots = build_py_roots(root, &[PathBuf::from("custom"), PathBuf::from("missing")]);

        assert!(roots.contains(&root_canon));
        assert!(roots.contains(&root_canon.join("src")));
        assert!(roots.contains(&root_canon.join("src/app")));
        assert!(roots.contains(&root_canon.join("services")));
        assert!(roots.contains(&extra_dir_canon));
    }

    #[test]
    fn test_normalize_module_id_index_files() {
        // Test that index files are normalized but preserve language
        let ts_index = normalize_module_id("src/components/index.ts");
        let rs_index = normalize_module_id("src/components/index.rs");

        assert_eq!(ts_index.path, "src/components");
        assert_eq!(ts_index.lang, "ts");

        assert_eq!(rs_index.path, "src/components");
        assert_eq!(rs_index.lang, "rs");

        // Should not collide
        assert_ne!(ts_index.as_key(), rs_index.as_key());
        assert_eq!(ts_index.as_key(), "src/components:ts");
        assert_eq!(rs_index.as_key(), "src/components:rs");
    }

    #[test]
    fn test_normalize_module_id_cross_language_collision() {
        // This is the core bug fix - ensure foo.rs and foo.ts don't collide
        let rust_file = normalize_module_id("src/foo.rs");
        let ts_file = normalize_module_id("src/foo.ts");
        let ts_index = normalize_module_id("src/foo/index.ts");

        // All have same base path
        assert_eq!(rust_file.path, "src/foo");
        assert_eq!(ts_file.path, "src/foo");
        assert_eq!(ts_index.path, "src/foo");

        // But different languages
        assert_eq!(rust_file.lang, "rs");
        assert_eq!(ts_file.lang, "ts");
        assert_eq!(ts_index.lang, "ts");

        // Keys should prevent collisions
        assert_eq!(rust_file.as_key(), "src/foo:rs");
        assert_eq!(ts_file.as_key(), "src/foo:ts");
        assert_eq!(ts_index.as_key(), "src/foo:ts");

        // Rust and TS files should NOT match
        assert_ne!(rust_file.as_key(), ts_file.as_key());

        // TS file and TS index SHOULD match (same language, same normalized path)
        assert_eq!(ts_file.as_key(), ts_index.as_key());
    }

    #[test]
    fn test_normalized_module_from_key() {
        let module = NormalizedModule {
            path: "src/utils".to_string(),
            lang: "ts".to_string(),
        };

        let key = module.as_key();
        assert_eq!(key, "src/utils:ts");

        let parsed = NormalizedModule::from_key(&key).unwrap();
        assert_eq!(parsed.path, "src/utils");
        assert_eq!(parsed.lang, "ts");
        assert_eq!(parsed, module);
    }

    #[test]
    fn test_normalized_module_various_extensions() {
        // Test all supported extensions
        let extensions = vec![
            ("file.ts", "ts"),
            ("file.tsx", "ts"),
            ("file.js", "ts"),
            ("file.jsx", "ts"),
            ("file.mjs", "ts"),
            ("file.cjs", "ts"),
            ("file.rs", "rs"),
            ("file.py", "py"),
            ("file.css", "css"),
            ("file.svelte", "ts"),
            ("file.vue", "ts"),
        ];

        for (input, expected_lang) in extensions {
            let module = normalize_module_id(input);
            assert_eq!(module.path, "file", "Failed for {}", input);
            assert_eq!(module.lang, expected_lang, "Failed for {}", input);
        }
    }

    #[test]
    fn test_normalized_module_windows_paths() {
        // Test Windows-style paths are normalized
        let module = normalize_module_id("src\\utils\\helpers.ts");
        assert_eq!(module.path, "src/utils/helpers");
        assert_eq!(module.lang, "ts");
        assert_eq!(module.as_key(), "src/utils/helpers:ts");
    }

    #[test]
    fn test_normalized_module_from_key_round_trip() {
        // Test round-trip conversion between module and key
        let test_cases = vec![
            ("src/utils:ts", "src/utils", "ts"),
            ("components/Button:ts", "components/Button", "ts"),
            ("lib/helpers:ts", "lib/helpers", "ts"),
            ("core:rs", "core", "rs"),
        ];

        for (key, expected_path, expected_lang) in test_cases {
            let module = NormalizedModule::from_key(key).unwrap();
            assert_eq!(module.path, expected_path);
            assert_eq!(module.lang, expected_lang);
            assert_eq!(module.as_key(), key);
        }
    }

    #[test]
    fn test_normalized_module_from_key_invalid() {
        // Test that invalid keys return None
        assert!(NormalizedModule::from_key("invalid_key_without_colon").is_none());
        assert!(NormalizedModule::from_key("").is_none());
    }

    #[test]
    fn test_normalized_module_hash_and_eq() {
        // Test that NormalizedModule can be used in HashSet/HashMap
        use std::collections::HashSet;

        let mut set = HashSet::new();

        let mod1 = normalize_module_id("src/utils.ts");
        let mod2 = normalize_module_id("src/utils.ts");
        let mod3 = normalize_module_id("src/utils.rs");

        set.insert(mod1.clone());

        // Same module shouldn't be inserted twice
        assert!(set.contains(&mod2));
        assert_eq!(set.len(), 1);

        // Different language should be different
        set.insert(mod3.clone());
        assert_eq!(set.len(), 2);
    }

    #[test]
    fn test_is_index_like_vue_files() {
        // Test that Vue index files are recognized
        assert!(is_index_like("src/components/index.vue"));
        assert!(is_index_like("components/index.vue"));
        assert!(is_index_like("/absolute/path/index.vue"));

        // Test case insensitivity
        assert!(is_index_like("src/components/INDEX.VUE"));

        // Non-index Vue files should return false
        assert!(!is_index_like("src/components/Button.vue"));
        assert!(!is_index_like("src/App.vue"));

        // Other index files should still work
        assert!(is_index_like("src/index.ts"));
        assert!(is_index_like("src/index.js"));
        assert!(is_index_like("src/index.svelte"));
    }
}