rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
18.11k stars 1.63k forks source link

Setting build directory to "." breaks git repository on Windows #833

Open andrewhu-uw opened 5 years ago

andrewhu-uw commented 5 years ago

I was trying to get mdbook to output the HTML in the root of my git repository by setting

[build]
build-dir = "."

in my book.toml. However, when I ran mdbook build I got this error

2018-11-25 20:33:10 [INFO] (mdbook::book): Book building has started
2018-11-25 20:33:10 [INFO] (mdbook::book): Running the html backend
2018-11-25 20:33:10 [ERROR] (mdbook::utils): Error: Unable to clear output directory
2018-11-25 20:33:10 [ERROR] (mdbook::utils):    Caused By: Access is denied. (os error 5)

Then, when I went to check git status I got

fatal: Not a git repository (or any of the parent directories): .git

Now, I know the build command is supposed to erase everything in the build-dir, which was pointed out in #436 but in this case, you can't just go git reset --hard since it breaks the git repo.

Now, on Linux it works "as intended" and erases all of the source files and the .git folder

Dylan-DPC-zz commented 5 years ago

@andrewhu-uw are you still facing this issue?

chmp commented 4 years ago

Not the original author, but I observed the same behavior. Specifically it seems that mdbooks copies the .git directory when src = ".". Deleting the copied .git in the build folder allows to rebuild. However, then again the .git folder is re-created in the build folder has to be manually deleted.

This issues seems to only occur after the first commit. On a freshly initialized repo without a single commit, I could not reproduce the issue. After doing the first commit I observed the described behavior.

I have added a minimal failing example in the details below.

File tree: ``` mdfiles/.git/ mdfiles/subdir/SUMMARY.md mdfiles/subdir/chapter_1.md mdfiles/book.toml mdfiles/SUMMARY.md book/ ``` I tried two configurations either using `src="."` or using `src="subdir"`. The former fails, the latter works. Reproducing the issue (using Powershell) ``` > mdbook.exe --version mdbook v0.3.7 > cat .\mdfiles\SUMMARY.md # Summary - [Chapter 1](./subdir/chapter_1.md) > cat .\mdfiles\subdir\SUMMARY.md # Summary - [Chapter 1](./chapter_1.md) > cat .\book.toml [book] title = "Foo" ## Fails: src = "." [build] build-dir = "../book" > mdbook.exe build 2020-04-17 09:53:06 [INFO] (mdbook::book): Book building has started 2020-04-17 09:53:06 [INFO] (mdbook::book): Running the html backend > mdbook.exe build 2020-04-17 09:55:54 [INFO] (mdbook::book): Book building has started 2020-04-17 09:55:54 [INFO] (mdbook::book): Running the html backend 2020-04-17 09:55:54 [ERROR] (mdbook::utils): Error: Rendering failed 2020-04-17 09:55:54 [ERROR] (mdbook::utils): Caused By: Unable to remove stale HTML output 2020-04-17 09:55:54 [ERROR] (mdbook::utils): Caused By: Access is denied. (os error 5) > cat .\book.toml [book] title = "Foo" ## Works: src = "subdir" [build] build-dir = "../../book" > mdbook.exe build 2020-04-17 09:57:03 [INFO] (mdbook::book): Book building has started 2020-04-17 09:57:03 [INFO] (mdbook::book): Running the html backend > mdbook.exe build 2020-04-17 10:03:13 [INFO] (mdbook::book): Book building has started 2020-04-17 10:03:13 [INFO] (mdbook::book): Running the html backend ```
chmp commented 4 years ago

Okay it seems the issue pops up in src/renderer/html_handlebars/hbs_renderer.rs:

// Copy all remaining files, avoid a recursive copy from/to the book build dir
utils::fs::copy_files_except_ext(&src_dir, &destination, true, Some(&build_dir), &["md"])?;

That also explains, why you can just rerun with the subdir without deleting the .git dir. It will simply not attempt to copy it, hence there is no issue.

I'm happy to contribute a fix for this issue. However, I guess there are mutliple ways to go about it:

  1. Hardcode the exlusion of .git directories
  2. Implementing a general exclusion mechanism ala #1187 and letting it default to [".git"]
  3. Something completely different ;)

Any preference how to fix this issue?

Edit: I implemented Option 2 here: https://github.com/chmp/mdBook/tree/feature/html-exclude