solidity-language-server 0.1.36

A fast Solidity language server powered by solc and Foundry
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
//! Direct `solc --standard-json` runner for fast AST generation.
//!
//! The output is normalized into the same shape that `forge build --json --ast`
//! produces, so all downstream consumers (goto, hover, completions, etc.) work
//! unchanged.

use crate::config::FoundryConfig;
use crate::links;
use crate::runner::RunnerError;
use serde_json::{Map, Value, json};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use tokio::process::Command;
use tower_lsp::lsp_types::Url;

/// Cached list of installed solc versions. Populated on first access,
/// invalidated after a successful `svm::install`.
static INSTALLED_VERSIONS: OnceLock<Mutex<Vec<SemVer>>> = OnceLock::new();

fn get_installed_versions() -> Vec<SemVer> {
    let mutex = INSTALLED_VERSIONS.get_or_init(|| Mutex::new(scan_installed_versions()));
    mutex.lock().unwrap().clone()
}

fn invalidate_installed_versions() {
    if let Some(mutex) = INSTALLED_VERSIONS.get() {
        *mutex.lock().unwrap() = scan_installed_versions();
    }
}

/// Convert a `semver::Version` (from svm-rs) to our lightweight `SemVer`.
fn semver_to_local(v: &semver::Version) -> SemVer {
    SemVer {
        major: v.major as u32,
        minor: v.minor as u32,
        patch: v.patch as u32,
    }
}

/// Resolve the path to the solc binary.
///
/// Resolution order:
/// 1. Parse `pragma solidity` from the source file.
///    - **Exact pragma** (`=0.7.6`): always use the file's version — foundry.toml
///      cannot override an exact pragma without breaking compilation.
///    - **Wildcard pragma** (`^0.8.0`, `>=0.8.0`, `>=0.6.2 <0.9.0`): if
///      `foundry.toml` specifies a solc version that satisfies the constraint,
///      use it. Otherwise pick the latest matching installed version.
/// 2. If no pragma, use the `foundry.toml` solc version if set.
/// 3. If no match is installed, auto-install via `svm install`.
/// 4. Fall back to whatever `solc` is on `$PATH`.
pub async fn resolve_solc_binary(
    config: &FoundryConfig,
    constraint: Option<&PragmaConstraint>,
    client: Option<&tower_lsp::Client>,
) -> PathBuf {
    // 1. Try pragma constraint (may be tightened from the full import graph)
    if let Some(constraint) = constraint {
        // For exact pragmas, always honour the file — foundry.toml can't override
        // without causing a compilation failure.
        // For wildcard pragmas, prefer the foundry.toml version if it satisfies
        // the constraint. This mirrors `forge build` behaviour where the project
        // config picks the version but the pragma must still be satisfied.
        if !matches!(constraint, PragmaConstraint::Exact(_))
            && let Some(ref config_ver) = config.solc_version
            && let Some(parsed) = SemVer::parse(config_ver)
            && version_satisfies(&parsed, constraint)
            && let Some(path) = find_solc_binary(config_ver)
        {
            if let Some(c) = client {
                c.log_message(
                    tower_lsp::lsp_types::MessageType::INFO,
                    format!("using solc {config_ver} (pragma {constraint})"),
                )
                .await;
            }
            return path;
        }

        let installed = get_installed_versions();
        if let Some(version) = find_matching_version(constraint, &installed)
            && let Some(path) = find_solc_binary(&version.to_string())
        {
            if let Some(c) = client {
                c.log_message(
                    tower_lsp::lsp_types::MessageType::INFO,
                    format!("using solc {version}"),
                )
                .await;
            }
            return path;
        }

        // No matching version installed — try auto-install via svm
        let install_version = version_to_install(constraint);
        if let Some(ref ver_str) = install_version {
            if let Some(c) = client {
                c.show_message(
                    tower_lsp::lsp_types::MessageType::INFO,
                    format!("Installing solc {ver_str}..."),
                )
                .await;
            }

            if svm_install(ver_str).await {
                // Refresh the cached version list after install
                invalidate_installed_versions();

                if let Some(c) = client {
                    c.show_message(
                        tower_lsp::lsp_types::MessageType::INFO,
                        format!("Installed solc {ver_str}"),
                    )
                    .await;
                }
                if let Some(path) = find_solc_binary(ver_str) {
                    return path;
                }
            } else if let Some(c) = client {
                c.show_message(
                    tower_lsp::lsp_types::MessageType::WARNING,
                    format!(
                        "Failed to install solc {ver_str}. \
                             Install it manually: svm install {ver_str}"
                    ),
                )
                .await;
            }
        }
    }

    // 2. No pragma — use foundry.toml version if available
    if let Some(ref version) = config.solc_version
        && let Some(path) = find_solc_binary(version)
    {
        if let Some(c) = client {
            c.log_message(
                tower_lsp::lsp_types::MessageType::INFO,
                format!(
                    "solc: no pragma, using foundry.toml version {version} → {}",
                    path.display()
                ),
            )
            .await;
        }
        return path;
    }

    // 3. Fall back to system solc
    if let Some(c) = client {
        c.log_message(
            tower_lsp::lsp_types::MessageType::INFO,
            "solc: no pragma match, falling back to system solc",
        )
        .await;
    }
    PathBuf::from("solc")
}

/// Determine which version to install for a pragma constraint.
///
/// - Exact: install that version
/// - Caret `^0.8.20`: install `0.8.20` (minimum satisfying)
/// - Gte `>=0.8.0`: install `0.8.0` (minimum satisfying)
/// - Range `>=0.6.2 <0.9.0`: install `0.6.2` (minimum satisfying)
fn version_to_install(constraint: &PragmaConstraint) -> Option<String> {
    match constraint {
        PragmaConstraint::Exact(v) => Some(v.to_string()),
        PragmaConstraint::Caret(v) => Some(v.to_string()),
        PragmaConstraint::Gte(v) => Some(v.to_string()),
        PragmaConstraint::Range(lower, _) => Some(lower.to_string()),
    }
}

/// Install a solc version using svm-rs library.
///
/// Returns `true` if the install succeeded.
async fn svm_install(version: &str) -> bool {
    let ver = match semver::Version::parse(version) {
        Ok(v) => v,
        Err(_) => return false,
    };
    svm::install(&ver).await.is_ok()
}

/// Look up a solc binary by version string using `svm::version_binary()`.
fn find_solc_binary(version: &str) -> Option<PathBuf> {
    let path = svm::version_binary(version);
    if path.is_file() {
        return Some(path);
    }
    None
}

// ── Pragma parsing ────────────────────────────────────────────────────────

/// A parsed semver version (major.minor.patch).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct SemVer {
    pub major: u32,
    pub minor: u32,
    pub patch: u32,
}

impl SemVer {
    fn parse(s: &str) -> Option<SemVer> {
        let parts: Vec<&str> = s.split('.').collect();
        if parts.len() != 3 {
            return None;
        }
        Some(SemVer {
            major: parts[0].parse().ok()?,
            minor: parts[1].parse().ok()?,
            patch: parts[2].parse().ok()?,
        })
    }
}

impl std::fmt::Display for SemVer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

/// A version constraint from `pragma solidity`.
#[derive(Debug, Clone, PartialEq)]
pub enum PragmaConstraint {
    /// `0.8.26` — exact match
    Exact(SemVer),
    /// `^0.8.0` — same major.minor, patch >= specified
    /// Actually in Solidity: `^0.8.0` means `>=0.8.0 <0.9.0`
    Caret(SemVer),
    /// `>=0.8.0` — at least this version
    Gte(SemVer),
    /// `>=0.6.2 <0.9.0` — range
    Range(SemVer, SemVer),
}

impl std::fmt::Display for PragmaConstraint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PragmaConstraint::Exact(v) => write!(f, "={v}"),
            PragmaConstraint::Caret(v) => write!(f, "^{v}"),
            PragmaConstraint::Gte(v) => write!(f, ">={v}"),
            PragmaConstraint::Range(lo, hi) => write!(f, ">={lo} <{hi}"),
        }
    }
}

/// Resolve a Solidity import path to an absolute filesystem path.
///
/// Handles relative imports (`./`, `../`) and remapped imports.
fn resolve_import_to_abs(
    project_root: &Path,
    importer_abs: &Path,
    import_path: &str,
    remappings: &[String],
) -> Option<PathBuf> {
    if import_path.starts_with("./") || import_path.starts_with("../") {
        let base = importer_abs.parent()?;
        return Some(lexical_normalize(&base.join(import_path)));
    }

    for remap in remappings {
        let mut it = remap.splitn(2, '=');
        let prefix = it.next().unwrap_or_default();
        let target = it.next().unwrap_or_default();
        if prefix.is_empty() || target.is_empty() {
            continue;
        }
        if import_path.starts_with(prefix) {
            let suffix = import_path.strip_prefix(prefix).unwrap_or_default();
            return Some(lexical_normalize(
                &project_root.join(format!("{target}{suffix}")),
            ));
        }
    }

    Some(lexical_normalize(&project_root.join(import_path)))
}

/// Normalize a path by resolving `.` and `..` components lexically
/// (without hitting the filesystem).
fn lexical_normalize(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for comp in path.components() {
        match comp {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                out.pop();
            }
            _ => out.push(comp.as_os_str()),
        }
    }
    out
}

/// Collect pragma constraints from a file and all its transitive imports.
///
/// Walks the import graph using simple string scanning (no tree-sitter),
/// resolving import paths via remappings.  Returns all pragmas found so
/// that the caller can pick a solc version satisfying every file.
fn collect_import_pragmas(
    file_path: &Path,
    project_root: &Path,
    remappings: &[String],
) -> Vec<PragmaConstraint> {
    let mut pragmas = Vec::new();
    let mut visited = HashSet::new();
    collect_import_pragmas_recursive(
        file_path,
        project_root,
        remappings,
        &mut pragmas,
        &mut visited,
    );
    pragmas
}

fn collect_import_pragmas_recursive(
    file_path: &Path,
    project_root: &Path,
    remappings: &[String],
    pragmas: &mut Vec<PragmaConstraint>,
    visited: &mut HashSet<PathBuf>,
) {
    if !visited.insert(file_path.to_path_buf()) {
        return;
    }
    let source = match std::fs::read_to_string(file_path) {
        Ok(s) => s,
        Err(_) => return,
    };
    if let Some(pragma) = parse_pragma(&source) {
        pragmas.push(pragma);
    }
    for imp in links::ts_find_imports(source.as_bytes()) {
        if let Some(abs) = resolve_import_to_abs(project_root, file_path, &imp.path, remappings) {
            collect_import_pragmas_recursive(&abs, project_root, remappings, pragmas, visited);
        }
    }
}

/// Tighten a set of pragma constraints into a single constraint that
/// satisfies all of them.
///
/// Rules:
/// - An exact pragma always wins (if any file requires `0.8.23`, we must
///   use exactly `0.8.23`).
/// - Multiple exact pragmas that disagree → returns the first one (solc
///   will error anyway, but we still try).
/// - For wildcard pragmas, compute the intersection range and return it.
fn tightest_constraint(pragmas: &[PragmaConstraint]) -> Option<PragmaConstraint> {
    if pragmas.is_empty() {
        return None;
    }

    // If any pragma is Exact, that version must be used.
    for p in pragmas {
        if matches!(p, PragmaConstraint::Exact(_)) {
            return Some(p.clone());
        }
    }

    // Normalize every constraint to a (lower, upper) range, then intersect.
    let mut lower = SemVer {
        major: 0,
        minor: 0,
        patch: 0,
    };
    let mut upper: Option<SemVer> = None;

    for p in pragmas {
        let (lo, hi) = constraint_to_range(p);
        if lo > lower {
            lower = lo;
        }
        if let Some(hi) = hi {
            upper = Some(match upper {
                Some(cur) if hi < cur => hi,
                Some(cur) => cur,
                None => hi,
            });
        }
    }

    match upper {
        Some(hi) if lower >= hi => None, // empty intersection
        Some(hi) => Some(PragmaConstraint::Range(lower, hi)),
        None => Some(PragmaConstraint::Gte(lower)),
    }
}

/// Convert a pragma constraint to an inclusive lower bound and optional
/// exclusive upper bound.
fn constraint_to_range(constraint: &PragmaConstraint) -> (SemVer, Option<SemVer>) {
    match constraint {
        PragmaConstraint::Exact(v) => (
            v.clone(),
            Some(SemVer {
                major: v.major,
                minor: v.minor,
                patch: v.patch + 1,
            }),
        ),
        PragmaConstraint::Caret(v) => (
            v.clone(),
            Some(SemVer {
                major: v.major,
                minor: v.minor + 1,
                patch: 0,
            }),
        ),
        PragmaConstraint::Gte(v) => (v.clone(), None),
        PragmaConstraint::Range(lo, hi) => (lo.clone(), Some(hi.clone())),
    }
}

/// Parse `pragma solidity <constraint>;` from Solidity source.
///
/// Handles:
/// - `pragma solidity 0.8.26;` → Exact
/// - `pragma solidity ^0.8.0;` → Caret
/// - `pragma solidity >=0.8.0;` → Gte
/// - `pragma solidity >=0.6.2 <0.9.0;` → Range
pub fn parse_pragma(source: &str) -> Option<PragmaConstraint> {
    // Find the pragma line — only scan the first ~20 lines for performance
    let pragma_line = source
        .lines()
        .take(20)
        .find(|line| line.trim_start().starts_with("pragma solidity"))?;

    // Extract the constraint string between "pragma solidity" and ";"
    let after_keyword = pragma_line
        .trim_start()
        .strip_prefix("pragma solidity")?
        .trim();
    let constraint_str = after_keyword
        .strip_suffix(';')
        .unwrap_or(after_keyword)
        .trim();

    if constraint_str.is_empty() {
        return None;
    }

    // Range: >=X.Y.Z <A.B.C
    if let Some(rest) = constraint_str.strip_prefix(">=") {
        let rest = rest.trim();
        if let Some(space_idx) = rest.find(|c: char| c.is_whitespace() || c == '<') {
            let lower_str = rest[..space_idx].trim();
            let upper_part = rest[space_idx..].trim();
            if let Some(upper_str) = upper_part.strip_prefix('<') {
                let upper_str = upper_str.trim();
                if let (Some(lower), Some(upper)) =
                    (SemVer::parse(lower_str), SemVer::parse(upper_str))
                {
                    return Some(PragmaConstraint::Range(lower, upper));
                }
            }
        }
        // Just >=X.Y.Z
        if let Some(ver) = SemVer::parse(rest) {
            return Some(PragmaConstraint::Gte(ver));
        }
    }

    // Caret: ^X.Y.Z
    if let Some(rest) = constraint_str.strip_prefix('^')
        && let Some(ver) = SemVer::parse(rest.trim())
    {
        return Some(PragmaConstraint::Caret(ver));
    }

    // Exact: X.Y.Z
    if let Some(ver) = SemVer::parse(constraint_str) {
        return Some(PragmaConstraint::Exact(ver));
    }

    None
}

/// List installed solc versions (cached — use `get_installed_versions()` internally).
pub fn list_installed_versions() -> Vec<SemVer> {
    get_installed_versions()
}

/// Scan the filesystem for installed solc versions using `svm::installed_versions()`.
///
/// Returns sorted, deduplicated versions (ascending).
fn scan_installed_versions() -> Vec<SemVer> {
    svm::installed_versions()
        .unwrap_or_default()
        .iter()
        .map(semver_to_local)
        .collect()
}

/// Find the best matching installed version for a pragma constraint.
///
/// For all constraint types, picks the **latest** installed version that
/// satisfies the constraint.
pub fn find_matching_version(
    constraint: &PragmaConstraint,
    installed: &[SemVer],
) -> Option<SemVer> {
    let candidates: Vec<&SemVer> = installed
        .iter()
        .filter(|v| version_satisfies(v, constraint))
        .collect();

    // Pick the latest (last, since installed is sorted ascending)
    candidates.last().cloned().cloned()
}

/// Check if a version satisfies a pragma constraint.
pub fn version_satisfies(version: &SemVer, constraint: &PragmaConstraint) -> bool {
    match constraint {
        PragmaConstraint::Exact(v) => version == v,
        PragmaConstraint::Caret(v) => {
            // Solidity caret: ^0.8.0 means >=0.8.0 <0.9.0
            // i.e. same major, next minor is the ceiling
            version.major == v.major && version >= v && version.minor < v.minor + 1
        }
        PragmaConstraint::Gte(v) => version >= v,
        PragmaConstraint::Range(lower, upper) => version >= lower && version < upper,
    }
}

/// Fetch remappings by running `forge remappings` in the project root.
///
/// Falls back to config remappings, then to an empty list.
pub async fn resolve_remappings(config: &FoundryConfig) -> Vec<String> {
    // Try `forge remappings` first — it merges all sources (foundry.toml,
    // remappings.txt, auto-detected libs).
    let output = Command::new("forge")
        .arg("remappings")
        .current_dir(&config.root)
        .env("FOUNDRY_DISABLE_NIGHTLY_WARNING", "1")
        .output()
        .await;

    if let Ok(output) = output
        && output.status.success()
    {
        let stdout = String::from_utf8_lossy(&output.stdout);
        let remappings: Vec<String> = stdout
            .lines()
            .filter(|l| !l.trim().is_empty())
            .map(|l| l.to_string())
            .collect();
        if !remappings.is_empty() {
            return remappings;
        }
    }

    // Fall back to remappings from foundry.toml
    if !config.remappings.is_empty() {
        return config.remappings.clone();
    }

    // Fall back to remappings.txt at project root
    let remappings_txt = config.root.join("remappings.txt");
    if let Ok(content) = std::fs::read_to_string(&remappings_txt) {
        return content
            .lines()
            .filter(|l| !l.trim().is_empty())
            .map(|l| l.to_string())
            .collect();
    }

    Vec::new()
}

/// Build the `--standard-json` input for solc.
///
/// Reads compiler settings from the `FoundryConfig` (parsed from `foundry.toml`)
/// and maps them to the solc standard JSON `settings` object:
///
/// - `via_ir` → `settings.viaIR`
/// - `evm_version` → `settings.evmVersion`
///
/// Note: `optimizer` and `evm.gasEstimates` are intentionally excluded.
/// The optimizer adds ~3s and doesn't affect AST/doc quality.
/// Gas estimates force solc through full EVM codegen — benchmarking on
/// a 510-file project showed 56s with vs 6s without (88% of cost).
/// Build a standard-json input for a single-file solc compilation.
///
/// Only the entry file is listed in `sources`.  Solc discovers imported
/// files on its own via the import callback and reads them from disk.
///
/// When `source_content` is provided, the text is inlined as `"content"`
/// so solc compiles the editor's live buffer instead of the on-disk version.
/// When `None`, solc reads the file from disk via `"urls"`.
pub fn build_standard_json_input(
    file_path: &str,
    remappings: &[String],
    config: &FoundryConfig,
    source_content: Option<&str>,
) -> Value {
    let contract_outputs = vec![
        "devdoc",
        "userdoc",
        "evm.methodIdentifiers",
        "storageLayout",
        "transientStorageLayout",
    ];

    let mut settings = json!({
        "remappings": remappings,
        "outputSelection": {
            "*": {
                "*": contract_outputs,
                "": ["ast"]
            }
        }
    });

    if config.via_ir {
        settings["viaIR"] = json!(true);
    }

    // EVM version
    if let Some(ref evm_version) = config.evm_version {
        settings["evmVersion"] = json!(evm_version);
    }

    let source_value = match source_content {
        Some(content) => json!({ "content": content }),
        None => json!({ "urls": [file_path] }),
    };

    // Build sources manually to avoid json! macro key interpolation issues.
    let mut sources = serde_json::Map::new();
    sources.insert(file_path.to_string(), source_value);

    json!({
        "language": "Solidity",
        "sources": sources,
        "settings": settings
    })
}

/// Run `solc --standard-json` and return the parsed output.
pub async fn run_solc(
    solc_binary: &Path,
    input: &Value,
    project_root: &Path,
) -> Result<Value, RunnerError> {
    let _ = crate::project_cache::save_last_solc_input(project_root, input);
    let input_str = serde_json::to_string(input)?;

    let mut child = Command::new(solc_binary)
        .arg("--standard-json")
        .current_dir(project_root)
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()?;

    // Write the standard-json input to solc's stdin.
    if let Some(mut stdin) = child.stdin.take() {
        use tokio::io::AsyncWriteExt;
        stdin
            .write_all(input_str.as_bytes())
            .await
            .map_err(RunnerError::CommandError)?;
        // Drop stdin to close it, signaling EOF to solc.
    }

    let output = child
        .wait_with_output()
        .await
        .map_err(RunnerError::CommandError)?;

    // solc writes JSON to stdout even on errors (errors are in the JSON)
    let stdout = String::from_utf8_lossy(&output.stdout);
    if stdout.trim().is_empty() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(RunnerError::CommandError(std::io::Error::other(format!(
            "solc produced no output, stderr: {stderr}"
        ))));
    }

    let parsed: Value = serde_json::from_str(&stdout)?;
    Ok(parsed)
}

/// Normalize raw solc `--standard-json` output into the canonical shape.
///
/// Solc's native shape is already close to canonical:
/// - `sources[path] = { id, ast }` — kept as-is
/// - `contracts[path][name] = { abi, evm, ... }` — kept as-is
/// - `errors` — kept as-is (defaults to `[]` if absent)
///
/// When `project_root` is provided, relative source paths are resolved to
/// absolute paths so that downstream code (goto, hover, links) can map AST
/// paths back to `file://` URIs. This is necessary because `solc_ast()`
/// passes a relative path to solc (to fix import resolution), and solc then
/// returns relative paths in the AST `absolutePath` and source keys.
///
/// Constructs `source_id_to_path` from source IDs for cross-file resolution.
///
/// Takes ownership and uses `Value::take()` to move AST nodes in-place,
/// avoiding expensive clones of multi-MB AST data.
///
/// Also resolves `absolutePath` on nested `ImportDirective` nodes so that
/// goto-definition on import strings works regardless of CWD.
pub fn normalize_solc_output(mut solc_output: Value, project_root: Option<&Path>) -> Value {
    /// Walk an AST node tree and resolve `absolutePath` on `ImportDirective` nodes.
    fn resolve_import_absolute_paths(node: &mut Value, resolve: &dyn Fn(&str) -> String) {
        let is_import = node.get("nodeType").and_then(|v| v.as_str()) == Some("ImportDirective");

        if is_import && let Some(abs_path) = node.get("absolutePath").and_then(|v| v.as_str()) {
            let resolved = resolve(abs_path);
            node.as_object_mut()
                .unwrap()
                .insert("absolutePath".to_string(), json!(resolved));
        }

        // Recurse into "nodes" array (top-level AST children)
        if let Some(nodes) = node.get_mut("nodes").and_then(|v| v.as_array_mut()) {
            for child in nodes {
                resolve_import_absolute_paths(child, resolve);
            }
        }
    }
    let mut result = Map::new();

    // Move errors out (defaults to [] if absent)
    let errors = solc_output
        .get_mut("errors")
        .map(Value::take)
        .unwrap_or_else(|| json!([]));
    result.insert("errors".to_string(), errors);

    // Helper: resolve a path to absolute using the project root.
    // If the path is already absolute or no project root is given, return as-is.
    let resolve = |p: &str| -> String {
        if let Some(root) = project_root {
            let path = Path::new(p);
            if path.is_relative() {
                return root.join(path).to_string_lossy().into_owned();
            }
        }
        p.to_string()
    };

    // Sources: rekey with absolute paths and update AST absolutePath fields.
    // Also build source_id_to_path for cross-file resolution.
    let mut source_id_to_path = Map::new();
    let mut resolved_sources = Map::new();

    if let Some(sources) = solc_output
        .get_mut("sources")
        .and_then(|s| s.as_object_mut())
    {
        // Collect keys first to avoid borrow issues
        let keys: Vec<String> = sources.keys().cloned().collect();
        for key in keys {
            if let Some(mut source_data) = sources.remove(&key) {
                let abs_key = resolve(&key);

                // Update the AST absolutePath field to match, and resolve
                // absolutePath on nested ImportDirective nodes so that
                // goto-definition works regardless of CWD.
                if let Some(ast) = source_data.get_mut("ast") {
                    if let Some(abs_path) = ast.get("absolutePath").and_then(|v| v.as_str()) {
                        let resolved = resolve(abs_path);
                        ast.as_object_mut()
                            .unwrap()
                            .insert("absolutePath".to_string(), json!(resolved));
                    }
                    resolve_import_absolute_paths(ast, &resolve);
                }

                if let Some(id) = source_data.get("id") {
                    source_id_to_path.insert(id.to_string(), json!(&abs_key));
                }

                resolved_sources.insert(abs_key, source_data);
            }
        }
    }

    result.insert("sources".to_string(), Value::Object(resolved_sources));

    // Contracts: rekey with absolute paths
    let mut resolved_contracts = Map::new();
    if let Some(contracts) = solc_output
        .get_mut("contracts")
        .and_then(|c| c.as_object_mut())
    {
        let keys: Vec<String> = contracts.keys().cloned().collect();
        for key in keys {
            if let Some(contract_data) = contracts.remove(&key) {
                resolved_contracts.insert(resolve(&key), contract_data);
            }
        }
    }
    result.insert("contracts".to_string(), Value::Object(resolved_contracts));

    // Construct source_id_to_path for cross-file resolution
    result.insert(
        "source_id_to_path".to_string(),
        Value::Object(source_id_to_path),
    );

    Value::Object(result)
}

/// Normalize forge `build --json --ast` output into the canonical shape.
///
/// Forge wraps data in arrays with metadata:
/// - `sources[path] = [{ source_file: { id, ast }, build_id, profile, version }]`
/// - `contracts[path][name] = [{ contract: { abi, evm, ... }, build_id, profile, version }]`
/// - `build_infos = [{ source_id_to_path: { ... } }]`
///
/// This unwraps to the canonical flat shape:
/// - `sources[path] = { id, ast }`
/// - `contracts[path][name] = { abi, evm, ... }`
/// - `source_id_to_path = { ... }`
pub fn normalize_forge_output(mut forge_output: Value) -> Value {
    let mut result = Map::new();

    // Move errors out
    let errors = forge_output
        .get_mut("errors")
        .map(Value::take)
        .unwrap_or_else(|| json!([]));
    result.insert("errors".to_string(), errors);

    // Unwrap sources: [{ source_file: { id, ast } }] → { id, ast }
    let mut normalized_sources = Map::new();
    if let Some(sources) = forge_output
        .get_mut("sources")
        .and_then(|s| s.as_object_mut())
    {
        for (path, entries) in sources.iter_mut() {
            if let Some(arr) = entries.as_array_mut()
                && let Some(first) = arr.first_mut()
                && let Some(sf) = first.get_mut("source_file")
            {
                normalized_sources.insert(path.clone(), sf.take());
            }
        }
    }
    result.insert("sources".to_string(), Value::Object(normalized_sources));

    // Unwrap contracts: [{ contract: { ... } }] → { ... }
    let mut normalized_contracts = Map::new();
    if let Some(contracts) = forge_output
        .get_mut("contracts")
        .and_then(|c| c.as_object_mut())
    {
        for (path, names) in contracts.iter_mut() {
            let mut path_contracts = Map::new();
            if let Some(names_obj) = names.as_object_mut() {
                for (name, entries) in names_obj.iter_mut() {
                    if let Some(arr) = entries.as_array_mut()
                        && let Some(first) = arr.first_mut()
                        && let Some(contract) = first.get_mut("contract")
                    {
                        path_contracts.insert(name.clone(), contract.take());
                    }
                }
            }
            normalized_contracts.insert(path.clone(), Value::Object(path_contracts));
        }
    }
    result.insert("contracts".to_string(), Value::Object(normalized_contracts));

    // Extract source_id_to_path from build_infos
    let source_id_to_path = forge_output
        .get_mut("build_infos")
        .and_then(|bi| bi.as_array_mut())
        .and_then(|arr| arr.first_mut())
        .and_then(|info| info.get_mut("source_id_to_path"))
        .map(Value::take)
        .unwrap_or_else(|| json!({}));
    result.insert("source_id_to_path".to_string(), source_id_to_path);

    Value::Object(result)
}

/// Run solc for a file and return normalized output.
///
/// This is the main entry point used by the LSP. Reads the file source
/// to detect the pragma version and resolve the correct solc binary.
///
/// When `content_cache` is provided, open editor buffers are fed to solc
/// When `source_content` is provided, the text is inlined so solc compiles
/// the editor's live buffer instead of reading from disk.
pub async fn solc_ast(
    file_path: &str,
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
    source_content: Option<&str>,
) -> Result<Value, RunnerError> {
    let remappings = resolve_remappings(config).await;

    // Collect pragma constraints from the file and all its transitive imports
    // so we pick a solc version that satisfies the entire dependency graph.
    // This is a synchronous recursive FS crawl — run it on the blocking pool
    // so we don't stall the tokio async runtime on large projects.
    let file_abs = Path::new(file_path).to_path_buf();
    let config_root = config.root.clone();
    let remappings_clone = remappings.clone();
    let pragmas = tokio::task::spawn_blocking(move || {
        collect_import_pragmas(&file_abs, &config_root, &remappings_clone)
    })
    .await
    .unwrap_or_default();
    let constraint = tightest_constraint(&pragmas);
    let solc_binary = resolve_solc_binary(config, constraint.as_ref(), client).await;

    // Solc's import resolver fails when sources use absolute paths — it resolves
    // 0 transitive imports, causing "No matching declaration found" errors for
    // inherited members. Convert to a path relative to the project root so solc
    // can properly resolve `src/`, `lib/`, and remapped imports.
    let rel_path = Path::new(file_path)
        .strip_prefix(&config.root)
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_else(|_| file_path.to_string());

    let input = build_standard_json_input(&rel_path, &remappings, config, source_content);
    let raw_output = run_solc(&solc_binary, &input, &config.root).await?;

    Ok(normalize_solc_output(raw_output, Some(&config.root)))
}

/// Run solc for build diagnostics (same output, just used for error extraction).
pub async fn solc_build(
    file_path: &str,
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
) -> Result<Value, RunnerError> {
    solc_ast(file_path, config, client, None).await
}

// ── Project-wide indexing ──────────────────────────────────────────────────

/// Discover all Solidity source files under the project root.
///
/// Walks the entire project directory, including `test/`, `script/`, and
/// any other user-authored directories. Only skips:
/// - Directories listed in `config.libs` (default: `["lib"]`)
/// - Directories in `DISCOVER_SKIP_DIRS` (build artifacts)
/// - Hidden directories (starting with `.`)
///
/// Includes `.t.sol` (test) and `.s.sol` (script) files so that
/// find-references and rename work across the full project.
/// Discover the project's own source files by walking only the directories
/// configured in `foundry.toml`: `src`, `test`, and `script`.
///
/// This mirrors how Forge discovers compilable files — it never walks
/// directories outside these three (plus libs).  Stray directories like
/// `certora/` or `hardhat/` are ignored, preventing broken imports from
/// poisoning the solc batch.
pub fn discover_source_files(config: &FoundryConfig) -> Vec<PathBuf> {
    discover_source_files_inner(config, false)
}

/// Discover only the `src` directory files (no test, no script).
///
/// Used as the seed set for phase-1 of two-phase project indexing, where
/// we want to compile only the production source closure first for fast
/// time-to-first-reference.
pub fn discover_src_only_files(config: &FoundryConfig) -> Vec<PathBuf> {
    let root = &config.root;
    if !root.is_dir() {
        return Vec::new();
    }
    let mut files = Vec::new();
    let dir = root.join(&config.sources_dir);
    if dir.is_dir() {
        discover_recursive(&dir, &[], &mut files);
    }
    files.sort();
    files
}

/// Discover the compilation closure seeded only from `src` files.
///
/// Like [`discover_compilation_closure`] but seeds only from
/// [`discover_src_only_files`] instead of all project directories.
/// This produces the minimal set of files needed to compile the
/// production source code, excluding test and script files.
pub fn discover_src_only_closure(config: &FoundryConfig, remappings: &[String]) -> Vec<PathBuf> {
    let seeds = discover_src_only_files(config);
    let mut visited: HashSet<PathBuf> = HashSet::new();
    let mut queue: std::collections::VecDeque<PathBuf> = seeds.into_iter().collect();

    while let Some(file) = queue.pop_front() {
        if !visited.insert(file.clone()) {
            continue;
        }
        let source = match std::fs::read_to_string(&file) {
            Ok(s) => s,
            Err(_) => continue,
        };
        for imp in links::ts_find_imports(source.as_bytes()) {
            if let Some(abs) = resolve_import_to_abs(&config.root, &file, &imp.path, remappings)
                && abs.exists()
                && !visited.contains(&abs)
            {
                queue.push_back(abs);
            }
        }
    }

    let mut result: Vec<PathBuf> = visited.into_iter().collect();
    result.sort();
    result
}

/// Discover source files including library directories.
///
/// When `fullProjectScan` is enabled, this includes files from the configured
/// `libs` directories (e.g. `dependencies/`, `node_modules/`).  Files with
/// incompatible pragma versions are handled by the error-driven retry loop
/// in [`solc_project_index_from_files`].
pub fn discover_source_files_with_libs(config: &FoundryConfig) -> Vec<PathBuf> {
    discover_source_files_inner(config, true)
}

fn discover_source_files_inner(config: &FoundryConfig, include_libs: bool) -> Vec<PathBuf> {
    let root = &config.root;
    if !root.is_dir() {
        return Vec::new();
    }

    let mut files = Vec::new();
    let no_skip: &[String] = &[];

    // Walk only the configured source directories (src, test, script).
    // This matches Forge's behaviour: only files under these three directories
    // are considered project sources.  Directories like `certora/`, `hardhat/`,
    // etc. are never seeded.
    for dir_name in [&config.sources_dir, &config.test_dir, &config.script_dir] {
        let dir = root.join(dir_name);
        if dir.is_dir() {
            discover_recursive(&dir, no_skip, &mut files);
        }
    }

    // When include_libs is requested, also walk lib directories.
    if include_libs {
        for lib_name in &config.libs {
            let lib_dir = root.join(lib_name);
            if lib_dir.is_dir() {
                discover_recursive(&lib_dir, no_skip, &mut files);
            }
        }
    }

    files.sort();
    files
}

/// Discover the true compilation closure by tracing imports from the
/// project's own source files (`src/`, `test/`, `script/`, and any other
/// non-lib top-level directories).
///
/// Starting from every `.sol` file returned by [`discover_source_files`]
/// (project files only, no lib dirs), this BFS-walks the import graph using
/// the provided remappings to resolve each `import` statement to an absolute
/// path.  It adds every reachable file — including lib files that are actually
/// imported — to the result set.
///
/// Files whose imports cannot be resolved (missing external deps that aren't
/// in this project) are silently skipped at that edge; the importer is still
/// included.
///
/// This produces a much smaller, self-consistent set than scanning all files
/// in lib directories, and avoids pulling in lib files that have broken
/// transitive deps (e.g. chainlink automation files that need `@eth-optimism`
/// which is not vendored here).
pub fn discover_compilation_closure(config: &FoundryConfig, remappings: &[String]) -> Vec<PathBuf> {
    // Seed: all project source files (no lib dirs).
    let seeds = discover_source_files(config);
    let mut visited: HashSet<PathBuf> = HashSet::new();
    let mut queue: std::collections::VecDeque<PathBuf> = seeds.into_iter().collect();

    while let Some(file) = queue.pop_front() {
        if !visited.insert(file.clone()) {
            continue;
        }
        let source = match std::fs::read_to_string(&file) {
            Ok(s) => s,
            Err(_) => continue,
        };
        for imp in links::ts_find_imports(source.as_bytes()) {
            if let Some(abs) = resolve_import_to_abs(&config.root, &file, &imp.path, remappings)
                && abs.exists()
                && !visited.contains(&abs)
            {
                queue.push_back(abs);
            }
        }
    }

    let mut result: Vec<PathBuf> = visited.into_iter().collect();
    result.sort();
    result
}

/// Directories that are always skipped during source file discovery,
/// regardless of the `include_libs` setting.
const DISCOVER_SKIP_DIRS: &[&str] = &["out", "artifacts", "cache", "target", "broadcast"];

fn discover_recursive(dir: &Path, skip_libs: &[String], files: &mut Vec<PathBuf>) {
    let entries = match std::fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return,
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                // Skip hidden directories (e.g., .git, .github)
                if name.starts_with('.') {
                    continue;
                }
                // Skip build artifact directories
                if DISCOVER_SKIP_DIRS.contains(&name) {
                    continue;
                }
                // Skip user-configured library directories (unless include_libs)
                if skip_libs.iter().any(|lib| lib == name) {
                    continue;
                }
            }
            discover_recursive(&path, skip_libs, files);
        } else if let Some(name) = path.file_name().and_then(|n| n.to_str())
            && name.ends_with(".sol")
        {
            files.push(path);
        }
    }
}

/// Build a `--standard-json` input that compiles all given source files at once.
///
/// Each file is added as a source entry with a `urls` field (relative to project root).
/// This produces a single AST covering the entire project in one solc invocation.
///
/// See [`build_standard_json_input`] for rationale on excluded settings.
pub fn build_batch_standard_json_input(
    source_files: &[PathBuf],
    remappings: &[String],
    config: &FoundryConfig,
) -> Value {
    build_batch_standard_json_input_with_cache(source_files, remappings, config, None)
}

/// Build a batch standard-json input for solc.
///
/// When `content_cache` is provided, files whose URI string appears as a key
/// are included with `"content"` (in-memory source).  Files not in the cache
/// fall back to `"urls"` (solc reads from disk).
///
/// This allows the re-index after a rename to feed solc the updated import
/// paths from our text_cache without requiring the editor to have flushed
/// them to disk yet.
pub fn build_batch_standard_json_input_with_cache(
    source_files: &[PathBuf],
    remappings: &[String],
    config: &FoundryConfig,
    content_cache: Option<&HashMap<crate::types::DocumentUri, (i32, String)>>,
) -> Value {
    let contract_outputs = vec![
        "devdoc",
        "userdoc",
        "evm.methodIdentifiers",
        "storageLayout",
        "transientStorageLayout",
    ];

    let mut settings = json!({
        "remappings": remappings,
        "outputSelection": {
            "*": {
                "*": contract_outputs,
                "": ["ast"]
            }
        }
    });

    if config.via_ir {
        settings["viaIR"] = json!(true);
    }
    if let Some(ref evm_version) = config.evm_version {
        settings["evmVersion"] = json!(evm_version);
    }

    let mut sources = serde_json::Map::new();
    for file in source_files {
        let rel_path = file
            .strip_prefix(&config.root)
            .map(|p| p.to_string_lossy().into_owned())
            .unwrap_or_else(|_| file.to_string_lossy().into_owned());

        // Try to use cached content so solc doesn't need to read from disk.
        let cached_content = content_cache.and_then(|cache| {
            let uri = Url::from_file_path(file).ok()?;
            cache.get(uri.as_str()).map(|(_, c)| c.as_str())
        });

        if let Some(content) = cached_content {
            sources.insert(rel_path, json!({ "content": content }));
        } else {
            sources.insert(rel_path.clone(), json!({ "urls": [rel_path] }));
        }
    }

    json!({
        "language": "Solidity",
        "sources": sources,
        "settings": settings
    })
}

/// Build an AST-only batch standard-json input for sub-cache builds.
///
/// Unlike the full batch input, this omits all codegen-affecting settings
/// (`viaIR`, `evmVersion`, optimizer) and only requests the AST — no
/// `devdoc`, `userdoc`, or `evm.methodIdentifiers`.  This is significantly
/// faster because solc skips type-checking contract outputs and codegen.
///
/// Sub-caches only need the AST for cross-file reference lookup (node IDs,
/// `referencedDeclaration`, source locations).
pub fn build_batch_standard_json_input_ast_only(
    source_files: &[PathBuf],
    remappings: &[String],
    root: &Path,
) -> Value {
    let settings = json!({
        "remappings": remappings,
        "outputSelection": {
            "*": {
                "": ["ast"]
            }
        }
    });

    let mut sources = serde_json::Map::new();
    for file in source_files {
        let rel_path = file
            .strip_prefix(root)
            .map(|p| p.to_string_lossy().into_owned())
            .unwrap_or_else(|_| file.to_string_lossy().into_owned());
        sources.insert(rel_path.clone(), json!({ "urls": [rel_path] }));
    }

    json!({
        "language": "Solidity",
        "sources": sources,
        "settings": settings
    })
}

/// Build a parse-only standard-json input (``stopAfter: "parsing"``).
///
/// Unlike the full batch input this mode stops before import resolution and
/// type-checking.  That means:
///
/// * No version 5333 errors cascade from imported incompatible files — the
///   compatible files are NOT fetched from disk as imports.
/// * The resulting ASTs contain all declaration nodes and local
///   ``referencedDeclaration`` IDs but **not** cross-file resolved IDs.
/// * Only ``ast`` output is requested; contract outputs (abi, gas …) are
///   omitted because they require type-checking.
///
/// This is used for the compatible-file batch in the mixed-version project
/// index so we can get parse-time ASTs for all project/lib files that satisfy
/// the project pragma, without being blocked by imports into incompatible lib
/// files.
pub fn build_parse_only_json_input(
    source_files: &[PathBuf],
    remappings: &[String],
    config: &FoundryConfig,
) -> Value {
    let settings = json!({
        "stopAfter": "parsing",
        "remappings": remappings,
        "outputSelection": {
            "*": {
                "": ["ast"]
            }
        }
    });

    let mut sources = serde_json::Map::new();
    for file in source_files {
        let rel_path = file
            .strip_prefix(&config.root)
            .map(|p| p.to_string_lossy().into_owned())
            .unwrap_or_else(|_| file.to_string_lossy().into_owned());
        sources.insert(rel_path.clone(), json!({ "urls": [rel_path] }));
    }

    json!({
        "language": "Solidity",
        "sources": sources,
        "settings": settings
    })
}

/// Run a project-wide solc compilation and return normalized output.
///
/// Discovers all source files, compiles them in a single `solc --standard-json`
/// invocation, and returns the normalized AST data.
///
/// When `text_cache` is provided, files whose URI string appears as a key
/// are fed to solc via `"content"` (in-memory) rather than `"urls"` (disk).
/// This ensures the re-index after a rename uses the updated import paths
/// from our cache, even if the editor hasn't flushed them to disk yet.
pub async fn solc_project_index(
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
    text_cache: Option<&HashMap<crate::types::DocumentUri, (i32, String)>>,
) -> Result<Value, RunnerError> {
    // Resolve remappings first — needed for import tracing.
    let remappings = resolve_remappings(config).await;

    // Trace imports from project source files to find the true compilation
    // closure.  This avoids pulling in lib files that are never imported by
    // the project (e.g. chainlink automation files that need @eth-optimism,
    // which isn't vendored here).
    let source_files = discover_compilation_closure(config, &remappings);
    if source_files.is_empty() {
        return Err(RunnerError::CommandError(std::io::Error::other(
            "no source files found for project index",
        )));
    }

    solc_project_index_from_files(config, client, text_cache, &source_files).await
}

/// AST-only project index for sub-cache builds.
///
/// Identical to [`solc_project_index`] but requests only AST output —
/// no `devdoc`, `userdoc`, or `evm.methodIdentifiers`.  Also omits
/// `viaIR`, `evmVersion`, and optimizer settings since they only affect
/// codegen (which is skipped when no contract outputs are requested).
///
/// This is significantly faster because solc skips all codegen work.
/// "Stack too deep" errors cannot occur in AST-only mode.
pub async fn solc_project_index_ast_only(
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
) -> Result<Value, RunnerError> {
    let remappings = resolve_remappings(config).await;
    let source_files = discover_compilation_closure(config, &remappings);
    if source_files.is_empty() {
        return Err(RunnerError::CommandError(std::io::Error::other(
            "no source files found for project index",
        )));
    }
    solc_project_index_from_files_ast_only(config, client, &source_files).await
}

/// AST-only compile over a list of source files.
///
/// Like [`solc_project_index_from_files`] but uses
/// [`build_batch_standard_json_input_ast_only`] — no codegen settings,
/// no contract outputs.
async fn solc_project_index_from_files_ast_only(
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
    source_files: &[PathBuf],
) -> Result<Value, RunnerError> {
    if source_files.is_empty() {
        return Err(RunnerError::CommandError(std::io::Error::other(
            "no source files found for AST-only project index",
        )));
    }

    let remappings = resolve_remappings(config).await;

    let project_version: Option<SemVer> =
        config.solc_version.as_ref().and_then(|v| SemVer::parse(v));
    let constraint: Option<PragmaConstraint> = if let Some(ref v) = project_version {
        Some(PragmaConstraint::Exact(v.clone()))
    } else {
        source_files.iter().find_map(|f| {
            std::fs::read_to_string(f)
                .ok()
                .and_then(|src| parse_pragma(&src))
        })
    };
    let solc_binary = resolve_solc_binary(config, constraint.as_ref(), client).await;

    // Pre-scan pragmas to separate compatible vs incompatible files.
    let (compatible_files, incompatible_files) = if let Some(ref ver) = project_version {
        let mut compat = Vec::with_capacity(source_files.len());
        let mut incompat = Vec::new();
        for file in source_files {
            let is_compatible = std::fs::read_to_string(file)
                .ok()
                .and_then(|src| parse_pragma(&src))
                .map(|pragma| version_satisfies(ver, &pragma))
                .unwrap_or(true);
            if is_compatible {
                compat.push(file.clone());
            } else {
                incompat.push(file.clone());
            }
        }
        (compat, incompat)
    } else {
        (source_files.to_vec(), Vec::new())
    };

    if !incompatible_files.is_empty()
        && let Some(c) = client
    {
        c.log_message(
            tower_lsp::lsp_types::MessageType::INFO,
            format!(
                "project index: {} compatible, {} incompatible with solc {}",
                compatible_files.len(),
                incompatible_files.len(),
                project_version
                    .as_ref()
                    .map(|v| v.to_string())
                    .unwrap_or_default(),
            ),
        )
        .await;
    }

    let mut result = if compatible_files.is_empty() {
        json!({"sources": {}, "contracts": {}, "errors": [], "source_id_to_path": {}})
    } else {
        let input =
            build_batch_standard_json_input_ast_only(&compatible_files, &remappings, &config.root);
        let raw = run_solc(&solc_binary, &input, &config.root).await?;
        normalize_solc_output(raw, Some(&config.root))
    };

    if incompatible_files.is_empty() {
        return Ok(result);
    }

    // Compile incompatible files individually with their own solc versions.
    for file in &incompatible_files {
        let pragma = std::fs::read_to_string(file)
            .ok()
            .and_then(|src| parse_pragma(&src));
        let file_binary = resolve_solc_binary(config, pragma.as_ref(), client).await;
        let input = build_batch_standard_json_input_ast_only(
            std::slice::from_ref(file),
            &remappings,
            &config.root,
        );
        if let Ok(raw) = run_solc(&file_binary, &input, &config.root).await {
            let normalized = normalize_solc_output(raw, Some(&config.root));
            merge_normalized_outputs(&mut result, normalized);
        }
    }

    if let Some(c) = client {
        let total = result
            .get("sources")
            .and_then(|s| s.as_object())
            .map_or(0, |obj| obj.len());
        c.log_message(
            tower_lsp::lsp_types::MessageType::INFO,
            format!(
                "project index: compiled {} files ({} needed different solc version)",
                total,
                incompatible_files.len(),
            ),
        )
        .await;
    }

    Ok(result)
}

/// Run a scoped project-index compile over a selected file list.
///
/// This is intended for aggressive incremental reindex strategies where only
/// a dependency-closure subset should be recompiled.
pub async fn solc_project_index_scoped(
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
    text_cache: Option<&HashMap<crate::types::DocumentUri, (i32, String)>>,
    source_files: &[PathBuf],
) -> Result<Value, RunnerError> {
    if source_files.is_empty() {
        return Err(RunnerError::CommandError(std::io::Error::other(
            "no source files provided for scoped project index",
        )));
    }

    solc_project_index_from_files(config, client, text_cache, source_files).await
}

/// Extract source file paths from solc error code 5333 ("Source file requires
/// different compiler version") errors.  Returns the relative paths exactly
/// as they appear in `sourceLocation.file`.
#[cfg(test)]
fn extract_version_error_files(solc_output: &Value) -> HashSet<String> {
    let mut files = HashSet::new();
    if let Some(errors) = solc_output.get("errors").and_then(|e| e.as_array()) {
        for err in errors {
            let is_5333 = err.get("errorCode").and_then(|c| c.as_str()) == Some("5333");
            if is_5333
                && let Some(file) = err
                    .get("sourceLocation")
                    .and_then(|sl| sl.get("file"))
                    .and_then(|f| f.as_str())
            {
                files.insert(file.to_string());
            }
        }
    }
    files
}

/// Extract source file paths from solc error code 6275 ("Source not found")
/// errors.  Returns the relative paths of source files whose imports failed.
#[cfg(test)]
#[allow(dead_code)]
fn extract_import_error_files(solc_output: &Value) -> HashSet<String> {
    let mut files = HashSet::new();
    if let Some(errors) = solc_output.get("errors").and_then(|e| e.as_array()) {
        for err in errors {
            let is_6275 = err.get("errorCode").and_then(|c| c.as_str()) == Some("6275");
            if is_6275
                && let Some(file) = err
                    .get("sourceLocation")
                    .and_then(|sl| sl.get("file"))
                    .and_then(|f| f.as_str())
            {
                files.insert(file.to_string());
            }
        }
    }
    files
}

/// Build a reverse-import closure: given a set of files to exclude, find all
/// files that transitively import any of them.  Those files must also be
/// excluded because solc will still resolve their imports from disk and fail.
///
/// Returns the full exclusion set (seed files + their transitive importers).
#[cfg(test)]
fn reverse_import_closure(
    source_files: &[PathBuf],
    exclude_abs: &HashSet<PathBuf>,
    project_root: &Path,
    remappings: &[String],
) -> HashSet<PathBuf> {
    // Build forward import graph: file -> set of files it imports.
    // Then invert to get reverse edges: imported_file -> set of importers.
    let mut reverse_edges: HashMap<PathBuf, HashSet<PathBuf>> = HashMap::new();

    for file in source_files {
        let Ok(bytes) = std::fs::read(file) else {
            continue;
        };
        for imp in links::ts_find_imports(&bytes) {
            if let Some(imported_abs) =
                resolve_import_to_abs(project_root, file, &imp.path, remappings)
            {
                reverse_edges
                    .entry(imported_abs)
                    .or_default()
                    .insert(file.clone());
            }
        }
    }

    // BFS from excluded files through reverse edges.
    let mut closure: HashSet<PathBuf> = exclude_abs.clone();
    let mut queue: std::collections::VecDeque<PathBuf> = exclude_abs.iter().cloned().collect();

    while let Some(current) = queue.pop_front() {
        if let Some(importers) = reverse_edges.get(&current) {
            for importer in importers {
                if closure.insert(importer.clone()) {
                    queue.push_back(importer.clone());
                }
            }
        }
    }

    closure
}

/// Merge two normalized solc outputs at the `Value` level.
///
/// Combines `sources`, `contracts`, `source_id_to_path`, and `errors` from
/// `other` into `base`.  Source IDs in `other` are remapped to avoid
/// collisions with `base`.
fn merge_normalized_outputs(base: &mut Value, other: Value) {
    // Merge sources (keyed by absolute path — no collisions across partitions).
    if let (Some(base_sources), Some(other_sources)) = (
        base.get_mut("sources").and_then(|s| s.as_object_mut()),
        other.get("sources").and_then(|s| s.as_object()),
    ) {
        // Find the max source ID in base so we can remap other's IDs.
        let max_base_id = base_sources
            .values()
            .filter_map(|v| v.get("id").and_then(|id| id.as_u64()))
            .max()
            .map(|m| m + 1)
            .unwrap_or(0);

        // Collect other's id -> path mappings for source_id_to_path.
        let mut remapped_id_to_path: Vec<(String, String)> = Vec::new();

        for (path, mut source_data) in other_sources.clone() {
            // Remap the source ID to avoid collisions.
            if let Some(id) = source_data.get("id").and_then(|id| id.as_u64()) {
                let new_id = id + max_base_id;
                source_data
                    .as_object_mut()
                    .unwrap()
                    .insert("id".to_string(), json!(new_id));
                remapped_id_to_path.push((new_id.to_string(), path.clone()));
            }
            base_sources.insert(path, source_data);
        }

        // Merge source_id_to_path.
        if let Some(base_id_map) = base
            .get_mut("source_id_to_path")
            .and_then(|m| m.as_object_mut())
        {
            for (id, path) in remapped_id_to_path {
                base_id_map.insert(id, json!(path));
            }
        }
    }

    // Merge contracts.
    if let (Some(base_contracts), Some(other_contracts)) = (
        base.get_mut("contracts").and_then(|c| c.as_object_mut()),
        other.get("contracts").and_then(|c| c.as_object()),
    ) {
        for (path, contract_data) in other_contracts {
            base_contracts.insert(path.clone(), contract_data.clone());
        }
    }

    // Don't merge errors — the retry errors from incompatible files are noise.
    // The base already has the clean errors from the successful compilation.
}

async fn solc_project_index_from_files(
    config: &FoundryConfig,
    client: Option<&tower_lsp::Client>,
    text_cache: Option<&HashMap<crate::types::DocumentUri, (i32, String)>>,
    source_files: &[PathBuf],
) -> Result<Value, RunnerError> {
    if source_files.is_empty() {
        return Err(RunnerError::CommandError(std::io::Error::other(
            "no source files found for project index",
        )));
    }

    let remappings = resolve_remappings(config).await;

    // Resolve the project's solc version from foundry.toml.
    let project_version: Option<SemVer> =
        config.solc_version.as_ref().and_then(|v| SemVer::parse(v));

    // When no version is pinned in foundry.toml, derive a constraint from the
    // source files' pragmas so that svm can auto-install a matching binary.
    let constraint: Option<PragmaConstraint> = if let Some(ref v) = project_version {
        Some(PragmaConstraint::Exact(v.clone()))
    } else {
        source_files.iter().find_map(|f| {
            std::fs::read_to_string(f)
                .ok()
                .and_then(|src| parse_pragma(&src))
        })
    };
    let solc_binary = resolve_solc_binary(config, constraint.as_ref(), client).await;

    // -- Pre-scan pragmas to separate compatible vs incompatible files. --
    //
    // Solc emits ZERO ASTs when any file in the batch has a version error
    // (5333).  We must exclude incompatible files before compiling so the
    // batch produces full AST output for all compatible files.
    let (compatible_files, incompatible_files) = if let Some(ref ver) = project_version {
        let mut compat = Vec::with_capacity(source_files.len());
        let mut incompat = Vec::new();
        for file in source_files {
            let is_compatible = std::fs::read_to_string(file)
                .ok()
                .and_then(|src| parse_pragma(&src))
                .map(|pragma| version_satisfies(ver, &pragma))
                // Files without a pragma are assumed compatible.
                .unwrap_or(true);
            if is_compatible {
                compat.push(file.clone());
            } else {
                incompat.push(file.clone());
            }
        }
        (compat, incompat)
    } else {
        // No project version configured — compile everything in one batch.
        (source_files.to_vec(), Vec::new())
    };

    if !incompatible_files.is_empty()
        && let Some(c) = client
    {
        c.log_message(
            tower_lsp::lsp_types::MessageType::INFO,
            format!(
                "project index: {} compatible, {} incompatible with solc {}",
                compatible_files.len(),
                incompatible_files.len(),
                project_version
                    .as_ref()
                    .map(|v| v.to_string())
                    .unwrap_or_default(),
            ),
        )
        .await;
    }

    // -- Full batch compile of compatible files. --
    //
    // The source file list comes from discover_compilation_closure which only
    // includes files reachable via imports from src/test/script — so all files
    // in the batch are version-compatible and their transitive imports resolve.
    // A full (non-parse-only) compile is required so that cross-file
    // referencedDeclaration IDs are populated for goto-references to work.
    let mut result = if compatible_files.is_empty() {
        json!({"sources": {}, "contracts": {}, "errors": [], "source_id_to_path": {}})
    } else {
        let input = build_batch_standard_json_input_with_cache(
            &compatible_files,
            &remappings,
            config,
            text_cache,
        );
        let raw = run_solc(&solc_binary, &input, &config.root).await?;
        normalize_solc_output(raw, Some(&config.root))
    };

    let batch_source_count = result
        .get("sources")
        .and_then(|s| s.as_object())
        .map_or(0, |obj| obj.len());

    if incompatible_files.is_empty() {
        return Ok(result);
    }

    if let Some(c) = client {
        // Log first few errors from the batch to understand why sources=0.
        let batch_errors: Vec<String> = result
            .get("errors")
            .and_then(|e| e.as_array())
            .map(|arr| {
                arr.iter()
                    .filter(|e| e.get("severity").and_then(|s| s.as_str()) == Some("error"))
                    .take(3)
                    .map(|e| {
                        let msg = e.get("message").and_then(|m| m.as_str()).unwrap_or("?");
                        let file = e
                            .get("sourceLocation")
                            .and_then(|sl| sl.get("file"))
                            .and_then(|f| f.as_str())
                            .unwrap_or("?");
                        format!("{file}: {msg}")
                    })
                    .collect()
            })
            .unwrap_or_default();

        c.log_message(
            tower_lsp::lsp_types::MessageType::INFO,
            format!(
                "project index: batch produced {} sources, now compiling {} incompatible files individually{}",
                batch_source_count,
                incompatible_files.len(),
                if batch_errors.is_empty() {
                    String::new()
                } else {
                    format!(" [first errors: {}]", batch_errors.join("; "))
                },
            ),
        )
        .await;
    }

    // -- Individually compile incompatible files with their matching solc. --
    let mut compiled = 0usize;
    let mut skipped = 0usize;
    for file in &incompatible_files {
        let pragma = std::fs::read_to_string(file)
            .ok()
            .and_then(|src| parse_pragma(&src));

        let Some(file_constraint) = pragma else {
            skipped += 1;
            continue;
        };

        let file_binary = resolve_solc_binary(config, Some(&file_constraint), client).await;
        let input = build_batch_standard_json_input_with_cache(
            std::slice::from_ref(file),
            &remappings,
            config,
            text_cache,
        );
        match run_solc(&file_binary, &input, &config.root).await {
            Ok(raw) => {
                let normalized = normalize_solc_output(raw, Some(&config.root));
                merge_normalized_outputs(&mut result, normalized);
                compiled += 1;
            }
            Err(e) => {
                if let Some(c) = client {
                    c.log_message(
                        tower_lsp::lsp_types::MessageType::WARNING,
                        format!(
                            "project index: incompatible file {} failed: {e}",
                            file.display(),
                        ),
                    )
                    .await;
                }
                skipped += 1;
            }
        }
    }

    if let Some(c) = client {
        c.log_message(
            tower_lsp::lsp_types::MessageType::INFO,
            format!(
                "project index: incompatible files done — {compiled} compiled, {skipped} skipped",
            ),
        )
        .await;
    }

    Ok(result)
}

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

    #[test]
    fn test_normalize_solc_sources() {
        let solc_output = json!({
            "sources": {
                "src/Foo.sol": {
                    "id": 0,
                    "ast": {
                        "nodeType": "SourceUnit",
                        "absolutePath": "src/Foo.sol",
                        "id": 100
                    }
                },
                "src/Bar.sol": {
                    "id": 1,
                    "ast": {
                        "nodeType": "SourceUnit",
                        "absolutePath": "src/Bar.sol",
                        "id": 200
                    }
                }
            },
            "contracts": {},
            "errors": []
        });

        let normalized = normalize_solc_output(solc_output, None);

        // Sources kept in solc-native shape: path -> { id, ast }
        let sources = normalized.get("sources").unwrap().as_object().unwrap();
        assert_eq!(sources.len(), 2);

        let foo = sources.get("src/Foo.sol").unwrap();
        assert_eq!(foo.get("id").unwrap(), 0);
        assert_eq!(
            foo.get("ast")
                .unwrap()
                .get("nodeType")
                .unwrap()
                .as_str()
                .unwrap(),
            "SourceUnit"
        );

        // Check source_id_to_path constructed
        let id_to_path = normalized
            .get("source_id_to_path")
            .unwrap()
            .as_object()
            .unwrap();
        assert_eq!(id_to_path.len(), 2);
    }

    #[test]
    fn test_normalize_solc_contracts() {
        let solc_output = json!({
            "sources": {},
            "contracts": {
                "src/Foo.sol": {
                    "Foo": {
                        "abi": [{"type": "function", "name": "bar"}],
                        "evm": {
                            "methodIdentifiers": {
                                "bar(uint256)": "abcd1234"
                            }
                        }
                    }
                }
            },
            "errors": []
        });

        let normalized = normalize_solc_output(solc_output, None);

        // Contracts kept in solc-native shape: path -> name -> { abi, evm, ... }
        let contracts = normalized.get("contracts").unwrap().as_object().unwrap();
        let foo_contracts = contracts.get("src/Foo.sol").unwrap().as_object().unwrap();
        let foo = foo_contracts.get("Foo").unwrap();

        let method_ids = foo
            .get("evm")
            .unwrap()
            .get("methodIdentifiers")
            .unwrap()
            .as_object()
            .unwrap();
        assert_eq!(
            method_ids.get("bar(uint256)").unwrap().as_str().unwrap(),
            "abcd1234"
        );
    }

    #[test]
    fn test_normalize_solc_errors_passthrough() {
        let solc_output = json!({
            "sources": {},
            "contracts": {},
            "errors": [{
                "sourceLocation": {"file": "src/Foo.sol", "start": 0, "end": 10},
                "type": "Warning",
                "component": "general",
                "severity": "warning",
                "errorCode": "2394",
                "message": "test warning",
                "formattedMessage": "Warning: test warning"
            }]
        });

        let normalized = normalize_solc_output(solc_output, None);

        let errors = normalized.get("errors").unwrap().as_array().unwrap();
        assert_eq!(errors.len(), 1);
        assert_eq!(
            errors[0].get("errorCode").unwrap().as_str().unwrap(),
            "2394"
        );
    }

    #[test]
    fn test_normalize_empty_solc_output() {
        let solc_output = json!({
            "sources": {},
            "contracts": {}
        });

        let normalized = normalize_solc_output(solc_output, None);

        assert!(
            normalized
                .get("sources")
                .unwrap()
                .as_object()
                .unwrap()
                .is_empty()
        );
        assert!(
            normalized
                .get("contracts")
                .unwrap()
                .as_object()
                .unwrap()
                .is_empty()
        );
        assert_eq!(
            normalized.get("errors").unwrap().as_array().unwrap().len(),
            0
        );
        assert!(
            normalized
                .get("source_id_to_path")
                .unwrap()
                .as_object()
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn test_build_standard_json_input() {
        let config = FoundryConfig::default();
        let input = build_standard_json_input(
            "/path/to/Foo.sol",
            &[
                "ds-test/=lib/forge-std/lib/ds-test/src/".to_string(),
                "forge-std/=lib/forge-std/src/".to_string(),
            ],
            &config,
            None,
        );

        let sources = input.get("sources").unwrap().as_object().unwrap();
        assert!(sources.contains_key("/path/to/Foo.sol"));

        let settings = input.get("settings").unwrap();
        let remappings = settings.get("remappings").unwrap().as_array().unwrap();
        assert_eq!(remappings.len(), 2);

        let output_sel = settings.get("outputSelection").unwrap();
        assert!(output_sel.get("*").is_some());

        // Default config: no optimizer, no viaIR, no evmVersion
        assert!(settings.get("optimizer").is_none());
        assert!(settings.get("viaIR").is_none());
        assert!(settings.get("evmVersion").is_none());

        // gasEstimates is never requested — forces full EVM codegen (88% of compile time)
        let outputs = settings["outputSelection"]["*"]["*"].as_array().unwrap();
        let output_names: Vec<&str> = outputs.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(!output_names.contains(&"evm.gasEstimates"));
        assert!(!output_names.contains(&"abi")); // ABI is intentionally omitted — no consumer
        assert!(output_names.contains(&"devdoc"));
        assert!(output_names.contains(&"userdoc"));
        assert!(output_names.contains(&"evm.methodIdentifiers"));
    }

    #[test]
    fn test_build_standard_json_input_with_config() {
        let config = FoundryConfig {
            optimizer: true,
            optimizer_runs: 9999999,
            via_ir: true,
            evm_version: Some("osaka".to_string()),
            ..Default::default()
        };
        let input = build_standard_json_input("/path/to/Foo.sol", &[], &config, None);

        let settings = input.get("settings").unwrap();

        // Optimizer is never passed — adds ~3s and doesn't affect AST/ABI/docs
        assert!(settings.get("optimizer").is_none());

        // viaIR IS passed when config has it (some contracts require it to compile)
        assert!(settings.get("viaIR").unwrap().as_bool().unwrap());

        // gasEstimates is never requested regardless of viaIR
        let outputs = settings["outputSelection"]["*"]["*"].as_array().unwrap();
        let output_names: Vec<&str> = outputs.iter().map(|v| v.as_str().unwrap()).collect();
        assert!(!output_names.contains(&"evm.gasEstimates"));

        // EVM version
        assert_eq!(
            settings.get("evmVersion").unwrap().as_str().unwrap(),
            "osaka"
        );
    }

    #[tokio::test]
    async fn test_resolve_solc_binary_default() {
        let config = FoundryConfig::default();
        let binary = resolve_solc_binary(&config, None, None).await;
        assert_eq!(binary, PathBuf::from("solc"));
    }

    #[test]
    fn test_parse_pragma_exact() {
        let source = "// SPDX\npragma solidity 0.8.26;\n";
        assert_eq!(
            parse_pragma(source),
            Some(PragmaConstraint::Exact(SemVer {
                major: 0,
                minor: 8,
                patch: 26
            }))
        );
    }

    #[test]
    fn test_parse_pragma_caret() {
        let source = "pragma solidity ^0.8.0;\n";
        assert_eq!(
            parse_pragma(source),
            Some(PragmaConstraint::Caret(SemVer {
                major: 0,
                minor: 8,
                patch: 0
            }))
        );
    }

    #[test]
    fn test_parse_pragma_gte() {
        let source = "pragma solidity >=0.8.0;\n";
        assert_eq!(
            parse_pragma(source),
            Some(PragmaConstraint::Gte(SemVer {
                major: 0,
                minor: 8,
                patch: 0
            }))
        );
    }

    #[test]
    fn test_parse_pragma_range() {
        let source = "pragma solidity >=0.6.2 <0.9.0;\n";
        assert_eq!(
            parse_pragma(source),
            Some(PragmaConstraint::Range(
                SemVer {
                    major: 0,
                    minor: 6,
                    patch: 2
                },
                SemVer {
                    major: 0,
                    minor: 9,
                    patch: 0
                },
            ))
        );
    }

    #[test]
    fn test_parse_pragma_none() {
        let source = "contract Foo {}\n";
        assert_eq!(parse_pragma(source), None);
    }

    #[test]
    fn test_version_satisfies_exact() {
        let v = SemVer {
            major: 0,
            minor: 8,
            patch: 26,
        };
        assert!(version_satisfies(&v, &PragmaConstraint::Exact(v.clone())));
        assert!(!version_satisfies(
            &SemVer {
                major: 0,
                minor: 8,
                patch: 25
            },
            &PragmaConstraint::Exact(v)
        ));
    }

    #[test]
    fn test_version_satisfies_caret() {
        let constraint = PragmaConstraint::Caret(SemVer {
            major: 0,
            minor: 8,
            patch: 0,
        });
        assert!(version_satisfies(
            &SemVer {
                major: 0,
                minor: 8,
                patch: 0
            },
            &constraint
        ));
        assert!(version_satisfies(
            &SemVer {
                major: 0,
                minor: 8,
                patch: 26
            },
            &constraint
        ));
        // 0.9.0 is outside ^0.8.0
        assert!(!version_satisfies(
            &SemVer {
                major: 0,
                minor: 9,
                patch: 0
            },
            &constraint
        ));
        // 0.7.0 is below
        assert!(!version_satisfies(
            &SemVer {
                major: 0,
                minor: 7,
                patch: 0
            },
            &constraint
        ));
    }

    #[test]
    fn test_version_satisfies_gte() {
        let constraint = PragmaConstraint::Gte(SemVer {
            major: 0,
            minor: 8,
            patch: 0,
        });
        assert!(version_satisfies(
            &SemVer {
                major: 0,
                minor: 8,
                patch: 0
            },
            &constraint
        ));
        assert!(version_satisfies(
            &SemVer {
                major: 0,
                minor: 9,
                patch: 0
            },
            &constraint
        ));
        assert!(!version_satisfies(
            &SemVer {
                major: 0,
                minor: 7,
                patch: 0
            },
            &constraint
        ));
    }

    #[test]
    fn test_version_satisfies_range() {
        let constraint = PragmaConstraint::Range(
            SemVer {
                major: 0,
                minor: 6,
                patch: 2,
            },
            SemVer {
                major: 0,
                minor: 9,
                patch: 0,
            },
        );
        assert!(version_satisfies(
            &SemVer {
                major: 0,
                minor: 6,
                patch: 2
            },
            &constraint
        ));
        assert!(version_satisfies(
            &SemVer {
                major: 0,
                minor: 8,
                patch: 26
            },
            &constraint
        ));
        // 0.9.0 is the upper bound (exclusive)
        assert!(!version_satisfies(
            &SemVer {
                major: 0,
                minor: 9,
                patch: 0
            },
            &constraint
        ));
        assert!(!version_satisfies(
            &SemVer {
                major: 0,
                minor: 6,
                patch: 1
            },
            &constraint
        ));
    }

    #[test]
    fn test_find_matching_version() {
        let installed = vec![
            SemVer {
                major: 0,
                minor: 8,
                patch: 0,
            },
            SemVer {
                major: 0,
                minor: 8,
                patch: 20,
            },
            SemVer {
                major: 0,
                minor: 8,
                patch: 26,
            },
            SemVer {
                major: 0,
                minor: 8,
                patch: 33,
            },
        ];
        // ^0.8.20 should pick latest: 0.8.33
        let constraint = PragmaConstraint::Caret(SemVer {
            major: 0,
            minor: 8,
            patch: 20,
        });
        let matched = find_matching_version(&constraint, &installed);
        assert_eq!(
            matched,
            Some(SemVer {
                major: 0,
                minor: 8,
                patch: 33
            })
        );

        // exact 0.8.20
        let constraint = PragmaConstraint::Exact(SemVer {
            major: 0,
            minor: 8,
            patch: 20,
        });
        let matched = find_matching_version(&constraint, &installed);
        assert_eq!(
            matched,
            Some(SemVer {
                major: 0,
                minor: 8,
                patch: 20
            })
        );

        // exact 0.8.15 — not installed
        let constraint = PragmaConstraint::Exact(SemVer {
            major: 0,
            minor: 8,
            patch: 15,
        });
        let matched = find_matching_version(&constraint, &installed);
        assert_eq!(matched, None);
    }

    #[test]
    fn test_list_installed_versions() {
        // Just verify it doesn't panic — actual versions depend on system
        let versions = list_installed_versions();
        // Versions should be sorted
        for w in versions.windows(2) {
            assert!(w[0] <= w[1]);
        }
    }

    // -------------------------------------------------------------------
    // Tests for mixed-version retry helpers
    // -------------------------------------------------------------------

    #[test]
    fn test_extract_version_error_files_basic() {
        let output = json!({
            "errors": [
                {
                    "errorCode": "5333",
                    "severity": "error",
                    "message": "Source file requires different compiler version",
                    "sourceLocation": {
                        "file": "lib/openzeppelin/contracts/token/ERC20/ERC20.sol",
                        "start": 32,
                        "end": 58
                    }
                },
                {
                    "errorCode": "5333",
                    "severity": "error",
                    "message": "Source file requires different compiler version",
                    "sourceLocation": {
                        "file": "lib/old-lib/src/Legacy.sol",
                        "start": 32,
                        "end": 58
                    }
                },
                {
                    "errorCode": "9574",
                    "severity": "error",
                    "message": "Some other error",
                    "sourceLocation": {
                        "file": "src/Main.sol",
                        "start": 100,
                        "end": 200
                    }
                }
            ]
        });

        let files = extract_version_error_files(&output);
        assert_eq!(files.len(), 2);
        assert!(files.contains("lib/openzeppelin/contracts/token/ERC20/ERC20.sol"));
        assert!(files.contains("lib/old-lib/src/Legacy.sol"));
        // Non-5333 error files should NOT be included.
        assert!(!files.contains("src/Main.sol"));
    }

    #[test]
    fn test_extract_version_error_files_empty() {
        let output = json!({
            "errors": []
        });
        assert!(extract_version_error_files(&output).is_empty());

        // No errors key at all.
        let output = json!({});
        assert!(extract_version_error_files(&output).is_empty());
    }

    #[test]
    fn test_extract_version_error_files_no_source_location() {
        let output = json!({
            "errors": [
                {
                    "errorCode": "5333",
                    "severity": "error",
                    "message": "Source file requires different compiler version"
                    // No sourceLocation field.
                }
            ]
        });
        assert!(extract_version_error_files(&output).is_empty());
    }

    #[test]
    fn test_extract_version_error_files_dedup() {
        let output = json!({
            "errors": [
                {
                    "errorCode": "5333",
                    "severity": "error",
                    "sourceLocation": { "file": "lib/same.sol", "start": 0, "end": 10 }
                },
                {
                    "errorCode": "5333",
                    "severity": "error",
                    "sourceLocation": { "file": "lib/same.sol", "start": 50, "end": 70 }
                }
            ]
        });
        let files = extract_version_error_files(&output);
        assert_eq!(files.len(), 1);
        assert!(files.contains("lib/same.sol"));
    }

    #[test]
    fn test_reverse_import_closure_simple() {
        // Create a temp directory with three files:
        //   a.sol imports b.sol
        //   b.sol imports c.sol
        //   d.sol (standalone)
        //
        // If c.sol is excluded, the closure should include: c.sol, b.sol, a.sol
        // (b imports c, a imports b — both are transitive importers of c).
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        std::fs::write(
            root.join("a.sol"),
            "// SPDX-License-Identifier: MIT\nimport \"./b.sol\";\ncontract A {}",
        )
        .unwrap();
        std::fs::write(
            root.join("b.sol"),
            "// SPDX-License-Identifier: MIT\nimport \"./c.sol\";\ncontract B {}",
        )
        .unwrap();
        std::fs::write(
            root.join("c.sol"),
            "// SPDX-License-Identifier: MIT\ncontract C {}",
        )
        .unwrap();
        std::fs::write(
            root.join("d.sol"),
            "// SPDX-License-Identifier: MIT\ncontract D {}",
        )
        .unwrap();

        let files: Vec<PathBuf> = vec![
            root.join("a.sol"),
            root.join("b.sol"),
            root.join("c.sol"),
            root.join("d.sol"),
        ];

        let exclude: HashSet<PathBuf> = [root.join("c.sol")].into_iter().collect();
        let closure = reverse_import_closure(&files, &exclude, root, &[]);

        assert!(
            closure.contains(&root.join("c.sol")),
            "seed file in closure"
        );
        assert!(closure.contains(&root.join("b.sol")), "direct importer");
        assert!(closure.contains(&root.join("a.sol")), "transitive importer");
        assert!(
            !closure.contains(&root.join("d.sol")),
            "unrelated file not in closure"
        );
        assert_eq!(closure.len(), 3);
    }

    #[test]
    fn test_reverse_import_closure_no_importers() {
        // Excluding a file that nothing imports — closure is just the seed.
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        std::fs::write(root.join("a.sol"), "contract A {}").unwrap();
        std::fs::write(root.join("b.sol"), "contract B {}").unwrap();

        let files: Vec<PathBuf> = vec![root.join("a.sol"), root.join("b.sol")];
        let exclude: HashSet<PathBuf> = [root.join("a.sol")].into_iter().collect();

        let closure = reverse_import_closure(&files, &exclude, root, &[]);
        assert_eq!(closure.len(), 1);
        assert!(closure.contains(&root.join("a.sol")));
    }

    #[test]
    fn test_reverse_import_closure_diamond() {
        // Diamond pattern:
        //   a.sol imports b.sol and c.sol
        //   b.sol imports d.sol
        //   c.sol imports d.sol
        //
        // Excluding d.sol → closure = {d, b, c, a}
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        std::fs::write(
            root.join("a.sol"),
            "import \"./b.sol\";\nimport \"./c.sol\";\ncontract A {}",
        )
        .unwrap();
        std::fs::write(root.join("b.sol"), "import \"./d.sol\";\ncontract B {}").unwrap();
        std::fs::write(root.join("c.sol"), "import \"./d.sol\";\ncontract C {}").unwrap();
        std::fs::write(root.join("d.sol"), "contract D {}").unwrap();

        let files: Vec<PathBuf> = vec![
            root.join("a.sol"),
            root.join("b.sol"),
            root.join("c.sol"),
            root.join("d.sol"),
        ];
        let exclude: HashSet<PathBuf> = [root.join("d.sol")].into_iter().collect();

        let closure = reverse_import_closure(&files, &exclude, root, &[]);
        assert_eq!(closure.len(), 4);
    }

    #[test]
    fn test_merge_normalized_outputs_basic() {
        let mut base = json!({
            "sources": {
                "/abs/src/A.sol": { "id": 0, "ast": { "nodeType": "SourceUnit" } },
                "/abs/src/B.sol": { "id": 1, "ast": { "nodeType": "SourceUnit" } }
            },
            "contracts": {
                "/abs/src/A.sol": { "A": { "abi": [] } }
            },
            "errors": [],
            "source_id_to_path": {
                "0": "/abs/src/A.sol",
                "1": "/abs/src/B.sol"
            }
        });

        let other = json!({
            "sources": {
                "/abs/lib/C.sol": { "id": 0, "ast": { "nodeType": "SourceUnit" } }
            },
            "contracts": {
                "/abs/lib/C.sol": { "C": { "abi": [] } }
            },
            "errors": [],
            "source_id_to_path": {
                "0": "/abs/lib/C.sol"
            }
        });

        merge_normalized_outputs(&mut base, other);

        // Sources should now have 3 entries.
        let sources = base["sources"].as_object().unwrap();
        assert_eq!(sources.len(), 3);
        assert!(sources.contains_key("/abs/lib/C.sol"));

        // The merged source's ID should be remapped (0 + max_base_id=2 → 2).
        let c_id = sources["/abs/lib/C.sol"]["id"].as_u64().unwrap();
        assert_eq!(
            c_id, 2,
            "remapped id should be max_base_id (2) + original (0)"
        );

        // source_id_to_path should have 3 entries.
        let id_map = base["source_id_to_path"].as_object().unwrap();
        assert_eq!(id_map.len(), 3);
        assert_eq!(id_map["2"].as_str().unwrap(), "/abs/lib/C.sol");

        // Contracts should have 2 entries.
        let contracts = base["contracts"].as_object().unwrap();
        assert_eq!(contracts.len(), 2);
        assert!(contracts.contains_key("/abs/lib/C.sol"));
    }

    #[test]
    fn test_merge_normalized_outputs_empty_other() {
        let mut base = json!({
            "sources": {
                "/abs/src/A.sol": { "id": 0, "ast": {} }
            },
            "contracts": {},
            "errors": [],
            "source_id_to_path": { "0": "/abs/src/A.sol" }
        });

        let other = json!({
            "sources": {},
            "contracts": {},
            "errors": [],
            "source_id_to_path": {}
        });

        merge_normalized_outputs(&mut base, other);

        let sources = base["sources"].as_object().unwrap();
        assert_eq!(sources.len(), 1);
    }

    #[test]
    fn test_merge_normalized_outputs_empty_base() {
        let mut base = json!({
            "sources": {},
            "contracts": {},
            "errors": [],
            "source_id_to_path": {}
        });

        let other = json!({
            "sources": {
                "/abs/lib/X.sol": { "id": 0, "ast": {} }
            },
            "contracts": {
                "/abs/lib/X.sol": { "X": { "abi": [] } }
            },
            "errors": [],
            "source_id_to_path": { "0": "/abs/lib/X.sol" }
        });

        merge_normalized_outputs(&mut base, other);

        let sources = base["sources"].as_object().unwrap();
        assert_eq!(sources.len(), 1);
        // max_base_id is 0 (no entries), so remapped id = 0 + 0 = 0.
        let x_id = sources["/abs/lib/X.sol"]["id"].as_u64().unwrap();
        assert_eq!(x_id, 0);
    }
}