From fb0cbc90e356fa83e9cf153b5d6ec7c2f2069407 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Fri, 28 Oct 2022 11:13:33 -0700 Subject: [PATCH 1/5] Add support for watching additional directories --- src/cmd/watch.rs | 4 ++++ src/config.rs | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index 9336af77..f5cf37f8 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -146,6 +146,10 @@ where // Add the book.toml file to the watcher if it exists let _ = watcher.watch(book.root.join("book.toml"), NonRecursive); + for dir in &book.config.build.extra_watch_dirs { + let _ = watcher.watch(dir, Recursive); + } + info!("Listening for changes..."); loop { diff --git a/src/config.rs b/src/config.rs index b7d03d1a..7f4adc42 100644 --- a/src/config.rs +++ b/src/config.rs @@ -437,6 +437,8 @@ pub struct BuildConfig { /// Should the default preprocessors always be used when they are /// compatible with the renderer? pub use_default_preprocessors: bool, + /// Extra directories to trigger rebuild when watching/serving + pub extra_watch_dirs: Vec, } impl Default for BuildConfig { @@ -445,6 +447,7 @@ impl Default for BuildConfig { build_dir: PathBuf::from("book"), create_missing: true, use_default_preprocessors: true, + extra_watch_dirs: Vec::new(), } } } @@ -770,6 +773,7 @@ mod tests { build_dir: PathBuf::from("outputs"), create_missing: false, use_default_preprocessors: true, + extra_watch_dirs: Vec::new(), }; let rust_should_be = RustConfig { edition: None }; let playground_should_be = Playground { @@ -980,6 +984,7 @@ mod tests { build_dir: PathBuf::from("my-book"), create_missing: true, use_default_preprocessors: true, + extra_watch_dirs: Vec::new(), }; let html_should_be = HtmlConfig { From 852a882fab8961cb430845f2eaa06c7acb524db8 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Mon, 7 Nov 2022 12:20:59 -0800 Subject: [PATCH 2/5] Improve error handling of extra-watch-dirs watching, fix not watching whitelisted files outside book root --- src/cmd/watch.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index f5cf37f8..a3c9d546 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -147,7 +147,11 @@ where let _ = watcher.watch(book.root.join("book.toml"), NonRecursive); for dir in &book.config.build.extra_watch_dirs { - let _ = watcher.watch(dir, Recursive); + let path = dir.canonicalize().unwrap(); + if let Err(e) = watcher.watch(&path, Recursive) { + error!("Error while watching extra directory {path:?}:\n {e:?}"); + std::process::exit(1); + } } info!("Listening for changes..."); @@ -170,7 +174,11 @@ where }) .collect::>(); - let paths = remove_ignored_files(&book.root, &paths[..]); + // If we are watching files outside the current repository (via extra-watch-dirs), then they are definitionally + // ignored by gitignore. So we handle this case by including such files into the watched paths list. + let any_external_paths = paths.iter().filter(|p| !p.starts_with(&book.root)).cloned(); + let mut paths = remove_ignored_files(&book.root, &paths[..]); + paths.extend(any_external_paths); if !paths.is_empty() { closure(paths, &book.root); From b1ca805d2ae30e2e3f23ae3814404f3d4c27a2d9 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Mon, 7 Nov 2022 14:08:31 -0800 Subject: [PATCH 3/5] Fix tests --- tests/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/init.rs b/tests/init.rs index 4deb8401..1c3b962b 100644 --- a/tests/init.rs +++ b/tests/init.rs @@ -95,7 +95,7 @@ fn run_mdbook_init_with_custom_book_and_src_locations() { let contents = fs::read_to_string(temp.path().join("book.toml")).unwrap(); assert_eq!( contents, - "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nuse-default-preprocessors = true\n" + "[book]\nauthors = []\nlanguage = \"en\"\nmultilingual = false\nsrc = \"in\"\n\n[build]\nbuild-dir = \"out\"\ncreate-missing = true\nextra-watch-dirs = []\nuse-default-preprocessors = true\n" ); } From 8b486dfc717fcf3a3492e715f7c7adb86e3e9282 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Sat, 12 Nov 2022 14:23:05 -0800 Subject: [PATCH 4/5] Fix compilation failure --- src/cmd/watch.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/cmd/watch.rs b/src/cmd/watch.rs index a3c9d546..68921810 100644 --- a/src/cmd/watch.rs +++ b/src/cmd/watch.rs @@ -149,7 +149,10 @@ where for dir in &book.config.build.extra_watch_dirs { let path = dir.canonicalize().unwrap(); if let Err(e) = watcher.watch(&path, Recursive) { - error!("Error while watching extra directory {path:?}:\n {e:?}"); + error!( + "Error while watching extra directory {:?}:\n {:?}", + path, e + ); std::process::exit(1); } } From 144a1e4009a80c3eb01775963a9c9349ecc00e02 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Sat, 12 Nov 2022 14:28:43 -0800 Subject: [PATCH 5/5] Add documentation for extra-watch-dirs --- guide/src/format/configuration/general.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/guide/src/format/configuration/general.md b/guide/src/format/configuration/general.md index a00247ec..af9d532e 100644 --- a/guide/src/format/configuration/general.md +++ b/guide/src/format/configuration/general.md @@ -87,6 +87,7 @@ This controls the build process of your book. build-dir = "book" # the directory where the output is placed create-missing = true # whether or not to create missing pages use-default-preprocessors = true # use the default preprocessors +extra-watch-dirs = [] # directories to watch for triggering builds ``` - **build-dir:** The directory to put the rendered book in. By default this is @@ -108,3 +109,6 @@ use-default-preprocessors = true # use the default preprocessors default preprocessors from running. - Adding `[preprocessor.links]`, for example, will ensure, regardless of `use-default-preprocessors` that `links` it will run. +- **extra-watch-dirs**: A list of paths to directories that will be watched in + the `watch` and `serve` commands. Changes to files under these directories will + trigger rebuilds. Useful if your book depends on files outside its `src` directory. \ No newline at end of file