rsconstruct 0.9.164

Rust based fast build system
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
//! The sandbox execution model (`[build] execution_model = "sandbox"`).
//!
//! A correctness check, not a security boundary: see
//! docs/src/internal/sandbox.md. Each external tool a product runs is
//! wrapped in bubblewrap with the whole filesystem read-only except `$HOME`
//! and `/tmp` (writable, as natively), the network unshared unless the
//! processor sets `sandbox_network`, and an overlayfs on the project root.
//! Every write the tool makes inside the project lands in the overlay's
//! upper directory instead of the real tree. When the tool exits:
//!
//! - on success, the upper directory is inspected: changes to the product's
//!   declared outputs and output directories are applied to the real tree,
//!   and any other change fails the product, naming each path;
//! - on failure, everything the tool wrote is discarded, so a failed product
//!   leaves no partial outputs behind.
//!
//! The executor records which product is running in a thread-local
//! ([`set_spec`]); `processor::run_command_inner` asks [`prepare`] to wrap
//! the command and calls [`Run::finish`] afterwards. Commands run outside a
//! product (discovery planners, version probes, `tool install`) see no spec
//! and run natively.

use anyhow::{Context, Result};
use std::cell::RefCell;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};

use crate::config::{ExecutionModel, StandardConfig};

/// The executable the sandbox runs tools under.
pub const SANDBOX_TOOL: &str = "bwrap";

/// Where each sandboxed run keeps its overlay directories, relative to the
/// project root. Inside the project so that moving an output from the
/// upper directory into the tree is a rename, not a copy.
const SANDBOX_DIR: &str = ".rsconstruct/sandbox";

/// The xattr overlayfs sets on an upper directory that hides the lower
/// directory's contents: the tool removed the directory and created a new
/// one in its place. bwrap mounts the overlay with `userxattr`, so the
/// attribute lives in the `user.` namespace.
const OPAQUE_XATTR: &str = "user.overlay.opaque";

/// What the running product may change in the project tree.
#[derive(Clone, Debug)]
pub struct Spec {
    /// The processor instance, for messages.
    pub label: String,
    /// Declared output files, relative to the project root.
    pub outputs: Vec<PathBuf>,
    /// Declared output directories: everything beneath them is the product's.
    pub output_dirs: Vec<PathBuf>,
    /// Whether the tool may reach the network.
    pub network: bool,
    /// Where the tools may write their own state: the `sandbox_writable`
    /// entries of every tool the processor runs plus its own config, as
    /// written (parsed by [`Writable::parse`] in [`prepare`]).
    pub writable: Vec<String>,
    /// The outputs the previous build left, which must go before the tool
    /// writes (a restored output is a read-only hardlink). A native run
    /// removes them from the tree beforehand; a sandboxed run removes them
    /// inside its overlay, as the first step of its first command, so that
    /// a run that fails changes nothing. Cleared once a run is applied:
    /// later commands of the same product must not remove what it wrote.
    pub stale: Vec<PathBuf>,
}

/// A place a sandboxed tool may write its own state.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Writable {
    /// A path relative to the project root: it and everything beneath it.
    /// When it already exists it is bind-mounted over the overlay, so the
    /// tool writes the real tree directly (no copy-up of a large `target`);
    /// when it does not, what the tool creates there is moved into the tree
    /// after a successful run like an output.
    Path(PathBuf),
    /// `**/NAME`: every directory called NAME, anywhere in the tree
    /// (`**/__pycache__`). What the tool writes beneath one is moved into
    /// the tree after a successful run.
    Named(String),
}

impl Writable {
    /// Parse one `sandbox_writable` entry: a relative path without `..` or
    /// wildcards, or `**/NAME` with NAME a single wildcard-free component.
    pub fn parse(entry: &str) -> std::result::Result<Self, String> {
        let bad = |why: &str| Err(format!("sandbox_writable entry '{entry}': {why}"));
        if let Some(name) = entry.strip_prefix("**/") {
            if name.is_empty() || name.contains('/') || name.contains('*') {
                return bad("after '**/' must come one directory name, without '/' or '*'");
            }
            return Ok(Self::Named(name.to_string()));
        }
        if entry.is_empty() {
            return bad("empty");
        }
        if entry.contains('*') {
            return bad("wildcards are only allowed as a leading '**/'");
        }
        let path = Path::new(entry);
        if path.is_absolute() {
            return bad("must be relative to the project root");
        }
        if path
            .components()
            .any(|c| matches!(c, std::path::Component::ParentDir))
        {
            return bad("must not contain '..'");
        }
        let clean: PathBuf = path
            .components()
            .filter(|c| !matches!(c, std::path::Component::CurDir))
            .collect();
        if clean.as_os_str().is_empty() {
            return bad("names the project root itself");
        }
        Ok(Self::Path(clean))
    }

    /// Whether `path` (relative to the project root) lies in this place.
    fn covers(&self, path: &Path) -> bool {
        match self {
            Self::Path(p) => path.starts_with(p),
            Self::Named(name) => path.components().any(|c| c.as_os_str() == name.as_str()),
        }
    }
}

thread_local! {
    static SPEC: RefCell<Option<Spec>> = const { RefCell::new(None) };
}

/// Record the product (or batch) about to run on this thread, or clear it
/// with `None`. Set and cleared by the executor around `execute` /
/// `execute_batch`, next to the declared-tools record.
pub fn set_spec(spec: Option<Spec>) {
    SPEC.with(|s| *s.borrow_mut() = spec);
}

/// Add `dir` to the output directories of the product running on this
/// thread, if it runs sandboxed. For the private directories rsconstruct
/// itself gives a tool to write into during a product — a mass
/// generator's staging and doctrees directories under `.rsconstruct/` —
/// whose contents rsconstruct checks against its own plan afterwards. A
/// no-op natively and outside a product.
pub fn declare_output_dir(dir: &Path) {
    SPEC.with(|s| {
        if let Some(spec) = s.borrow_mut().as_mut() {
            spec.output_dirs.push(dir.to_path_buf());
        }
    });
}

/// Declare `paths` as outputs of the product running on this thread and
/// have the sandbox remove the previous copies inside its overlay before
/// the tool starts, if it runs sandboxed. For a tool that writes the files
/// of many products in one run (a mass generator's planned outputs): the
/// run is one product's, but it rewrites them all. Returns false natively
/// or outside a product, where the caller removes the old copies itself.
pub fn declare_replaced_outputs(paths: &[&Path]) -> bool {
    SPEC.with(|s| {
        let mut spec = s.borrow_mut();
        let Some(spec) = spec.as_mut() else {
            return false;
        };
        for path in paths {
            spec.outputs.push(path.to_path_buf());
            spec.stale.push(path.to_path_buf());
        }
        true
    })
}

/// Set in the environment of every sandboxed run, so everything it starts,
/// however deep, knows it is inside bwrap.
const INSIDE_ENV: &str = "RSCONSTRUCT_IN_SANDBOX";

/// Whether the product running on this thread runs inside bwrap: it runs
/// sandboxed, or this whole process does (an rsconstruct started by a
/// sandboxed tool, such as a test run by a sandboxed `cargo nextest`, runs
/// its own products natively but still inside the outer bwrap). A processor
/// whose tool runs Chromium asks this to turn Chromium's own sandbox off,
/// which cannot start inside bwrap; natively it stays on. See
/// docs/src/internal/sandbox-chromium.md.
pub fn active() -> bool {
    SPEC.with(|s| s.borrow().is_some()) || std::env::var_os(INSIDE_ENV).is_some()
}

fn current_spec() -> Option<Spec> {
    SPEC.with(|s| s.borrow().clone())
}

/// Let the product running on this thread, when it runs sandboxed, write
/// anything beneath `dirs`. A mass generator calls this with its
/// `output_dirs`: it checks what its tool wrote there against its plan
/// itself (an error, or a warning with `loose_manifest`), with a message
/// that says which files are off the plan, so the sandbox leaves those
/// directories to that check. pdflatex calls it with the scratch directory
/// its passes share and it removes afterwards. Natively it does nothing.
pub fn allow_output_dirs(dirs: &[PathBuf]) {
    SPEC.with(|s| {
        if let Some(spec) = s.borrow_mut().as_mut() {
            for dir in dirs {
                if !spec.output_dirs.contains(dir) {
                    spec.output_dirs.push(dir.clone());
                }
            }
        }
    });
}

/// Forget the stale outputs of the product running on this thread: a run
/// that removed them was applied, so they are gone from the tree.
fn clear_stale() {
    SPEC.with(|s| {
        if let Some(spec) = s.borrow_mut().as_mut() {
            spec.stale.clear();
        }
    });
}

/// The shell script a sandboxed command runs before the tool, with its
/// arguments: remove each stale output, create each output directory (a
/// tool may assume they exist), then `exec` the tool. The paths are
/// absolute, so none of them is the `--` that ends each list.
const PRELUDE: &str = "while [ \"$1\" != -- ]; do rm -f -- \"$1\" || exit; shift; done; shift; \
                       while [ \"$1\" != -- ]; do mkdir -p -- \"$1\" || exit; shift; done; shift; \
                       exec \"$@\"";

/// Whether the processor named `pname` can run in a sandbox, by its plugin's
/// `sandboxable` flag.
fn plugin_is_sandboxable(pname: &str) -> bool {
    crate::registries::find_plugin(pname).is_none_or(|p| p.sandboxable)
}

/// The enabled instances among `instances` that would run sandboxed under
/// `build_default` although `sandboxable` says their processor cannot,
/// sorted. Read from the raw instance tables, before any processor is
/// created, so a build can refuse them before doing any work.
fn unsandboxable_instances(
    instances: &[crate::config::ProcessorInstance],
    build_default: ExecutionModel,
    sandboxable: &dyn Fn(&str) -> bool,
) -> Vec<String> {
    let mut out: Vec<String> = instances
        .iter()
        .filter(|inst| {
            let table = &inst.config_toml;
            let enabled = table
                .get("enabled")
                .and_then(toml::Value::as_bool)
                .unwrap_or(true);
            let model = table
                .get(crate::config::EXECUTION_MODEL)
                .and_then(|v| v.clone().try_into::<ExecutionModel>().ok())
                .unwrap_or(build_default);
            enabled && model == ExecutionModel::Sandbox && !sandboxable(&inst.pname)
        })
        .map(|inst| inst.instance_name.clone())
        .collect();
    out.sort();
    out
}

/// Fail when any configured, enabled instance would run sandboxed although
/// its processor cannot run in a sandbox. Called at the start of a build. No
/// processor is marked unsandboxable today (see
/// docs/src/internal/sandbox-chromium.md); the check stays for one that is.
pub fn check_sandboxable(
    instances: &[crate::config::ProcessorInstance],
    build_default: ExecutionModel,
) -> Result<()> {
    check_sandboxable_by(instances, build_default, &plugin_is_sandboxable)
}

/// [`check_sandboxable`] with what counts as sandboxable given, so the
/// refusal can be tested while every real processor is sandboxable.
fn check_sandboxable_by(
    instances: &[crate::config::ProcessorInstance],
    build_default: ExecutionModel,
    sandboxable: &dyn Fn(&str) -> bool,
) -> Result<()> {
    let bad = unsandboxable_instances(instances, build_default, sandboxable);
    if bad.is_empty() {
        return Ok(());
    }
    let msg = format!(
        "These processors would run under execution_model = \"sandbox\" but cannot run in a sandbox:\n  {}\nSet execution_model = \"native\" on each of them, or disable them.",
        bad.join("\n  ")
    );
    Err(crate::exit_code::RsconstructError::new(
        crate::exit_code::RsconstructExitCode::ConfigError,
        msg,
    )
    .into())
}

/// Whether a processor with this config runs sandboxed under the build-wide
/// `build_default`: when its execution model resolves to `sandbox`, and
/// this rsconstruct is not itself running inside a sandboxed run.
///
/// An rsconstruct started inside bwrap (a test run by a sandboxed
/// `cargo nextest`, a script that calls `rsconstruct build`) cannot sandbox
/// its products: bwrap cannot start inside bwrap where unprivileged user
/// namespaces are restricted, as on Ubuntu ("No permissions to create a new
/// namespace"). It runs them natively, which loses nothing that matters:
/// the outer run's overlay already holds every write the nested build
/// makes, and the outer product's declared outputs are what is checked.
/// This is not a fallback for a missing bwrap; a top-level build without
/// bwrap still fails in the preflight.
pub fn is_sandboxed(standard: &StandardConfig, build_default: ExecutionModel) -> bool {
    std::env::var_os(INSIDE_ENV).is_none() && resolves_to_sandbox(standard, build_default)
}

/// Whether a processor's execution model, its own or else `build_default`,
/// is `sandbox`.
fn resolves_to_sandbox(standard: &StandardConfig, build_default: ExecutionModel) -> bool {
    standard.execution_model.unwrap_or(build_default) == ExecutionModel::Sandbox
}

/// The enabled processors (by `is_enabled`) that run under the sandbox, so
/// need [`SANDBOX_TOOL`], sorted. Builtin processors count too: a sandboxed
/// builtin runs in a child rsconstruct inside bwrap (`execute_builtin`).
/// Shared by the build's preflight and `tool install machine`, so the tool
/// the build demands is the tool the install provides.
pub fn sandboxed_processors(
    processors: &crate::processor::ProcessorMap,
    is_enabled: &dyn Fn(&str) -> bool,
    build_default: ExecutionModel,
) -> Vec<String> {
    let mut names: Vec<String> = processors
        .iter()
        .filter(|(name, p)| is_enabled(name) && is_sandboxed(p.scan_config(), build_default))
        .map(|(name, _)| name.clone())
        .collect();
    names.sort();
    names
}

/// What every sandboxed run may write, whatever its tools: state no build
/// output ever is, which any tool can write without rsconstruct knowing it
/// runs the program that does.
///
/// - `**/__pycache__`: Python's bytecode cache. Any script may run Python
///   (a generic stanza's `scripts/x.py`, manim, a checker importing from
///   `.venv`), and a tool rsconstruct does not know as Python declares
///   nothing. Found by the 2026-11-09 fleet run: five repos failed on it.
/// - `.rsconstruct/tools`: the project's tool installs (`tool_store`). A
///   tool may keep a cache in its own install prefix, as lilypond does with
///   its fontconfig cache; it is rsconstruct's tool state, not the
///   project's.
const ALWAYS_WRITABLE: &[&str] = &["**/__pycache__", crate::tool_store::REPO_TOOLS_DIR];

/// The `sandbox_writable` entries a processor running `tools` gets: what
/// each tool declares (`ToolInfo::sandbox_writable`), then the processor's
/// own config, then [`ALWAYS_WRITABLE`], without duplicates.
pub fn writable_for(standard: &StandardConfig, tools: &[String]) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    let declared = tools
        .iter()
        .filter_map(|t| crate::tools::tool_info(t))
        .flat_map(|info| info.sandbox_writable.iter().map(|s| (*s).to_string()));
    let always = ALWAYS_WRITABLE.iter().map(|s| (*s).to_string());
    for entry in declared
        .chain(standard.sandbox_writable.iter().cloned())
        .chain(always)
    {
        if !out.contains(&entry) {
            out.push(entry);
        }
    }
    out
}

/// Whether a processor running `tools` gets the network: when any of them
/// declares that it needs it (`ToolInfo::needs_network`), or when its
/// config says so (`sandbox_network`, for a script whose tool is unknown).
pub fn network_for(standard: &StandardConfig, tools: &[String]) -> bool {
    standard.sandbox_network
        || tools
            .iter()
            .filter_map(|t| crate::tools::tool_info(t))
            .any(|info| info.needs_network)
}

/// The spec for running `products` (one, or a batch) of a processor whose
/// config is `standard` and which runs `tools`, or `None` when the
/// processor runs natively.
pub fn spec_for(
    standard: &StandardConfig,
    build_default: ExecutionModel,
    products: &[&crate::graph::Product],
    tools: &[String],
) -> Option<Spec> {
    if !is_sandboxed(standard, build_default) {
        return None;
    }
    let label = products
        .first()
        .map(|p| p.processor.clone())
        .unwrap_or_default();
    let outputs = products
        .iter()
        .flat_map(|p| p.outputs.iter().cloned())
        .collect();
    let output_dirs = products
        .iter()
        .flat_map(|p| p.output_dirs.iter().map(|d| d.as_ref().clone()))
        .collect();
    Some(Spec {
        label,
        outputs,
        output_dirs,
        network: network_for(standard, tools),
        writable: writable_for(standard, tools),
        stale: Vec::new(),
    })
}

// ─── Builtin processors ──────────────────────────────────────────────────────
//
// A builtin processor does its work in rsconstruct's own code, not in a
// tool, so there is no command to wrap. When one runs sandboxed, the build
// runs rsconstruct itself under bwrap instead: the hidden `sandbox-run`
// subcommand recreates the processor from the same config, executes the
// products it is handed, and reports each one's result. Being an ordinary
// command, it goes through the same sandbox as any tool: its writes land in
// the overlay and are checked against the products' declared outputs.

/// One builtin processor instance, as `sandbox-run` recreates it.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct BuiltinInstance {
    pub instance: String,
    pub pname: String,
    /// The instance's config table, overrides (`--iset`, `--pset`, CLI
    /// flags) already applied.
    pub config: toml::Value,
}

/// What `sandbox-run` needs besides its products: the same for every
/// product of a build. Built from the build's config by the builder and
/// handed to the executor.
#[derive(Clone, Debug, Default, serde::Serialize, serde::Deserialize)]
pub struct BuiltinSetup {
    /// Every builtin processor instance of the build.
    pub instances: Vec<BuiltinInstance>,
    /// How the build walks the tree (`Config::file_index_walk_dirs`), so the
    /// child's discovery sees the same files.
    pub force_dirs: Vec<String>,
    pub exclude_roots: Vec<String>,
    pub warn_symlinks: bool,
    /// `[build] python`.
    pub python: String,
    /// The settings the build puts on its `BuildContext`.
    pub max_arg_len: usize,
    pub command_timeout_secs: u64,
    /// `[cache] webcache_ttl_secs`.
    pub webcache_ttl_secs: u64,
    /// `[build] worker_stack_mib`: the child runs its products on a thread
    /// with this stack, as the build's workers do natively, so a deeply
    /// nested file that a worker checks does not overflow the child.
    pub worker_stack_mib: usize,
}

impl BuiltinSetup {
    pub fn from_config(config: &crate::config::Config) -> Self {
        let (exclude_roots, force_dirs) = config.file_index_walk_dirs();
        Self {
            instances: config
                .processor
                .instances
                .iter()
                .filter(|inst| crate::registries::processor::is_builtin(&inst.pname))
                .map(|inst| BuiltinInstance {
                    instance: inst.instance_name.clone(),
                    pname: inst.pname.clone(),
                    config: inst.config_toml.clone(),
                })
                .collect(),
            force_dirs,
            exclude_roots,
            warn_symlinks: config.build.warn_symlinks,
            python: config.build.python.clone(),
            max_arg_len: config.build.max_arg_len,
            command_timeout_secs: config.build.command_timeout_secs,
            webcache_ttl_secs: config.cache.webcache_ttl_secs,
            worker_stack_mib: config.build.worker_stack_mib,
        }
    }
}

/// What `sandbox-run` reports after [`RESULTS_MARKER`].
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct JobResults {
    /// One per product, in order: `None` for success, else its error.
    results: Vec<Option<String>>,
    /// What the child downloaded into no cache (see `crate::webcache`),
    /// for the build to store.
    fetched: Vec<crate::webcache::Download>,
}

/// One product as `sandbox-run` receives it.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct JobProduct {
    id: usize,
    inputs: Vec<PathBuf>,
    outputs: Vec<PathBuf>,
    output_dirs: Vec<PathBuf>,
    variant: Option<String>,
}

/// What the build sends `sandbox-run` on its standard input.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct BuiltinJob {
    setup: BuiltinSetup,
    instance: BuiltinInstance,
    products: Vec<JobProduct>,
    /// Run the products through `execute_batch` (one invocation, as the
    /// executor would natively), not `execute`.
    batch: bool,
}

/// The line `sandbox-run` prints before its results, so they can be told
/// from whatever the processor printed to standard output before them.
const RESULTS_MARKER: &str = "\u{1e}rsconstruct-sandbox-results\u{1e}";

/// Execute `products` of the builtin processor instance `instance` in a
/// sandbox, as `execute` (one product, `batch` false) or `execute_batch`
/// would natively; one result per product, in order. The executor has set
/// the sandbox spec for these products, so the command below runs wrapped.
pub fn execute_builtin(
    ctx: &crate::build_context::BuildContext,
    setup: &BuiltinSetup,
    instance: &str,
    products: &[&crate::graph::Product],
    batch: bool,
) -> Vec<Result<()>> {
    let all = |e: &anyhow::Error| {
        products
            .iter()
            .map(|_| Err(anyhow::anyhow!("{e:#}")))
            .collect()
    };
    match run_builtin_job(ctx, setup, instance, products, batch) {
        Ok(results) => results
            .into_iter()
            .map(|r| r.map_or(Ok(()), |e| Err(anyhow::anyhow!(e))))
            .collect(),
        Err(e) => all(&e),
    }
}

/// The parent's side of `execute_builtin`: send the job, relay what the
/// child printed, read its results (`None` for a product that succeeded,
/// its error otherwise).
fn run_builtin_job(
    ctx: &crate::build_context::BuildContext,
    setup: &BuiltinSetup,
    instance: &str,
    products: &[&crate::graph::Product],
    batch: bool,
) -> Result<Vec<Option<String>>> {
    let instance = setup
        .instances
        .iter()
        .find(|i| i.instance == instance)
        .cloned()
        .with_context(|| format!("internal error: no config for builtin processor '{instance}'"))?;
    let label = instance.instance.clone();
    let job = BuiltinJob {
        setup: BuiltinSetup {
            instances: Vec::new(),
            ..setup.clone()
        },
        instance,
        products: products
            .iter()
            .map(|p| JobProduct {
                id: p.id,
                inputs: p.inputs.clone(),
                outputs: p.outputs.clone(),
                output_dirs: p.output_dirs.iter().map(|d| d.as_ref().clone()).collect(),
                variant: p.variant.clone(),
            })
            .collect(),
        batch,
    };
    let payload = serde_json::to_vec(&job).context("Failed to encode the sandboxed job")?;

    let exe = std::env::current_exe().context("Failed to locate the rsconstruct executable")?;
    let mut cmd = Command::new(&exe);
    cmd.arg("--color")
        .arg(if crate::runtime_flags::color_enabled() {
            "always"
        } else {
            "never"
        });
    for (on, flag) in [
        (crate::runtime_flags::json_mode(), "--json"),
        (crate::runtime_flags::quiet(), "--quiet"),
        (crate::runtime_flags::show_output(), "--show-output"),
        (
            crate::runtime_flags::show_child_processes(),
            "--show-child-processes",
        ),
    ] {
        if on {
            cmd.arg(flag);
        }
    }
    cmd.arg("sandbox-run");
    let output = crate::processor::run_command_with_stdin(ctx, &cmd, &payload)?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let (printed, results) = match stdout.rfind(RESULTS_MARKER) {
        Some(at) => (&stdout[..at], Some(&stdout[at + RESULTS_MARKER.len()..])),
        None => (&*stdout, None),
    };
    relay(printed, &String::from_utf8_lossy(&output.stderr));
    if !output.status.success() {
        anyhow::bail!(
            "[{label}] the sandboxed run of rsconstruct failed ({})",
            output.status
        );
    }
    let results = results.with_context(|| {
        format!("[{label}] the sandboxed run of rsconstruct reported no results")
    })?;
    let JobResults { results, fetched } = serde_json::from_str(results.trim())
        .with_context(|| format!("[{label}] Failed to read the sandboxed run's results"))?;
    crate::webcache::defer_store(fetched);
    anyhow::ensure!(
        results.len() == products.len(),
        "[{label}] the sandboxed run reported {} results for {} products",
        results.len(),
        products.len()
    );
    Ok(results)
}

/// Pass on what the child printed, as the processor would have printed it
/// running in the build's own process.
fn relay(stdout: &str, stderr: &str) {
    use std::io::Write;
    if !stdout.is_empty() {
        let mut out = std::io::stdout().lock();
        let _ = out.write_all(stdout.as_bytes());
        let _ = out.flush();
    }
    if !stderr.is_empty() {
        let _ = std::io::stderr().lock().write_all(stderr.as_bytes());
    }
}

/// `rsconstruct sandbox-run`: the child's side of [`execute_builtin`]. Reads
/// the job from standard input, recreates the processor and runs its
/// discovery (a processor may keep state from it for execute: tera keeps
/// the templates `{% include %}` can name), executes the products, and
/// prints [`RESULTS_MARKER`] and their results. An error here (bad job,
/// config the processor rejects) fails every product of the job.
pub fn sandbox_run(ctx: &crate::build_context::BuildContext) -> Result<()> {
    let input = std::io::read_to_string(std::io::stdin())
        .context("Failed to read the sandboxed job from standard input")?;
    let job: BuiltinJob =
        serde_json::from_str(&input).context("Failed to decode the sandboxed job")?;
    let setup = &job.setup;

    // Inside the sandbox the build's own caches are out of reach (the
    // parent holds them), and nothing here is sandboxed again: commands the
    // processor runs execute directly, inside this sandbox. The webcache
    // (`.rsconstruct/webcache.redb`, iyamlschema's schemas) is only read:
    // storing into it would be a write the products do not declare, and
    // children running side by side cannot share a writer's lock. What this
    // child downloads goes back with its results for the build to store.
    ctx.set_mtime_check(false);
    ctx.set_max_arg_len(setup.max_arg_len);
    ctx.set_command_timeout_secs(setup.command_timeout_secs);
    ctx.set_webcache_ttl_secs(setup.webcache_ttl_secs);
    crate::webcache::set_child_mode();
    crate::python_env::set_interpreter(&setup.python);

    let processor =
        crate::builder::create_processor_for_instance(&job.instance.pname, &job.instance.config)?
            .with_context(|| format!("Unknown processor type: '{}'", job.instance.pname))?;
    let force_refs: Vec<&str> = setup.force_dirs.iter().map(String::as_str).collect();
    let file_index = crate::file_index::FileIndex::build_with_force_dirs(
        &force_refs,
        &setup.exclude_roots,
        setup.warn_symlinks,
    )?;
    processor.discover(
        &mut crate::graph::BuildGraph::new(),
        &file_index,
        &job.instance.instance,
    )?;

    let products: Vec<crate::graph::Product> = job
        .products
        .into_iter()
        .map(|p| {
            let mut product =
                crate::graph::Product::new(p.inputs, p.outputs, &job.instance.instance, p.id, None);
            product.output_dirs = p.output_dirs.into_iter().map(std::sync::Arc::new).collect();
            product.variant = p.variant;
            product
        })
        .collect();
    let refs: Vec<&crate::graph::Product> = products.iter().collect();

    let results: Vec<Result<()>> = std::thread::scope(|scope| {
        std::thread::Builder::new()
            .name("sandbox-run".to_string())
            .stack_size(setup.worker_stack_mib * 1024 * 1024)
            .spawn_scoped(scope, || {
                crate::processor::set_declared_tools(Some(processor.required_tools()));
                let results = if job.batch {
                    processor.execute_batch(ctx, &refs)
                } else {
                    refs.iter().map(|p| processor.execute(ctx, p)).collect()
                };
                crate::processor::set_declared_tools(None);
                results
            })
            .context("Failed to start the sandboxed run's worker thread")?
            .join()
            .map_err(|_| anyhow::anyhow!("the sandboxed run's worker thread panicked"))
    })?;

    let results = JobResults {
        results: results
            .into_iter()
            .map(|r| r.err().map(|e| format!("{e:#}")))
            .collect(),
        fetched: crate::webcache::take_fetched(),
    };
    let encoded = serde_json::to_string(&results).context("Failed to encode the results")?;
    use std::io::Write;
    let mut out = std::io::stdout().lock();
    writeln!(out, "{RESULTS_MARKER}{encoded}")
        .and_then(|()| out.flush())
        .context("Failed to write the sandboxed run's results")?;
    Ok(())
}

/// A command wrapped for one sandboxed run, plus what is needed to settle
/// the run afterwards.
pub struct Run {
    /// The bwrap invocation to execute instead of the original command.
    pub command: Command,
    root: PathBuf,
    dir: PathBuf,
    upper: PathBuf,
    spec: Spec,
    /// `spec.writable`, parsed.
    writable: Vec<Writable>,
}

/// Wrap `cmd` for the product running on this thread, or return `None`
/// when no product is running or its processor runs natively.
pub fn prepare(cmd: &Command) -> Result<Option<Run>> {
    let Some(mut spec) = current_spec() else {
        return Ok(None);
    };
    let root =
        std::env::current_dir().context("Failed to determine the project root for the sandbox")?;
    spec.outputs = spec.outputs.iter().map(|p| relative_to(&root, p)).collect();
    spec.output_dirs = spec
        .output_dirs
        .iter()
        .map(|p| relative_to(&root, p))
        .collect();
    spec.stale = spec.stale.iter().map(|p| relative_to(&root, p)).collect();
    let writable = spec
        .writable
        .iter()
        .map(|entry| Writable::parse(entry).map_err(|e| anyhow::anyhow!("[{}] {e}", spec.label)))
        .collect::<Result<Vec<_>>>()?;

    static NEXT_RUN: AtomicU64 = AtomicU64::new(0);
    let dir = root.join(SANDBOX_DIR).join(format!(
        "{}-{}",
        std::process::id(),
        NEXT_RUN.fetch_add(1, Ordering::Relaxed)
    ));
    let upper = dir.join("upper");
    let work = dir.join("work");
    for d in [&upper, &work] {
        fs::create_dir_all(d)
            .with_context(|| format!("Failed to create sandbox directory {}", d.display()))?;
    }

    let mut command = Command::new(SANDBOX_TOOL);
    command.args(["--die-with-parent", "--ro-bind", "/", "/"]);
    if let Some(home) = std::env::var_os("HOME").filter(|h| !h.is_empty()) {
        command.arg("--bind").arg(&home).arg(&home);
    }
    // The user's runtime directory is per-user scratch space like /tmp:
    // dotnet makes its named mutexes there, and failed with EROFS on a
    // GitHub runner when it was read-only.
    if let Some(runtime) =
        std::env::var_os("XDG_RUNTIME_DIR").filter(|d| !d.is_empty() && Path::new(d).is_dir())
    {
        command.arg("--bind").arg(&runtime).arg(&runtime);
    }
    command.args([
        "--bind",
        "/tmp",
        "/tmp",
        "--dev-bind",
        "/dev",
        "/dev",
        "--proc",
        "/proc",
    ]);
    if !spec.network {
        command.arg("--unshare-net");
    }
    command
        .arg("--overlay-src")
        .arg(&root)
        .arg("--overlay")
        .arg(&upper)
        .arg(&work)
        .arg(&root);
    // A state path that already exists goes on top of the overlay, straight
    // to the real tree. One that does not is left to the overlay: creating
    // it here would leave an empty directory in the tree (an empty `.git`
    // turns a directory into a broken repository), and what the tool
    // creates there is moved in after the run like an output.
    for place in &writable {
        if let Writable::Path(rel) = place {
            let path = root.join(rel);
            if fs::symlink_metadata(&path).is_ok() {
                command.arg("--bind").arg(&path).arg(&path);
            }
        }
    }
    // `join` keeps an absolute current_dir as is and anchors a relative one
    // at the root, which is where a native run would resolve it too.
    let cwd = cmd
        .get_current_dir()
        .map_or_else(|| root.clone(), |d| root.join(d));
    command.arg("--chdir").arg(&cwd);
    command.arg("--").args(inner_command(&root, &spec, cmd));
    command.env(INSIDE_ENV, "1");
    for (key, value) in cmd.get_envs() {
        if let Some(value) = value {
            command.env(key, value);
        }
    }
    Ok(Some(Run {
        command,
        root,
        dir,
        upper,
        spec,
        writable,
    }))
}

/// What bwrap runs inside the sandbox: `cmd`, behind [`PRELUDE`] when there
/// are stale outputs to remove or output directories to create. Both happen
/// inside the overlay, never in the real tree: a failed run changes nothing.
fn inner_command(root: &Path, spec: &Spec, cmd: &Command) -> Vec<std::ffi::OsString> {
    let mut args: Vec<std::ffi::OsString> = Vec::new();
    if !spec.stale.is_empty() || !spec.output_dirs.is_empty() {
        args.extend(["sh", "-c", PRELUDE, "sh"].map(Into::into));
        args.extend(spec.stale.iter().map(|p| root.join(p).into_os_string()));
        args.push("--".into());
        args.extend(
            spec.output_dirs
                .iter()
                .map(|d| root.join(d).into_os_string()),
        );
        args.push("--".into());
    }
    args.push(cmd.get_program().to_os_string());
    args.extend(cmd.get_args().map(std::ffi::OsStr::to_os_string));
    args
}

/// A path relative to the project root, as declared outputs are compared.
fn relative_to(root: &Path, path: &Path) -> PathBuf {
    let path = path.strip_prefix(root).unwrap_or(path);
    path.strip_prefix(".").unwrap_or(path).to_path_buf()
}

/// One change a tool made to the project tree, as its overlay recorded it.
#[derive(Debug, PartialEq, Eq)]
enum Change {
    /// A file or symlink was created or modified.
    Written(PathBuf),
    /// A file or directory of the real tree was removed.
    Removed(PathBuf),
    /// A directory of the real tree was removed and created anew.
    Replaced(PathBuf),
}

impl Change {
    fn path(&self) -> &Path {
        match self {
            Self::Written(p) | Self::Removed(p) | Self::Replaced(p) => p,
        }
    }

    fn describe(&self) -> String {
        match self {
            Self::Written(p) => format!("wrote    {}", p.display()),
            Self::Removed(p) => format!("removed  {}", p.display()),
            Self::Replaced(p) => format!("replaced {}", p.display()),
        }
    }
}

impl Run {
    /// Settle the run: on success apply the declared changes (or fail on
    /// undeclared ones), on failure discard everything; then remove the
    /// run's directory.
    pub fn finish(self, tool_succeeded: bool) -> Result<()> {
        let applied = if tool_succeeded { self.apply() } else { Ok(()) };
        if tool_succeeded && applied.is_ok() {
            clear_stale();
        }
        let cleaned = remove_run_dir(&self.dir);
        applied?;
        cleaned
    }

    /// Whether the product may change `path`. The stale outputs are
    /// included: removing them was rsconstruct's own first step.
    fn declares(&self, path: &Path) -> bool {
        self.spec.stale.iter().any(|s| s == path)
            || self.spec.outputs.iter().any(|o| o == path)
            || self.spec.output_dirs.iter().any(|d| path.starts_with(d))
            || self.writable.iter().any(|w| w.covers(path))
    }

    /// What `change` touched that the product does not declare, described.
    /// A removed or replaced directory touched every file the real tree had
    /// beneath it, so each of those is checked rather than the directory's
    /// own path: a tool that runs `rm -rf docx` and writes `docx/` anew
    /// changes nothing undeclared when every file `docx` held is a declared
    /// output. The new contents of a replaced directory are separate
    /// `Written` changes.
    fn undeclared_in(&self, change: &Change) -> Result<Vec<String>> {
        let path = change.path();
        if self.declares(path) {
            return Ok(Vec::new());
        }
        let (Change::Removed(_) | Change::Replaced(_)) = change else {
            return Ok(vec![change.describe()]);
        };
        let real = self.root.join(path);
        if !fs::symlink_metadata(&real).is_ok_and(|m| m.is_dir()) {
            return Ok(vec![change.describe()]);
        }
        let mut files = Vec::new();
        real_files_under(&self.root, path, &mut files)?;
        Ok(files
            .into_iter()
            .filter(|f| !self.declares(f))
            .map(|f| Change::Removed(f).describe())
            .collect())
    }

    fn apply(&self) -> Result<()> {
        let mut changes = Vec::new();
        collect_changes(&self.upper, Path::new(""), &self.root, &mut changes)?;
        let mut undeclared: Vec<String> = Vec::new();
        for change in &changes {
            undeclared.extend(self.undeclared_in(change)?);
        }
        if !undeclared.is_empty() {
            anyhow::bail!(
                "[{}] the tool changed paths its product does not declare (execution_model = \"sandbox\"):\n  {}\n\
                 Nothing it wrote was kept. Declare these paths as outputs, stop the tool from writing them, \
                 or set execution_model = \"native\" on this processor.",
                self.spec.label,
                undeclared.join("\n  ")
            );
        }
        // Removals first, so a replaced directory is emptied before the new
        // contents are moved in.
        for change in &changes {
            match change {
                Change::Removed(p) => remove_real(&self.root.join(p))?,
                // A writable place (cargo's `target`) is shared state that
                // runs alongside this one may be writing into: a run that
                // started before it existed created its own, which overlayfs
                // marks opaque, and by now another run may have made the
                // real one and be bound to it. Emptying it would pull it out
                // from under that run ("Directory not empty" when cargo
                // writes while it is removed), so this run's files are
                // merged into it instead, as a native run's would be.
                Change::Replaced(p) if !self.writable.iter().any(|w| w.covers(p)) => {
                    remove_real(&self.root.join(p))?;
                }
                Change::Replaced(_) | Change::Written(_) => {}
            }
        }
        for change in &changes {
            if let Change::Written(p) = change {
                let from = self.upper.join(p);
                let to = self.root.join(p);
                if let Some(parent) = to.parent() {
                    fs::create_dir_all(parent).with_context(|| {
                        format!("Failed to create directory {}", parent.display())
                    })?;
                }
                fs::rename(&from, &to).with_context(|| {
                    format!(
                        "Failed to move sandbox output {} to {}",
                        from.display(),
                        to.display()
                    )
                })?;
            }
        }
        Ok(())
    }
}

/// Walk the upper directory below `rel` and record every change it holds.
/// Directories themselves are not changes: overlayfs copies a directory up
/// whenever something inside it changes, and an empty new directory is
/// left alone. A directory marked opaque is a change only when the real
/// tree has something there to replace.
fn collect_changes(upper: &Path, rel: &Path, root: &Path, out: &mut Vec<Change>) -> Result<()> {
    collect_changes_in(upper, rel, root, false, out)
}

/// [`collect_changes`], knowing whether `rel` lies in a directory that
/// replaces a real one (`replacing`). Applying the replacement removes the
/// real directory first, so every file in it must be moved in, even one
/// identical to the real file it replaces.
fn collect_changes_in(
    upper: &Path,
    rel: &Path,
    root: &Path,
    replacing: bool,
    out: &mut Vec<Change>,
) -> Result<()> {
    let dir = upper.join(rel);
    let mut entries: Vec<_> = fs::read_dir(&dir)
        .with_context(|| format!("Failed to read sandbox directory {}", dir.display()))?
        .collect::<std::io::Result<_>>()
        .with_context(|| format!("Failed to read sandbox directory {}", dir.display()))?;
    entries.sort_by_key(fs::DirEntry::file_name);
    for entry in entries {
        let child = rel.join(entry.file_name());
        let path = entry.path();
        let meta = fs::symlink_metadata(&path)
            .with_context(|| format!("Failed to stat {}", path.display()))?;
        if crate::platform::is_overlay_whiteout(&meta) {
            out.push(Change::Removed(child));
        } else if meta.is_dir() {
            let opaque = crate::platform::get_xattr(&path, OPAQUE_XATTR)
                .with_context(|| format!("Failed to read xattrs of {}", path.display()))?
                .is_some_and(|v| v == b"y");
            let replaces = opaque && fs::symlink_metadata(root.join(&child)).is_ok();
            if replaces {
                out.push(Change::Replaced(child.clone()));
            }
            collect_changes_in(upper, &child, root, replacing || replaces, out)?;
        } else if replacing || !same_as_real(&path, &meta, &root.join(&child))? {
            out.push(Change::Written(child));
        }
    }
    Ok(())
}

/// Whether the upper directory's file at `upper` is the real tree's file at
/// `real` unchanged. overlayfs copies a file up as soon as a tool opens it
/// for writing, even when it writes nothing (Python's `shelve` opens its
/// database read-write to read it), and such a file changed nothing.
fn same_as_real(upper: &Path, upper_meta: &fs::Metadata, real: &Path) -> Result<bool> {
    let Ok(real_meta) = fs::symlink_metadata(real) else {
        return Ok(false);
    };
    if !upper_meta.is_file()
        || !real_meta.is_file()
        || upper_meta.len() != real_meta.len()
        || crate::platform::get_mode(upper_meta) != crate::platform::get_mode(&real_meta)
    {
        return Ok(false);
    }
    same_content(upper, real)
}

/// Whether two files of the same length hold the same bytes, read in
/// chunks so a large file is never held whole.
fn same_content(a: &Path, b: &Path) -> Result<bool> {
    use std::io::Read;
    let open =
        |p: &Path| fs::File::open(p).with_context(|| format!("Failed to open {}", p.display()));
    let (mut fa, mut fb) = (open(a)?, open(b)?);
    let mut ba = vec![0u8; 64 * 1024];
    let mut bb = vec![0u8; 64 * 1024];
    loop {
        let n = fa
            .read(&mut ba)
            .with_context(|| format!("Failed to read {}", a.display()))?;
        if n == 0 {
            return Ok(true);
        }
        fb.read_exact(&mut bb[..n])
            .with_context(|| format!("Failed to read {}", b.display()))?;
        if ba[..n] != bb[..n] {
            return Ok(false);
        }
    }
}

/// Every file and symlink of the real tree beneath directory `rel`
/// (relative to `root`), as root-relative paths, sorted. Directories
/// themselves are not listed: removing an empty one changes no file.
fn real_files_under(root: &Path, rel: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    let dir = root.join(rel);
    let mut entries: Vec<_> = fs::read_dir(&dir)
        .with_context(|| format!("Failed to read directory {}", dir.display()))?
        .collect::<std::io::Result<_>>()
        .with_context(|| format!("Failed to read directory {}", dir.display()))?;
    entries.sort_by_key(fs::DirEntry::file_name);
    for entry in entries {
        let child = rel.join(entry.file_name());
        let meta = fs::symlink_metadata(entry.path())
            .with_context(|| format!("Failed to stat {}", entry.path().display()))?;
        if meta.is_dir() {
            real_files_under(root, &child, out)?;
        } else {
            out.push(child);
        }
    }
    Ok(())
}

/// Remove `path` from the real tree, whatever it is; absent is fine.
fn remove_real(path: &Path) -> Result<()> {
    let Ok(meta) = fs::symlink_metadata(path) else {
        return Ok(());
    };
    if meta.is_dir() {
        fs::remove_dir_all(path)
    } else {
        fs::remove_file(path)
    }
    .with_context(|| format!("Failed to remove {}", path.display()))
}

/// Remove a run's directory. overlayfs leaves `work/work` with mode 000,
/// which must be opened up before it can be deleted.
/// Remove what killed builds left under `.rsconstruct/sandbox/` in the
/// project at `root`, and the directory itself; returns how many runs were
/// removed. A run's directory normally goes when the run is settled, but a
/// build killed mid-run (SIGKILL, OOM, power loss) leaves it behind, and
/// overlayfs's `work/work` has mode 000, so a plain `rm -rf` fails on it.
///
/// Called at the start of a build, when nothing can be using them: the
/// build holds the project's cache database open, which redb locks
/// exclusively, so no other rsconstruct process is running in this project,
/// and this one has not started a run yet. Nothing in a leftover run is
/// worth keeping: a run reaches the tree only once it is accepted.
pub fn remove_leftover_runs(root: &Path) -> Result<usize> {
    let sandbox = root.join(SANDBOX_DIR);
    let entries = match fs::read_dir(&sandbox) {
        Ok(entries) => entries,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(0),
        Err(e) => {
            return Err(e).with_context(|| {
                format!("Failed to read sandbox directory {}", sandbox.display())
            });
        }
    };
    let mut removed = 0;
    for entry in entries {
        let entry = entry
            .with_context(|| format!("Failed to read sandbox directory {}", sandbox.display()))?;
        let path = entry.path();
        if fs::symlink_metadata(&path)
            .with_context(|| format!("Failed to stat {}", path.display()))?
            .is_dir()
        {
            remove_run_dir(&path)?;
        } else {
            fs::remove_file(&path)
                .with_context(|| format!("Failed to remove {}", path.display()))?;
        }
        removed += 1;
    }
    fs::remove_dir(&sandbox)
        .with_context(|| format!("Failed to remove sandbox directory {}", sandbox.display()))?;
    Ok(removed)
}

fn remove_run_dir(dir: &Path) -> Result<()> {
    let inner = dir.join("work").join("work");
    if inner.exists() {
        crate::platform::set_permissions_mode(&inner, 0o700)
            .with_context(|| format!("Failed to open up {}", inner.display()))?;
    }
    fs::remove_dir_all(dir)
        .with_context(|| format!("Failed to remove sandbox directory {}", dir.display()))
}

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

    fn instance(
        instance_name: &str,
        pname: &str,
        toml_src: &str,
    ) -> crate::config::ProcessorInstance {
        crate::config::ProcessorInstance {
            instance_name: instance_name.into(),
            pname: pname.into(),
            config_toml: toml::from_str(toml_src).expect("instance toml"),
            provenance: crate::config::ProvenanceMap::new(),
        }
    }

    /// No real processor is unsandboxable today, so the test treats marp as
    /// one to exercise the refusal.
    fn marp_is_unsandboxable(pname: &str) -> bool {
        pname != "processor.generator.marp"
    }

    #[test]
    fn unsandboxable_processors_are_refused_only_when_sandboxed() {
        let marp = instance("processor.generator.marp", "processor.generator.marp", "");
        let ruff = instance("processor.checker.ruff", "processor.checker.ruff", "");
        let native_marp = instance(
            "processor.generator.marp.a",
            "processor.generator.marp",
            "execution_model = \"native\"",
        );
        let disabled_marp = instance(
            "processor.generator.marp.b",
            "processor.generator.marp",
            "enabled = false",
        );
        let sandboxed_marp = instance(
            "processor.generator.marp.c",
            "processor.generator.marp",
            "execution_model = \"sandbox\"",
        );
        let all = [marp, ruff, native_marp, disabled_marp, sandboxed_marp];
        // Build-wide sandbox: marp and its explicitly sandboxed instance are
        // refused; ruff, the native override and the disabled one are not.
        assert_eq!(
            unsandboxable_instances(&all, ExecutionModel::Sandbox, &marp_is_unsandboxable),
            ["processor.generator.marp", "processor.generator.marp.c"]
        );
        // Build-wide native: only the instance that opts into the sandbox.
        assert_eq!(
            unsandboxable_instances(&all, ExecutionModel::Native, &marp_is_unsandboxable),
            ["processor.generator.marp.c"]
        );
        assert!(
            check_sandboxable_by(&all[1..3], ExecutionModel::Sandbox, &marp_is_unsandboxable)
                .is_ok()
        );
        let err = check_sandboxable_by(&all, ExecutionModel::Sandbox, &marp_is_unsandboxable)
            .unwrap_err()
            .to_string();
        assert!(
            err.contains("processor.generator.marp")
                && err.contains("execution_model = \"native\""),
            "{err}"
        );
    }

    /// Every processor can run in a sandbox, the Chromium-based ones
    /// included (they turn Chromium's own sandbox off when sandboxed; see
    /// docs/src/internal/sandbox-chromium.md), and the real build check
    /// refuses none of them.
    #[test]
    fn every_processor_is_sandboxable() {
        let unsandboxable: Vec<String> = crate::registries::processor::all_plugins()
            .filter(|p| !p.sandboxable)
            .map(crate::registries::ProcessorPlugin::pname)
            .collect();
        assert_eq!(unsandboxable, Vec::<String>::new());
        let marp = instance("processor.generator.marp", "processor.generator.marp", "");
        assert!(check_sandboxable(&[marp], ExecutionModel::Sandbox).is_ok());
    }

    fn run_with(outputs: &[&str], dirs: &[&str]) -> Run {
        Run {
            command: Command::new("true"),
            root: PathBuf::from("/p"),
            dir: PathBuf::from("/p/.rsconstruct/sandbox/x"),
            upper: PathBuf::from("/p/.rsconstruct/sandbox/x/upper"),
            spec: Spec {
                label: "processor.checker.x".into(),
                outputs: outputs.iter().map(PathBuf::from).collect(),
                output_dirs: dirs.iter().map(PathBuf::from).collect(),
                network: false,
                writable: Vec::new(),
                stale: Vec::new(),
            },
            writable: Vec::new(),
        }
    }

    #[test]
    fn writable_entries_parse_as_paths_or_named_directories() {
        assert_eq!(
            Writable::parse("target"),
            Ok(Writable::Path(PathBuf::from("target")))
        );
        assert_eq!(
            Writable::parse("./sub/.cache"),
            Ok(Writable::Path(PathBuf::from("sub/.cache")))
        );
        assert_eq!(
            Writable::parse("**/__pycache__"),
            Ok(Writable::Named("__pycache__".into()))
        );
        for bad in [
            "", ".", "/abs", "../up", "a/../b", "*.pyc", "a/**/b", "**/", "**/a/b", "**/*",
        ] {
            assert!(Writable::parse(bad).is_err(), "{bad:?} must be rejected");
        }
    }

    #[test]
    fn writable_places_are_declared() {
        let mut run = run_with(&["out/a"], &[]);
        run.writable = vec![
            Writable::Path(PathBuf::from("target")),
            Writable::Named("__pycache__".into()),
        ];
        assert!(run.declares(Path::new("target/debug/x")));
        assert!(run.declares(Path::new(
            ".venv/lib/python3/site-packages/m/__pycache__/m.pyc"
        )));
        assert!(run.declares(Path::new("__pycache__/a.pyc")));
        assert!(!run.declares(Path::new("targets/x")));
        assert!(!run.declares(Path::new("src/m.py")));
    }

    /// Every tool's declared state places must parse; a bad entry would
    /// otherwise fail every sandboxed run of every processor using it.
    #[test]
    fn every_tool_declares_valid_writable_places() {
        for tool in crate::tools::all_tools() {
            for entry in tool.sandbox_writable {
                assert!(
                    Writable::parse(entry).is_ok(),
                    "tool {}: {:?}",
                    tool.name,
                    Writable::parse(entry)
                );
            }
        }
    }

    /// A processor gets the places of every tool it runs, then its config.
    #[test]
    fn writable_for_unions_tools_and_config() {
        let standard = StandardConfig {
            sandbox_writable: vec!["extra".into(), "target".into()],
            ..Default::default()
        };
        let tools = [
            "cargo".to_string(),
            "python".to_string(),
            "no-such-tool".to_string(),
        ];
        assert_eq!(
            writable_for(&standard, &tools),
            [
                "target",
                "**/target",
                "**/Cargo.lock",
                "**/__pycache__",
                "extra",
                ".rsconstruct/tools"
            ]
        );
        assert_eq!(
            writable_for(&StandardConfig::default(), &[]),
            ["**/__pycache__", ".rsconstruct/tools"]
        );
    }

    /// The network comes from a tool that declares it needs it, or from the
    /// processor's config; otherwise it is cut off.
    #[test]
    fn network_comes_from_the_tools_or_the_config() {
        let standard = StandardConfig::default();
        let tools = |names: &[&str]| names.iter().map(|s| (*s).to_string()).collect::<Vec<_>>();
        assert!(!network_for(&standard, &tools(&["python", "ruff"])));
        assert!(network_for(&standard, &tools(&["python", "cargo-deny"])));
        assert!(network_for(&standard, &tools(&["cargo"])));
        assert!(network_for(&standard, &tools(&["taplo"])));
        let configured = StandardConfig {
            sandbox_network: true,
            ..Default::default()
        };
        assert!(network_for(&configured, &tools(&[])));
    }

    /// The tools that reach the network are exactly the ones that install
    /// packages or fetch remote data on every run.
    #[test]
    fn network_tools_are_the_installers_and_fetchers() {
        let mut names: Vec<&str> = crate::tools::all_tools()
            .filter(|t| t.needs_network)
            .map(|t| t.name)
            .collect();
        names.sort_unstable();
        assert_eq!(
            names,
            ["bundle", "cargo", "cargo-deny", "npm", "pip", "taplo", "uv"]
        );
    }

    /// A mass generator's staging directory becomes an output directory of
    /// the running product, and only while a sandboxed product runs.
    #[test]
    fn declared_output_dirs_join_the_running_spec() {
        declare_output_dir(Path::new(".rsconstruct/mass_generator/x.staging"));
        assert!(
            current_spec().is_none(),
            "no product running: nothing to join"
        );
        set_spec(Some(run_with(&[], &[]).spec));
        declare_output_dir(Path::new(".rsconstruct/mass_generator/x.staging"));
        let spec = current_spec().unwrap();
        set_spec(None);
        assert_eq!(
            spec.output_dirs,
            [PathBuf::from(".rsconstruct/mass_generator/x.staging")]
        );
    }

    /// The tool runs behind the prelude only when there is something for it
    /// to do, with the stale outputs and output directories made absolute.
    #[test]
    fn the_prelude_runs_only_when_needed() {
        let mut cmd = Command::new("tool");
        cmd.args(["-x", "--", "in"]);
        let root = Path::new("/p");
        let mut spec = run_with(&[], &[]).spec;
        assert_eq!(inner_command(root, &spec, &cmd), ["tool", "-x", "--", "in"]);

        spec.stale = vec![PathBuf::from("out/a"), PathBuf::from("out/b")];
        spec.output_dirs = vec![PathBuf::from("out")];
        assert_eq!(
            inner_command(root, &spec, &cmd),
            [
                "sh", "-c", PRELUDE, "sh", "/p/out/a", "/p/out/b", "--", "/p/out", "--", "tool",
                "-x", "--", "in"
            ]
        );
    }

    /// The prelude removes the stale outputs (read-only ones too: a restored
    /// output is a read-only hardlink), creates the output directories, and
    /// runs the tool with its arguments untouched.
    #[test]
    fn the_prelude_clears_stale_outputs_then_runs_the_tool() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        fs::create_dir(root.join("out")).unwrap();
        fs::write(root.join("out/old"), "old").unwrap();
        fs::write(root.join("out/restored"), "restored").unwrap();
        crate::platform::set_permissions_mode(&root.join("out/restored"), 0o444).unwrap();
        let mut spec = run_with(&[], &["out", "made/here"]).spec;
        spec.stale = vec![
            PathBuf::from("out/old"),
            PathBuf::from("out/restored"),
            PathBuf::from("out/gone"),
        ];
        let mut tool = Command::new("sh");
        tool.args([
            "-c",
            "printf '%s|' \"$@\" > out/ran",
            "sh",
            "a b",
            "--",
            "c",
        ]);
        let args = inner_command(root, &spec, &tool);
        let status = Command::new(&args[0])
            .args(&args[1..])
            .current_dir(root)
            .status()
            .unwrap();
        assert!(status.success());
        assert!(!root.join("out/old").exists());
        assert!(!root.join("out/restored").exists());
        assert!(root.join("made/here").is_dir());
        assert_eq!(
            fs::read_to_string(root.join("out/ran")).unwrap(),
            "a b|--|c|"
        );
    }

    /// A killed build's runs are removed, the mode-000 `work/work` that
    /// overlayfs leaves included, and so is the sandbox directory; a project
    /// without one is fine.
    #[test]
    fn leftover_runs_are_removed() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        assert_eq!(remove_leftover_runs(root).unwrap(), 0);

        let run = root.join(SANDBOX_DIR).join("4242-0");
        fs::create_dir_all(run.join("upper/out")).unwrap();
        fs::write(run.join("upper/out/half.html"), "half").unwrap();
        fs::create_dir_all(run.join("work/work")).unwrap();
        crate::platform::set_permissions_mode(&run.join("work/work"), 0o000).unwrap();
        fs::create_dir_all(root.join(SANDBOX_DIR).join("4242-1")).unwrap();

        assert_eq!(remove_leftover_runs(root).unwrap(), 2);
        assert!(!root.join(SANDBOX_DIR).exists());
        assert!(root.join(".rsconstruct").is_dir(), "only the sandbox goes");
    }

    /// Removing a stale output is rsconstruct's own first step, so it is
    /// never reported as undeclared.
    #[test]
    fn stale_outputs_may_be_removed() {
        let mut run = run_with(&[], &[]);
        assert!(!run.declares(Path::new("old/out.html")));
        run.spec.stale = vec![PathBuf::from("old/out.html")];
        assert!(run.declares(Path::new("old/out.html")));
    }

    /// The stale outputs are removed by the first run that is applied, and
    /// by no run after it, which would remove what that run wrote. A run
    /// that fails is discarded, so the next attempt must remove them again.
    #[test]
    fn stale_outputs_are_forgotten_once_a_run_is_applied() {
        let settle = |succeeded: bool| {
            let tmp = tempfile::tempdir().unwrap();
            let mut run = run_with(&[], &[]);
            run.root = tmp.path().to_path_buf();
            run.dir = tmp.path().join("run");
            run.upper = run.dir.join("upper");
            fs::create_dir_all(&run.upper).unwrap();
            let mut spec = run.spec.clone();
            spec.stale = vec![PathBuf::from("out/old")];
            set_spec(Some(spec));
            run.finish(succeeded).unwrap();
            let left = current_spec().unwrap().stale;
            set_spec(None);
            left
        };
        assert_eq!(settle(false), [PathBuf::from("out/old")]);
        let none: Vec<PathBuf> = Vec::new();
        assert_eq!(settle(true), none);
    }

    /// A run that rewrites other products' files declares them and leaves
    /// their removal to the overlay; natively there is nothing to declare.
    #[test]
    fn replaced_outputs_are_declared_and_removed_in_the_overlay() {
        set_spec(None);
        assert!(!declare_replaced_outputs(&[Path::new("_site/a.html")]));
        set_spec(Some(run_with(&["_site/a.html"], &[]).spec));
        assert!(declare_replaced_outputs(&[Path::new("_site/b.html")]));
        let spec = current_spec().unwrap();
        set_spec(None);
        assert!(spec.outputs.contains(&PathBuf::from("_site/b.html")));
        assert_eq!(spec.stale, [PathBuf::from("_site/b.html")]);
    }

    /// A removed or replaced directory is judged by the real files it held:
    /// fine when each is a declared output, refused for the ones that are not.
    #[test]
    fn replaced_directory_is_checked_file_by_file() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path().to_path_buf();
        fs::create_dir_all(root.join("docx/sub")).unwrap();
        fs::write(root.join("docx/a.docx"), "a").unwrap();
        fs::write(root.join("docx/sub/b.docx"), "b").unwrap();
        let mut run = run_with(&["docx/a.docx", "docx/sub/b.docx"], &[]);
        run.root = root.clone();
        let replaced = Change::Replaced(PathBuf::from("docx"));
        let none: Vec<String> = Vec::new();
        assert_eq!(run.undeclared_in(&replaced).unwrap(), none);
        assert_eq!(
            run.undeclared_in(&Change::Removed(PathBuf::from("docx")))
                .unwrap(),
            none
        );
        fs::write(root.join("docx/notes.txt"), "n").unwrap();
        assert_eq!(
            run.undeclared_in(&replaced).unwrap(),
            ["removed  docx/notes.txt"]
        );
        // A removed file is its own path.
        assert_eq!(
            run.undeclared_in(&Change::Removed(PathBuf::from("other")))
                .unwrap(),
            ["removed  other"]
        );
    }

    #[test]
    fn declares_outputs_and_everything_under_output_dirs() {
        let run = run_with(&["out/a.html"], &["docs"]);
        assert!(run.declares(Path::new("out/a.html")));
        assert!(run.declares(Path::new("docs")));
        assert!(run.declares(Path::new("docs/x/y.html")));
        assert!(!run.declares(Path::new("out/b.html")));
        assert!(!run.declares(Path::new("out")));
        // Path-component prefix, not string prefix.
        assert!(!run.declares(Path::new("docsx/y.html")));
    }

    #[test]
    fn relative_to_strips_the_root_and_a_leading_dot() {
        let root = Path::new("/p");
        assert_eq!(
            relative_to(root, Path::new("/p/out/a")),
            PathBuf::from("out/a")
        );
        assert_eq!(
            relative_to(root, Path::new("./out/a")),
            PathBuf::from("out/a")
        );
        assert_eq!(
            relative_to(root, Path::new("out/a")),
            PathBuf::from("out/a")
        );
    }

    #[test]
    fn native_unless_the_processor_or_the_build_says_sandbox() {
        let mut standard = StandardConfig::default();
        assert!(!resolves_to_sandbox(&standard, ExecutionModel::Native));
        assert!(resolves_to_sandbox(&standard, ExecutionModel::Sandbox));
        standard.execution_model = Some(ExecutionModel::Native);
        assert!(!resolves_to_sandbox(&standard, ExecutionModel::Sandbox));
        standard.execution_model = Some(ExecutionModel::Sandbox);
        assert!(resolves_to_sandbox(&standard, ExecutionModel::Native));
    }

    #[test]
    fn collect_changes_reports_files_and_ignores_plain_directories() {
        let tmp = tempfile::tempdir().unwrap();
        let upper = tmp.path().join("upper");
        let root = tmp.path().join("root");
        fs::create_dir_all(upper.join("out/sub")).unwrap();
        fs::create_dir_all(upper.join("empty")).unwrap();
        fs::create_dir_all(&root).unwrap();
        fs::write(upper.join("out/sub/a.txt"), "a").unwrap();
        fs::write(upper.join("stray"), "s").unwrap();
        let mut changes = Vec::new();
        collect_changes(&upper, Path::new(""), &root, &mut changes).unwrap();
        assert_eq!(
            changes,
            vec![
                Change::Written(PathBuf::from("out/sub/a.txt")),
                Change::Written(PathBuf::from("stray")),
            ]
        );
    }

    /// A file copied up unchanged (opened for writing, nothing written) is
    /// not a change; one whose bytes differ is.
    #[test]
    fn files_copied_up_unchanged_are_not_changes() {
        let tmp = tempfile::tempdir().unwrap();
        let upper = tmp.path().join("upper");
        let root = tmp.path().join("root");
        fs::create_dir_all(&upper).unwrap();
        fs::create_dir_all(&root).unwrap();
        for (name, real, written) in [("same", "abc", "abc"), ("edited", "abc", "abd")] {
            fs::write(root.join(name), real).unwrap();
            fs::write(upper.join(name), written).unwrap();
        }
        let mut changes = Vec::new();
        collect_changes(&upper, Path::new(""), &root, &mut changes).unwrap();
        assert_eq!(changes, vec![Change::Written(PathBuf::from("edited"))]);
    }

    /// In a directory that replaces a real one, an identical file is still
    /// written: applying the replacement removes the real directory first.
    #[test]
    fn identical_files_in_a_replaced_directory_are_written() {
        let tmp = tempfile::tempdir().unwrap();
        let upper = tmp.path().join("upper");
        let root = tmp.path().join("root");
        fs::create_dir_all(upper.join("marp")).unwrap();
        fs::create_dir_all(root.join("marp")).unwrap();
        fs::write(root.join("marp/a.css"), "a").unwrap();
        fs::write(upper.join("marp/a.css"), "a").unwrap();
        xattr::set(upper.join("marp"), OPAQUE_XATTR, b"y").unwrap();
        let mut changes = Vec::new();
        collect_changes(&upper, Path::new(""), &root, &mut changes).unwrap();
        assert_eq!(
            changes,
            vec![
                Change::Replaced(PathBuf::from("marp")),
                Change::Written(PathBuf::from("marp/a.css")),
            ]
        );
    }

    /// A run that started before `target` existed made its own, opaque in
    /// the overlay; meanwhile another run created the real one and wrote
    /// into it. Applying merges into the shared writable place instead of
    /// emptying it (seen on GitHub runners: cargo's parallel products failed
    /// with "Failed to remove target: Directory not empty"). A replaced
    /// directory that is a declared output is still emptied first.
    #[test]
    fn replaced_writable_place_is_merged_not_emptied() {
        let tmp = tempfile::tempdir().unwrap();
        let upper = tmp.path().join("upper");
        let root = tmp.path().join("root");
        for dir in ["target", "docs"] {
            fs::create_dir_all(upper.join(dir)).unwrap();
            fs::create_dir_all(root.join(dir)).unwrap();
            fs::write(root.join(dir).join("theirs"), "t").unwrap();
            fs::write(upper.join(dir).join("mine"), "m").unwrap();
            xattr::set(upper.join(dir), OPAQUE_XATTR, b"y").unwrap();
        }
        let mut run = run_with(&[], &["docs"]);
        run.root = root.clone();
        run.upper = upper;
        run.writable = vec![Writable::parse("target").unwrap()];
        run.apply().unwrap();
        assert!(root.join("target/theirs").exists());
        assert!(root.join("target/mine").exists());
        assert!(!root.join("docs/theirs").exists());
        assert!(root.join("docs/mine").exists());
    }
}