Commit c695abbf59

c695abbf59c7fb5f63a7efc19d4042a9d7f6f49a

parent: 871cbf2008

Verified · cmc ci/pages: success ci/test: success

cmc <hello@cleberg.net> · 2026-10-18 20:02 UTC

Ignore filesystem access events in watch

inotify's watch mask includes IN_OPEN, so reading a file raises an event
on it — and a build reads every source file. Each rebuild therefore
triggered the next, as fast as the machine could build; under `orgo
serve` that reloads the browser every few milliseconds. macOS and
Windows report no access events, so only Linux was affected.

Access(Close(Write)) still counts as a change: it follows a write, and
the only files a build writes are its own output, which ChangeFilter
already drops.

Layout: unified · split

src/watch.rs +29 −3
@@ -4,14 +4,17 @@
44//! compare mtimes. Native events cost nothing while nothing happens, and arrive in
55//! milliseconds when something does.
66//!
7//! Two things matter more than the watching itself:
7//! Three things matter more than the watching itself:
88//!
99//! 1. **Not watching our own output.** `orgo watch . -o _site` puts the output inside
1010//! the source. Rebuilding writes files, writing files raises events, and events
1111//! trigger a rebuild — a loop that never stops and never idles. [`ChangeFilter`] is
1212//! what prevents it, and it is a pure function precisely so it can be tested without
1313//! a filesystem.
14//! 2. **Debouncing.** Saving a file in an editor is rarely one event: editors write a
14//! 2. **Not treating a read as a change.** A build reads every source file, and inotify
15//! reports opens. [`is_change`] is what keeps that from closing the same loop by a
16//! different route.
17//! 3. **Debouncing.** Saving a file in an editor is rarely one event: editors write a
1518//! temp file, rename it over the original, and touch the directory. Rebuilding per
1619//! event would rebuild several times per save.
1720
@@ -20,7 +23,8 @@ use std::time::Duration;
2023
2124use anyhow::{Context, Result};
2225use camino::{Utf8Path, Utf8PathBuf};
23use notify::{Config as NotifyConfig, RecursiveMode, Watcher};
26use notify::event::{AccessKind, AccessMode};
27use notify::{Config as NotifyConfig, EventKind, RecursiveMode, Watcher};
2428
2529use crate::site::{build_site, BuildOptions};
2630
@@ -256,12 +260,34 @@ fn summarize(changed: &[Utf8PathBuf]) -> String {
256260 }
257261}
258262
263/// Does this event report a change to the tree, rather than someone reading it?
264///
265/// inotify's watch mask includes `IN_OPEN`, so reading a file raises an event on it — and
266/// a build reads every source file. Forwarding those makes each rebuild the trigger for
267/// the next, at whatever rate the machine can build. macOS and Windows report no access
268/// events, which is why this only showed up on Linux.
269///
270/// `Access(Close(Write))` is the exception: it follows a write, not a read, and the only
271/// files a build writes are its own output, which [`ChangeFilter`] already drops.
272/// Everything that is not an access is kept, the backends' catch-all `Any` included —
273/// FSEvents and the Windows backend both report changes they cannot classify that way, so
274/// an allow-list of `Create`/`Modify`/`Remove` would drop real edits.
275pub fn is_change(kind: &EventKind) -> bool {
276 match kind {
277 EventKind::Access(access) => matches!(access, AccessKind::Close(AccessMode::Write)),
278 _ => true,
279 }
280}
281
259282/// The platform's native watcher, falling back to polling where that is unavailable —
260283/// some network and container filesystems have no event API, and `watch` failing outright
261284/// there would be worse than being slow.
262285fn make_watcher(tx: mpsc::Sender<Vec<Utf8PathBuf>>) -> Result<Box<dyn Watcher>> {
263286 let handler = move |result: notify::Result<notify::Event>| {
264287 if let Ok(event) = result {
288 if !is_change(&event.kind) {
289 return;
290 }
265291 let paths: Vec<Utf8PathBuf> = event
266292 .paths
267293 .into_iter()
tests/watch.rs +38
@@ -129,6 +129,44 @@ fn absolute_event_paths_are_reduced_to_a_sorted_unique_set() {
129129 );
130130}
131131
132// ---------------------------------------------------------------------------
133// Event kinds
134// ---------------------------------------------------------------------------
135
136/// A read is not a change. inotify reports opens, and a build reads every source file, so
137/// an access forwarded as a change makes each rebuild trigger the next.
138#[test]
139fn access_events_do_not_count_as_changes() {
140 use notify::event::{AccessKind, AccessMode, CreateKind, DataChange, ModifyKind, RemoveKind};
141 use notify::EventKind;
142 use orgo::watch::is_change;
143
144 for read in [
145 AccessKind::Any,
146 AccessKind::Read,
147 AccessKind::Open(AccessMode::Any),
148 AccessKind::Open(AccessMode::Read),
149 AccessKind::Close(AccessMode::Read),
150 ] {
151 assert!(
152 !is_change(&EventKind::Access(read)),
153 "{read:?} is a read, not a change"
154 );
155 }
156
157 assert!(
158 is_change(&EventKind::Access(AccessKind::Close(AccessMode::Write))),
159 "closing a file that was written to is a change"
160 );
161 assert!(is_change(&EventKind::Create(CreateKind::File)));
162 assert!(is_change(&EventKind::Modify(ModifyKind::Data(DataChange::Any))));
163 assert!(is_change(&EventKind::Remove(RemoveKind::File)));
164 assert!(
165 is_change(&EventKind::Any),
166 "FSEvents and the Windows backend report unclassified changes this way"
167 );
168}
169
132170// ---------------------------------------------------------------------------
133171// End to end
134172// ---------------------------------------------------------------------------