watchexec 8.4.0

Library to execute commands in response to file modifications
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
//! Configuration and builders for [`crate::Watchexec`].

use std::{future::Future, time::Duration};

use tokio::sync::watch;
use tracing::{debug, trace};

use crate::{
	action::{ActionHandler, ActionReturn},
	changeable::{Changeable, ChangeableFn},
	filter::{ChangeableFilterer, Filterer},
	sources::fs::{WatchedPath, Watcher},
	ErrorHook,
};

/// Configuration for [`Watchexec`][crate::Watchexec].
///
/// Almost every field is a [`Changeable`], such that its value can be changed from a `&self`.
///
/// Fields are public for advanced use, but in most cases changes should be made through the
/// methods provided: not only are they more convenient, each calls `debug!` on the new value,
/// providing a quick insight into what your application sets.
///
/// The methods also set the "change signal" of the Config: this notifies some parts of Watchexec
/// they should re-read the config. If you modify values via the fields directly, you should call
/// `signal_change()` yourself. Note that this doesn't mean that changing values _without_ calling
/// this will prevent Watchexec changing until it's called: most parts of Watchexec take a
/// "just-in-time" approach and read a config item immediately before it's needed, every time it's
/// needed, and thus don't need to listen for the change signal.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct Config {
	/// This monotonic revision is incremented by the change methods whenever they're called, and
	/// notifies Watchexec that it should read the configuration again.
	pub(crate) change_signal: watch::Sender<u64>,

	/// The main handler to define: what to do when an action is triggered.
	///
	/// This handler is called with the [`Action`] environment, look at its doc for more detail.
	///
	/// If this handler is not provided, or does nothing, Watchexec in turn will do nothing, not
	/// even quit. Hence, you really need to provide a handler. This is enforced when using
	/// [`Watchexec::new()`], but not when using [`Watchexec::default()`].
	///
	/// It is possible to change the handler or any other configuration inside the previous handler.
	/// This and other handlers are fetched "just in time" when needed, so changes to handlers can
	/// appear instant, or may lag a little depending on lock contention, but a handler being called
	/// does not hold its lock. A handler changing while it's being called doesn't affect the run of
	/// a previous version of the handler: it will neither be stopped nor retried with the new code.
	///
	/// It is important for this handler to return quickly: avoid performing blocking work in it.
	/// This is true for all handlers, but especially for this one, as it will block the event loop
	/// and you'll find that the internal event queues quickly fill up and it all grinds to a halt.
	/// Spawn threads or tasks, or use channels or other async primitives to communicate with your
	/// expensive code.
	pub action_handler: ChangeableFn<ActionHandler, ActionReturn>,

	/// Runtime error handler.
	///
	/// This is run on every runtime error that occurs within Watchexec. The default handler
	/// is a no-op.
	///
	/// # Examples
	///
	/// Set the error handler:
	///
	/// ```
	/// # use watchexec::{config::Config, ErrorHook};
	/// let mut config = Config::default();
	/// config.on_error(|err: ErrorHook| {
	///     tracing::error!("{}", err.error);
	/// });
	/// ```
	///
	/// Output a critical error (which will terminate Watchexec):
	///
	/// ```
	/// # use watchexec::{config::Config, ErrorHook, error::{CriticalError, RuntimeError}};
	/// let mut config = Config::default();
	/// config.on_error(|err: ErrorHook| {
	///     tracing::error!("{}", err.error);
	///
	///     if matches!(err.error, RuntimeError::FsWatcher { .. }) {
	///         err.critical(CriticalError::External("fs watcher failed".into()));
	///     }
	/// });
	/// ```
	///
	/// Elevate a runtime error to critical (will preserve the error information):
	///
	/// ```
	/// # use watchexec::{config::Config, ErrorHook, error::RuntimeError};
	/// let mut config = Config::default();
	/// config.on_error(|err: ErrorHook| {
	///     tracing::error!("{}", err.error);
	///
	///     if matches!(err.error, RuntimeError::FsWatcher { .. }) {
	///            err.elevate();
	///     }
	/// });
	/// ```
	///
	/// It is important for this to return quickly: avoid performing blocking work. Locking and
	/// writing to stdio is fine, but waiting on the network is a bad idea. Of course, an
	/// asynchronous log writer or separate UI thread is always a better idea than `println!` if
	/// have that ability.
	pub error_handler: ChangeableFn<ErrorHook, ()>,

	/// The set of filesystem paths to be watched.
	///
	/// If this is non-empty, the filesystem event source is started and configured to provide
	/// events for these paths. If it becomes empty, the filesystem event source is shut down.
	///
	/// Watched paths are themselves never filtered.
	pub pathset: Changeable<Vec<WatchedPath>>,

	/// The kind of filesystem watcher to be used.
	pub file_watcher: Changeable<Watcher>,

	/// Whether to follow directory symlinks when watching paths.
	///
	/// When enabled, directory symlink targets are included in recursive watches where supported.
	/// Native macOS filesystem watching does not follow directory symlinks outside the watched
	/// hierarchy.
	pub follow_symlinks: Changeable<bool>,

	/// Listen for Unix job-control signals (`SIGTSTP` and `SIGCONT`).
	///
	/// This is disabled by default because installing a `SIGTSTP` listener suppresses the operating
	/// system's default suspend behaviour. Applications which enable this must suspend themselves
	/// after handling the emitted [`Signal::TerminalSuspend`](watchexec_signals::Signal) event.
	///
	/// This has no effect on non-Unix platforms. It is unchangeable at runtime and must be set
	/// before Watchexec instantiation because Unix signal dispositions cannot be restored after a
	/// Tokio signal listener is installed.
	pub signal_job_control: bool,

	/// Watch stdin and emit events when input comes in over the keyboard.
	///
	/// If this is true, the keyboard event source is started and stdin is switched to raw mode
	/// (disabling line buffering). Individual key events are emitted, as well as EOF. If it
	/// becomes false, the keyboard event source is shut down, cooked mode is restored, and stdin
	/// may flow to commands again.
	///
	/// This requires a TTY and is opt-in.
	pub keyboard_events: Changeable<bool>,

	/// How long to wait for events to build up before executing an action.
	///
	/// This is sometimes called "debouncing." We debounce on the trailing edge: an action is
	/// triggered only after that amount of time has passed since the first event in the cycle. The
	/// action is called with all the collected events in the cycle.
	///
	/// Default is 50ms.
	pub throttle: Changeable<Duration>,

	/// The filterer implementation used for event and source-directory filtering.
	///
	/// The default is a no-op, which passes every event and directory.
	pub filterer: ChangeableFilterer,

	/// The buffer size of the channel which carries runtime errors.
	///
	/// The default (64) is usually fine. If you expect a much larger throughput of runtime errors,
	/// or if your `error_handler` is slow, adjusting this value may help.
	///
	/// This is unchangeable at runtime and must be set before Watchexec instantiation.
	pub error_channel_size: usize,

	/// The buffer size of the channel which carries events.
	///
	/// The default (4096) is usually fine. If you expect a much larger throughput of events,
	/// adjusting this value may help.
	///
	/// This is unchangeable at runtime and must be set before Watchexec instantiation.
	pub event_channel_size: usize,

	/// Signalled by the filesystem worker after it settles reconciliation for an observed config
	/// revision. Subscribe via [`Config::fs_ready()`] before changing filesystem configuration to
	/// avoid missing or misattributing the notification.
	pub(crate) fs_ready: watch::Sender<()>,
}

impl Default for Config {
	fn default() -> Self {
		Self {
			change_signal: watch::channel(0).0,
			action_handler: ChangeableFn::new(ActionReturn::Sync),
			error_handler: Default::default(),
			pathset: Default::default(),
			file_watcher: Default::default(),
			follow_symlinks: Changeable::new(true),
			signal_job_control: false,
			keyboard_events: Default::default(),
			throttle: Changeable::new(Duration::from_millis(50)),
			filterer: Default::default(),
			error_channel_size: 64,
			event_channel_size: 4096,
			fs_ready: watch::channel(()).0,
		}
	}
}

impl Config {
	/// Signal that the configuration has changed.
	///
	/// This is called automatically by all other methods here, so most of the time calling this
	/// isn't needed, but it can be useful for some advanced uses.
	#[allow(
		clippy::must_use_candidate,
		reason = "this return can explicitly be ignored"
	)]
	pub fn signal_change(&self) -> &Self {
		self.change_signal.send_modify(|revision| {
			*revision = revision
				.checked_add(1)
				.expect("configuration revision overflow");
		});
		self
	}

	/// Watch the config for a change, but run once first.
	///
	/// This returns a Stream where the first value is available immediately, and then every
	/// subsequent one is from a change signal for this Config.
	#[must_use]
	pub(crate) fn watch(&self) -> ConfigWatched {
		ConfigWatched::new(self.change_signal.subscribe())
	}

	/// Subscribe to filesystem worker readiness notifications.
	///
	/// The receiver is notified after the filesystem worker finishes applying the latest observed
	/// configuration.
	///
	/// Path-specific failures are reported through the error handler and do not prevent readiness.
	///
	/// Notifications carry no configuration revision and may be coalesced. To wait for a change,
	/// subscribe before making it, then call `.changed().await`.
	#[must_use]
	pub fn fs_ready(&self) -> watch::Receiver<()> {
		self.fs_ready.subscribe()
	}

	/// Set the pathset to be watched.
	pub fn pathset<I, P>(&self, pathset: I) -> &Self
	where
		I: IntoIterator<Item = P>,
		P: Into<WatchedPath>,
	{
		let pathset = pathset.into_iter().map(std::convert::Into::into).collect();
		debug!(?pathset, "Config: pathset");
		self.pathset.replace(pathset);
		self.signal_change()
	}

	/// Set the file watcher type to use.
	pub fn file_watcher(&self, watcher: Watcher) -> &Self {
		debug!(?watcher, "Config: file watcher");
		self.file_watcher.replace(watcher);
		self.signal_change()
	}

	/// Set whether symlinks are followed when watching paths.
	pub fn follow_symlinks(&self, follow: bool) -> &Self {
		debug!(?follow, "Config: follow symlinks");
		self.follow_symlinks.replace(follow);
		self.signal_change()
	}

	/// Enable keyboard/stdin event source.
	pub fn keyboard_events(&self, enable: bool) -> &Self {
		debug!(?enable, "Config: keyboard");
		self.keyboard_events.replace(enable);
		self.signal_change()
	}

	/// Set the throttle.
	pub fn throttle(&self, throttle: impl Into<Duration>) -> &Self {
		let throttle = throttle.into();
		debug!(?throttle, "Config: throttle");
		self.throttle.replace(throttle);
		self.signal_change()
	}

	/// Set the filterer implementation to use.
	pub fn filterer(&self, filterer: impl Filterer + 'static) -> &Self {
		debug!(?filterer, "Config: filterer");
		self.filterer.replace(filterer);
		self.signal_change()
	}

	/// Set the runtime error handler.
	pub fn on_error(&self, handler: impl Fn(ErrorHook) + Send + Sync + 'static) -> &Self {
		debug!("Config: on_error");
		self.error_handler.replace(handler);
		self.signal_change()
	}

	/// Set the action handler.
	pub fn on_action(
		&self,
		handler: impl (Fn(ActionHandler) -> ActionHandler) + Send + Sync + 'static,
	) -> &Self {
		debug!("Config: on_action");
		self.action_handler
			.replace(move |action| ActionReturn::Sync(handler(action)));
		self.signal_change()
	}

	/// Set the action handler to a future-returning closure.
	pub fn on_action_async(
		&self,
		handler: impl (Fn(ActionHandler) -> Box<dyn Future<Output = ActionHandler> + Send + Sync>)
			+ Send
			+ Sync
			+ 'static,
	) -> &Self {
		debug!("Config: on_action_async");
		self.action_handler
			.replace(move |action| ActionReturn::Async(handler(action)));
		self.signal_change()
	}
}

#[derive(Debug)]
pub(crate) struct ConfigWatched {
	first_run: bool,
	revision: watch::Receiver<u64>,
}

impl ConfigWatched {
	const fn new(revision: watch::Receiver<u64>) -> Self {
		Self {
			first_run: true,
			revision,
		}
	}

	pub async fn next(&mut self) -> u64 {
		if self.first_run {
			let revision = *self.revision.borrow_and_update();
			trace!(revision, "ConfigWatched: first run");
			self.first_run = false;
			revision
		} else {
			trace!("ConfigWatched: waiting for change");
			self.revision
				.changed()
				.await
				.expect("configuration change sender dropped");
			let revision = *self.revision.borrow_and_update();
			trace!(revision, "ConfigWatched: changed");
			revision
		}
	}

	/// Whether a revision is already waiting to be observed.
	///
	/// Filesystem recursion uses this before every bounded state-machine step so
	/// an obsolete reconciliation cannot advance into its destructive sweep.
	pub fn pending(&self) -> bool {
		self.first_run
			|| self
				.revision
				.has_changed()
				.expect("configuration change sender dropped")
	}
}

#[cfg(test)]
mod tests {
	use futures::FutureExt as _;

	use super::Config;

	#[test]
	fn config_watch_first_run_is_immediate() {
		let config = Config::default();
		let mut watched = config.watch();

		assert_eq!(watched.next().now_or_never(), Some(0));
	}

	#[test]
	fn config_watch_waits_after_first_run() {
		let config = Config::default();
		let mut watched = config.watch();

		assert!(watched.next().now_or_never().is_some());
		assert!(watched.next().now_or_never().is_none());
	}

	#[test]
	fn config_watch_observes_change_between_calls() {
		let config = Config::default();
		let mut watched = config.watch();

		assert_eq!(watched.next().now_or_never(), Some(0));
		config.signal_change();
		assert_eq!(watched.next().now_or_never(), Some(1));
	}
}