diff --git a/book-example/src/for_developers/backends.md b/book-example/src/for_developers/backends.md index b72cfc7c..609b5a80 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 diff --git a/src/book/mod.rs b/src/book/mod.rs index 830bf9db..f464b496 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -190,19 +190,6 @@ impl MDBook { renderer.name().to_string(), ); - 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) { 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 1490f8af..7a7f0ef6 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -282,6 +282,11 @@ impl Renderer for HtmlHandlebars { let destination = &ctx.destination; let book = &ctx.book; + if destination.exists() { + utils::fs::remove_dir_content(destination) + .chain_err(|| "Unable to remove stale HTML output")?; + } + trace!("render"); let mut handlebars = Handlebars::new();