From d1b5a8f982dc695b19a21e5519ef9ca5a3467e08 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sun, 21 Jul 2019 02:35:18 +0800 Subject: [PATCH 1/4] The MDBook::build() method no longer cleans the renderer's build directory --- src/book/mod.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 830bf9db..c8cab192 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -192,16 +192,6 @@ impl MDBook { let name = renderer.name(); let build_dir = self.build_dir_for(name); - if build_dir.exists() { - debug!( - "Cleaning build dir for the \"{}\" renderer ({})", - name, - build_dir.display() - ); - - utils::fs::remove_dir_content(&build_dir) - .chain_err(|| "Unable to clear output directory")?; - } for preprocessor in &self.preprocessors { if preprocessor_should_run(&**preprocessor, renderer, &self.config) { From e56c41a1c2c9ebe9f861ec09212bdc8fee4e2204 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sun, 21 Jul 2019 02:37:09 +0800 Subject: [PATCH 2/4] The HTML renderer now cleans its own build directory --- src/renderer/html_handlebars/hbs_renderer.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 1490f8af..16c884e1 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -282,6 +282,9 @@ impl Renderer for HtmlHandlebars { let destination = &ctx.destination; let book = &ctx.book; + utils::remove_dir_contents(destination) + .chain_err(|| "Unable to remove stale HTML output")? + trace!("render"); let mut handlebars = Handlebars::new(); From 5b0a23ebabcf5473f36f9e7666182e2b55b2f683 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sun, 21 Jul 2019 02:40:58 +0800 Subject: [PATCH 3/4] Updated the documentation --- book-example/src/for_developers/backends.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/book-example/src/for_developers/backends.md b/book-example/src/for_developers/backends.md index f8ddb0f3..ec7ecd79 100644 --- a/book-example/src/for_developers/backends.md +++ b/book-example/src/for_developers/backends.md @@ -261,6 +261,10 @@ in [`RenderContext`]. > **Note:** There is no guarantee that the destination directory exists or is > empty (`mdbook` may leave the previous contents to let backends do caching), > so it's always a good idea to create it with `fs::create_dir_all()`. +> +> If the destination directory already exists, don't assume it will be empty. +> To allow backends to cache the results from previous runs, `mdbook` may leave +> old content in the directory. There's always the possibility that an error will occur while processing a book (just look at all the `unwrap()`'s we've written already), so `mdbook` will From bb412edf5370823797dc7ab94f9aade65f5681c9 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sun, 21 Jul 2019 04:32:28 +0800 Subject: [PATCH 4/4] Made sure the tests pass --- src/book/mod.rs | 3 --- src/renderer/html_handlebars/hbs_renderer.rs | 6 ++++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index c8cab192..f464b496 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -190,9 +190,6 @@ impl MDBook { renderer.name().to_string(), ); - let name = renderer.name(); - let build_dir = self.build_dir_for(name); - for preprocessor in &self.preprocessors { if preprocessor_should_run(&**preprocessor, renderer, &self.config) { debug!("Running the {} preprocessor.", preprocessor.name()); diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 16c884e1..7a7f0ef6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -282,8 +282,10 @@ impl Renderer for HtmlHandlebars { let destination = &ctx.destination; let book = &ctx.book; - utils::remove_dir_contents(destination) - .chain_err(|| "Unable to remove stale HTML output")? + if destination.exists() { + utils::fs::remove_dir_content(destination) + .chain_err(|| "Unable to remove stale HTML output")?; + } trace!("render"); let mut handlebars = Handlebars::new();