fallow-config 3.40.0

Configuration types and workspace discovery for fallow codebase intelligence
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
use std::net::Ipv6Addr;
use std::path::{Path, PathBuf};
use std::time::Duration;

use fallow_types::path_util::is_absolute_path_any_platform;
use rustc_hash::{FxHashMap, FxHashSet};

use super::FallowConfig;

/// Config file names the loader accepts, in discovery priority order.
///
/// Public because surfaces outside config loading have to react to the same
/// set: the language server registers a file watcher per name, so a name added
/// here reaches the editor without a second list to keep in step.
pub const CONFIG_FILE_NAMES: &[&str] = &[
    ".fallowrc.json",
    ".fallowrc.jsonc",
    "fallow.toml",
    ".fallow.toml",
];

/// Supported config file names in priority order.
const CONFIG_NAMES: &[&str] = CONFIG_FILE_NAMES;

const MAX_EXTENDS_DEPTH: usize = 10;

/// Prefix for npm package specifiers in the `extends` field.
const NPM_PREFIX: &str = "npm:";

/// Prefix for HTTPS URL specifiers in the `extends` field.
const HTTPS_PREFIX: &str = "https://";

/// Prefix for HTTP URL specifiers (rejected with a clear error).
const HTTP_PREFIX: &str = "http://";

/// Default timeout for fetching remote configs via URL extends.
const DEFAULT_URL_TIMEOUT_SECS: u64 = 5;

/// Host-controlled trust policy for loading a fallow config.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ConfigLoadOptions {
    /// Permit `https://` entries in the config inheritance graph.
    pub allow_remote_extends: bool,
}

/// Detect config format from file extension.
pub(super) enum ConfigFormat {
    Toml,
    Json,
}

impl ConfigFormat {
    pub(super) fn from_path(path: &Path) -> Self {
        match path.extension().and_then(|e| e.to_str()) {
            Some("json" | "jsonc") => Self::Json,
            _ => Self::Toml,
        }
    }
}

/// Deep-merge two JSON values. `base` is lower-priority, `overlay` is higher.
/// Objects: merge field by field. Arrays/scalars: overlay replaces base.
fn deep_merge_json(base: &mut serde_json::Value, overlay: serde_json::Value) {
    match (base, overlay) {
        (serde_json::Value::Object(base_map), serde_json::Value::Object(overlay_map)) => {
            for (key, value) in overlay_map {
                if let Some(base_value) = base_map.get_mut(&key) {
                    deep_merge_json(base_value, value);
                } else {
                    base_map.insert(key, value);
                }
            }
        }
        (base, overlay) => {
            *base = overlay;
        }
    }
}

pub(super) fn parse_config_to_value(path: &Path) -> Result<serde_json::Value, miette::Report> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| miette::miette!("Failed to read config file {}: {}", path.display(), e))?;
    let content = content.trim_start_matches('\u{FEFF}');

    match ConfigFormat::from_path(path) {
        ConfigFormat::Toml => {
            let toml_value: toml::Value = toml::from_str(content).map_err(|e| {
                miette::miette!("Failed to parse config file {}: {}", path.display(), e)
            })?;
            serde_json::to_value(toml_value).map_err(|e| {
                miette::miette!(
                    "Failed to convert TOML to JSON for {}: {}",
                    path.display(),
                    e
                )
            })
        }
        ConfigFormat::Json => crate::jsonc::parse_to_value(content)
            .map_err(|e| miette::miette!("Failed to parse config file {}: {}", path.display(), e)),
    }
}

fn is_repo_root(dir: &Path) -> bool {
    dir.join(".git").exists() || dir.join(".hg").exists() || dir.join(".svn").exists()
}

fn resolve_confined(
    base_dir: &Path,
    resolved: &Path,
    context: &str,
    source_config: &Path,
) -> Result<PathBuf, miette::Report> {
    let canonical_base = dunce::canonicalize(base_dir)
        .map_err(|e| miette::miette!("Failed to resolve base dir {}: {}", base_dir.display(), e))?;
    let canonical_file = dunce::canonicalize(resolved).map_err(|e| {
        miette::miette!(
            "Config file not found: {} ({}, referenced from {}): {}",
            resolved.display(),
            context,
            source_config.display(),
            e
        )
    })?;
    if !canonical_file.starts_with(&canonical_base) {
        return Err(miette::miette!(
            "Path traversal detected: {} escapes package directory {} ({}, referenced from {})",
            resolved.display(),
            base_dir.display(),
            context,
            source_config.display()
        ));
    }
    Ok(canonical_file)
}

fn validate_npm_package_name(name: &str, source_config: &Path) -> Result<(), miette::Report> {
    if name.starts_with('@') && !name.contains('/') {
        return Err(miette::miette!(
            "Invalid scoped npm package name '{}': must be '@scope/name' (referenced from {})",
            name,
            source_config.display()
        ));
    }
    if name.split('/').any(|c| c == ".." || c == ".") {
        return Err(miette::miette!(
            "Invalid npm package name '{}': path traversal components not allowed (referenced from {})",
            name,
            source_config.display()
        ));
    }
    Ok(())
}

fn parse_npm_specifier(specifier: &str) -> (&str, Option<&str>) {
    if specifier.starts_with('@') {
        let mut slashes = 0;
        for (i, ch) in specifier.char_indices() {
            if ch == '/' {
                slashes += 1;
                if slashes == 2 {
                    return (&specifier[..i], Some(&specifier[i + 1..]));
                }
            }
        }
        (specifier, None)
    } else if let Some(slash) = specifier.find('/') {
        (&specifier[..slash], Some(&specifier[slash + 1..]))
    } else {
        (specifier, None)
    }
}

fn resolve_package_exports(pkg: &serde_json::Value, package_dir: &Path) -> Option<PathBuf> {
    let exports = pkg.get("exports")?;
    match exports {
        serde_json::Value::String(s) => Some(package_dir.join(s.as_str())),
        serde_json::Value::Object(map) => {
            let dot_export = map.get(".")?;
            match dot_export {
                serde_json::Value::String(s) => Some(package_dir.join(s.as_str())),
                serde_json::Value::Object(conditions) => {
                    for key in ["default", "node", "import", "require"] {
                        if let Some(serde_json::Value::String(s)) = conditions.get(key) {
                            return Some(package_dir.join(s.as_str()));
                        }
                    }
                    None
                }
                _ => None,
            }
        }
        _ => None,
    }
}

fn find_config_in_npm_package(
    package_dir: &Path,
    source_config: &Path,
) -> Result<PathBuf, miette::Report> {
    let pkg_json_path = package_dir.join("package.json");
    if pkg_json_path.exists() {
        let content = std::fs::read_to_string(&pkg_json_path)
            .map_err(|e| miette::miette!("Failed to read {}: {}", pkg_json_path.display(), e))?;
        let pkg: serde_json::Value = serde_json::from_str(&content)
            .map_err(|e| miette::miette!("Failed to parse {}: {}", pkg_json_path.display(), e))?;
        if let Some(config_path) = resolve_package_exports(&pkg, package_dir)
            && config_path.exists()
        {
            return resolve_confined(
                package_dir,
                &config_path,
                "package.json exports",
                source_config,
            );
        }
        if let Some(main) = pkg.get("main").and_then(|v| v.as_str()) {
            let main_path = package_dir.join(main);
            if main_path.exists() {
                return resolve_confined(
                    package_dir,
                    &main_path,
                    "package.json main",
                    source_config,
                );
            }
        }
    }

    for config_name in CONFIG_NAMES {
        let config_path = package_dir.join(config_name);
        if config_path.exists() {
            return resolve_confined(
                package_dir,
                &config_path,
                "config name fallback",
                source_config,
            );
        }
    }

    Err(miette::miette!(
        "No fallow config found in npm package at {}. \
         Expected package.json with main/exports pointing to a config file, \
         or one of: {}",
        package_dir.display(),
        CONFIG_NAMES.join(", ")
    ))
}

fn resolve_npm_package(
    config_dir: &Path,
    specifier: &str,
    source_config: &Path,
) -> Result<PathBuf, miette::Report> {
    let specifier = specifier.trim();
    if specifier.is_empty() {
        return Err(miette::miette!(
            "Empty npm specifier in extends (in {})",
            source_config.display()
        ));
    }

    let (package_name, subpath) = parse_npm_specifier(specifier);
    validate_npm_package_name(package_name, source_config)?;

    let mut dir = Some(config_dir);
    while let Some(d) = dir {
        let candidate = d.join("node_modules").join(package_name);
        if candidate.is_dir() {
            return if let Some(sub) = subpath {
                let file = candidate.join(sub);
                if file.exists() {
                    resolve_confined(
                        &candidate,
                        &file,
                        &format!("subpath '{sub}'"),
                        source_config,
                    )
                } else {
                    Err(miette::miette!(
                        "File not found in npm package: {} (looked for '{}' in {}, referenced from {})",
                        file.display(),
                        sub,
                        candidate.display(),
                        source_config.display()
                    ))
                }
            } else {
                find_config_in_npm_package(&candidate, source_config)
            };
        }
        dir = d.parent();
    }

    Err(miette::miette!(
        "npm package '{}' not found. \
         Searched for node_modules/{} in ancestor directories of {} (referenced from {}). \
         If this package should be available, install it and ensure it is listed in your project's dependencies",
        package_name,
        package_name,
        config_dir.display(),
        source_config.display()
    ))
}

/// Normalize a URL for config resource identity.
fn normalize_url_for_dedup(url: &str) -> String {
    let Some((scheme, rest)) = url.split_once("://") else {
        return url.to_string();
    };
    let scheme = scheme.to_ascii_lowercase();

    let rest = rest.split_once('#').map_or(rest, |(value, _)| value);
    let authority_end = rest.find(['/', '?']).unwrap_or(rest.len());
    let (authority, tail) = rest.split_at(authority_end);
    let authority = normalize_url_authority(authority, &scheme);

    let tail = if tail == "/" {
        ""
    } else if tail.starts_with("/?") {
        &tail[1..]
    } else {
        tail
    };

    if tail.is_empty() {
        format!("{scheme}://{authority}")
    } else {
        format!("{scheme}://{authority}{tail}")
    }
}

/// Normalize only the case-insensitive parts of a URL authority and its default port.
fn normalize_url_authority(authority: &str, scheme: &str) -> String {
    let (userinfo, host_port) = authority
        .rsplit_once('@')
        .map_or((None, authority), |(userinfo, host_port)| {
            (Some(userinfo), host_port)
        });

    let (host, port) = if host_port.starts_with('[') {
        host_port.find("]:").map_or((host_port, None), |separator| {
            let (host, port) = host_port.split_at(separator + 1);
            (host, port.strip_prefix(':'))
        })
    } else {
        host_port
            .rsplit_once(':')
            .map_or((host_port, None), |(host, port)| (host, Some(port)))
    };

    let port = port.map(|value| {
        value
            .parse::<u16>()
            .map_or_else(|_| value.to_string(), |number| number.to_string())
    });
    let port =
        port.filter(|port| !matches!((scheme, port.as_str()), ("http", "80") | ("https", "443")));
    let host = host
        .strip_prefix('[')
        .and_then(|value| value.strip_suffix(']'))
        .and_then(|value| value.parse::<Ipv6Addr>().ok())
        .map_or_else(
            || host.to_ascii_lowercase(),
            |address| format!("[{address}]"),
        );

    match (userinfo, port.as_deref()) {
        (Some(userinfo), Some(port)) => format!("{userinfo}@{host}:{port}"),
        (Some(userinfo), None) => format!("{userinfo}@{host}"),
        (None, Some(port)) => format!("{host}:{port}"),
        (None, None) => host,
    }
}

/// Format a remote config location for diagnostics without exposing URL secrets.
fn remote_config_display(location: &str) -> String {
    let Some((scheme, rest)) = location.split_once("://") else {
        return location.to_string();
    };

    let authority_end = rest.find(['/', '?', '#']).unwrap_or(rest.len());
    let (authority, tail) = rest.split_at(authority_end);
    let authority = authority
        .rsplit_once('@')
        .map_or(authority, |(_, host)| host);
    let path_end = tail.find(['?', '#']).unwrap_or(tail.len());
    let path = &tail[..path_end];

    format!("{scheme}://{authority}{path}")
}

/// Format a fetch error without trusting the HTTP client's URL rendering.
fn remote_fetch_error_display(error: &ureq::Error, _url: &str) -> String {
    match error {
        ureq::Error::RequireHttpsOnly(redirect_target) => format!(
            "configured for https only: {}",
            remote_config_display(redirect_target)
        ),
        ureq::Error::StatusCode(code) => format!("http status {code}"),
        ureq::Error::HostNotFound => "host not found".to_string(),
        ureq::Error::Timeout(_) => "request timed out".to_string(),
        _ => "request failed".to_string(),
    }
}

fn starts_with_url_prefix(value: &str, prefix: &str) -> bool {
    value
        .get(..prefix.len())
        .is_some_and(|candidate| candidate.eq_ignore_ascii_case(prefix))
}

/// Read the `FALLOW_EXTENDS_TIMEOUT_SECS` env var, falling back to [`DEFAULT_URL_TIMEOUT_SECS`].
fn url_timeout() -> Duration {
    url_timeout_from(std::env::var("FALLOW_EXTENDS_TIMEOUT_SECS").ok().as_deref())
}

/// Parse a raw `FALLOW_EXTENDS_TIMEOUT_SECS` value into a timeout, falling back
/// to [`DEFAULT_URL_TIMEOUT_SECS`] for absent, zero, or non-numeric input. Pure
/// so the parsing branches stay testable without mutating the process env.
fn url_timeout_from(raw: Option<&str>) -> Duration {
    raw.and_then(|v| v.parse::<u64>().ok().filter(|&n| n > 0))
        .map_or(
            Duration::from_secs(DEFAULT_URL_TIMEOUT_SECS),
            Duration::from_secs,
        )
}

/// Maximum response body size for fetched config files.
const MAX_URL_CONFIG_BYTES: u64 = 1024 * 1024;

/// Fetch a remote JSON config from an HTTPS URL.
fn fetch_url_config(url: &str, source: &str) -> Result<serde_json::Value, miette::Report> {
    let url_display = remote_config_display(url);
    let source_display = remote_config_display(source);
    let timeout = url_timeout();
    let agent = ureq::Agent::config_builder()
        .timeout_global(Some(timeout))
        .https_only(true)
        .build()
        .new_agent();

    let mut response = agent.get(url).call().map_err(|e| {
        let error_display = remote_fetch_error_display(&e, url);
        miette::miette!(
            "Failed to fetch remote config from {url_display} \
             (referenced from {source_display}): {error_display}. \
             If this URL is unavailable, use a local path or npm: specifier instead"
        )
    })?;

    let body = response
        .body_mut()
        .with_config()
        .limit(MAX_URL_CONFIG_BYTES)
        .read_to_string()
        .map_err(|e| {
            miette::miette!(
                "Failed to read response body from {url_display} \
                 (referenced from {source_display}): {e}"
            )
        })?;

    crate::jsonc::parse_to_value(&body).map_err(|e| {
        miette::miette!(
            "Failed to parse remote config as JSON from {url_display} \
             (referenced from {source_display}): {e}. \
             Only JSON/JSONC is supported for URL-sourced configs"
        )
    })
}

trait RemoteConfigFetcher {
    fn fetch(&mut self, url: &str, source: &str) -> Result<serde_json::Value, miette::Report>;
}

struct NetworkRemoteConfigFetcher;

impl RemoteConfigFetcher for NetworkRemoteConfigFetcher {
    fn fetch(&mut self, url: &str, source: &str) -> Result<serde_json::Value, miette::Report> {
        fetch_url_config(url, source)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum ConfigResourceId {
    Local(PathBuf),
    Remote(String),
}

/// A fetcher for walks that list the local config files only. It reads no
/// remote config and gives an empty one in its place.
struct NoRemoteFetcher;

impl RemoteConfigFetcher for NoRemoteFetcher {
    fn fetch(&mut self, _url: &str, _source: &str) -> Result<serde_json::Value, miette::Report> {
        Ok(serde_json::Value::Object(serde_json::Map::new()))
    }
}

struct ExtendsResolver<'a, Fetcher> {
    options: ConfigLoadOptions,
    active: FxHashSet<ConfigResourceId>,
    resolved: FxHashMap<ConfigResourceId, ResolvedConfig>,
    /// The canonical local files that the walk reads, in read order.
    local_files: Vec<PathBuf>,
    /// The merge order of each source that is resolving now, innermost
    /// last. It is empty when the walk does not record the merge order. See
    /// [`MergeOrder`].
    order_frames: Vec<MergeOrder>,
    /// The merge order of the walk after the root resolves, when the walk
    /// records it.
    merge_order: MergeOrder,
    record_merge_order: bool,
    fetcher: &'a mut Fetcher,
}

/// Config sources in the order that the load merges their own keys: the
/// extends targets of a file come before the file itself, so a later source
/// overrides an earlier one. The merge can apply a source more than one
/// time (a diamond or a lattice of extends). Only the last time matters,
/// so each source keeps only its last position. The length is then at most
/// the number of distinct sources.
#[derive(Default, Clone)]
struct MergeOrder(Vec<ConfigResourceId>);

impl MergeOrder {
    /// Append `sources` in order. A source that is already in the order
    /// moves to its new, later position.
    fn append(&mut self, sources: &[ConfigResourceId]) {
        for source in sources {
            if let Some(position) = self.0.iter().position(|known| known == source) {
                self.0.remove(position);
            }
            self.0.push(source.clone());
        }
    }
}

/// A config source that the walk resolved once, kept for reuse.
struct ResolvedConfig {
    /// The merged value of the source and its extends chain.
    value: serde_json::Value,
    /// The merge order of the source: its extends chain, then the source
    /// itself. A reuse appends it again, because the merge applies the full
    /// cached value again. It is empty when the walk does not record the
    /// merge order.
    merge_order: MergeOrder,
}

#[derive(Clone, Copy)]
struct LocalExtendsEntry<'a> {
    path: &'a Path,
    config_dir: &'a Path,
    entry: &'a str,
    sealed: bool,
    sealed_dir_canonical: Option<&'a Path>,
    depth: usize,
}

impl<'a, Fetcher: RemoteConfigFetcher> ExtendsResolver<'a, Fetcher> {
    fn new(options: ConfigLoadOptions, fetcher: &'a mut Fetcher) -> Self {
        Self {
            options,
            active: FxHashSet::default(),
            resolved: FxHashMap::default(),
            local_files: Vec::new(),
            order_frames: Vec::new(),
            merge_order: MergeOrder::default(),
            record_merge_order: false,
            fetcher,
        }
    }

    /// Record the merge order of the walk. A normal load does not need it.
    fn recording_merge_order(mut self) -> Self {
        self.record_merge_order = true;
        self
    }

    /// Start the merge order of a source that resolves now.
    fn open_order_frame(&mut self) {
        if self.record_merge_order {
            self.order_frames.push(MergeOrder::default());
        }
    }

    /// Finish the merge order of the source that resolved now: append the
    /// source itself, then give the order to the source that extends it.
    fn close_order_frame(&mut self, identity: &ConfigResourceId, resolved: bool) -> MergeOrder {
        if !self.record_merge_order {
            return MergeOrder::default();
        }
        let mut order = self.order_frames.pop().unwrap_or_default();
        if resolved {
            order.append(std::slice::from_ref(identity));
            self.append_to_parent_order(&order);
        }
        order
    }

    fn append_to_parent_order(&mut self, order: &MergeOrder) {
        let parent = self
            .order_frames
            .last_mut()
            .unwrap_or(&mut self.merge_order);
        parent.append(&order.0);
    }

    fn resolve_local(
        &mut self,
        path: &Path,
        depth: usize,
    ) -> Result<serde_json::Value, miette::Report> {
        if depth >= MAX_EXTENDS_DEPTH {
            return Err(miette::miette!(
                "Config extends chain too deep (>={MAX_EXTENDS_DEPTH} levels) at {}",
                path.display()
            ));
        }
        let canonical = dunce::canonicalize(path).map_err(|e| {
            miette::miette!(
                "Config file not found or unresolvable: {}: {}",
                path.display(),
                e
            )
        })?;
        let identity = ConfigResourceId::Local(canonical.clone());
        if let Some(value) = self.reuse_resolved(&identity) {
            return Ok(value);
        }
        if !self.active.insert(identity.clone()) {
            return Err(miette::miette!(
                "Circular extends detected: {} is already active in the extends chain",
                path.display()
            ));
        }

        self.local_files.push(canonical.clone());
        self.open_order_frame();
        let result = self.resolve_local_uncached(&canonical, depth);
        self.active.remove(&identity);
        let order = self.close_order_frame(&identity, result.is_ok());
        if let Ok(value) = &result {
            self.remember_resolved(identity, value, order);
        }
        result
    }

    /// The cached value of a source that the walk resolved before. The merge
    /// applies the full cached value again, so the merge order gets the
    /// sources of that value again, in their first order.
    fn reuse_resolved(&mut self, identity: &ConfigResourceId) -> Option<serde_json::Value> {
        let resolved = self.resolved.get(identity)?;
        let value = resolved.value.clone();
        if self.record_merge_order {
            let order = resolved.merge_order.clone();
            self.append_to_parent_order(&order);
        }
        Some(value)
    }

    fn remember_resolved(
        &mut self,
        identity: ConfigResourceId,
        value: &serde_json::Value,
        merge_order: MergeOrder,
    ) {
        self.resolved.insert(
            identity,
            ResolvedConfig {
                value: value.clone(),
                merge_order,
            },
        );
    }

    fn resolve_local_uncached(
        &mut self,
        path: &Path,
        depth: usize,
    ) -> Result<serde_json::Value, miette::Report> {
        let mut value = parse_config_to_value(path)?;
        let extends = extract_extends(&mut value, &path.display().to_string())?;
        if extends.is_empty() {
            return Ok(value);
        }

        let config_dir = path.parent().unwrap_or_else(|| Path::new("."));
        let sealed = value
            .get("sealed")
            .and_then(serde_json::Value::as_bool)
            .unwrap_or(false);
        let sealed_dir_canonical = sealed_config_dir(config_dir, sealed)?;
        let mut merged = serde_json::Value::Object(serde_json::Map::new());
        for entry in &extends {
            let base = self.resolve_local_entry(LocalExtendsEntry {
                path,
                config_dir,
                entry,
                sealed,
                sealed_dir_canonical: sealed_dir_canonical.as_deref(),
                depth,
            })?;
            deep_merge_json(&mut merged, base);
        }
        deep_merge_json(&mut merged, value);
        Ok(merged)
    }

    fn resolve_local_entry(
        &mut self,
        input: LocalExtendsEntry<'_>,
    ) -> Result<serde_json::Value, miette::Report> {
        let LocalExtendsEntry {
            path,
            config_dir,
            entry,
            sealed,
            sealed_dir_canonical,
            depth,
        } = input;
        if starts_with_url_prefix(entry, HTTPS_PREFIX) {
            reject_sealed_remote_extends(path, entry, sealed, "URL")?;
            return self.resolve_remote(entry, depth + 1);
        }
        if starts_with_url_prefix(entry, HTTP_PREFIX) {
            let entry_display = remote_config_display(entry);
            return Err(miette::miette!(
                "URL extends must use https://, got http:// URL '{}' (in {}). \
                 Change the URL to use https:// instead",
                entry_display,
                path.display()
            ));
        }
        if let Some(npm_specifier) = entry.strip_prefix(NPM_PREFIX) {
            reject_sealed_remote_extends(path, entry, sealed, "npm")?;
            let npm_path = resolve_npm_package(config_dir, npm_specifier, path)?;
            return self.resolve_local(&npm_path, depth + 1);
        }
        if is_absolute_path_any_platform(Path::new(entry)) {
            return Err(miette::miette!(
                "extends paths must be relative, got absolute path: {} (in {})",
                entry,
                path.display()
            ));
        }
        let resolved_path = config_dir.join(entry);
        if !resolved_path.exists() {
            return Err(miette::miette!(
                "Extended config file not found: {} (referenced from {})",
                resolved_path.display(),
                path.display()
            ));
        }
        validate_sealed_relative_extends(path, entry, &resolved_path, sealed_dir_canonical)?;
        self.resolve_local(&resolved_path, depth + 1)
    }

    fn resolve_remote(
        &mut self,
        url: &str,
        depth: usize,
    ) -> Result<serde_json::Value, miette::Report> {
        if depth >= MAX_EXTENDS_DEPTH {
            let url_display = remote_config_display(url);
            return Err(miette::miette!(
                "Config extends chain too deep (>={MAX_EXTENDS_DEPTH} levels) at {url_display}"
            ));
        }
        if !self.options.allow_remote_extends {
            let url_display = remote_config_display(url);
            return Err(miette::miette!(
                "Remote config extends '{url_display}' is disabled by default. \
                 CLI users can pass --allow-remote-extends. Library callers can use \
                 ConfigLoadOptions {{ allow_remote_extends: true }}"
            ));
        }

        let identity = ConfigResourceId::Remote(normalize_url_for_dedup(url));
        if let Some(value) = self.reuse_resolved(&identity) {
            return Ok(value);
        }
        if !self.active.insert(identity.clone()) {
            let url_display = remote_config_display(url);
            return Err(miette::miette!(
                "Circular extends detected: {url_display} is already active in the extends chain"
            ));
        }

        self.open_order_frame();
        let result = self.resolve_remote_uncached(url, depth);
        self.active.remove(&identity);
        let order = self.close_order_frame(&identity, result.is_ok());
        if let Ok(value) = &result {
            self.remember_resolved(identity, value, order);
        }
        result
    }

    fn resolve_remote_uncached(
        &mut self,
        url: &str,
        depth: usize,
    ) -> Result<serde_json::Value, miette::Report> {
        let mut value = self.fetcher.fetch(url, url)?;
        let extends = extract_extends(&mut value, &remote_config_display(url))?;
        if extends.is_empty() {
            return Ok(value);
        }

        let url_display = remote_config_display(url);
        let mut merged = serde_json::Value::Object(serde_json::Map::new());
        for entry in &extends {
            let base = if starts_with_url_prefix(entry, HTTPS_PREFIX) {
                self.resolve_remote(entry, depth + 1)?
            } else if starts_with_url_prefix(entry, HTTP_PREFIX) {
                let entry_display = remote_config_display(entry);
                return Err(miette::miette!(
                    "URL extends must use https://, got http:// URL '{}' (in remote config {}). \
                     Change the URL to use https:// instead",
                    entry_display,
                    url_display
                ));
            } else if let Some(npm_specifier) = entry.strip_prefix(NPM_PREFIX) {
                let cwd = std::env::current_dir().map_err(|e| {
                    miette::miette!(
                        "Cannot resolve npm: specifier from URL-sourced config: \
                         failed to determine current directory: {e}"
                    )
                })?;
                let path_placeholder = PathBuf::from(&url_display);
                let npm_path = resolve_npm_package(&cwd, npm_specifier, &path_placeholder)?;
                self.resolve_local(&npm_path, depth + 1)?
            } else {
                return Err(miette::miette!(
                    "Relative paths in 'extends' are not supported when the base config was \
                     fetched from a URL ('{url_display}'). Use another https:// URL or npm: reference \
                     instead. Got: '{entry}'"
                ));
            };
            deep_merge_json(&mut merged, base);
        }
        deep_merge_json(&mut merged, value);
        Ok(merged)
    }
}

/// Extract the `extends` array from a parsed JSON config value.
///
/// Fails loud on malformed values: the key is removed before
/// deserialization, so `FallowConfig`'s `deny_unknown_fields` never sees a
/// bad `extends` and the base config would otherwise go unmerged silently.
fn extract_extends(
    value: &mut serde_json::Value,
    source: &str,
) -> Result<Vec<String>, miette::Report> {
    let Some(extends) = value.as_object_mut().and_then(|obj| obj.remove("extends")) else {
        return Ok(Vec::new());
    };
    match extends {
        serde_json::Value::String(s) => Ok(vec![s]),
        serde_json::Value::Array(arr) => arr
            .into_iter()
            .map(|entry| match entry {
                serde_json::Value::String(s) => Ok(s),
                other => Err(miette::miette!(
                    "extends entries must be strings, got {} (in {source})",
                    json_type_name(&other)
                )),
            })
            .collect(),
        other => Err(miette::miette!(
            "extends must be a string or an array of strings, got {} (in {source})",
            json_type_name(&other)
        )),
    }
}

fn json_type_name(value: &serde_json::Value) -> &'static str {
    match value {
        serde_json::Value::Null => "null",
        serde_json::Value::Bool(_) => "a boolean",
        serde_json::Value::Number(_) => "a number",
        serde_json::Value::String(_) => "a string",
        serde_json::Value::Array(_) => "an array",
        serde_json::Value::Object(_) => "an object",
    }
}

#[cfg(test)]
fn resolve_url_extends(
    url: &str,
    _visited: &mut FxHashSet<String>,
    depth: usize,
) -> Result<serde_json::Value, miette::Report> {
    let mut fetcher = NetworkRemoteConfigFetcher;
    ExtendsResolver::new(
        ConfigLoadOptions {
            allow_remote_extends: true,
        },
        &mut fetcher,
    )
    .resolve_remote(url, depth)
}

fn sealed_config_dir(config_dir: &Path, sealed: bool) -> Result<Option<PathBuf>, miette::Report> {
    if !sealed {
        return Ok(None);
    }
    dunce::canonicalize(config_dir).map(Some).map_err(|e| {
        miette::miette!(
            "Sealed config directory '{}' could not be canonicalized: {e}",
            config_dir.display()
        )
    })
}

fn reject_sealed_remote_extends(
    path: &Path,
    entry: &str,
    sealed: bool,
    kind: &str,
) -> Result<(), miette::Report> {
    if sealed {
        let entry_display = remote_config_display(entry);
        Err(miette::miette!(
            "'sealed: true' config at {} rejects {} extends '{}'. \
             Sealed configs only allow file-relative extends within \
             the config's directory",
            path.display(),
            kind,
            entry_display
        ))
    } else {
        Ok(())
    }
}

fn validate_sealed_relative_extends(
    path: &Path,
    entry: &str,
    resolved_path: &Path,
    sealed_dir_canonical: Option<&Path>,
) -> Result<(), miette::Report> {
    let Some(dir_canonical) = sealed_dir_canonical else {
        return Ok(());
    };
    let p_canonical = dunce::canonicalize(resolved_path).map_err(|e| {
        miette::miette!(
            "Sealed config extends path '{}' could not be canonicalized: {e}",
            resolved_path.display()
        )
    })?;
    if p_canonical.starts_with(dir_canonical) {
        Ok(())
    } else {
        Err(miette::miette!(
            "'sealed: true' config at {} rejects extends '{}' which resolves \
             outside the config's directory ({}). Sealed configs only allow \
             extends within the config's directory",
            path.display(),
            entry,
            p_canonical.display()
        ))
    }
}

/// Public entry point: resolve a config file with all its extends chain.
///
/// Delegates to [`resolve_extends_file`] with a fresh visited set.
#[cfg(test)]
fn resolve_extends(
    path: &Path,
    _visited: &mut FxHashSet<String>,
    depth: usize,
) -> Result<serde_json::Value, miette::Report> {
    let mut fetcher = NetworkRemoteConfigFetcher;
    ExtendsResolver::new(ConfigLoadOptions::default(), &mut fetcher).resolve_local(path, depth)
}

/// Collect every unknown key under `rules` or `overrides[].rules` in a merged
/// config value (issue #467, phase 1).
///
/// Today `RulesConfig` / `PartialRulesConfig` carry serde aliases but NOT
/// `deny_unknown_fields`, so typos like `unsued-files` are silently dropped and
/// the user's intent is lost. This pass walks the merged value before
/// deserialization and surfaces every unknown key, with a Levenshtein-distance
/// suggestion when the typo is close to a known name.
///
/// Returns the findings so the caller can render them; tests can assert
/// against the list without subscribing to tracing output.
///
/// Phase 2 (a future minor release) flips both structs to
/// `#[serde(deny_unknown_fields)]` and the warning becomes a hard error.
fn collect_unknown_rule_keys(merged: &serde_json::Value) -> Vec<super::rules::UnknownRuleKey> {
    use super::rules::find_unknown_rule_keys;

    let mut findings = Vec::new();

    if let Some(rules) = merged.get("rules") {
        findings.extend(find_unknown_rule_keys(rules, "rules"));
    }

    if let Some(overrides) = merged.get("overrides").and_then(|v| v.as_array()) {
        for (i, entry) in overrides.iter().enumerate() {
            if let Some(rules) = entry.get("rules") {
                let context = format!("overrides[{i}].rules");
                findings.extend(find_unknown_rule_keys(rules, &context));
            }
        }
    }

    findings
}

thread_local! {
    /// Per-thread capture of unknown-rule findings, for the wiring regression
    /// test in this module. Each test installs a fresh capture via
    /// [`capture_unknown_rule_warnings`], runs `FallowConfig::load`, and reads
    /// back the findings. Thread-local so parallel test execution does not
    /// race; bypassed entirely in production code (`UnknownRuleCapture::None`).
    #[cfg(test)]
    static UNKNOWN_RULE_CAPTURE: std::cell::RefCell<Option<Vec<super::rules::UnknownRuleKey>>> =
        const { std::cell::RefCell::new(None) };
}

/// Install a thread-local capture buffer and run `body`. Returns the findings
/// emitted by every `warn_on_unknown_rule_keys` call within `body`'s call tree
/// on the current thread, in order. Test-only.
#[cfg(test)]
fn capture_unknown_rule_warnings<F: FnOnce() -> R, R>(
    body: F,
) -> (R, Vec<super::rules::UnknownRuleKey>) {
    UNKNOWN_RULE_CAPTURE.with(|cell| {
        *cell.borrow_mut() = Some(Vec::new());
    });
    let result = body();
    let findings = UNKNOWN_RULE_CAPTURE.with(|cell| cell.borrow_mut().take().unwrap_or_default());
    (result, findings)
}

/// Emit a `tracing::warn!` per finding from [`collect_unknown_rule_keys`].
///
/// `config_path` is the file the merged value originated from; it appears in
/// the warning text AND in the dedupe key so two different config files with
/// the same typo each warn once instead of the second one being silenced.
///
/// Deduplicates within the process: `FallowConfig::load` runs multiple times
/// per analysis (combined mode runs check + dupes + health, each through the
/// same config load path), so without a dedupe the same typo emits 3+ warnings
/// per run.
/// `MAJOR.MINOR.PATCH` as a comparable triple. Deliberately not a full semver
/// parser: the config field mirrors the release tag format, which the release
/// workflow already constrains to exactly three numbers.
fn version_triple(value: &str) -> Option<(u64, u64, u64)> {
    let mut parts = value.split('.');
    let triple = (
        parts.next()?.parse().ok()?,
        parts.next()?.parse().ok()?,
        parts.next()?.parse().ok()?,
    );
    parts.next().is_none().then_some(triple)
}

/// Refuse a config written for a newer binary before `deny_unknown_fields`
/// reports the first field this build happens not to know. Without this a team
/// that commits a field introduced in a newer version breaks every runner still
/// on the old one with an error that names the field, so the reader's first
/// guess is a typo rather than version skew.
fn assert_binary_meets_minimum_version(
    path: &Path,
    merged: &serde_json::Value,
) -> Result<(), miette::Report> {
    let Some(declared) = merged.get("minimumVersion") else {
        return Ok(());
    };
    let declared = declared.as_str().and_then(version_triple).ok_or_else(|| {
        miette::miette!(
            "minimumVersion must be MAJOR.MINOR.PATCH in {}, found {declared}",
            path.display()
        )
    })?;

    let running = env!("CARGO_PKG_VERSION");
    let Some(current) = version_triple(running) else {
        return Ok(());
    };
    if current >= declared {
        return Ok(());
    }

    let (major, minor, patch) = declared;
    Err(miette::miette!(
        "{} requires fallow {major}.{minor}.{patch} or newer, running {running}. \
         Upgrade fallow, or remove minimumVersion and the fields that need it.",
        path.display()
    ))
}

fn warn_on_unknown_rule_keys(config_path: &Path, merged: &serde_json::Value) {
    use std::sync::{Mutex, OnceLock};

    static WARNED: OnceLock<Mutex<FxHashSet<String>>> = OnceLock::new();
    let warned = WARNED.get_or_init(|| Mutex::new(FxHashSet::default()));

    let path_display = config_path.display().to_string();

    for finding in collect_unknown_rule_keys(merged) {
        let dedupe_key = format!("{path_display}::{}::{}", finding.context, finding.key);
        if let Ok(mut set) = warned.lock()
            && !set.insert(dedupe_key)
        {
            continue;
        }

        #[cfg(test)]
        UNKNOWN_RULE_CAPTURE.with(|cell| {
            if let Some(buf) = cell.borrow_mut().as_mut() {
                buf.push(finding.clone());
            }
        });

        if let Some(suggestion) = finding.suggestion {
            tracing::warn!(
                "unknown rule '{key}' in {context} of {path} (did you mean '{suggestion}'?); \
                 the rule will be ignored. A future release will reject unknown rule names.",
                key = finding.key,
                context = finding.context,
                path = path_display,
            );
        } else {
            tracing::warn!(
                "unknown rule '{key}' in {context} of {path}; the rule will be ignored. \
                 A future release will reject unknown rule names.",
                key = finding.key,
                context = finding.context,
                path = path_display,
            );
        }
    }
}

/// Return the lower-precedence config names from [`CONFIG_NAMES`] that ALSO
/// exist in `dir`, given that `chosen_index` is the index of the first-match
/// (winning) name.
///
/// Only indices after `chosen_index` are scanned: a higher-precedence name
/// cannot coexist undetected, because it would have been the first match.
fn shadowed_config_names(dir: &Path, chosen_index: usize) -> Vec<&'static str> {
    CONFIG_NAMES
        .iter()
        .skip(chosen_index + 1)
        .filter(|name| dir.join(name).exists())
        .copied()
        .collect()
}

/// A captured coexistence warning: `(chosen file name, shadowed file names)`.
/// Test-only; populated by `warn_on_coexisting_configs` under capture.
#[cfg(test)]
type CoexistWarning = (String, Vec<String>);

thread_local! {
    /// Per-thread capture of coexisting-config warnings, for the wiring
    /// regression test in this module. Mirrors [`UNKNOWN_RULE_CAPTURE`]: each
    /// test installs a fresh capture via
    /// [`capture_coexisting_config_warnings`], runs `find_and_load`, and reads
    /// back the `(chosen, shadowed)` pairs. Thread-local so parallel test
    /// execution does not race; bypassed entirely in production code.
    #[cfg(test)]
    static COEXIST_CAPTURE: std::cell::RefCell<Option<Vec<CoexistWarning>>> =
        const { std::cell::RefCell::new(None) };
}

/// Install a thread-local capture buffer and run `body`. Returns every
/// `(chosen, shadowed)` pair emitted by `warn_on_coexisting_configs` within
/// `body`'s call tree on the current thread, in order. Test-only.
#[cfg(test)]
fn capture_coexisting_config_warnings<F: FnOnce() -> R, R>(body: F) -> (R, Vec<CoexistWarning>) {
    COEXIST_CAPTURE.with(|cell| {
        *cell.borrow_mut() = Some(Vec::new());
    });
    let result = body();
    let findings = COEXIST_CAPTURE.with(|cell| cell.borrow_mut().take().unwrap_or_default());
    (result, findings)
}

/// Warn once per config file about each key under `workspaces` that fallow
/// does not know. An error would break configs that carry a stray key today.
fn warn_on_unknown_workspace_keys(config_path: &Path, workspaces: Option<&crate::WorkspaceConfig>) {
    use std::sync::{Mutex, OnceLock};

    static WARNED: OnceLock<Mutex<FxHashSet<String>>> = OnceLock::new();
    let Some(workspaces) = workspaces else {
        return;
    };
    let warned = WARNED.get_or_init(|| Mutex::new(FxHashSet::default()));
    for key in workspaces.unknown_keys.keys() {
        let dedupe_key = format!("{}::{key}", config_path.display());
        if let Ok(mut set) = warned.lock()
            && !set.insert(dedupe_key)
        {
            continue;
        }
        tracing::warn!(
            "unknown key 'workspaces.{key}' in {}; fallow ignores it. The known keys are \
             'patterns', 'changedSince' and 'settings'.",
            config_path.display(),
        );
    }
}

/// Emit a `tracing::warn!` when `find_and_load` picked `chosen_path` while one
/// or more lower-precedence config files (`shadowed`) coexist in the same
/// directory. Silent precedence is the worst class of config bug: the user
/// sees correct-looking output produced from the wrong source (#458).
///
/// `chosen_path` is the absolute candidate path of the winning config;
/// `shadowed` are the bare names of the lower-precedence files that also exist.
///
/// Deduplicates within the process keyed on the canonical directory, because
/// `find_and_load` runs multiple times per analysis (combined mode loads config
/// for check + dupes + health); without the dedupe the same directory would
/// warn 3+ times per run. Two different directories with coexisting configs
/// warn independently.
fn warn_on_coexisting_configs(chosen_path: &Path, shadowed: &[&str]) {
    use std::sync::{Mutex, OnceLock};

    if shadowed.is_empty() {
        return;
    }

    let chosen_name = chosen_path.file_name().map_or_else(
        || chosen_path.display().to_string(),
        |n| n.to_string_lossy().into_owned(),
    );
    let dir = chosen_path.parent().unwrap_or(chosen_path);

    #[cfg(test)]
    COEXIST_CAPTURE.with(|cell| {
        if let Some(buf) = cell.borrow_mut().as_mut() {
            buf.push((
                chosen_name.clone(),
                shadowed.iter().map(|s| (*s).to_owned()).collect(),
            ));
        }
    });

    static WARNED: OnceLock<Mutex<FxHashSet<String>>> = OnceLock::new();
    let warned = WARNED.get_or_init(|| Mutex::new(FxHashSet::default()));
    let dedupe_key = std::fs::canonicalize(dir)
        .unwrap_or_else(|_| dir.to_path_buf())
        .display()
        .to_string();
    if let Ok(mut set) = warned.lock()
        && !set.insert(dedupe_key)
    {
        return;
    }

    tracing::warn!(
        "multiple fallow config files in {dir}: loaded '{chosen}', ignoring '{shadowed}'. \
         fallow uses the first match in precedence order \
         (.fallowrc.json > .fallowrc.jsonc > fallow.toml > .fallow.toml); \
         remove the unused file(s) to silence this warning.",
        dir = dir.display(),
        chosen = chosen_name,
        shadowed = shadowed.join(", "),
    );
}

fn load_with_fetcher<Fetcher: RemoteConfigFetcher>(
    path: &Path,
    options: ConfigLoadOptions,
    fetcher: &mut Fetcher,
) -> Result<FallowConfig, miette::Report> {
    let merged = ExtendsResolver::new(options, fetcher).resolve_local(path, 0)?;
    FallowConfig::from_merged(path, merged)
}

impl FallowConfig {
    /// Load config from a fallow config file (TOML or JSON/JSONC).
    ///
    /// The format is detected from the file extension:
    /// - `.toml` → TOML
    /// - `.json` → JSON (with JSONC comment stripping)
    ///
    /// Supports `extends` for config inheritance. Extended configs are loaded
    /// and deep-merged before this config's values are applied.
    ///
    /// User-supplied glob patterns (`entry`, `ignorePatterns`,
    /// `dynamicallyLoaded`, `duplicates.ignore`, `similarCode.ignore`, `health.ignore`,
    /// `health.thresholdOverrides[].files`, `boundaries.zones[].patterns`, `overrides[].files`,
    /// `ignoreExports[].file`, `ignoreCatalogReferences[].consumer`) are
    /// validated against absolute paths, `..` traversal segments, and invalid
    /// glob syntax. Loading fails loud on any rejection so silent no-match
    /// configs surface to the user. See issue #463.
    ///
    /// # Errors
    ///
    /// Returns an error when the config file cannot be read, merged, or
    /// deserialized, or when any user-supplied glob pattern is rejected.
    pub fn load(path: &Path) -> Result<Self, miette::Report> {
        Self::load_with_options(path, ConfigLoadOptions::default())
    }

    /// Load config with a host-controlled inheritance trust policy.
    ///
    /// Remote `https://` extends are denied unless
    /// [`ConfigLoadOptions::allow_remote_extends`] is explicitly enabled for
    /// this call. Local and `npm:` extends are unaffected.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`Self::load`], plus a trust-policy error
    /// when a remote extends target is encountered without opt-in.
    pub fn load_with_options(
        path: &Path,
        options: ConfigLoadOptions,
    ) -> Result<Self, miette::Report> {
        let mut fetcher = NetworkRemoteConfigFetcher;
        load_with_fetcher(path, options, &mut fetcher)
    }

    /// The local files that a load of the config file at `path` reads.
    ///
    /// The list holds `path` and each local or `npm:` `extends` target, as
    /// canonical paths in the order that the load reads them. It leaves out
    /// remote `https://` targets and does not fetch them. The walk stops at
    /// the first entry that does not resolve, so a broken chain gives the
    /// files up to that entry.
    ///
    /// A host that keeps a loaded config across runs compares these files to
    /// know when to load the config again.
    #[must_use]
    pub fn local_source_files(path: &Path) -> Vec<PathBuf> {
        let mut fetcher = NoRemoteFetcher;
        let mut resolver = ExtendsResolver::new(
            ConfigLoadOptions {
                allow_remote_extends: true,
            },
            &mut fetcher,
        );
        let _ = resolver.resolve_local(path, 0);
        resolver.local_files
    }

    /// The config sources of the file at `path` in merge order, or `None`
    /// when the extends chain does not resolve. A later entry overrides an
    /// earlier one. A remote `https://` source is `None` and is not fetched.
    pub(super) fn merge_order(path: &Path) -> Option<Vec<Option<PathBuf>>> {
        let mut fetcher = NoRemoteFetcher;
        let mut resolver = ExtendsResolver::new(
            ConfigLoadOptions {
                allow_remote_extends: true,
            },
            &mut fetcher,
        )
        .recording_merge_order();
        resolver.resolve_local(path, 0).ok()?;
        let sources = resolver
            .merge_order
            .0
            .into_iter()
            .map(|source| match source {
                ConfigResourceId::Local(path) => Some(path),
                ConfigResourceId::Remote(_) => None,
            });
        Some(sources.collect())
    }

    fn from_merged(path: &Path, merged: serde_json::Value) -> Result<Self, miette::Report> {
        assert_binary_meets_minimum_version(path, &merged)?;
        warn_on_unknown_rule_keys(path, &merged);

        let private_type_leaks_configured = merged
            .get("rules")
            .and_then(serde_json::Value::as_object)
            .is_some_and(|rules| {
                rules.contains_key("private-type-leaks") || rules.contains_key("private-type-leak")
            });
        let mut config: Self = serde_json::from_value(merged).map_err(|e| {
            let reason = e.to_string();
            // Unknown fields on overrides/ignoreExports entries are usually
            // inline annotations; point at JSONC comments as the fix.
            let hint = if matches!(ConfigFormat::from_path(path), ConfigFormat::Json)
                && reason.contains("unknown field")
                && (reason.contains("expected `files` or `rules`")
                    || reason.contains("expected `file` or `exports`"))
            {
                " (annotations belong in a // comment; .fallowrc.json accepts JSONC)"
            } else {
                ""
            };
            miette::miette!("{reason} in {}{hint}", path.display())
        })?;
        config.rules.private_type_leaks_configured = private_type_leaks_configured;
        warn_on_unknown_workspace_keys(path, config.workspaces.as_ref());

        config.validate_user_globs().map_err(|errors| {
            let joined = errors
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join("\n  - ");
            miette::miette!("invalid config:\n  - {}", joined)
        })?;
        if !config.security.request_receivers_are_valid() {
            return Err(miette::miette!(
                "invalid config:\n  - security.requestReceivers entries must be non-empty strings"
            ));
        }
        let mut health_errors = config.health.threshold_override_errors();
        health_errors.extend(config.health.change_coupling.errors());
        if !health_errors.is_empty() {
            return Err(miette::miette!(
                "invalid config:\n  - {}",
                health_errors.join("\n  - ")
            ));
        }
        if let Some(pattern) = &config.unused_component_props.ignore_pattern
            && let Err(e) = regex::Regex::new(pattern)
        {
            return Err(miette::miette!(
                "invalid config:\n  - unusedComponentProps.ignorePattern is not a valid regex: {e}"
            ));
        }

        Ok(config)
    }

    /// Validate all user-supplied glob patterns and directory paths in this config.
    ///
    /// Accumulates errors from every glob- or path-bearing field so the user
    /// sees ALL offending values in one run rather than fixing them one at a
    /// time.
    ///
    /// Covered filesystem glob fields: `entry`, `ignorePatterns`, `ignoreFindings`,
    /// `dynamicallyLoaded`, `duplicates.ignore`, `similarCode.ignore`, `health.ignore`,
    /// `health.thresholdOverrides[].files`, `overrides[].files`, `ignoreExports[].file`,
    /// `ignoreCatalogReferences[].consumer`, `boundaries.zones[].patterns`,
    /// `boundaries.coverage.allowUnmatched`,
    /// plus every glob-bearing field on inline `framework[]` plugin
    /// definitions (entry points, always-used, config patterns, used-exports
    /// patterns, and `fileExists` detection patterns; the last reaches
    /// `glob::glob` on disk so a `..` segment there is a real path traversal).
    ///
    /// Covered specifier glob fields: `ignoreUnresolvedImports`. These match
    /// raw import strings, so parent-relative specifiers like `../generated/**`
    /// are valid and only glob syntax is checked.
    ///
    /// Covered directory-path fields: `boundaries.zones[].root` and
    /// `boundaries.zones[].autoDiscover`. These are literal paths (not
    /// globs), so only the absolute-path + traversal checks apply.
    ///
    /// # Errors
    ///
    /// Returns a non-empty `Vec` of
    /// [`glob_validation::GlobValidationError`](super::glob_validation::GlobValidationError)
    /// when any field contains a rejected value.
    pub fn validate_user_globs(
        &self,
    ) -> Result<(), Vec<super::glob_validation::GlobValidationError>> {
        let mut errors = Vec::new();

        self.validate_top_level_globs(&mut errors);
        self.validate_ignore_rule_globs(&mut errors);
        self.validate_boundary_globs(&mut errors);
        self.validate_workspace_baseline_keys(&mut errors);

        for plugin in &self.framework {
            if let Err(mut plugin_errors) = plugin.validate_user_globs() {
                errors.append(&mut plugin_errors);
            }
        }

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    /// Validate the top-level filesystem and specifier glob fields plus the
    /// per-override and threshold-override file globs.
    fn validate_top_level_globs(
        &self,
        errors: &mut Vec<super::glob_validation::GlobValidationError>,
    ) {
        use super::glob_validation::{
            validate_user_dependency_globs, validate_user_finding_ignore_globs,
            validate_user_globs, validate_user_ignore_pattern_globs, validate_user_specifier_globs,
        };

        validate_user_globs(&self.entry, "entry", errors);
        validate_user_globs(&self.support_entry, "supportEntry", errors);
        if let Some(workspaces) = &self.workspaces {
            for (key, settings) in &workspaces.settings {
                validate_user_globs(
                    std::slice::from_ref(key),
                    "workspaces.settings (key)",
                    errors,
                );
                validate_user_dependency_globs(
                    &settings.ignore_dependencies,
                    "workspaces.settings[].ignoreDependencies",
                    errors,
                );
            }
        }
        validate_user_ignore_pattern_globs(&self.ignore_patterns, "ignorePatterns", errors);
        validate_user_finding_ignore_globs(&self.ignore_findings, "ignoreFindings", errors);
        validate_user_globs(&self.dynamically_loaded, "dynamicallyLoaded", errors);
        validate_user_dependency_globs(&self.ignore_dependencies, "ignoreDependencies", errors);
        validate_user_dependency_globs(&self.ignore_binaries, "ignoreBinaries", errors);
        validate_user_specifier_globs(
            &self.ignore_unresolved_imports,
            "ignoreUnresolvedImports",
            errors,
        );
        validate_user_globs(&self.duplicates.ignore, "duplicates.ignore", errors);
        validate_user_globs(&self.similar_code.ignore, "similarCode.ignore", errors);
        validate_user_globs(&self.health.ignore, "health.ignore", errors);
        for override_entry in &self.health.threshold_overrides {
            validate_user_globs(
                &override_entry.files,
                "health.thresholdOverrides[].files",
                errors,
            );
        }
        for override_entry in &self.overrides {
            validate_user_globs(&override_entry.files, "overrides[].files", errors);
        }
    }

    /// Validate the `ignoreExports` and `ignoreCatalogReferences` rule globs.
    fn validate_ignore_rule_globs(
        &self,
        errors: &mut Vec<super::glob_validation::GlobValidationError>,
    ) {
        use super::glob_validation::compile_user_glob;

        for rule in &self.ignore_exports {
            if let Err(e) = compile_user_glob(&rule.file, "ignoreExports[].file") {
                errors.push(e);
            }
        }

        for rule in &self.ignore_catalog_references {
            if let Some(consumer) = &rule.consumer
                && let Err(e) = compile_user_glob(consumer, "ignoreCatalogReferences[].consumer")
            {
                errors.push(e);
            }
        }
    }

    /// Validate the `boundaries.zones[]` patterns/roots/autoDiscover and the
    /// coverage `allowUnmatched` globs.
    fn validate_boundary_globs(
        &self,
        errors: &mut Vec<super::glob_validation::GlobValidationError>,
    ) {
        use super::glob_validation::{
            validate_user_globs, validate_user_path, validate_user_paths,
        };

        for zone in &self.boundaries.zones {
            validate_user_globs(&zone.patterns, "boundaries.zones[].patterns", errors);
            if let Some(root) = &zone.root
                && let Err(e) = validate_user_path(root, "boundaries.zones[].root")
            {
                errors.push(e);
            }
            validate_user_paths(
                &zone.auto_discover,
                "boundaries.zones[].autoDiscover",
                errors,
            );
        }
        validate_user_globs(
            &self.boundaries.coverage.allow_unmatched,
            "boundaries.coverage.allowUnmatched",
            errors,
        );
    }

    /// Validate the shape of the `workspaces.changedSince` keys. Whether a
    /// key names a discovered workspace is known only after discovery, so the
    /// engine checks that when a run reads the map.
    fn validate_workspace_baseline_keys(
        &self,
        errors: &mut Vec<super::glob_validation::GlobValidationError>,
    ) {
        let Some(workspaces) = &self.workspaces else {
            return;
        };
        for key in workspaces.changed_since.keys() {
            if let Err(e) =
                super::glob_validation::validate_workspace_root_key(key, "workspaces.changedSince")
            {
                errors.push(e);
            }
        }
    }

    /// Find the config file path without loading it.
    /// Searches the same locations as `find_and_load`.
    #[must_use]
    pub fn find_config_path(start: &Path) -> Option<PathBuf> {
        let mut dir = start;
        loop {
            for name in CONFIG_NAMES {
                let candidate = dir.join(name);
                if candidate.exists() {
                    return Some(candidate);
                }
            }
            if is_repo_root(dir) {
                break;
            }
            dir = dir.parent()?;
        }
        None
    }

    /// Find and load config, searching from `start` up to the project root.
    ///
    /// # Errors
    ///
    /// Returns an error if a config file is found but cannot be read or parsed.
    pub fn find_and_load(start: &Path) -> Result<Option<(Self, PathBuf)>, String> {
        Self::find_and_load_with_options(start, ConfigLoadOptions::default())
    }

    /// Find and load config with a host-controlled inheritance trust policy.
    ///
    /// # Errors
    ///
    /// Returns an error if a config file is found but cannot be read, parsed,
    /// or is not permitted by `options`.
    pub fn find_and_load_with_options(
        start: &Path,
        options: ConfigLoadOptions,
    ) -> Result<Option<(Self, PathBuf)>, String> {
        let mut dir = start;
        loop {
            for (idx, name) in CONFIG_NAMES.iter().enumerate() {
                let candidate = dir.join(name);
                if candidate.exists() {
                    warn_on_coexisting_configs(&candidate, &shadowed_config_names(dir, idx));
                    match Self::load_with_options(&candidate, options) {
                        Ok(config) => return Ok(Some((config, candidate))),
                        Err(e) => {
                            // Most load errors already name the config file;
                            // add the path only when the reason lacks it, so
                            // it is mentioned exactly once.
                            let msg = e.to_string();
                            return Err(if msg.contains(&candidate.display().to_string()) {
                                msg
                            } else {
                                format!("Failed to parse {}: {msg}", candidate.display())
                            });
                        }
                    }
                }
            }
            if is_repo_root(dir) {
                break;
            }
            dir = match dir.parent() {
                Some(parent) => parent,
                None => break,
            };
        }
        Ok(None)
    }

    /// Generate JSON Schema for the configuration format.
    #[must_use]
    pub fn json_schema() -> serde_json::Value {
        serde_json::to_value(schemars::schema_for!(FallowConfig)).unwrap_or_default()
    }

    /// Validate boundary zone references and zone-root-prefix conflicts AFTER
    /// preset and auto-discover expansion.
    ///
    /// Runs the same expand sequence as [`FallowConfig::resolve`] (preset
    /// expansion gated on tsconfig `rootDir`, then `expand_auto_discover`)
    /// before invoking
    /// [`BoundaryConfig::validate_zone_references`](super::boundaries::BoundaryConfig::validate_zone_references)
    /// and
    /// [`BoundaryConfig::validate_root_prefixes`](super::boundaries::BoundaryConfig::validate_root_prefixes),
    /// so Bulletproof-style presets whose authored rule references logical
    /// groups (`features`) still load cleanly.
    ///
    /// Call sites (`runtime_support::load_config_for_analysis` in the CLI,
    /// `core::lib::config_for_project` for LSP and programmatic embedders)
    /// surface every collected error in a single rendered diagnostic, then
    /// exit with code 2. A failure that only emits `tracing::error!` and
    /// continues produces a flood of false-positive boundary violations at
    /// analysis time (#468).
    ///
    /// `root` is the project root used by `expand_auto_discover` to scan for
    /// child directories. Caller is responsible for passing the same root it
    /// later hands to `resolve()`.
    ///
    /// # Errors
    ///
    /// Returns a non-empty `Vec<ZoneValidationError>` aggregating every
    /// offending zone reference and redundant-root-prefix pattern; the empty
    /// case becomes `Ok(())`.
    pub fn validate_resolved_boundaries(
        &self,
        root: &Path,
    ) -> Result<(), Vec<super::boundaries::ZoneValidationError>> {
        use super::boundaries::ZoneValidationError;

        let mut boundaries = self.boundaries.clone();
        if boundaries.preset.is_some() {
            let source_root = crate::workspace::parse_tsconfig_root_dir(root)
                .filter(|r| r != "." && !r.starts_with("..") && !Path::new(r).is_absolute())
                .unwrap_or_else(|| "src".to_owned());
            boundaries.expand(&source_root);
        }
        let _logical_groups = boundaries.expand_auto_discover(root);

        let mut errors: Vec<ZoneValidationError> = boundaries
            .validate_zone_references()
            .into_iter()
            .map(ZoneValidationError::UnknownZoneReference)
            .collect();
        errors.extend(
            boundaries
                .validate_root_prefixes()
                .into_iter()
                .map(ZoneValidationError::RedundantRootPrefix),
        );
        errors.extend(
            boundaries
                .validate_call_rules()
                .into_iter()
                .map(ZoneValidationError::InvalidForbiddenCallee),
        );

        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }
}

#[cfg(test)]
#[path = "parsing_tests.rs"]
mod tests;