watchexec-cli 2.8.0

Executes 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
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
use std::{
	collections::HashSet,
	io,
	path::{Path, PathBuf},
};

use ignore_files::{IgnoreFile, IgnoreFilesFromOriginArgs};
use miette::{miette, IntoDiagnostic, Result};
use project_origins::ProjectType;
use tracing::{debug, info, warn};
use watchexec::paths::common_prefix;

use crate::args::{command::CommandArgs, filtering::FilteringArgs, Args};

pub async fn project_origin(
	FilteringArgs {
		project_origin,
		recursive_paths,
		non_recursive_paths,
		..
	}: &FilteringArgs,
	CommandArgs { workdir, .. }: &CommandArgs,
) -> Result<PathBuf> {
	let project_origin = if let Some(origin) = project_origin {
		debug!(?origin, "project origin override");
		canonicalize(origin).await.into_diagnostic()?
	} else {
		let homedir = match dirs::home_dir() {
			None => None,
			Some(dir) => Some(canonicalize(dir).await.into_diagnostic()?),
		};
		debug!(?homedir, "home directory");

		// The watched paths aren't resolved yet at this point (that needs the origin), so do it
		// here against the workdir, without canonicalising, so that missing paths aren't fatal.
		let paths = watch_candidates(recursive_paths, non_recursive_paths, workdir.as_deref());
		debug!(?paths, "candidate paths for origin discovery");

		let homedir_requested = homedir
			.as_ref()
			.map_or(false, |home| paths.iter().any(|path| path == home));
		debug!(
			?homedir_requested,
			"resolved whether the homedir is explicitly requested"
		);

		let mut origins = HashSet::new();
		for path in &paths {
			origins.extend(project_origins::origins(path).await);
		}

		match (homedir, homedir_requested) {
			(Some(ref dir), false) if origins.contains(dir) => {
				debug!("removing homedir from origins");
				origins.remove(dir);
			}
			_ => {}
		}

		if origins.is_empty() {
			debug!("no origins, using current directory");
			origins.insert(workdir.clone().unwrap());
		}

		debug!(?origins, "resolved all project origins");

		canonicalize(
			common_prefix(&origins)
				.ok_or_else(|| miette!("no common prefix, but this should never fail"))?,
		)
		.await
		.into_diagnostic()?
	};
	debug!(?project_origin, "resolved common/project origin");

	Ok(project_origin)
}

/// `dunce::canonicalize` only strips the `\\?\` prefix off verbatim *disk* paths, so canonicalising
/// inside a network share yields `\\?\UNC\server\share\...`, which many programs (notably the Go
/// toolchain) refuse as a working directory. In that case keep the uncanonicalised path, as long as
/// it isn't verbatim itself.
fn prefer_non_verbatim(canonical: PathBuf, original: PathBuf) -> PathBuf {
	if is_verbatim(&canonical) && !is_verbatim(&original) {
		original
	} else {
		canonical
	}
}

fn is_verbatim(path: &Path) -> bool {
	path.as_os_str().as_encoded_bytes().starts_with(br"\\?\")
}

/// Canonicalises without leaving a verbatim `\\?\C:\...` path behind, which is what
/// `tokio::fs::canonicalize` returns on Windows and what the rest of the CLI avoids.
pub(crate) async fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> {
	let path = path.as_ref();
	let canonical = tokio::fs::canonicalize(path).await?;
	Ok(prefer_non_verbatim(
		dunce::simplified(&canonical).to_owned(),
		path.to_owned(),
	))
}

/// Resolves the paths given with `-w` / `-W` against the workdir, for use as starting points of
/// origin discovery. Relative paths are joined onto the workdir; the `/dev/null` sentinel (which
/// means "watch nothing") is skipped. If nothing is left, the workdir itself is the only candidate.
fn watch_candidates(
	recursive_paths: &[PathBuf],
	non_recursive_paths: &[PathBuf],
	workdir: Option<&Path>,
) -> Vec<PathBuf> {
	let mut paths: Vec<PathBuf> = recursive_paths
		.iter()
		.chain(non_recursive_paths)
		.filter(|path| path.as_path() != Path::new("/dev/null"))
		.map(|path| {
			if path.is_absolute() {
				path.clone()
			} else {
				workdir.map_or_else(|| path.clone(), |wd| wd.join(path))
			}
		})
		.collect();

	if paths.is_empty() {
		if let Some(workdir) = workdir {
			paths.push(workdir.to_owned());
		}
	}

	paths
}

pub async fn vcs_types(origin: &Path) -> Vec<ProjectType> {
	let vcs_types = project_origins::types(origin)
		.await
		.into_iter()
		.filter(|pt| pt.is_vcs())
		.collect::<Vec<_>>();
	info!(?vcs_types, "effective vcs types");
	vcs_types
}

pub async fn ignores(args: &Args, vcs_types: &[ProjectType]) -> Result<Vec<IgnoreFile>> {
	let origin = args.filtering.project_origin.clone().unwrap();
	let mut skip_git_global_excludes = false;

	let mut ignores = if args.filtering.no_project_ignore {
		Vec::new()
	} else {
		let ignore_files = args.filtering.ignore_files.iter().map(|path| {
			if path.is_absolute() {
				path.into()
			} else {
				origin.join(path)
			}
		});

		let (mut ignores, errors) = ignore_files::from_origin(
			IgnoreFilesFromOriginArgs::new_unchecked(
				&origin,
				args.filtering.paths.iter().map(PathBuf::from),
				ignore_files,
			)
			.canonicalise()
			.await
			.into_diagnostic()?,
		)
		.await;

		for err in errors {
			warn!("while discovering project-local ignore files: {}", err);
		}
		debug!(?ignores, "discovered ignore files from project origin");

		if !vcs_types.is_empty() {
			ignores = ignores
				.into_iter()
				.filter(|ig| match ig.applies_to {
					Some(pt) if pt.is_vcs() => vcs_types.contains(&pt),
					_ => true,
				})
				.inspect(|ig| {
					if let IgnoreFile {
						applies_to: Some(ProjectType::Git),
						applies_in: None,
						..
					} = ig
					{
						warn!("project git config overrides the global excludes");
						skip_git_global_excludes = true;
					}
				})
				.collect::<Vec<_>>();
			debug!(?ignores, "filtered ignores to only those for project vcs");
		}

		ignores
	};

	let global_ignores = if args.filtering.no_global_ignore {
		Vec::new()
	} else {
		let (mut global_ignores, errors) = ignore_files::from_environment(Some("watchexec")).await;
		for err in errors {
			warn!("while discovering global ignore files: {}", err);
		}
		debug!(?global_ignores, "discovered ignore files from environment");

		if skip_git_global_excludes {
			global_ignores = global_ignores
				.into_iter()
				.filter(|gig| {
					!matches!(
						gig,
						IgnoreFile {
							applies_to: Some(ProjectType::Git),
							applies_in: None,
							..
						}
					)
				})
				.collect::<Vec<_>>();
			debug!(
				?global_ignores,
				"filtered global ignores to exclude global git ignores"
			);
		}

		global_ignores
	};

	ignores.extend(global_ignores.into_iter().filter(|ig| match ig.applies_to {
		Some(pt) if pt.is_vcs() => vcs_types.contains(&pt),
		_ => true,
	}));
	debug!(
		?ignores,
		?vcs_types,
		"combined and applied overall vcs filter over ignores"
	);

	ignores.extend(args.filtering.ignore_files.iter().map(|ig| IgnoreFile {
		applies_to: None,
		applies_in: None,
		path: ig.clone(),
	}));
	debug!(
		?ignores,
		?args.filtering.ignore_files,
		"combined with ignore files from command line / env"
	);

	if args.filtering.no_project_ignore {
		ignores = ignores
			.into_iter()
			.filter(|ig| {
				!ig.applies_in
					.as_ref()
					.map_or(false, |p| p.starts_with(&origin))
			})
			.collect::<Vec<_>>();
		debug!(
			?ignores,
			"filtered ignores to exclude project-local ignores"
		);
	}

	if args.filtering.no_global_ignore {
		ignores = ignores
			.into_iter()
			.filter(|ig| ig.applies_in.is_some())
			.collect::<Vec<_>>();
		debug!(?ignores, "filtered ignores to exclude global ignores");
	}

	if args.filtering.no_vcs_ignore {
		ignores = ignores
			.into_iter()
			.filter(|ig| ig.applies_to.is_none())
			.collect::<Vec<_>>();
		debug!(?ignores, "filtered ignores to exclude VCS-specific ignores");
	}

	info!(files=?ignores.iter().map(|ig| ig.path.as_path()).collect::<Vec<_>>(), "found some ignores");
	Ok(ignores)
}

#[cfg(test)]
mod tests {
	use clap::Parser;

	use super::*;
	use crate::args::Args;

	fn args_in(workdir: &Path, extra: &[&str]) -> Args {
		let mut args = Args::parse_from(
			std::iter::once("watchexec")
				.chain(extra.iter().copied())
				.chain(std::iter::once("true")),
		);
		args.command.workdir = Some(workdir.to_owned());
		args
	}

	async fn origin_in(workdir: &Path, extra: &[&str]) -> PathBuf {
		let args = args_in(workdir, extra);
		project_origin(&args.filtering, &args.command)
			.await
			.expect("origin should resolve")
	}

	/// The origin has to be discovered by walking up from the watched paths (or the workdir when
	/// there are none), so running from anywhere inside a project must find the same origin, not
	/// whichever directory happens to be current.
	#[tokio::test]
	async fn discovers_the_same_origin_from_anywhere_in_the_project() {
		let tmp = tempfile::tempdir().expect("tempdir");
		let root = tmp.path().join("project");
		let deep = root.join("deep");
		let sub = deep.join("sub");
		std::fs::create_dir_all(&sub).expect("create dirs");
		std::fs::write(root.join("Cargo.toml"), "").expect("write marker");

		let from_root = origin_in(&root, &[]).await;
		let from_deep = origin_in(&deep, &[]).await;
		let from_sub = origin_in(&sub, &[]).await;

		assert_eq!(
			from_deep, from_root,
			"origin from {deep:?} should be the same as from {root:?}"
		);
		assert_eq!(
			from_sub, from_root,
			"origin from {sub:?} should be the same as from {root:?}"
		);
		assert_ne!(
			from_sub, sub,
			"origin should not just be the current directory"
		);
	}

	/// Same, but for a path given with '-w' rather than the workdir.
	#[tokio::test]
	async fn discovers_the_origin_of_a_watched_path() {
		let tmp = tempfile::tempdir().expect("tempdir");
		let root = tmp.path().join("project");
		let sub = root.join("deep").join("sub");
		let outside = tmp.path().join("outside");
		std::fs::create_dir_all(&sub).expect("create dirs");
		std::fs::create_dir_all(&outside).expect("create dirs");
		std::fs::write(root.join("Cargo.toml"), "").expect("write marker");

		let watched = origin_in(&outside, &["-w", sub.to_str().expect("utf-8 path")]).await;
		assert_eq!(
			watched,
			origin_in(&root, &[]).await,
			"origin of watched {sub:?} should be the project origin"
		);
	}

	/// Only Windows canonicalises to the verbatim form, but the handling is not platform-specific.
	#[tokio::test]
	async fn canonicalize_does_not_return_a_verbatim_path() {
		let tmp = tempfile::tempdir().expect("tempdir");
		let path = canonicalize(tmp.path()).await.expect("canonicalize");
		assert!(
			!path.as_os_str().as_encoded_bytes().starts_with(br"\\?\"),
			"{path:?} is verbatim, so it cannot compare equal to a discovered origin"
		);
	}

	#[test]
	fn keeps_uncanonicalised_path_when_canonical_is_verbatim_unc() {
		assert_eq!(
			prefer_non_verbatim(
				PathBuf::from(r"\\?\UNC\Mac\my-directory"),
				PathBuf::from(r"Z:\my-directory"),
			),
			PathBuf::from(r"Z:\my-directory"),
		);
	}

	#[test]
	fn keeps_canonical_path_when_it_is_not_verbatim() {
		assert_eq!(
			prefer_non_verbatim(PathBuf::from(r"Z:\real"), PathBuf::from(r"Z:\link")),
			PathBuf::from(r"Z:\real"),
		);
	}

	#[test]
	fn keeps_canonical_path_when_original_is_verbatim_too() {
		assert_eq!(
			prefer_non_verbatim(
				PathBuf::from(r"\\?\UNC\Mac\my-directory"),
				PathBuf::from(r"\\?\UNC\Mac\other"),
			),
			PathBuf::from(r"\\?\UNC\Mac\my-directory"),
		);
	}

	#[test]
	fn watch_candidates_resolve_against_the_workdir() {
		let workdir = Path::new("/work/dir");
		assert_eq!(
			watch_candidates(
				&[PathBuf::from("rel"), PathBuf::from("/abs")],
				&[PathBuf::from("nonrec")],
				Some(workdir),
			),
			vec![
				PathBuf::from("/work/dir/rel"),
				PathBuf::from("/abs"),
				PathBuf::from("/work/dir/nonrec"),
			]
		);
	}

	#[test]
	fn watch_candidates_fall_back_to_the_workdir() {
		let workdir = Path::new("/work/dir");
		assert_eq!(
			watch_candidates(&[], &[], Some(workdir)),
			vec![PathBuf::from("/work/dir")]
		);
		assert_eq!(
			watch_candidates(&[PathBuf::from("/dev/null")], &[], Some(workdir)),
			vec![PathBuf::from("/work/dir")],
			"/dev/null means watch nothing, so it isn't a discovery starting point"
		);
	}
}