lihaaf 0.1.2

Fast compile-fail and compile-pass test harness for Rust proc macros; a faster trybuild-style workflow
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
//! Staged suite workspace builder for opted-in `dev_deps` collection.
//!
//! `session` calls this builder for suites whose `build_targets` opt into
//! collector prebuilds. Suites that omit `build_targets` stay on the default
//! dylib build path.
#![allow(dead_code)]

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::process::Command;

use crate::config::Suite;
use crate::dylib::{self, BuildOutput};
use crate::error::{Error, Outcome};

const COLLECTOR_PACKAGE: &str = "__lihaaf_dev_deps_collector";

/// Parameters needed to synthesize and build an opted-in suite workspace.
#[derive(Debug, Clone)]
pub struct BuildParams<'a> {
    /// `dylib_crate` from `[package.metadata.lihaaf]`.
    pub dylib_crate: &'a str,
    /// The final resolved suite whose `dev_deps` and `features` are built.
    pub suite: &'a crate::config::Suite,
    /// Path to the metadata crate's `Cargo.toml`.
    pub metadata_manifest_path: &'a std::path::Path,
    /// The suite-specific lihaaf target directory.
    pub target_dir: &'a std::path::Path,
    /// Captured rustc identity for parity with the default build API.
    pub toolchain: &'a crate::toolchain::Toolchain,
}

/// Build the staged dylib package and synthetic dev-deps collector.
pub fn build(params: &BuildParams<'_>) -> Result<BuildOutput, Error> {
    validate_entry_guard(params.suite)?;

    let plan = synthesize_workspace(
        params.dylib_crate,
        params.suite,
        params.metadata_manifest_path,
        params.target_dir,
    )?;

    let mut cmd = Command::new("cargo");
    cmd.arg("build")
        .arg("-p")
        .arg(params.dylib_crate)
        .arg("-p")
        .arg(COLLECTOR_PACKAGE)
        .arg("--release")
        .arg("--message-format=json-render-diagnostics")
        .arg("--manifest-path")
        .arg(&plan.root_manifest)
        .arg("--target-dir")
        .arg(params.target_dir);

    for feature in &params.suite.features {
        cmd.arg("--features")
            .arg(format!("{}/{}", params.dylib_crate, feature));
    }

    let prior_rustflags = std::env::var("RUSTFLAGS").unwrap_or_default();
    let rustflags = if prior_rustflags.is_empty() {
        "-C prefer-dynamic".to_string()
    } else {
        format!("{prior_rustflags} -C prefer-dynamic")
    };
    cmd.env("RUSTFLAGS", &rustflags);

    let feature_args = params
        .suite
        .features
        .iter()
        .map(|f| format!(" --features {}/{}", params.dylib_crate, f))
        .collect::<String>();
    let invocation = format!(
        "RUSTFLAGS={:?} cargo build -p {} -p {} --release \
         --message-format=json-render-diagnostics --manifest-path {:?} --target-dir {:?}{}",
        rustflags,
        params.dylib_crate,
        COLLECTOR_PACKAGE,
        plan.root_manifest,
        params.target_dir,
        feature_args
    );

    let output = cmd.output().map_err(|e| Error::SubprocessSpawn {
        program: "cargo".to_string(),
        source: e,
    })?;

    if !output.status.success() {
        let mut stderr = String::from_utf8_lossy(&output.stderr).into_owned();
        append_offline_lockfile_note(&mut stderr, &plan);
        return Err(Error::Session(Outcome::DylibBuildFailed {
            invocation,
            stderr,
        }));
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let cargo_dylib_path =
        dylib::parse_dylib_path(&stdout, params.dylib_crate).ok_or_else(|| {
            Error::Session(Outcome::DylibNotFound {
                invocation: invocation.clone(),
                crate_name: params.dylib_crate.to_string(),
            })
        })?;

    let _ = params.toolchain;

    Ok(BuildOutput {
        cargo_dylib_path,
        deps_dir: params.target_dir.join("release/deps"),
        invocation,
    })
}

#[derive(Debug)]
struct Synthesis {
    root_manifest: PathBuf,
    collector_manifest: PathBuf,
    staged_dylib_manifest: PathBuf,
    staged_path_dep_manifests: Vec<PathBuf>,
    copied_lockfile: Option<PathBuf>,
}

#[derive(Debug, Clone)]
struct ManifestDoc {
    path: PathBuf,
    dir: PathBuf,
    table: toml::value::Table,
}

#[derive(Debug, Clone)]
struct WorkspaceContext {
    manifest: PathBuf,
    dir: PathBuf,
    table: toml::value::Table,
}

#[derive(Debug, Clone)]
struct StagedMember {
    package_name: String,
    source_manifest: PathBuf,
    member_dir: PathBuf,
    manifest_path: PathBuf,
}

struct SynthesisContext {
    dylib_crate: String,
    workspace_dir: PathBuf,
    workspace_dependencies: toml::value::Table,
    staged_by_source: BTreeMap<PathBuf, StagedMember>,
    staged_names: BTreeMap<String, PathBuf>,
    staged_path_dep_manifests: Vec<PathBuf>,
}

fn synthesize_workspace(
    dylib_crate: &str,
    suite: &Suite,
    metadata_manifest_path: &Path,
    target_dir: &Path,
) -> Result<Synthesis, Error> {
    validate_entry_guard(suite)?;

    let metadata = read_manifest(metadata_manifest_path)?;
    let metadata_package = package_name(&metadata)?;
    let workspace_context = find_workspace_context(&metadata.dir)?;
    let dylib_manifest = resolve_dylib_manifest(dylib_crate, &metadata, &workspace_context)?;
    let workspace_dir = target_dir.join("lihaaf-suite-workspace");

    crate::util::remove_path_race_free(&workspace_dir, "suite workspace")?;
    std::fs::create_dir_all(&workspace_dir)
        .map_err(|e| Error::io(e, "creating suite workspace", Some(workspace_dir.clone())))?;

    let copied_lockfile = copy_ancestor_lockfile(&metadata.dir, &workspace_dir)?;

    let mut root_workspace = build_root_workspace_table(&workspace_context, dylib_crate)?;
    let workspace_dependencies = root_workspace
        .get("dependencies")
        .and_then(toml::Value::as_table)
        .cloned()
        .unwrap_or_default();

    let mut ctx = SynthesisContext {
        dylib_crate: dylib_crate.to_string(),
        workspace_dir: workspace_dir.clone(),
        workspace_dependencies,
        staged_by_source: BTreeMap::new(),
        staged_names: BTreeMap::new(),
        staged_path_dep_manifests: Vec::new(),
    };

    if workspace_context
        .as_ref()
        .is_none_or(|workspace| workspace.manifest != dylib_manifest.path)
    {
        reject_member_local_overrides(&dylib_manifest.table, &dylib_manifest.dir)?;
    }
    let mut dylib_table = stage_manifest_table(&dylib_manifest, StageKind::Dylib)?;
    if metadata_package != dylib_crate {
        reject_package_workspace_split_roots(&metadata, &workspace_context)?;
    }
    ctx.rewrite_dependency_sections(
        &mut dylib_table,
        &dylib_manifest.dir,
        &workspace_dir.join("dylib"),
        CurrentPackage::Dylib,
    )?;

    let collector_dependencies = collector_dependencies(suite, &metadata, &mut ctx)?;
    let collector_table = collector_manifest_table(collector_dependencies);

    if !ctx.workspace_dependencies.is_empty() {
        root_workspace.insert(
            "dependencies".to_string(),
            toml::Value::Table(ctx.workspace_dependencies.clone()),
        );
    }

    let mut root = toml::value::Table::new();
    root_workspace.insert(
        "members".to_string(),
        toml::Value::Array(workspace_members(&ctx)),
    );
    root.insert("workspace".to_string(), toml::Value::Table(root_workspace));
    carry_workspace_root_top_level(&mut root, &workspace_context, dylib_crate)?;

    let root_manifest = workspace_dir.join("Cargo.toml");
    let staged_dylib_manifest = workspace_dir.join("dylib/Cargo.toml");
    let collector_manifest = workspace_dir.join("collector/Cargo.toml");
    write_manifest(&root_manifest, &root)?;
    write_manifest(&staged_dylib_manifest, &dylib_table)?;
    write_manifest(&collector_manifest, &collector_table)?;
    write_file(&workspace_dir.join("collector/src/lib.rs"), b"")?;

    Ok(Synthesis {
        root_manifest,
        collector_manifest,
        staged_dylib_manifest,
        staged_path_dep_manifests: ctx.staged_path_dep_manifests,
        copied_lockfile,
    })
}

fn validate_entry_guard(suite: &Suite) -> Result<(), Error> {
    let is_tests_target = suite
        .build_targets
        .as_slice()
        .iter()
        .map(String::as_str)
        .eq(["tests"]);
    if !is_tests_target {
        return Err(config_invalid(format!(
            "suite \"{}\" reached staged suite workspace build without \
             build_targets = [\"tests\"]. This path is only valid for opted-in suites.",
            suite.name
        )));
    }
    if suite.dev_deps.is_empty() {
        return Err(config_invalid(format!(
            "suite \"{}\" sets build_targets = [\"tests\"] but has no final dev_deps. \
             There is no fixture dependency graph to collect.",
            suite.name
        )));
    }
    Ok(())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StageKind {
    Dylib,
    PathDep,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CurrentPackage {
    Dylib,
    Other,
}

impl SynthesisContext {
    fn rewrite_dependency_sections(
        &mut self,
        manifest: &mut toml::value::Table,
        source_dir: &Path,
        staged_dir: &Path,
        current: CurrentPackage,
    ) -> Result<(), Error> {
        reject_member_local_overrides(manifest, source_dir)?;
        for section in ["dependencies", "dev-dependencies", "build-dependencies"] {
            if let Some(deps) = manifest
                .get_mut(section)
                .and_then(toml::Value::as_table_mut)
            {
                self.rewrite_dependency_table(deps, source_dir, staged_dir, current)?;
            }
        }

        if let Some(targets) = manifest
            .get_mut("target")
            .and_then(toml::Value::as_table_mut)
        {
            for (_, target) in targets.iter_mut() {
                let Some(target_table) = target.as_table_mut() else {
                    continue;
                };
                for section in ["dependencies", "dev-dependencies", "build-dependencies"] {
                    if let Some(deps) = target_table
                        .get_mut(section)
                        .and_then(toml::Value::as_table_mut)
                    {
                        self.rewrite_dependency_table(deps, source_dir, staged_dir, current)?;
                    }
                }
            }
        }
        Ok(())
    }

    fn rewrite_dependency_table(
        &mut self,
        deps: &mut toml::value::Table,
        source_dir: &Path,
        staged_dir: &Path,
        current: CurrentPackage,
    ) -> Result<(), Error> {
        let keys = deps.keys().cloned().collect::<Vec<_>>();
        for key in keys {
            let Some(value) = deps.get_mut(&key) else {
                continue;
            };
            self.rewrite_dependency_value(&key, value, source_dir, staged_dir, current)?;
        }
        Ok(())
    }

    fn rewrite_dependency_value(
        &mut self,
        key: &str,
        value: &mut toml::Value,
        source_dir: &Path,
        staged_dir: &Path,
        current: CurrentPackage,
    ) -> Result<(), Error> {
        match value {
            toml::Value::String(_) => {
                if key == self.dylib_crate {
                    return Err(config_invalid(format!(
                        "registry dependency `{key}` names dylib_crate `{}` in staged graph; \
                         registry/git self-edges are not supported for staged suite workspaces.",
                        self.dylib_crate
                    )));
                }
            }
            toml::Value::Table(spec) => {
                if spec.get("workspace").and_then(toml::Value::as_bool) == Some(true) {
                    self.select_workspace_dependency(key)?;
                    return Ok(());
                }

                let package = spec.get("package").and_then(toml::Value::as_str);
                let has_git = spec.contains_key("git");
                let has_path = spec.contains_key("path");
                if !has_path {
                    if key == self.dylib_crate || package == Some(self.dylib_crate.as_str()) {
                        let source = if has_git { "git" } else { "registry" };
                        return Err(config_invalid(format!(
                            "{source} dependency `{key}` names dylib_crate `{}` in staged graph; \
                             registry/git self-edges are not supported.",
                            self.dylib_crate
                        )));
                    }
                    return Ok(());
                }

                let path = spec
                    .get("path")
                    .and_then(toml::Value::as_str)
                    .ok_or_else(|| {
                        config_invalid(format!(
                            "path dependency `{key}` has non-string `path`; lihaaf cannot \
                             analyze this back-edge safely."
                        ))
                    })?;
                let dep_dir = absolutize_path(source_dir, Path::new(path));
                let dep_manifest = dep_dir.join("Cargo.toml");
                let dep = read_manifest(&dep_manifest).map_err(|e| {
                    config_invalid(format!(
                        "path dependency `{key}` at `{}` cannot be analyzed safely: {e}",
                        dep_manifest.display()
                    ))
                })?;
                let dep_name = package_name(&dep).map_err(|e| {
                    config_invalid(format!(
                        "path dependency `{key}` at `{}` cannot be analyzed safely: {e}",
                        dep_manifest.display()
                    ))
                })?;

                if dep_name == self.dylib_crate {
                    if current == CurrentPackage::Dylib {
                        return Err(config_invalid(format!(
                            "staged dylib manifest has a path dependency `{key}` back to \
                             `{}`; lihaaf cannot rewrite a self-cycle safely.",
                            self.dylib_crate
                        )));
                    }
                    spec.insert(
                        "path".to_string(),
                        toml::Value::String(relative_path_string(staged_dir, &self.dylib_dir())),
                    );
                    return Ok(());
                }

                let staged = self.stage_path_dependency(dep)?;
                spec.insert(
                    "path".to_string(),
                    toml::Value::String(relative_path_string(staged_dir, &staged.member_dir)),
                );
            }
            _ => {
                return Err(config_invalid(format!(
                    "dependency `{key}` uses an unsupported TOML shape; staged suite workspace \
                     synthesis supports string and table dependency specs."
                )));
            }
        }
        Ok(())
    }

    fn select_workspace_dependency(&mut self, key: &str) -> Result<(), Error> {
        let mut value = self
            .workspace_dependencies
            .get(key)
            .cloned()
            .ok_or_else(|| {
                config_invalid(format!(
                    "dependency `{key}` uses `workspace = true`, but the staged workspace has no \
                 `[workspace.dependencies.{key}]` entry to carry."
                ))
            })?;
        let workspace_dir = self.workspace_dir.clone();
        self.rewrite_dependency_value(
            key,
            &mut value,
            &workspace_dir,
            &workspace_dir,
            CurrentPackage::Other,
        )?;
        self.workspace_dependencies.insert(key.to_string(), value);
        Ok(())
    }

    fn stage_path_dependency(&mut self, dep: ManifestDoc) -> Result<StagedMember, Error> {
        let canonical = canonical_manifest_key(&dep.path)?;
        if let Some(existing) = self.staged_by_source.get(&canonical) {
            return Ok(existing.clone());
        }

        let package_name = package_name(&dep)?;
        if let Some(other_source) = self.staged_names.get(&package_name)
            && other_source != &canonical
        {
            return Err(config_invalid(format!(
                "multiple staged path dependencies use package name `{package_name}`; lihaaf \
                 cannot safely synthesize an unambiguous workspace package graph."
            )));
        }

        let member_dir = self
            .workspace_dir
            .join("staged-path-deps")
            .join(sanitize_member_dir(&package_name));
        let manifest_path = member_dir.join("Cargo.toml");
        let staged = StagedMember {
            package_name: package_name.clone(),
            source_manifest: canonical.clone(),
            member_dir: member_dir.clone(),
            manifest_path: manifest_path.clone(),
        };
        self.staged_by_source
            .insert(canonical.clone(), staged.clone());
        self.staged_names.insert(package_name, canonical);

        reject_member_local_overrides(&dep.table, &dep.dir)?;
        let mut table = stage_manifest_table(&dep, StageKind::PathDep)?;
        self.rewrite_dependency_sections(&mut table, &dep.dir, &member_dir, CurrentPackage::Other)?;
        write_manifest(&manifest_path, &table)?;
        self.staged_path_dep_manifests.push(manifest_path);

        Ok(staged)
    }

    fn dylib_dir(&self) -> PathBuf {
        self.workspace_dir.join("dylib")
    }
}

fn collector_dependencies(
    suite: &Suite,
    metadata: &ManifestDoc,
    ctx: &mut SynthesisContext,
) -> Result<toml::value::Table, Error> {
    let dev_deps = metadata
        .table
        .get("dev-dependencies")
        .and_then(toml::Value::as_table)
        .cloned()
        .unwrap_or_default();
    let mut out = toml::value::Table::new();

    for dep in &suite.dev_deps {
        let Some(mut spec) = dev_deps.get(dep).cloned() else {
            if target_specific_dev_dep_exists(&metadata.table, dep) {
                return Err(config_invalid(format!(
                    "dev_dep `{dep}` exists only in target-specific dev-dependencies; \
                     staged suite workspaces support only top-level [dev-dependencies]."
                )));
            }
            return Err(config_invalid(format!(
                "dev_dep `{dep}` is missing from top-level [dev-dependencies] in `{}`.",
                metadata.path.display()
            )));
        };

        reject_renamed_or_optional_dev_dep(dep, &spec)?;
        ctx.rewrite_dependency_value(
            dep,
            &mut spec,
            &metadata.dir,
            &ctx.workspace_dir.join("collector"),
            CurrentPackage::Other,
        )?;
        out.insert(dep.clone(), spec);
    }

    Ok(out)
}

fn collector_manifest_table(dependencies: toml::value::Table) -> toml::value::Table {
    let mut package = toml::value::Table::new();
    package.insert(
        "name".to_string(),
        toml::Value::String(COLLECTOR_PACKAGE.to_string()),
    );
    package.insert(
        "version".to_string(),
        toml::Value::String("0.0.0".to_string()),
    );
    package.insert(
        "edition".to_string(),
        toml::Value::String("2021".to_string()),
    );
    package.insert("publish".to_string(), toml::Value::Boolean(false));

    let mut lib = toml::value::Table::new();
    lib.insert(
        "path".to_string(),
        toml::Value::String("src/lib.rs".to_string()),
    );

    let mut root = toml::value::Table::new();
    root.insert("package".to_string(), toml::Value::Table(package));
    root.insert("lib".to_string(), toml::Value::Table(lib));
    root.insert("dependencies".to_string(), toml::Value::Table(dependencies));
    root
}

fn stage_manifest_table(
    manifest: &ManifestDoc,
    kind: StageKind,
) -> Result<toml::value::Table, Error> {
    reject_build_script(manifest)?;

    let mut table = manifest.table.clone();
    table.remove("bin");
    table.remove("example");
    table.remove("test");
    table.remove("bench");
    table.remove("dev-dependencies");
    table.remove("workspace");
    table.remove("patch");
    table.remove("replace");
    table
        .entry("dependencies".to_string())
        .or_insert_with(|| toml::Value::Table(toml::value::Table::new()));

    if let Some(package) = table.get_mut("package").and_then(toml::Value::as_table_mut) {
        package.remove("workspace");
        for key in ["autobins", "autoexamples", "autotests", "autobenches"] {
            package.insert(key.to_string(), toml::Value::Boolean(false));
        }
    }

    let lib_path = table
        .get("lib")
        .and_then(toml::Value::as_table)
        .and_then(|lib| lib.get("path"))
        .and_then(toml::Value::as_str)
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("src/lib.rs"));
    let absolute_lib_path = absolutize_path(&manifest.dir, &lib_path);

    let lib = table
        .entry("lib".to_string())
        .or_insert_with(|| toml::Value::Table(toml::value::Table::new()))
        .as_table_mut()
        .ok_or_else(|| {
            config_invalid(format!(
                "`[lib]` in `{}` is not a table; lihaaf cannot stage the package.",
                manifest.path.display()
            ))
        })?;
    lib.insert(
        "path".to_string(),
        toml::Value::String(absolute_lib_path.to_string_lossy().into_owned()),
    );
    if kind == StageKind::Dylib {
        ensure_dylib_crate_types(lib);
    }

    Ok(table)
}

fn ensure_dylib_crate_types(lib: &mut toml::value::Table) {
    let mut seen = BTreeSet::new();
    let mut values = Vec::new();
    for required in ["rlib", "dylib"] {
        seen.insert(required.to_string());
        values.push(toml::Value::String(required.to_string()));
    }
    if let Some(existing) = lib.get("crate-type").and_then(toml::Value::as_array) {
        for value in existing {
            let Some(kind) = value.as_str() else {
                continue;
            };
            if seen.insert(kind.to_string()) {
                values.push(toml::Value::String(kind.to_string()));
            }
        }
    }
    lib.insert("crate-type".to_string(), toml::Value::Array(values));
}

fn build_root_workspace_table(
    ctx: &Option<WorkspaceContext>,
    _dylib_crate: &str,
) -> Result<toml::value::Table, Error> {
    let mut workspace = toml::value::Table::new();
    if let Some(ctx) = ctx {
        let source_ws = ctx
            .table
            .get("workspace")
            .and_then(toml::Value::as_table)
            .ok_or_else(|| {
                config_invalid(format!(
                    "`{}` was identified as a workspace root but has no [workspace] table.",
                    ctx.manifest.display()
                ))
            })?;
        for key in ["resolver", "package", "lints", "metadata", "dependencies"] {
            if let Some(value) = source_ws.get(key).cloned() {
                workspace.insert(key.to_string(), value);
            }
        }
        if let Some(package) = workspace
            .get_mut("package")
            .and_then(toml::Value::as_table_mut)
        {
            absolutize_workspace_package_paths(package, &ctx.dir)?;
        }
        if let Some(deps) = workspace
            .get_mut("dependencies")
            .and_then(toml::Value::as_table_mut)
        {
            absolutize_dependency_table_paths(deps, &ctx.dir)?;
        }
    }
    Ok(workspace)
}

fn carry_workspace_root_top_level(
    staged_root: &mut toml::value::Table,
    ctx: &Option<WorkspaceContext>,
    dylib_crate: &str,
) -> Result<(), Error> {
    let Some(ctx) = ctx else {
        return Ok(());
    };
    if let Some(mut replace) = ctx.table.get("replace").cloned() {
        absolutize_replace_paths(&mut replace, &ctx.dir)?;
        staged_root.insert("replace".to_string(), replace);
    }
    if let Some(value) = ctx.table.get("profile").cloned() {
        staged_root.insert("profile".to_string(), value);
    }

    if let Some(patch) = ctx.table.get("patch").cloned() {
        let mut patch = patch.as_table().cloned().ok_or_else(|| {
            config_invalid(format!(
                "`[patch]` in `{}` is not a table; staged suite workspace cannot carry it.",
                ctx.manifest.display()
            ))
        })?;
        for (_, registry) in patch.iter_mut() {
            let entries = registry.as_table_mut().ok_or_else(|| {
                config_invalid(format!(
                    "`[patch]` registry in `{}` is not a table.",
                    ctx.manifest.display()
                ))
            })?;
            let keys = entries.keys().cloned().collect::<Vec<_>>();
            for key in keys {
                let Some(entry) = entries.get_mut(&key) else {
                    continue;
                };
                if key == dylib_crate
                    || entry
                        .as_table()
                        .and_then(|t| t.get("package"))
                        .and_then(toml::Value::as_str)
                        == Some(dylib_crate)
                {
                    return Err(config_invalid(format!(
                        "workspace-root patch entry `{key}` names dylib_crate `{dylib_crate}`; \
                         staged suite workspaces reject self patches."
                    )));
                }
                if let Some(table) = entry.as_table_mut()
                    && let Some(path) = table.get("path").and_then(toml::Value::as_str)
                {
                    table.insert(
                        "path".to_string(),
                        toml::Value::String(
                            absolutize_path(&ctx.dir, Path::new(path))
                                .to_string_lossy()
                                .into_owned(),
                        ),
                    );
                }
            }
        }
        staged_root.insert("patch".to_string(), toml::Value::Table(patch));
    }
    Ok(())
}

fn workspace_members(ctx: &SynthesisContext) -> Vec<toml::Value> {
    let mut members = vec![
        toml::Value::String("dylib".to_string()),
        toml::Value::String("collector".to_string()),
    ];
    for staged in ctx.staged_by_source.values() {
        members.push(toml::Value::String(format!(
            "staged-path-deps/{}",
            staged
                .member_dir
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or(&staged.package_name)
        )));
    }
    members
}

fn reject_renamed_or_optional_dev_dep(dep: &str, spec: &toml::Value) -> Result<(), Error> {
    let Some(table) = spec.as_table() else {
        return Ok(());
    };
    if table.contains_key("package") {
        return Err(config_invalid(format!(
            "dev_dep `{dep}` uses `package = ...`; renamed collector dependencies need extern \
             alias support and are not implemented in v0.1.0."
        )));
    }
    if table.get("optional").and_then(toml::Value::as_bool) == Some(true) {
        return Err(config_invalid(format!(
            "dev_dep `{dep}` is optional; collector feature-forcing semantics are not \
             implemented in v0.1.0."
        )));
    }
    Ok(())
}

fn reject_build_script(manifest: &ManifestDoc) -> Result<(), Error> {
    let package = manifest
        .table
        .get("package")
        .and_then(toml::Value::as_table)
        .ok_or_else(|| {
            config_invalid(format!(
                "`{}` has no [package] table; lihaaf cannot stage it.",
                manifest.path.display()
            ))
        })?;
    match package.get("build") {
        Some(toml::Value::Boolean(false)) => Ok(()),
        Some(_) => Err(config_invalid(format!(
            "staged package `{}` declares a build script in `{}`; build-script staging is not \
             implemented for suite workspaces.",
            package_name(manifest)?,
            manifest.path.display()
        ))),
        None if manifest.dir.join("build.rs").exists() => Err(config_invalid(format!(
            "staged package `{}` has default `build.rs` at `{}`; build-script staging is not \
             implemented for suite workspaces.",
            package_name(manifest)?,
            manifest.dir.join("build.rs").display()
        ))),
        None => Ok(()),
    }
}

fn reject_member_local_overrides(
    manifest: &toml::value::Table,
    source_dir: &Path,
) -> Result<(), Error> {
    if manifest.contains_key("patch") {
        return Err(config_invalid(format!(
            "member-local [patch.*] in `{}` is not supported by staged suite workspaces.",
            source_dir.join("Cargo.toml").display()
        )));
    }
    if manifest.contains_key("replace") {
        return Err(config_invalid(format!(
            "member-local [replace] in `{}` is not supported by staged suite workspaces.",
            source_dir.join("Cargo.toml").display()
        )));
    }
    Ok(())
}

fn reject_self_edges_in_dependency_table(
    deps: &toml::value::Table,
    dylib_crate: &str,
) -> Result<(), Error> {
    for (key, value) in deps {
        let package = value
            .as_table()
            .and_then(|t| t.get("package"))
            .and_then(toml::Value::as_str);
        let is_path = value
            .as_table()
            .and_then(|t| t.get("path"))
            .and_then(toml::Value::as_str)
            .is_some();
        if !is_path && (key == dylib_crate || package == Some(dylib_crate)) {
            return Err(config_invalid(format!(
                "workspace dependency `{key}` names dylib_crate `{dylib_crate}` through a \
                 registry/git source; staged suite workspaces reject registry/git self-edges."
            )));
        }
    }
    Ok(())
}

fn absolutize_workspace_package_paths(
    package: &mut toml::value::Table,
    base: &Path,
) -> Result<(), Error> {
    for key in ["readme", "license-file"] {
        if let Some(value) = package.get_mut(key) {
            match value {
                toml::Value::String(path) => {
                    *path = absolutize_path(base, Path::new(path))
                        .to_string_lossy()
                        .into_owned();
                }
                toml::Value::Boolean(_) if key == "readme" => {}
                _ => {
                    return Err(config_invalid(format!(
                        "workspace.package.{key} must be a string path{} for staged suite \
                         workspace carry-down.",
                        if key == "readme" { " or boolean" } else { "" }
                    )));
                }
            }
        }
    }
    Ok(())
}

fn absolutize_replace_paths(value: &mut toml::Value, base: &Path) -> Result<(), Error> {
    let replace = value.as_table_mut().ok_or_else(|| {
        config_invalid("[replace] must be a table for staged suite workspace carry-down.")
    })?;
    for (key, entry) in replace.iter_mut() {
        let table = entry.as_table_mut().ok_or_else(|| {
            config_invalid(format!(
                "[replace] entry `{key}` must be a table for staged suite workspace carry-down."
            ))
        })?;
        if let Some(path) = table.get("path").and_then(toml::Value::as_str) {
            table.insert(
                "path".to_string(),
                toml::Value::String(
                    absolutize_path(base, Path::new(path))
                        .to_string_lossy()
                        .into_owned(),
                ),
            );
        } else if table.contains_key("path") {
            return Err(config_invalid(format!(
                "[replace] entry `{key}` has non-string `path`; lihaaf cannot carry it safely."
            )));
        }
    }
    Ok(())
}

fn absolutize_dependency_table_paths(
    deps: &mut toml::value::Table,
    base: &Path,
) -> Result<(), Error> {
    for (key, value) in deps {
        let Some(table) = value.as_table_mut() else {
            continue;
        };
        if let Some(path) = table.get("path").and_then(toml::Value::as_str) {
            table.insert(
                "path".to_string(),
                toml::Value::String(
                    absolutize_path(base, Path::new(path))
                        .to_string_lossy()
                        .into_owned(),
                ),
            );
        } else if table.contains_key("path") {
            return Err(config_invalid(format!(
                "dependency `{key}` has non-string `path`; lihaaf cannot analyze it safely."
            )));
        }
    }
    Ok(())
}

fn target_specific_dev_dep_exists(root: &toml::value::Table, dep: &str) -> bool {
    root.get("target")
        .and_then(toml::Value::as_table)
        .map(|targets| {
            targets.values().any(|target| {
                target
                    .get("dev-dependencies")
                    .and_then(toml::Value::as_table)
                    .is_some_and(|deps| deps.contains_key(dep))
            })
        })
        .unwrap_or(false)
}

fn resolve_dylib_manifest(
    dylib_crate: &str,
    metadata: &ManifestDoc,
    workspace_context: &Option<WorkspaceContext>,
) -> Result<ManifestDoc, Error> {
    if package_name(metadata)? == dylib_crate {
        return Ok(metadata.clone());
    }
    let Some(workspace) = workspace_context else {
        return Err(config_invalid(format!(
            "dylib_crate `{dylib_crate}` differs from metadata package `{}` but no ancestor \
             virtual workspace was found to resolve the split-crate shape.",
            package_name(metadata)?
        )));
    };
    if workspace.table.contains_key("package") {
        return Err(config_invalid(format!(
            "dylib_crate `{dylib_crate}` differs from the metadata package, but ancestor \
             workspace `{}` also has [package]; package+workspace split-crate roots are not \
             supported by the staged suite workspace resolver.",
            workspace.manifest.display()
        )));
    }

    let (manifest, _) =
        crate::compat::overlay::resolve_workspace_member_manifest(&workspace.manifest, dylib_crate)
            .map_err(|e| {
                config_invalid(format!(
                    "could not resolve dylib_crate `{dylib_crate}` from ancestor virtual \
                     workspace `{}`: {e}",
                    workspace.manifest.display()
                ))
            })?;
    read_manifest(&manifest)
}

fn reject_package_workspace_split_roots(
    metadata: &ManifestDoc,
    workspace_context: &Option<WorkspaceContext>,
) -> Result<(), Error> {
    if let Some(workspace) = workspace_context
        && workspace.manifest == metadata.path
        && metadata.table.contains_key("workspace")
    {
        return Err(config_invalid(format!(
            "split-crate staged suite workspace resolution does not support package+workspace \
             roots like `{}`.",
            metadata.path.display()
        )));
    }
    Ok(())
}

fn find_workspace_context(start_dir: &Path) -> Result<Option<WorkspaceContext>, Error> {
    for dir in start_dir.ancestors() {
        let manifest = dir.join("Cargo.toml");
        if !manifest.exists() {
            continue;
        }
        let doc = read_manifest(&manifest)?;
        if doc.table.contains_key("workspace") {
            return Ok(Some(WorkspaceContext {
                manifest,
                dir: dir.to_path_buf(),
                table: doc.table,
            }));
        }
    }
    Ok(None)
}

fn copy_ancestor_lockfile(
    start_dir: &Path,
    workspace_dir: &Path,
) -> Result<Option<PathBuf>, Error> {
    for dir in start_dir.ancestors() {
        let lockfile = dir.join("Cargo.lock");
        if lockfile.exists() {
            std::fs::copy(&lockfile, workspace_dir.join("Cargo.lock")).map_err(|e| {
                Error::io(
                    e,
                    "copying ancestor Cargo.lock into suite workspace",
                    Some(lockfile.clone()),
                )
            })?;
            return Ok(Some(lockfile));
        }
    }
    Ok(None)
}

fn read_manifest(path: &Path) -> Result<ManifestDoc, Error> {
    let text = std::fs::read_to_string(path)
        .map_err(|e| Error::io(e, "reading Cargo.toml", Some(path.to_path_buf())))?;
    let value: toml::Value =
        toml::from_str(&text).map_err(|e: toml::de::Error| Error::TomlParse {
            path: path.to_path_buf(),
            message: e.to_string(),
        })?;
    let table = value.as_table().cloned().ok_or_else(|| {
        config_invalid(format!(
            "`{}` did not parse to a TOML table.",
            path.display()
        ))
    })?;
    let dir = path
        .parent()
        .map(Path::to_path_buf)
        .unwrap_or_else(|| PathBuf::from("."));
    Ok(ManifestDoc {
        path: path.to_path_buf(),
        dir,
        table,
    })
}

fn write_manifest(path: &Path, table: &toml::value::Table) -> Result<(), Error> {
    let bytes = toml::to_string_pretty(table)
        .map_err(|e| Error::JsonParse {
            context: format!("serializing staged manifest `{}`", path.display()),
            message: e.to_string(),
        })?
        .into_bytes();
    write_file(path, &bytes)
}

fn write_file(path: &Path, bytes: &[u8]) -> Result<(), Error> {
    crate::util::write_file_atomic(path, bytes)
}

fn append_offline_lockfile_note(stderr: &mut String, plan: &Synthesis) {
    if !looks_like_ambient_offline_failure(stderr) {
        return;
    }
    let staged_lockfile = plan
        .root_manifest
        .parent()
        .map(|dir| dir.join("Cargo.lock"));
    stderr.push_str("\n\nlihaaf: staged suite workspace note: Cargo appears to have failed under ambient offline/cache policy. ");
    if let (Some(source), Some(staged)) = (&plan.copied_lockfile, staged_lockfile) {
        stderr.push_str(&format!(
            "lihaaf copied the ancestor Cargo.lock from `{}` to `{}`; updates stay inside the temporary suite workspace. ",
            source.display(),
            staged.display()
        ));
    } else {
        stderr.push_str("No ancestor Cargo.lock was copied into the temporary suite workspace. ");
    }
    stderr.push_str(
        "Warm Cargo's registry/git cache for the collector dependencies, or remove \
         build_targets = [\"tests\"] for this suite in environments without registry access.",
    );
}

fn looks_like_ambient_offline_failure(stderr: &str) -> bool {
    let stderr = stderr.to_ascii_lowercase();
    stderr.contains("offline")
        || stderr.contains("no matching package named")
        || stderr.contains("failed to download")
        || stderr.contains("could not download")
        || stderr.contains("failed to fetch")
}

fn package_name(manifest: &ManifestDoc) -> Result<String, Error> {
    manifest
        .table
        .get("package")
        .and_then(toml::Value::as_table)
        .and_then(|p| p.get("name"))
        .and_then(toml::Value::as_str)
        .map(str::to_string)
        .ok_or_else(|| {
            config_invalid(format!(
                "`{}` has no [package].name; lihaaf cannot stage it.",
                manifest.path.display()
            ))
        })
}

fn canonical_manifest_key(path: &Path) -> Result<PathBuf, Error> {
    std::fs::canonicalize(path).map_err(|e| {
        Error::io(
            e,
            "canonicalizing staged path dependency manifest",
            Some(path.to_path_buf()),
        )
    })
}

fn absolutize_path(base: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        base.join(path)
    }
}

fn relative_path_string(from_dir: &Path, to_dir: &Path) -> String {
    let Ok(from_rel) = from_dir.strip_prefix(common_workspace_root(from_dir, to_dir)) else {
        return to_dir.to_string_lossy().into_owned();
    };
    let Ok(to_rel) = to_dir.strip_prefix(common_workspace_root(from_dir, to_dir)) else {
        return to_dir.to_string_lossy().into_owned();
    };
    let mut parts = Vec::new();
    for _ in from_rel.components() {
        parts.push("..".to_string());
    }
    for component in to_rel.components() {
        parts.push(component.as_os_str().to_string_lossy().into_owned());
    }
    if parts.is_empty() {
        ".".to_string()
    } else {
        parts.join("/")
    }
}

fn common_workspace_root<'a>(a: &'a Path, b: &'a Path) -> &'a Path {
    let mut last = Path::new("");
    for ancestor in a.ancestors() {
        if b.starts_with(ancestor) {
            last = ancestor;
            break;
        }
    }
    last
}

fn sanitize_member_dir(name: &str) -> String {
    name.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

fn config_invalid(message: impl Into<String>) -> Error {
    Error::Session(Outcome::ConfigInvalid {
        message: message.into(),
    })
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use tempfile::TempDir;

    use super::synthesize_workspace;
    use crate::config::{DEFAULT_COMPILE_FAIL_MARKER, DEFAULT_EDITION, DEFAULT_SUITE_NAME, Suite};
    use crate::error::{Error, Outcome};
    use crate::normalize::Substitution;

    fn suite(dev_deps: &[&str]) -> Suite {
        Suite {
            name: DEFAULT_SUITE_NAME.to_string(),
            extern_crates: vec!["core".to_string()],
            fixture_dirs: vec![PathBuf::from("tests/lihaaf")],
            features: Vec::new(),
            edition: DEFAULT_EDITION.to_string(),
            dev_deps: dev_deps.iter().map(|s| (*s).to_string()).collect(),
            build_targets: vec!["tests".to_string()]
                .try_into()
                .expect("valid build target"),
            compile_fail_marker: DEFAULT_COMPILE_FAIL_MARKER.to_string(),
            fixture_timeout_secs: 90,
            per_fixture_memory_mb: 1024,
            allow_lints: Vec::new(),
            extra_substitutions: Vec::<Substitution>::new(),
            strip_lines: Vec::new(),
            strip_line_prefixes: Vec::new(),
        }
    }

    fn write(path: &Path, contents: &str) {
        std::fs::create_dir_all(path.parent().expect("test path has parent"))
            .expect("create test parent");
        std::fs::write(path, contents).expect("write test file");
    }

    fn read_toml(path: &Path) -> toml::Value {
        toml::from_str(&std::fs::read_to_string(path).expect("read toml")).expect("parse toml")
    }

    fn table<'a>(value: &'a toml::Value, key: &str) -> &'a toml::value::Table {
        value
            .get(key)
            .and_then(toml::Value::as_table)
            .unwrap_or_else(|| panic!("missing table `{key}` in {value:#?}"))
    }

    fn dep_table<'a>(
        value: &'a toml::Value,
        table_name: &str,
        dep: &str,
    ) -> &'a toml::value::Table {
        table(value, table_name)
            .get(dep)
            .and_then(toml::Value::as_table)
            .unwrap_or_else(|| panic!("missing dependency `{dep}` in table `{table_name}`"))
    }

    fn config_invalid_message(err: Error) -> String {
        match err {
            Error::Session(Outcome::ConfigInvalid { message }) => message,
            other => panic!("expected ConfigInvalid, got {other:?}"),
        }
    }

    #[test]
    fn entry_guard_rejects_non_opted_or_empty_dev_deps_before_manifest_read() {
        let tmp = TempDir::new().expect("tempdir");

        let mut not_opted = suite(&["helper"]);
        not_opted.build_targets = Default::default();
        let err = synthesize_workspace(
            "core",
            &not_opted,
            &tmp.path().join("missing/Cargo.toml"),
            tmp.path(),
        )
        .unwrap_err();
        assert!(config_invalid_message(err).contains("build_targets"));

        let err = synthesize_workspace(
            "core",
            &suite(&[]),
            &tmp.path().join("missing/Cargo.toml"),
            tmp.path(),
        )
        .unwrap_err();
        assert!(config_invalid_message(err).contains("dev_deps"));
    }

    #[test]
    fn collector_deps_are_metadata_dev_deps_and_paths_are_absolutized() {
        let tmp = TempDir::new().expect("tempdir");
        let root = tmp.path();
        let metadata_manifest = root.join("metadata/Cargo.toml");
        let local_dep = root.join("metadata/local-helper");
        write(
            &metadata_manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[lib]
path = "src/lib.rs"

[dev-dependencies]
helper = { path = "local-helper", features = ["derive"], default-features = false }
serde = "1"
"#,
        );
        write(&root.join("metadata/src/lib.rs"), "");
        write(
            &local_dep.join("Cargo.toml"),
            r#"
[package]
name = "helper"
version = "0.1.0"
edition = "2021"
"#,
        );
        write(&local_dep.join("src/lib.rs"), "");

        let out = synthesize_workspace(
            "core",
            &suite(&["helper", "serde"]),
            &metadata_manifest,
            root,
        )
        .expect("synthesis succeeds");

        let collector = read_toml(&out.collector_manifest);
        let helper = dep_table(&collector, "dependencies", "helper");
        assert_eq!(
            helper.get("path").and_then(toml::Value::as_str),
            Some("../staged-path-deps/helper")
        );
        assert_eq!(
            helper
                .get("default-features")
                .and_then(toml::Value::as_bool),
            Some(false)
        );
        assert!(
            table(&collector, "dependencies").contains_key("serde"),
            "registry dev-dep should be copied to collector dependencies"
        );
        let staged_helper = out
            .staged_path_dep_manifests
            .iter()
            .find(|p| {
                table(&read_toml(p), "package")
                    .get("name")
                    .and_then(toml::Value::as_str)
                    == Some("helper")
            })
            .expect("helper staged");
        assert_eq!(
            table(&read_toml(staged_helper), "lib")
                .get("path")
                .and_then(toml::Value::as_str),
            Some(
                local_dep
                    .join("src/lib.rs")
                    .to_str()
                    .expect("utf8 temp path")
            )
        );

        let dylib = read_toml(&out.staged_dylib_manifest);
        assert!(
            !table(&dylib, "dependencies").contains_key("helper"),
            "metadata dev-deps must not be promoted into staged dylib dependencies"
        );
    }

    #[test]
    fn invalid_dev_dep_shapes_reject_before_cargo() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
renamed = { package = "actual", version = "1" }
optional_dep = { version = "1", optional = true }

[target.'cfg(unix)'.dev-dependencies]
target_only = "1"
"#,
        );
        write(&tmp.path().join("src/lib.rs"), "");

        let missing =
            synthesize_workspace("core", &suite(&["missing"]), &manifest, tmp.path()).unwrap_err();
        assert!(config_invalid_message(missing).contains("missing"));

        let target_only =
            synthesize_workspace("core", &suite(&["target_only"]), &manifest, tmp.path())
                .unwrap_err();
        assert!(config_invalid_message(target_only).contains("target-specific"));

        let renamed =
            synthesize_workspace("core", &suite(&["renamed"]), &manifest, tmp.path()).unwrap_err();
        let renamed_msg = config_invalid_message(renamed);
        assert!(renamed_msg.contains("renamed"));
        assert!(renamed_msg.contains("package"));

        let optional =
            synthesize_workspace("core", &suite(&["optional_dep"]), &manifest, tmp.path())
                .unwrap_err();
        let optional_msg = config_invalid_message(optional);
        assert!(optional_msg.contains("optional_dep"));
        assert!(optional_msg.contains("optional"));
    }

    #[test]
    fn staged_dylib_has_absolute_lib_path_crate_types_and_strips_targets() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"
autobins = true
autoexamples = true
autotests = true
autobenches = true

[lib]
path = "src/custom.rs"
name = "core_lib"
crate-type = ["cdylib"]

[[bin]]
name = "cli"
path = "src/main.rs"

[[example]]
name = "demo"
path = "examples/demo.rs"

[[test]]
name = "smoke"
path = "tests/smoke.rs"

[[bench]]
name = "bench"
path = "benches/bench.rs"

[dev-dependencies]
helper = "1"
"#,
        );
        write(&tmp.path().join("src/custom.rs"), "");

        let out = synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path())
            .expect("synthesis succeeds");
        let staged = read_toml(&out.staged_dylib_manifest);
        let lib = table(&staged, "lib");

        assert_eq!(
            lib.get("path").and_then(toml::Value::as_str),
            Some(
                tmp.path()
                    .join("src/custom.rs")
                    .to_str()
                    .expect("utf8 temp path")
            )
        );
        let crate_types: Vec<_> = lib
            .get("crate-type")
            .and_then(toml::Value::as_array)
            .expect("crate-type array")
            .iter()
            .map(|v| v.as_str().expect("string crate-type"))
            .collect();
        assert!(crate_types.contains(&"rlib"));
        assert!(crate_types.contains(&"dylib"));
        assert!(crate_types.contains(&"cdylib"));
        assert_eq!(
            table(&staged, "package")
                .get("autobins")
                .and_then(toml::Value::as_bool),
            Some(false)
        );
        assert!(staged.get("bin").is_none());
        assert!(staged.get("example").is_none());
        assert!(staged.get("test").is_none());
        assert!(staged.get("bench").is_none());
    }

    #[test]
    fn staged_dylib_strips_source_workspace_table() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[workspace]
members = ["."]
resolver = "2"

[dev-dependencies]
helper = "1"
"#,
        );
        write(&tmp.path().join("src/lib.rs"), "");

        let out = synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path())
            .expect("synthesis succeeds");
        let staged = read_toml(&out.staged_dylib_manifest);

        assert!(
            staged.get("workspace").is_none(),
            "staged workspace members must not retain their source `[workspace]` table"
        );
    }

    #[test]
    fn build_scripts_reject_for_staged_packages() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"
build = "build.rs"

[dev-dependencies]
helper = "1"
"#,
        );
        write(&tmp.path().join("src/lib.rs"), "");
        let err =
            synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path()).unwrap_err();
        assert!(config_invalid_message(err).contains("build script"));

        let implicit = tmp.path().join("implicit/Cargo.toml");
        write(
            &implicit,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = "1"
"#,
        );
        write(&tmp.path().join("implicit/src/lib.rs"), "");
        write(&tmp.path().join("implicit/build.rs"), "");
        let err =
            synthesize_workspace("core", &suite(&["helper"]), &implicit, tmp.path()).unwrap_err();
        assert!(config_invalid_message(err).contains("build.rs"));
    }

    #[test]
    fn path_back_edge_to_dylib_crate_is_rewritten_to_staged_member() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("core/Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = { path = "../helper" }
"#,
        );
        write(&tmp.path().join("core/src/lib.rs"), "");
        write(
            &tmp.path().join("helper/Cargo.toml"),
            r#"
[package]
name = "helper"
version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../core", features = ["macros"] }
"#,
        );
        write(&tmp.path().join("helper/src/lib.rs"), "");

        let out = synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path())
            .expect("synthesis succeeds");
        let staged_helper = out
            .staged_path_dep_manifests
            .iter()
            .find(|p| {
                table(&read_toml(p), "package")
                    .get("name")
                    .and_then(toml::Value::as_str)
                    == Some("helper")
            })
            .expect("helper staged");
        let helper = read_toml(staged_helper);
        let core = dep_table(&helper, "dependencies", "core");
        assert_eq!(
            core.get("path").and_then(toml::Value::as_str),
            Some("../../dylib")
        );
        assert_eq!(
            core.get("features")
                .and_then(toml::Value::as_array)
                .expect("features")
                .len(),
            1
        );
    }

    #[test]
    fn registry_or_git_self_edges_reject() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("core/Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = { path = "../helper" }
"#,
        );
        write(&tmp.path().join("core/src/lib.rs"), "");
        write(
            &tmp.path().join("helper/Cargo.toml"),
            r#"
[package]
name = "helper"
version = "0.1.0"
edition = "2021"

[dependencies]
core = "1"
"#,
        );
        write(&tmp.path().join("helper/src/lib.rs"), "");

        let err =
            synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path()).unwrap_err();
        let msg = config_invalid_message(err);
        assert!(msg.contains("registry") || msg.contains("git"));
        assert!(msg.contains("core"));
    }

    #[test]
    fn workspace_root_patches_are_carried_and_self_patches_reject() {
        let tmp = TempDir::new().expect("tempdir");
        let ws_manifest = tmp.path().join("Cargo.toml");
        write(
            &ws_manifest,
            r#"
[workspace]
members = ["core"]
resolver = "2"

[patch.crates-io]
helper = { path = "patched/helper" }
"#,
        );
        write(
            &tmp.path().join("patched/helper/Cargo.toml"),
            "[package]\nname = \"helper\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
        );
        write(&tmp.path().join("patched/helper/src/lib.rs"), "");
        let manifest = tmp.path().join("core/Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = "1"
"#,
        );
        write(&tmp.path().join("core/src/lib.rs"), "");

        let out = synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path())
            .expect("synthesis succeeds");
        let root = read_toml(&out.root_manifest);
        let patch = root
            .get("patch")
            .and_then(|v| v.get("crates-io"))
            .and_then(|v| v.get("helper"))
            .and_then(toml::Value::as_table)
            .expect("carried patch");
        assert_eq!(
            patch.get("path").and_then(toml::Value::as_str),
            Some(
                tmp.path()
                    .join("patched/helper")
                    .to_str()
                    .expect("utf8 temp path")
            )
        );

        write(
            &ws_manifest,
            r#"
[workspace]
members = ["core"]

[patch.crates-io]
core = { path = "core" }
"#,
        );
        let err =
            synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path()).unwrap_err();
        assert!(config_invalid_message(err).contains("patch"));
    }

    #[test]
    fn member_local_patch_and_replace_reject() {
        let tmp = TempDir::new().expect("tempdir");
        let manifest = tmp.path().join("core/Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = { path = "../helper" }
"#,
        );
        write(&tmp.path().join("core/src/lib.rs"), "");
        write(
            &tmp.path().join("helper/Cargo.toml"),
            r#"
[package]
name = "helper"
version = "0.1.0"
edition = "2021"

[patch.crates-io]
serde = { path = "../serde" }
"#,
        );
        write(&tmp.path().join("helper/src/lib.rs"), "");

        let err =
            synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path()).unwrap_err();
        assert!(config_invalid_message(err).contains("member-local [patch"));

        write(
            &tmp.path().join("helper/Cargo.toml"),
            r#"
[package]
name = "helper"
version = "0.1.0"
edition = "2021"

[replace]
"foo:1.0.0" = { path = "../foo" }
"#,
        );
        let err =
            synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path()).unwrap_err();
        assert!(config_invalid_message(err).contains("member-local [replace"));
    }

    #[test]
    fn selected_workspace_dependency_path_entry_is_staged_and_rewrite_aware() {
        let tmp = TempDir::new().expect("tempdir");
        write(
            &tmp.path().join("Cargo.toml"),
            r#"
[workspace]
members = ["core"]

[workspace.dependencies]
helper = { path = "helper" }
core = "1"
"#,
        );
        let manifest = tmp.path().join("core/Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = { workspace = true }
"#,
        );
        write(&tmp.path().join("core/src/lib.rs"), "");
        write(
            &tmp.path().join("helper/Cargo.toml"),
            r#"
[package]
name = "helper"
version = "0.1.0"
edition = "2021"

[dependencies]
core = { path = "../core" }
"#,
        );
        write(&tmp.path().join("helper/src/lib.rs"), "");

        let out = synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path())
            .expect("synthesis succeeds");
        let root = read_toml(&out.root_manifest);
        let ws_helper = root
            .get("workspace")
            .and_then(|v| v.get("dependencies"))
            .and_then(|v| v.get("helper"))
            .and_then(toml::Value::as_table)
            .expect("workspace dependency helper");
        assert_eq!(
            ws_helper.get("path").and_then(toml::Value::as_str),
            Some("staged-path-deps/helper")
        );
        assert_eq!(
            root.get("workspace")
                .and_then(|v| v.get("dependencies"))
                .and_then(|v| v.get("core"))
                .and_then(toml::Value::as_str),
            Some("1"),
            "unused workspace dependency self-edges remain inert"
        );

        let staged_helper = out
            .staged_path_dep_manifests
            .iter()
            .find(|p| {
                table(&read_toml(p), "package")
                    .get("name")
                    .and_then(toml::Value::as_str)
                    == Some("helper")
            })
            .expect("helper staged");
        let helper = read_toml(staged_helper);
        assert_eq!(
            dep_table(&helper, "dependencies", "core")
                .get("path")
                .and_then(toml::Value::as_str),
            Some("../../dylib")
        );
    }

    #[test]
    fn workspace_package_and_replace_paths_are_absolutized() {
        let tmp = TempDir::new().expect("tempdir");
        write(&tmp.path().join("README.md"), "");
        write(&tmp.path().join("LICENSE"), "");
        write(
            &tmp.path().join("Cargo.toml"),
            r#"
[workspace]
members = ["core"]
resolver = "2"

[workspace.package]
readme = "README.md"
license-file = "LICENSE"

[replace]
"vendor:1.0.0" = { path = "vendor" }
"#,
        );
        let manifest = tmp.path().join("core/Cargo.toml");
        write(
            &manifest,
            r#"
[package]
name = "core"
version = "0.1.0"
edition = "2021"

[dev-dependencies]
helper = "1"
"#,
        );
        write(&tmp.path().join("core/src/lib.rs"), "");
        write(
            &tmp.path().join("vendor/Cargo.toml"),
            "[package]\nname = \"vendor\"\nversion = \"1.0.0\"\nedition = \"2021\"\n",
        );

        let out = synthesize_workspace("core", &suite(&["helper"]), &manifest, tmp.path())
            .expect("synthesis succeeds");
        let root = read_toml(&out.root_manifest);
        let package = root
            .get("workspace")
            .and_then(|v| v.get("package"))
            .and_then(toml::Value::as_table)
            .expect("workspace package");
        assert_eq!(
            package.get("readme").and_then(toml::Value::as_str),
            Some(tmp.path().join("README.md").to_str().expect("utf8 path"))
        );
        assert_eq!(
            package.get("license-file").and_then(toml::Value::as_str),
            Some(tmp.path().join("LICENSE").to_str().expect("utf8 path"))
        );
        let replace = root
            .get("replace")
            .and_then(|v| v.get("vendor:1.0.0"))
            .and_then(toml::Value::as_table)
            .expect("replace entry");
        assert_eq!(
            replace.get("path").and_then(toml::Value::as_str),
            Some(tmp.path().join("vendor").to_str().expect("utf8 path"))
        );
    }

    #[test]
    fn offline_failure_note_mentions_staged_lockfile_and_remedies() {
        let tmp = TempDir::new().expect("tempdir");
        let root_manifest = tmp.path().join("lihaaf-suite-workspace/Cargo.toml");
        let source_lock = tmp.path().join("Cargo.lock");
        let plan = super::Synthesis {
            root_manifest,
            collector_manifest: tmp.path().join("collector/Cargo.toml"),
            staged_dylib_manifest: tmp.path().join("dylib/Cargo.toml"),
            staged_path_dep_manifests: Vec::new(),
            copied_lockfile: Some(source_lock.clone()),
        };
        let mut stderr =
            "error: no matching package named `serde` found in offline mode".to_string();

        super::append_offline_lockfile_note(&mut stderr, &plan);

        assert!(stderr.contains("copied the ancestor Cargo.lock"));
        assert!(stderr.contains(source_lock.to_str().expect("utf8 path")));
        assert!(stderr.contains("Warm Cargo"));
        assert!(stderr.contains("build_targets"));
    }
}