brrrty 0.0.22

Brrrty, a fast static site generator compatible with Eleventy (11ty) projects.
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
//! Watch the site's sources, rebuild on change, and point the server at each new build.
//!
//! Every build goes into a fresh folder under `<output>/.brrrty-serve/`, and the server is
//! switched to it once the build has finished, so a page is never served half written. A
//! build whose errors are only refused pages is served, as `brrrty build` writes it, and each
//! refused page whose last build was served keeps that file, so a typo in the page being
//! edited never turns it into a 404. A build that stopped before writing any page leaves the
//! last one in place. An old build folder is removed once no request is still using it.
//!
//! After every build served, the output folder is brought up to date too, as 11ty's `--serve`
//! writes it on every build: each file the build wrote whose bytes changed is copied in and
//! renamed into place, so `_site` is current while the session runs and after it is killed.
//! It gets exactly what `brrrty build` would write, so the pages kept from an earlier build
//! never go there. Like `brrrty build`, this never deletes a file from the output folder. When
//! the session stops, the server is pointed at the output folder and the staging folder is
//! removed.
//!
//! Pages served get a live-reload script ([`Session::live_reload`], see [`live`]).
//!
//! ```no_run
//! # use std::path::Path;
//! use brrrty::serve::{Session, Settings};
//! use brrrty_serve::Server;
//!
//! let mut session = Session::start(Path::new("."), Settings::default()).unwrap();
//! let server = Server::start(session.serving(), "localhost", 8080).unwrap();
//! session.live_reload(&server);
//! let stop = session.stop_handle(); // call `stop.stop()` from a Ctrl-C handler
//! session.run(&server);
//! ```

pub mod live;
pub mod signals;

use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender};
use std::time::{Duration, Instant};

use brrrty_core::ignore_files::IgnoreRules;
use brrrty_core::{BuildOptions, Problem, RunMode};
use brrrty_markdown::MarkdownIt;
use brrrty_serve::{RetiredRoot, RunningServer};
use notify::event::{AccessKind, AccessMode, ModifyKind};
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};

use crate::options::{Flags, build_options};
use crate::report;

pub use live::LiveReload;

/// The folder inside the output folder that holds the builds of a session.
pub const STAGING_DIR: &str = brrrty_core::SERVE_STAGING_DIR;

/// How long a file must stay unchanged before a rebuild starts. 11ty's watcher waits for a
/// file to be stable for 150 ms (chokidar's `awaitWriteFinish.stabilityThreshold`) and then
/// builds at once (`watchThrottleWaitTime` 0), so a burst of saves makes one build.
pub const DEBOUNCE: Duration = Duration::from_millis(150);

/// How often an idle session checks whether an old build folder can be removed.
const REAP_EVERY: Duration = Duration::from_millis(250);

/// How long stopping waits for requests still reading the last build before removing it.
const STOP_GRACE: Duration = Duration::from_secs(2);

/// Files in the project folder whose change means a rebuild: the configs and the ignore
/// files, as 11ty watches them, and the lock file, since JavaScript data loads packages.
const ROOT_FILES: &[&str] = &[
    brrrty_config::CONFIG_FILE,
    ".gitignore",
    ".eleventyignore",
    "package.json",
    "package-lock.json",
    "eleventy.config.js",
    "eleventy.config.cjs",
    "eleventy.config.mjs",
    ".eleventy.js",
];

/// How a session prints and waits.
#[derive(Debug, Clone)]
pub struct Settings {
    /// The build flags, as `brrrty build` takes them.
    pub flags: Flags,
    /// Don't print the summary line or the changed files.
    pub quiet: bool,
    /// Color the problems.
    pub color: bool,
    /// See [`DEBOUNCE`].
    pub debounce: Duration,
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            flags: Flags::default(),
            quiet: false,
            color: false,
            debounce: DEBOUNCE,
        }
    }
}

/// What one rebuild did, for the live-reload layer.
#[derive(Debug, Clone)]
pub struct Rebuild {
    /// The build had no errors and is now being served.
    pub ok: bool,
    /// The build is now being served, errors or not: it got as far as writing pages.
    pub served: bool,
    /// The templates, relative to the project folder, with a page this build refused.
    pub refused: Vec<PathBuf>,
    /// How many refused pages are served from an earlier build.
    pub kept: usize,
    /// The files whose change started it, relative to the project folder.
    pub changed_paths: Vec<PathBuf>,
    /// Every changed file is a stylesheet outside `_includes`, which 11ty's dev server
    /// swaps in place instead of reloading the page.
    pub css_only: bool,
    /// What the build reported, warnings included.
    pub problems: Vec<Problem>,
    /// The pages of the changed templates, as now served. Read only while live-reload tabs are
    /// connected ([`Session::live_reload`]); empty otherwise, and when the build failed.
    pub pages: Vec<ChangedPage>,
    /// The URLs of the changed stylesheets, as passthrough copied them.
    pub stylesheets: Vec<String>,
    /// What went wrong bringing the output folder up to date with this build.
    pub output_problems: Vec<Problem>,
}

/// A page of a changed template.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChangedPage {
    /// Its `page.url`.
    pub url: String,
    /// The HTML written.
    pub content: String,
}

/// What one build gave.
struct Built {
    /// No errors.
    ok: bool,
    problems: Vec<Problem>,
    /// The build's folder, when it got as far as writing pages and is to be served.
    dir: Option<PathBuf>,
    pages: Vec<brrrty_core::WrittenPage>,
    passthrough: Vec<(PathBuf, PathBuf)>,
    refused: Vec<PathBuf>,
    /// What the output folder should get from it.
    entries: Vec<Entry>,
}

/// What a file in a build holds, as far as bringing the output folder up to date goes.
#[derive(Debug, Clone)]
enum Version {
    /// A page, by the hash of the bytes the build wrote.
    Page(u64),
    /// A passthrough copy, by its source as it was just before the build; `None` when that
    /// could not be read, which matches nothing.
    Copy(Option<Stamp>),
}

impl PartialEq for Version {
    fn eq(&self, other: &Version) -> bool {
        match (self, other) {
            (Version::Page(a), Version::Page(b)) => a == b,
            // An unknown stamp never counts as unchanged.
            (Version::Copy(Some(a)), Version::Copy(Some(b))) => a == b,
            _ => false,
        }
    }
}

/// A file a build wrote, relative to the build folder.
#[derive(Debug, Clone)]
struct Entry {
    rel: PathBuf,
    version: Version,
}

/// A file the session put in the output folder (or found there already the same), and how
/// the output folder's file looked just after, so a change made there by anything else is
/// noticed without reading it.
#[derive(Debug, Clone)]
struct Record {
    version: Version,
    len: u64,
    modified: Option<std::time::SystemTime>,
}

/// Called after every rebuild, on the session's thread.
pub type RebuildHook = dyn Fn(&Rebuild) + Send + Sync;

/// What a session ended with.
#[derive(Debug, Default)]
pub struct Finished {
    /// Rebuilds run after the first build.
    pub rebuilds: usize,
    /// The output folder holds the last good build (false when there was none, or a file
    /// could not be written).
    pub output_written: bool,
    /// What went wrong bringing the output folder up to date or removing staging.
    pub problems: Vec<Problem>,
}

/// Why a session could not start.
#[derive(Debug)]
pub enum StartError {
    /// The config or the folders are wrong; nothing was built.
    Problems(Vec<Problem>),
    /// The staging folder could not be made.
    Staging(PathBuf, io::Error),
    /// Another `brrrty serve` (by process id, when known) is using the output folder.
    InUse(PathBuf, String),
    /// The file watcher could not start.
    Watch(notify::Error),
}

impl std::fmt::Display for StartError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StartError::Problems(p) => write!(f, "{} problem(s) in the config or folders", p.len()),
            StartError::Staging(path, e) => {
                write!(f, "cannot make the folder {}: {e}", path.display())
            }
            StartError::InUse(output, pid) => {
                let by = if pid.is_empty() {
                    String::new()
                } else {
                    format!(" (process {pid})")
                };
                write!(
                    f,
                    "another `brrrty serve`{by} is using the output folder {}",
                    output.display()
                )
            }
            StartError::Watch(e) => write!(f, "cannot watch the source files: {e}"),
        }
    }
}

impl std::error::Error for StartError {}

enum Msg {
    Fs(notify::Result<Event>),
    Stop,
}

/// Stops a running [`Session`]. Safe to call from a signal handler thread, and more than once.
#[derive(Clone)]
pub struct StopHandle(Sender<Msg>);

impl StopHandle {
    /// Ask the session to finish. A build already running finishes first.
    pub fn stop(&self) {
        let _ = self.0.send(Msg::Stop);
    }
}

/// A build folder waiting to be removed.
struct Retired {
    root: Option<RetiredRoot>,
    path: PathBuf,
}

/// A serve session: the watcher, the staging folder and the builds in it.
pub struct Session {
    /// Held while the session runs, so `brrrty build` and other sessions leave `staging` be.
    lock: Option<brrrty_core::ServeLock>,
    /// The highest of the output folder and its parents that the session made.
    made: Option<PathBuf>,
    settings: Settings,
    /// The flags with the input and output folders fixed at the first build.
    flags: Flags,
    root: PathBuf,
    input: PathBuf,
    output: PathBuf,
    staging: PathBuf,
    next_build: u64,
    /// The last good build, under `staging`.
    current: Option<PathBuf>,
    /// What the server should start on.
    serving: PathBuf,
    first_ok: bool,
    first_served: bool,
    first_problems: Vec<Problem>,
    /// The pages of the build being served, those kept from earlier builds included.
    served_pages: Vec<brrrty_core::WrittenPage>,
    /// What the session knows the output folder holds, by path relative to it.
    mirrored: HashMap<PathBuf, Record>,
    /// The files of the last build served, for another try at a copy that failed.
    last_entries: Vec<Entry>,
    retired: Vec<Retired>,
    passthrough: Vec<PathBuf>,
    /// The includes and layouts folders, as the last build's config set them.
    template_dirs: Vec<PathBuf>,
    /// What the last build's config and data scripts read, listed or checked, as real paths.
    reads: Vec<PathBuf>,
    rules: Option<IgnoreRules>,
    /// `None` once the session is finishing.
    watcher: Option<RecommendedWatcher>,
    /// Folders watched without their subfolders: the input folder and the project folder.
    tops: Vec<PathBuf>,
    /// Folders watched recursively.
    watched: Vec<PathBuf>,
    /// The watches can't be trusted (the watcher asked for a rescan, or the input folder
    /// itself went away): set them all up again before the next build.
    rewatch: bool,
    /// What went wrong the last time the output folder was brought up to date.
    mirror_problems: Vec<Problem>,
    /// A temporary file name counter, for copies on their way into the output folder.
    next_tmp: u64,
    /// The watcher's last error, so a repeated one is printed once.
    last_watch_error: Option<String>,
    /// The passthrough files as they were just before the last build, which copies them. On
    /// macOS, copying a file can make FSEvents report it again, flags and all, though nothing
    /// changed; an event for a file that still looks like this is that echo, not an edit.
    copied: HashMap<PathBuf, Stamp>,
    /// The same, just before the build before it; `None` until there was one. A file whose
    /// stamp moved between the two builds without being among the files that started the
    /// later one was edited in the moment between the debounce firing and the stamps being
    /// taken, so its event is real even though its stamp still matches.
    copied_before: Option<HashMap<PathBuf, Stamp>>,
    /// The files that started the last build, relative to the project folder.
    last_changed: Vec<PathBuf>,
    /// The live-reload channel, when attached: changed pages are read only for open tabs.
    live: Option<LiveReload>,
    tx: Sender<Msg>,
    rx: Receiver<Msg>,
    hooks: Vec<Arc<RebuildHook>>,
}

impl Session {
    /// Resolve the folders, start watching, and run the first build. A first build with
    /// errors does not stop the session: its problems are printed, and it is served when it got
    /// as far as writing pages; otherwise the output folder is served as it is (or an empty
    /// folder when there is none) until a build does. [`Session::first_ok`] and
    /// [`Session::first_served`] say which happened.
    pub fn start(root: &Path, settings: Settings) -> Result<Session, StartError> {
        let opts = build_options(root, &settings.flags).map_err(StartError::Problems)?;
        opts.validate().map_err(|p| StartError::Problems(vec![p]))?;
        let problem = |p| StartError::Problems(vec![p]);
        let root = opts.root_dir().map_err(problem)?;
        let input = opts.input_dir().map_err(problem)?;
        let output = opts.output_dir().map_err(problem)?;
        let mut flags = settings.flags.clone();
        flags.input = Some(opts.input.clone());
        flags.output = Some(opts.output.clone());

        let staging = output.join(STAGING_DIR);
        let in_use = |pid| StartError::InUse(output.clone(), pid);
        if fs::symlink_metadata(&staging).is_ok_and(|m| m.is_dir())
            && let Some(pid) = brrrty_core::serve_lock_holder(&staging)
        {
            return Err(in_use(pid));
        }
        // A folder left by a session that was killed before it could clean up, or a symlink
        // by that name, which is removed and never followed.
        if fs::symlink_metadata(&staging).is_ok() {
            remove_any(&staging).map_err(|e| StartError::Staging(staging.clone(), e))?;
        }
        // The highest folder this session makes, so one left empty goes at the end, as when
        // 11ty never writes a build.
        let made = output.ancestors().take_while(|a| !a.exists()).last();
        let made = made.map(Path::to_path_buf);
        fs::create_dir_all(&staging).map_err(|e| StartError::Staging(staging.clone(), e))?;
        let made = made.map(|m| m.canonicalize().unwrap_or(m));
        let lock = match brrrty_core::ServeLock::take(&staging) {
            Ok(l) => l,
            // Another session started at the same moment and won.
            Err(e) if e.kind() == io::ErrorKind::WouldBlock => {
                return Err(in_use(
                    brrrty_core::serve_lock_holder(&staging).unwrap_or_default(),
                ));
            }
            Err(e) => return Err(StartError::Staging(staging.clone(), e)),
        };
        // Compare against real paths: the watcher reports them.
        let output = output.canonicalize().unwrap_or(output);
        let staging = output.join(STAGING_DIR);

        let (tx, rx) = mpsc::channel();
        let events = tx.clone();
        let watcher = notify::recommended_watcher(move |res| {
            let _ = events.send(Msg::Fs(res));
        })
        .map_err(StartError::Watch)?;

        let mut session = Session {
            lock: Some(lock),
            made,
            settings,
            flags,
            root,
            input,
            output: output.clone(),
            staging,
            next_build: 0,
            current: None,
            serving: output,
            first_ok: false,
            first_served: false,
            first_problems: Vec::new(),
            served_pages: Vec::new(),
            mirrored: HashMap::new(),
            last_entries: Vec::new(),
            retired: Vec::new(),
            passthrough: Vec::new(),
            template_dirs: Vec::new(),
            reads: Vec::new(),
            rules: None,
            watcher: Some(watcher),
            tops: Vec::new(),
            watched: Vec::new(),
            rewatch: false,
            mirror_problems: Vec::new(),
            next_tmp: 0,
            last_watch_error: None,
            copied: HashMap::new(),
            copied_before: None,
            last_changed: Vec::new(),
            live: None,
            tx,
            rx,
            hooks: Vec::new(),
        };
        session.refresh(&opts);
        session.watch_all().map_err(StartError::Watch)?;

        let Built {
            ok,
            problems,
            dir,
            pages,
            entries,
            ..
        } = session.build();
        session.first_ok = ok;
        session.first_served = dir.is_some();
        session.first_problems = problems;
        match dir {
            Some(dir) => {
                session.mirror(&dir, entries);
                session.served_pages = pages;
                session.serving = dir.clone();
                session.current = Some(dir);
            }
            None if session.output.is_dir() => {}
            None => {
                let empty = session.new_build_dir();
                fs::create_dir_all(&empty).map_err(|e| StartError::Staging(empty.clone(), e))?;
                session.serving = empty;
            }
        }
        Ok(session)
    }

    /// The folder to start the server on.
    pub fn serving(&self) -> &Path {
        &self.serving
    }

    /// Whether the first build had no errors.
    pub fn first_ok(&self) -> bool {
        self.first_ok
    }

    /// Whether the first build is being served: it got as far as writing pages, though it may
    /// have refused some.
    pub fn first_served(&self) -> bool {
        self.first_served
    }

    /// What the first build reported.
    pub fn first_problems(&self) -> &[Problem] {
        &self.first_problems
    }

    /// The staging folder, inside the output folder.
    pub fn staging(&self) -> &Path {
        &self.staging
    }

    /// The output folder, as its real path.
    pub fn output(&self) -> &Path {
        &self.output
    }

    /// A handle that stops [`Session::run`].
    pub fn stop_handle(&self) -> StopHandle {
        StopHandle(self.tx.clone())
    }

    /// Call `hook` after every rebuild, after the hooks added before it.
    pub fn on_rebuild(&mut self, hook: Arc<RebuildHook>) {
        self.hooks.push(hook);
    }

    /// Inject the live-reload script into `server`'s pages and tell them about every rebuild.
    /// Pages that connect while the first build's errors stand, or while the output folder
    /// could not be written, are told about those.
    pub fn live_reload(&mut self, server: &RunningServer) -> LiveReload {
        let live = LiveReload::attach(server);
        if !self.first_ok {
            live.show_problems(&self.first_problems);
        } else if !self.mirror_problems.is_empty() {
            live.show_problems(&self.mirror_problems);
        }
        let hook = live.clone();
        self.on_rebuild(Arc::new(move |r: &Rebuild| hook.rebuilt(r)));
        self.live = Some(live.clone());
        live
    }

    /// Watch and rebuild until stopped, then point the server at the output folder, which
    /// holds the last good build, and remove the staging folder.
    pub fn run(mut self, server: &RunningServer) -> Finished {
        let mut finished = Finished::default();
        // The server may have been started on something else.
        if let Some(cur) = &self.current
            && server.root() != *cur
            && let Ok(old) = server.swap_root(cur)
        {
            self.retire(old);
        }
        'outer: loop {
            self.reap();
            let first = match self.rx.recv_timeout(REAP_EVERY) {
                Ok(Msg::Stop) | Err(RecvTimeoutError::Disconnected) => break,
                Ok(Msg::Fs(ev)) => ev,
                Err(RecvTimeoutError::Timeout) => continue,
            };
            let mut changed = Vec::new();
            self.collect(first, &mut changed);
            if changed.is_empty() {
                continue;
            }
            // Wait for the files to settle: every relevant event restarts the wait.
            let mut quiet_since = Instant::now();
            let mut stop = false;
            loop {
                let left = self.settings.debounce.saturating_sub(quiet_since.elapsed());
                if left.is_zero() {
                    break;
                }
                match self.rx.recv_timeout(left) {
                    Ok(Msg::Stop) | Err(RecvTimeoutError::Disconnected) => {
                        stop = true;
                        break;
                    }
                    Ok(Msg::Fs(ev)) => {
                        if self.collect(ev, &mut changed) {
                            quiet_since = Instant::now();
                        }
                    }
                    Err(RecvTimeoutError::Timeout) => break,
                }
            }
            if stop {
                break 'outer;
            }
            // Saves that land while this runs queue up in the channel and make exactly one
            // more build once it is done, after their own wait.
            self.rebuild(server, &changed);
            finished.rebuilds += 1;
        }
        self.finish(server, &mut finished);
        finished
    }

    /// End a session without serving it, such as when the server could not start or the
    /// first build failed and the caller won't go on: stop watching and remove the staging
    /// folder. The output folder holds the first build when it was good.
    pub fn close(mut self) -> Finished {
        let mut finished = Finished::default();
        self.watcher = None;
        if self.current.take().is_some() {
            finished.output_written = self.mirror_problems.is_empty();
            finished.problems.append(&mut self.mirror_problems);
        }
        self.cleanup(&mut finished);
        finished
    }

    fn rebuild(&mut self, server: &RunningServer, changed: &[PathBuf]) {
        if !self.settings.quiet {
            for p in changed {
                crate::say!("File changed: {}", shown(p));
            }
        }
        self.last_changed = changed.to_vec();
        let built = self.build();
        let (mut ok, problems) = (built.ok, built.problems);
        let tabs_open = self.live.as_ref().is_some_and(|l| l.clients() > 0);
        let mut pages = Vec::new();
        let mut output_problems = Vec::new();
        let mut served = false;
        let mut kept = 0;
        if let Some(dir) = built.dir {
            let kept_pages = if ok {
                Vec::new()
            } else {
                self.keep_refused(&dir, &built.refused, &built.pages)
            };
            match server.swap_root(&dir) {
                Ok(old) => {
                    served = true;
                    kept = kept_pages.len();
                    if kept > 0 && !self.settings.quiet {
                        let (noun, clause) = if kept == 1 {
                            ("page", "its template has")
                        } else {
                            ("pages", "their templates have")
                        };
                        crate::say!("Kept {kept} {noun} from the last build, as {clause} errors");
                    }
                    self.retire(old);
                    self.mirror(&dir, built.entries);
                    output_problems = self.mirror_problems.clone();
                    // The changed templates' pages, for a page showing one to patch itself.
                    for p in built.pages.iter().filter(|_| tabs_open) {
                        if changed.contains(&p.source)
                            && let Ok(content) = fs::read_to_string(dir.join(&p.output))
                        {
                            pages.push(ChangedPage {
                                url: p.url.clone(),
                                content,
                            });
                        }
                    }
                    let mut all = built.pages.clone();
                    all.extend(kept_pages);
                    self.served_pages = all;
                    self.current = Some(dir);
                }
                Err(e) => {
                    crate::say_err!("Brrrty could not serve the new build: {e}");
                    let _ = fs::remove_dir_all(&dir);
                    ok = false;
                }
            }
        }
        let css_only = !changed.is_empty()
            && changed.iter().all(|p| {
                p.extension().is_some_and(|e| e == "css")
                    && !self
                        .template_dirs
                        .iter()
                        .any(|d| self.root.join(p).starts_with(d))
            });
        let stylesheets = if served {
            built
                .passthrough
                .iter()
                .filter(|(src, _)| {
                    src.extension().is_some_and(|e| e == "css") && changed.contains(src)
                })
                .map(|(_, dest)| url_of(dest))
                .collect()
        } else {
            Vec::new()
        };
        let rebuild = Rebuild {
            ok,
            served,
            refused: built.refused,
            kept,
            changed_paths: changed.to_vec(),
            css_only,
            problems,
            pages,
            stylesheets,
            output_problems,
        };
        for hook in &self.hooks {
            hook(&rebuild);
        }
    }

    fn new_build_dir(&mut self) -> PathBuf {
        self.next_build += 1;
        self.staging.join(format!("build-{}", self.next_build))
    }

    /// Build into a fresh folder. Gives whether it had no errors, its problems, and the folder
    /// when it did; a failed build's folder is removed at once.
    fn build(&mut self) -> Built {
        let color = self.settings.color;
        let failed = |problems| Built {
            ok: false,
            problems,
            dir: None,
            pages: Vec::new(),
            passthrough: Vec::new(),
            refused: Vec::new(),
            entries: Vec::new(),
        };
        let mut opts = match build_options(&self.root, &self.flags) {
            Ok(o) => o,
            Err(problems) => {
                report::print_problems(&problems, &self.root, color);
                return failed(problems);
            }
        };
        self.refresh(&opts);
        self.template_dirs = vec![
            self.input.join(opts.config.includes_dir()),
            self.input.join(opts.config.layouts_dir()),
        ];
        let now = stamps(&self.passthrough);
        let first = self.next_build == 0;
        let before = std::mem::replace(&mut self.copied, now);
        self.copied_before = (!first).then_some(before);
        let dir = self.new_build_dir();
        // The user (or a `brrrty build`) may have removed the output folder meanwhile.
        let _ = fs::create_dir_all(&self.staging);
        if let Err(e) = fs::create_dir_all(&dir) {
            let p = Problem::error(
                brrrty_core::codes::WRITE_FAILED,
                format!("cannot make the build folder {}: {e}", dir.display()),
            );
            report::print_problems(std::slice::from_ref(&p), &self.root, color);
            return failed(vec![p]);
        }
        opts.write_to = Some(dir.clone());
        opts.run_mode = RunMode::Serve;
        let report = brrrty_core::build(&opts, &MarkdownIt::new());
        report::print_build(&report, &self.root, self.settings.quiet, color);
        let ok = !report.has_errors();
        // A build that stopped before writing pages is never served, nor under `--strict` one
        // with any error.
        let served = report.complete && (ok || !self.flags.strict);
        if !served {
            let _ = fs::remove_dir_all(&dir);
        } else if let Some(p) = report
            .problems
            .iter()
            .find(|p| p.code == brrrty_core::codes::CONFIG_TODO)
        {
            // Nothing was written: say why on every page instead of a bare 404. The page is
            // only in the build folder, never mirrored to the output folder.
            let _ = fs::write(dir.join("404.html"), unported_page(p));
        }
        self.watch_reads(&report.reads);
        let entries = report
            .pages
            .iter()
            .map(|p| Entry {
                rel: p.output.clone(),
                version: Version::Page(p.hash),
            })
            .chain(report.passthrough.iter().map(|(src, dest)| {
                let stamp = stamp_of(&self.copied, &self.root.join(src)).cloned();
                Entry {
                    rel: dest.clone(),
                    version: Version::Copy(stamp),
                }
            }))
            .collect();
        Built {
            ok,
            problems: report.problems,
            dir: served.then_some(dir),
            pages: report.pages,
            passthrough: report.passthrough,
            refused: report.refused,
            entries,
        }
    }

    /// Re-read what decides which changes count: the ignore files and the passthrough paths.
    fn refresh(&mut self, opts: &BuildOptions) {
        self.rules = opts
            .input_rel()
            .ok()
            .and_then(|rel| IgnoreRules::load(&self.root, &self.input, &rel).ok());
        let mut pt: Vec<PathBuf> = opts
            .config
            .passthrough
            .iter()
            .map(|e| PathBuf::from(&e.from))
            .chain(opts.passthrough.iter().cloned())
            .map(|p| self.root.join(p))
            .map(|p| p.canonicalize().unwrap_or(p))
            .collect();
        pt.sort();
        pt.dedup();
        self.passthrough = pt;
        if self.rewatch {
            self.rewatch_all();
        } else {
            self.sync_subfolders();
        }
        // Passthrough folders outside the input folder need watches of their own.
        let extra: Vec<PathBuf> = self
            .passthrough
            .iter()
            .filter(|p| !p.starts_with(&self.input) && p.exists())
            .cloned()
            .collect();
        for p in extra {
            if !self.watched.contains(&p) && !self.skipped(&p) {
                // A source that can't be watched still builds; its edits just aren't seen.
                if self.watch(&p, RecursiveMode::Recursive).is_ok() {
                    self.watched.push(p);
                }
            }
        }
    }

    /// Watch the input folder and the project folder. Each is watched without its
    /// subfolders, and each subfolder recursively, so `node_modules`, `.git` and the output
    /// folder are never watched at all (on Linux every watched folder costs an inotify watch).
    fn watch_all(&mut self) -> notify::Result<()> {
        let mut tops = vec![self.input.clone()];
        if self.root != self.input {
            tops.push(self.root.clone());
        }
        for top in tops {
            self.watch(&top, RecursiveMode::NonRecursive)?;
            self.tops.push(top.clone());
            // The project folder's own subfolders are not sources unless they hold the input.
            let deep = top == self.input;
            if let Ok(entries) = fs::read_dir(&top) {
                for e in entries.flatten() {
                    let p = e.path();
                    if p.is_dir() && deep {
                        self.watch_subfolder(&p);
                    }
                }
            }
        }
        Ok(())
    }

    /// Drop every watch and set them up again from what is on disk now. notify walks each
    /// folder it is asked to watch recursively, so this is a full-tree check.
    fn rewatch_all(&mut self) {
        let old: Vec<PathBuf> = self.tops.drain(..).chain(self.watched.drain(..)).collect();
        for p in old {
            self.unwatch(&p);
        }
        // The input folder may still be missing; try again before the next build.
        self.rewatch = self.watch_all().is_err();
        let extra: Vec<PathBuf> = self
            .passthrough
            .iter()
            .filter(|p| !p.starts_with(&self.input) && p.exists())
            .cloned()
            .collect();
        for p in extra {
            self.watch_subfolder(&p);
        }
        for p in self.read_folders() {
            self.watch_read_folder(&p);
        }
    }

    /// Note what the build's scripts read (relative to the project folder) and watch the
    /// folders holding the ones outside the input folder, which nothing else watches.
    fn watch_reads(&mut self, reads: &[PathBuf]) {
        // Each path both as the script gave it and as it resolves, so a change to a symlink
        // on the way (retargeting `data/current`) counts as well as a change to its target.
        let mut all = Vec::new();
        for r in reads {
            let p = self.root.join(r);
            if let Ok(real) = p.canonicalize()
                && real != p
            {
                all.push(real);
            }
            all.push(p);
        }
        all.sort();
        all.dedup();
        self.reads = all;
        for p in self.read_folders() {
            self.watch_read_folder(&p);
        }
    }

    /// The folders to watch for the reads: a listed folder itself, else the folder holding
    /// the file, or the nearest one above that exists. Folders a watch already covers are
    /// left out: the input folder's subfolders, and the project and input folders' own
    /// files, which their shallow watches see. Reads under `node_modules` and `.git` are
    /// watched too, though nothing else there is.
    fn read_folders(&self) -> Vec<PathBuf> {
        let mut folders: Vec<PathBuf> = self
            .reads
            .iter()
            .filter(|p| p.starts_with(&self.root) && !self.in_output(p))
            .filter_map(|p| {
                let mut f = p.as_path();
                if !f.is_dir() {
                    f = f.parent()?;
                }
                while !f.is_dir() {
                    f = f.parent()?;
                }
                Some(f.canonicalize().unwrap_or_else(|_| f.to_path_buf()))
            })
            // A symlink on the way to a read: the folder holding it sees it retargeted.
            .chain(self.reads.iter().flat_map(|p| {
                p.ancestors()
                    .take_while(|a| a.starts_with(&self.root) && *a != self.root)
                    .filter(|a| {
                        a.symlink_metadata()
                            .is_ok_and(|m| m.file_type().is_symlink())
                    })
                    .filter_map(|a| a.parent().map(Path::to_path_buf))
                    .collect::<Vec<_>>()
            }))
            .filter(|f| f.starts_with(&self.root) && !self.tops.contains(f))
            .collect();
        folders.sort();
        folders.dedup();
        folders
    }

    /// Watch a folder holding something a script read, recursively, unless a watch already
    /// covers it. Only the output folder is never watched.
    fn watch_read_folder(&mut self, p: &Path) {
        if self.in_output(p) || self.watched.iter().any(|w| p.starts_with(w)) {
            return;
        }
        if self.watch(p, RecursiveMode::Recursive).is_ok() {
            self.watched.push(p.to_path_buf());
        }
    }

    /// The output folder (staging included), or a folder holding it.
    fn in_output(&self, p: &Path) -> bool {
        p.starts_with(&self.output) || (self.output.starts_with(p) && p != self.root)
    }

    /// Make the recursive watches on the input folder's subfolders match the disk: forget
    /// the ones whose folder is gone and watch every subfolder that has none.
    fn sync_subfolders(&mut self) {
        let gone: Vec<PathBuf> = self
            .watched
            .iter()
            .filter(|p| !p.is_dir())
            .cloned()
            .collect();
        for p in gone {
            self.forget(&p);
        }
        if let Ok(entries) = fs::read_dir(&self.input) {
            for e in entries.flatten() {
                let p = e.path();
                if p.is_dir() {
                    self.watch_subfolder(&p);
                }
            }
        }
    }

    /// Drop the watches on `p` and on anything under it. With inotify a removed folder's
    /// watch is gone even if a folder of the same name comes back, so the name must not
    /// stay listed as watched.
    fn forget(&mut self, p: &Path) {
        let (gone, kept): (Vec<PathBuf>, Vec<PathBuf>) =
            self.watched.drain(..).partition(|w| w.starts_with(p));
        self.watched = kept;
        for w in gone {
            self.unwatch(&w);
        }
    }

    fn unwatch(&mut self, p: &Path) {
        if let Some(w) = &mut self.watcher {
            // It may already be gone with its folder.
            let _ = w.unwatch(p);
        }
    }

    fn watch_subfolder(&mut self, p: &Path) {
        let p = p.canonicalize().unwrap_or_else(|_| p.to_path_buf());
        if self.skipped(&p) || self.watched.contains(&p) {
            return;
        }
        if self.watch(&p, RecursiveMode::Recursive).is_ok() {
            self.watched.push(p);
        }
    }

    fn watch(&mut self, p: &Path, mode: RecursiveMode) -> notify::Result<()> {
        match &mut self.watcher {
            Some(w) => w.watch(p, mode),
            None => Ok(()),
        }
    }

    /// Never watched: the output folder (staging included), anything holding it, and
    /// `node_modules` and `.git`.
    fn skipped(&self, p: &Path) -> bool {
        p.starts_with(&self.output)
            || self.output.starts_with(p)
            || p.components().any(|c| {
                let c = c.as_os_str();
                c == "node_modules" || c == ".git"
            })
    }

    /// Add the paths of `ev` that should start a rebuild, relative to the project folder.
    /// Gives whether there was one, even one already in `changed`.
    fn collect(&mut self, ev: notify::Result<Event>, changed: &mut Vec<PathBuf>) -> bool {
        let ev = match ev {
            Ok(ev) => ev,
            Err(e) => {
                // Such as running out of inotify watches. Say so once, and set the watches
                // up again before the next build; no rebuild of its own, so an error that
                // keeps coming back can't loop.
                let message = e.to_string();
                if self.last_watch_error.as_deref() != Some(message.as_str()) {
                    crate::say_err!("Brrrty's file watcher reported a problem: {message}");
                    self.last_watch_error = Some(message);
                }
                self.rewatch = true;
                return false;
            }
        };
        // The watcher lost track (an inotify queue overflow, FSEvents dropping events):
        // watch everything again and rebuild, since any change may have been missed.
        if ev.need_rescan() {
            self.rewatch = true;
            let rel = self
                .input
                .strip_prefix(&self.root)
                .unwrap_or(&self.input)
                .to_path_buf();
            if !changed.contains(&rel) {
                changed.push(rel);
            }
            return true;
        }
        match ev.kind {
            EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) | EventKind::Any => {
            }
            EventKind::Access(AccessKind::Close(AccessMode::Write)) => {}
            _ => return false,
        }
        let mut any = false;
        let moved_or_removed = matches!(
            ev.kind,
            EventKind::Remove(_) | EventKind::Modify(ModifyKind::Name(_)) | EventKind::Any
        );
        let appeared = matches!(
            ev.kind,
            EventKind::Create(_) | EventKind::Modify(ModifyKind::Name(_)) | EventKind::Any
        );
        for p in &ev.paths {
            if moved_or_removed {
                // A watched folder (or one holding it) went away or was renamed.
                self.forget(p);
                if self.tops.iter().any(|t| t.starts_with(p)) {
                    self.rewatch = true;
                }
            }
            // A folder created, re-created or moved into the input folder, which is watched
            // shallowly, needs its own watch. Moved in arrives as a rename, not a create.
            if p.parent() == Some(self.input.as_path()) && p.is_dir() {
                if appeared {
                    // Possibly a new folder under an old name: its old watch is dead.
                    self.forget(p);
                }
                self.watch_subfolder(p);
            }
            if self.rewatch && p.is_dir() && (*p == self.input || *p == self.root) {
                self.rewatch_all();
            }
            let echo = {
                let rel = p.strip_prefix(&self.root).unwrap_or(p);
                is_echo(
                    Stamp::of(p).as_ref(),
                    self.copied.get(p),
                    self.copied_before.as_ref().map(|b| b.get(p)),
                    self.last_changed.iter().any(|c| c == rel),
                )
            };
            if !echo && self.counts(p) {
                any = true;
                let rel = p.strip_prefix(&self.root).unwrap_or(p).to_path_buf();
                if !changed.contains(&rel) {
                    changed.push(rel);
                }
            }
        }
        any
    }

    /// Whether a change to `p` should start a rebuild.
    fn counts(&self, p: &Path) -> bool {
        // Writes into the output folder are the build's own.
        if p.starts_with(&self.output) {
            return false;
        }
        // A file a script read, one in a folder it listed, or a folder on the way to a file
        // it looked for and didn't find. Ignore files don't hide these, nor does
        // `node_modules`: the script read them.
        if self
            .reads
            .iter()
            .any(|r| p.starts_with(r) || (r.starts_with(p) && p != self.root))
        {
            return true;
        }
        if self.skipped(p) {
            return false;
        }
        let root_file = p.parent() == Some(self.root.as_path())
            && p.file_name()
                .is_some_and(|n| ROOT_FILES.iter().any(|f| n == *f));
        let source = p.starts_with(&self.input) && p != self.input;
        let passthrough = self.passthrough.iter().any(|t| p.starts_with(t));
        if !(root_file || source || passthrough) {
            return false;
        }
        !self.rules.as_ref().is_some_and(|r| r.is_ignored(p, false))
    }

    fn retire(&mut self, old: RetiredRoot) {
        let path = old.path().to_path_buf();
        // Only folders this session made are removed; anything else is left alone.
        if path.starts_with(&self.staging) && path != self.staging {
            self.retired.push(Retired {
                root: Some(old),
                path,
            });
        }
    }

    /// Remove the old build folders no request is using any more. A folder that can't be
    /// removed yet (a file still open on Windows) is tried again later.
    fn reap(&mut self) {
        self.retired.retain(|r| {
            if r.root.as_ref().is_some_and(RetiredRoot::in_use) {
                return true;
            }
            match fs::remove_dir_all(&r.path) {
                Ok(()) => false,
                Err(e) => e.kind() != io::ErrorKind::NotFound,
            }
        });
    }

    fn finish(mut self, server: &RunningServer, finished: &mut Finished) {
        // Stop watching first: nothing after this is an edit.
        self.watcher = None;
        if let Some(cur) = self.current.take() {
            // The output folder is brought up to date after every build; only a copy that
            // failed needs another try.
            if !self.mirror_problems.is_empty() {
                let entries = std::mem::take(&mut self.last_entries);
                self.mirror(&cur, entries);
            }
            finished.output_written = self.mirror_problems.is_empty();
            finished.problems.append(&mut self.mirror_problems);
        }
        // Serve the output folder, which holds the same files, before staging goes, so no
        // request in between finds nothing.
        if self.output.is_dir()
            && let Ok(old) = server.swap_root(&self.output)
        {
            self.retire(old);
        }
        self.cleanup(finished);
    }

    fn cleanup(&mut self, finished: &mut Finished) {
        let until = Instant::now() + STOP_GRACE;
        while self
            .retired
            .iter()
            .any(|r| r.root.as_ref().is_some_and(RetiredRoot::in_use))
            && Instant::now() < until
        {
            std::thread::sleep(Duration::from_millis(20));
        }
        self.retired.clear();
        // Release the lock first: a held file can't be removed everywhere.
        self.lock = None;
        if let Err(e) = fs::remove_dir_all(&self.staging)
            && e.kind() != io::ErrorKind::NotFound
        {
            finished.problems.push(Problem::warning(
                brrrty_core::codes::SERVE_STAGING_LEFT,
                format!(
                    "could not remove the staging folder {}: {e}",
                    self.staging.display()
                ),
            ));
        }
        // Folders the session made and no build filled (the first build failed and none
        // followed): 11ty leaves no output folder then either.
        if let Some(made) = &self.made {
            for dir in self.output.ancestors() {
                if !dir.starts_with(made) || fs::remove_dir(dir).is_err() {
                    break;
                }
            }
        }
    }

    /// Give each refused page that the build being served had the file it had there, in the
    /// new build `dir`, unless the new build wrote something at that path. Gives the pages
    /// kept.
    fn keep_refused(
        &mut self,
        dir: &Path,
        refused: &[PathBuf],
        written: &[brrrty_core::WrittenPage],
    ) -> Vec<brrrty_core::WrittenPage> {
        let Some(cur) = &self.current else {
            return Vec::new();
        };
        let mut kept = Vec::new();
        for page in &self.served_pages {
            if !refused.contains(&page.source) || written.iter().any(|w| w.output == page.output) {
                continue;
            }
            let to = dir.join(&page.output);
            if fs::symlink_metadata(&to).is_ok() {
                // A passthrough file took the path.
                continue;
            }
            let from = cur.join(&page.output);
            let copied = (|| {
                if let Some(parent) = to.parent() {
                    fs::create_dir_all(parent)?;
                }
                // Build folders are never written to once made, so a link is as good as a copy.
                fs::hard_link(&from, &to).or_else(|_| fs::copy(&from, &to).map(drop))
            })();
            if copied.is_ok() {
                kept.push(page.clone());
            }
        }
        kept
    }

    /// Bring the output folder up to date with the files `entries` lists in the build in
    /// `build`, printing what went wrong. A file the session put there itself, still as it
    /// left it, and of the same version, is not looked at again.
    fn mirror(&mut self, build: &Path, entries: Vec<Entry>) {
        let mut problems = Vec::new();
        let mut problem = |message: String| {
            problems.push(Problem::warning(brrrty_core::codes::WRITE_FAILED, message));
        };
        if entries.iter().any(|e| {
            e.rel.components().next() == Some(std::path::Component::Normal(STAGING_DIR.as_ref()))
        }) {
            // `<output>/.brrrty-serve` is the staging folder: the site's own file by that
            // name is left out, and must never be shipped.
            problem(format!(
                "the site wrote {STAGING_DIR}, which is Brrrty's staging folder, so it was left out of {}",
                self.output.display()
            ));
        }
        for e in &entries {
            if e.rel.components().next() == Some(std::path::Component::Normal(STAGING_DIR.as_ref()))
            {
                continue;
            }
            let src = build.join(&e.rel);
            let dst = self.output.join(&e.rel);
            let existing = fs::symlink_metadata(&dst).ok();
            let as_left = |r: &Record, m: &fs::Metadata| {
                m.is_file() && r.len == m.len() && r.modified == m.modified().ok()
            };
            match (self.mirrored.get(&e.rel), &existing) {
                (Some(r), Some(m)) if as_left(r, m) && r.version == e.version => continue,
                (Some(r), Some(m)) if as_left(r, m) => {}
                // Not known: compare once, so unchanged files keep their times.
                (_, Some(m)) if m.is_file() && same_bytes(&src, &dst) => {
                    self.record(e, &dst);
                    continue;
                }
                _ => {}
            }
            self.next_tmp += 1;
            let tmp = self.staging.join(format!("copy-{}", self.next_tmp));
            let copied = (|| {
                make_parents(&self.output, &e.rel)?;
                fs::copy(&src, &tmp)?;
                if existing.as_ref().is_some_and(|m| m.is_dir()) {
                    fs::remove_dir_all(&dst)?;
                }
                fs::rename(&tmp, &dst)
            })();
            match copied {
                Ok(()) => self.record(e, &dst),
                Err(err) => {
                    let _ = fs::remove_file(&tmp);
                    self.mirrored.remove(&e.rel);
                    problem(format!("cannot write {}: {err}", dst.display()));
                }
            }
        }
        self.last_entries = entries;
        self.mirror_problems = problems;
        if !self.mirror_problems.is_empty() {
            report::print_problems(&self.mirror_problems, &self.root, self.settings.color);
        }
    }

    fn record(&mut self, e: &Entry, dst: &Path) {
        match fs::symlink_metadata(dst) {
            Ok(m) => {
                self.mirrored.insert(
                    e.rel.clone(),
                    Record {
                        version: e.version.clone(),
                        len: m.len(),
                        modified: m.modified().ok(),
                    },
                );
            }
            Err(_) => {
                self.mirrored.remove(&e.rel);
            }
        }
    }
}

/// What identifies a file's version without reading it: its size and times, and on Unix its
/// inode and change time, which any write, rename or `touch` moves.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Stamp {
    len: u64,
    modified: Option<std::time::SystemTime>,
    #[cfg(unix)]
    changed: (i64, i64, u64),
}

impl Stamp {
    fn of(p: &Path) -> Option<Stamp> {
        let m = fs::metadata(p).ok().filter(|m| m.is_file())?;
        #[cfg(unix)]
        let changed = {
            use std::os::unix::fs::MetadataExt;
            (m.ctime(), m.ctime_nsec(), m.ino())
        };
        Some(Stamp {
            len: m.len(),
            modified: m.modified().ok(),
            #[cfg(unix)]
            changed,
        })
    }
}

/// Whether an event for a passthrough file is the echo of the last build copying it: the file
/// looks as it did just before that build (`now` and `at_build`), and either it looked the
/// same before the build before (`before`; `None` when there was none) or its change was
/// among the files that started the last build (`announced`).
fn is_echo(
    now: Option<&Stamp>,
    at_build: Option<&Stamp>,
    before: Option<Option<&Stamp>>,
    announced: bool,
) -> bool {
    let Some(at_build) = at_build else {
        return false;
    };
    if now != Some(at_build) {
        return false;
    }
    match before {
        None => true,
        Some(b) => b == Some(at_build) || announced,
    }
}

/// The stamps of every file at or under `paths`. Symlinked folders are not followed, as the
/// passthrough copy doesn't follow them, so a link back up the tree can't loop; a symlinked
/// file is stamped by its target.
fn stamps(paths: &[PathBuf]) -> HashMap<PathBuf, Stamp> {
    let mut out = HashMap::new();
    let mut todo: Vec<PathBuf> = paths.to_vec();
    while let Some(p) = todo.pop() {
        let Ok(meta) = fs::symlink_metadata(&p) else {
            continue;
        };
        if meta.is_dir() {
            if let Ok(entries) = fs::read_dir(&p) {
                todo.extend(entries.flatten().map(|e| e.path()));
            }
        } else if let Some(s) = Stamp::of(&p) {
            // A file, or a symlink to one; a symlink to a folder has no stamp.
            out.insert(p, s);
        }
    }
    out
}

/// The stamp `stamps` took for the passthrough file at `src`. `stamps` walks the resolved
/// passthrough roots, so its keys sit under real folders: look `src` up as written, then with
/// its folder resolved, then resolved whole (a passthrough entry that is a symlink to a file).
fn stamp_of<'a>(stamps: &'a HashMap<PathBuf, Stamp>, src: &Path) -> Option<&'a Stamp> {
    if let Some(s) = stamps.get(src) {
        return Some(s);
    }
    let in_real_dir = src
        .parent()
        .zip(src.file_name())
        .and_then(|(dir, name)| dir.canonicalize().ok().map(|d| d.join(name)));
    in_real_dir
        .and_then(|p| stamps.get(&p))
        .or_else(|| src.canonicalize().ok().and_then(|p| stamps.get(&p)))
}

/// A path relative to the project folder, for printing as 11ty does (`./src/index.md`). The
/// project folder itself is `.` (the watcher lost track of an input folder that is the
/// project folder). A path outside the project folder is printed whole.
fn shown(rel: &Path) -> String {
    if rel.as_os_str().is_empty() {
        ".".to_string()
    } else if rel.is_absolute() {
        rel.display().to_string()
    } else {
        format!("./{}", rel.display())
    }
}

/// The URL of a file at `rel` in the output folder.
fn url_of(rel: &Path) -> String {
    let parts: Vec<String> = rel
        .components()
        .map(|c| c.as_os_str().to_string_lossy().into_owned())
        .collect();
    format!("/{}", parts.join("/"))
}

fn remove_any(p: &Path) -> io::Result<()> {
    if fs::symlink_metadata(p)?.is_dir() {
        fs::remove_dir_all(p)
    } else {
        fs::remove_file(p)
    }
}

/// Make the folders `rel` goes in, under `output`. A file or symlink where a folder belongs
/// is replaced, never followed, so nothing lands outside `output`.
fn make_parents(output: &Path, rel: &Path) -> io::Result<()> {
    let mut at = output.to_path_buf();
    let Some(parent) = rel.parent() else {
        return Ok(());
    };
    for c in parent.components() {
        at.push(c);
        match fs::symlink_metadata(&at) {
            Ok(m) if m.is_dir() => continue,
            Ok(_) => {
                fs::remove_file(&at)?;
                fs::create_dir(&at)?;
            }
            Err(e) if e.kind() == io::ErrorKind::NotFound => fs::create_dir(&at)?,
            Err(e) => return Err(e),
        }
    }
    Ok(())
}

/// Whether two regular files hold the same bytes.
fn same_bytes(a: &Path, b: &Path) -> bool {
    let (Ok(ma), Ok(mb)) = (fs::metadata(a), fs::metadata(b)) else {
        return false;
    };
    if ma.len() != mb.len() {
        return false;
    }
    matches!((fs::read(a), fs::read(b)), (Ok(x), Ok(y)) if x == y)
}

/// The page `brrrty serve` shows for every path while `brrrty init`'s TODO markers are left.
fn unported_page(p: &Problem) -> String {
    let esc = |s: &str| {
        s.replace('&', "&amp;")
            .replace('<', "&lt;")
            .replace('>', "&gt;")
            .replace('"', "&quot;")
    };
    let at = match (&p.path, p.line) {
        (Some(path), Some(line)) => format!("{}:{line}", path.display()),
        (Some(path), None) => path.display().to_string(),
        _ => String::new(),
    };
    format!(
        "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Brrrty: config not ported yet</title>\n</head>\n<body>\n<h1>No pages yet: the config isn't fully ported</h1>\n<p><code>{}</code> ({}): {}</p>\n<p>{}</p>\n</body>\n</html>\n",
        esc(p.code),
        esc(&at),
        esc(&p.message),
        esc(p.help.as_deref().unwrap_or_default())
    )
}

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

    fn stamp(len: u64) -> Stamp {
        Stamp {
            len,
            modified: None,
            #[cfg(unix)]
            changed: (0, 0, 0),
        }
    }

    #[test]
    fn the_server_hides_the_staging_folder() {
        assert_eq!(STAGING_DIR, brrrty_serve::HIDDEN_DIR);
    }

    #[test]
    fn changed_paths_print_as_11ty_prints_them() {
        assert_eq!(shown(Path::new("src/index.md")), "./src/index.md");
        // The input folder is the project folder and the watcher lost track of it.
        assert_eq!(shown(Path::new("")), ".");
        // A passthrough source outside the project folder.
        assert_eq!(shown(Path::new("/elsewhere/x.css")), "/elsewhere/x.css");
    }

    #[test]
    fn echoes_and_edits_are_told_apart() {
        let (a, b) = (stamp(1), stamp(2));
        // After the first build: unchanged is an echo, changed or gone is an edit.
        assert!(is_echo(Some(&a), Some(&a), None, false));
        assert!(!is_echo(Some(&b), Some(&a), None, false));
        assert!(!is_echo(None, Some(&a), None, false));
        // A file that isn't a passthrough file is never an echo.
        assert!(!is_echo(Some(&a), None, None, false));
        // Unchanged across two builds: an echo.
        assert!(is_echo(Some(&a), Some(&a), Some(Some(&a)), false));
        // Changed between the two builds and announced by the last one: an echo.
        assert!(is_echo(Some(&b), Some(&b), Some(Some(&a)), true));
        // Changed between them but not announced: edited after the debounce fired and
        // before the stamps were taken, so the event is the edit's own.
        assert!(!is_echo(Some(&b), Some(&b), Some(Some(&a)), false));
        assert!(!is_echo(Some(&b), Some(&b), Some(None), false));
    }
}