Commit c695abbf59
Verified · cmc ci/pages: success ci/test: success
Layout: unified · split
src/watch.rs +29 −3
| @@ -4,14 +4,17 @@ | ||
| 4 | 4 | //! compare mtimes. Native events cost nothing while nothing happens, and arrive in |
| 5 | 5 | //! milliseconds when something does. |
| 6 | 6 | //! |
| 7 | //! Two things matter more than the watching itself: | |
| 7 | //! Three things matter more than the watching itself: | |
| 8 | 8 | //! |
| 9 | 9 | //! 1. **Not watching our own output.** `orgo watch . -o _site` puts the output inside |
| 10 | 10 | //! the source. Rebuilding writes files, writing files raises events, and events |
| 11 | 11 | //! trigger a rebuild — a loop that never stops and never idles. [`ChangeFilter`] is |
| 12 | 12 | //! what prevents it, and it is a pure function precisely so it can be tested without |
| 13 | 13 | //! 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 | |
| 15 | 18 | //! temp file, rename it over the original, and touch the directory. Rebuilding per |
| 16 | 19 | //! event would rebuild several times per save. |
| 17 | 20 | |
| @@ -20,7 +23,8 @@ use std::time::Duration; | ||
| 20 | 23 | |
| 21 | 24 | use anyhow::{Context, Result}; |
| 22 | 25 | use camino::{Utf8Path, Utf8PathBuf}; |
| 23 | use notify::{Config as NotifyConfig, RecursiveMode, Watcher}; | |
| 26 | use notify::event::{AccessKind, AccessMode}; | |
| 27 | use notify::{Config as NotifyConfig, EventKind, RecursiveMode, Watcher}; | |
| 24 | 28 | |
| 25 | 29 | use crate::site::{build_site, BuildOptions}; |
| 26 | 30 | |
| @@ -256,12 +260,34 @@ fn summarize(changed: &[Utf8PathBuf]) -> String { | ||
| 256 | 260 | } |
| 257 | 261 | } |
| 258 | 262 | |
| 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. | |
| 275 | pub fn is_change(kind: &EventKind) -> bool { | |
| 276 | match kind { | |
| 277 | EventKind::Access(access) => matches!(access, AccessKind::Close(AccessMode::Write)), | |
| 278 | _ => true, | |
| 279 | } | |
| 280 | } | |
| 281 | ||
| 259 | 282 | /// The platform's native watcher, falling back to polling where that is unavailable — |
| 260 | 283 | /// some network and container filesystems have no event API, and `watch` failing outright |
| 261 | 284 | /// there would be worse than being slow. |
| 262 | 285 | fn make_watcher(tx: mpsc::Sender<Vec<Utf8PathBuf>>) -> Result<Box<dyn Watcher>> { |
| 263 | 286 | let handler = move |result: notify::Result<notify::Event>| { |
| 264 | 287 | if let Ok(event) = result { |
| 288 | if !is_change(&event.kind) { | |
| 289 | return; | |
| 290 | } | |
| 265 | 291 | let paths: Vec<Utf8PathBuf> = event |
| 266 | 292 | .paths |
| 267 | 293 | .into_iter() |
tests/watch.rs +38
| @@ -129,6 +129,44 @@ fn absolute_event_paths_are_reduced_to_a_sorted_unique_set() { | ||
| 129 | 129 | ); |
| 130 | 130 | } |
| 131 | 131 | |
| 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] | |
| 139 | fn 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 | ||
| 132 | 170 | // --------------------------------------------------------------------------- |
| 133 | 171 | // End to end |
| 134 | 172 | // --------------------------------------------------------------------------- |