Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML renderer doesn't handle windows symlinks #1209

Closed
Michael-F-Bryan opened this issue May 4, 2020 · 1 comment · Fixed by #1323
Closed

HTML renderer doesn't handle windows symlinks #1209

Michael-F-Bryan opened this issue May 4, 2020 · 1 comment · Fixed by #1323

Comments

@Michael-F-Bryan
Copy link
Contributor

In one of my books, I'd like to distribute a copy of a PDF. To make sure the document in my book doesn't get out of sync with the original I'm using a symlink.

I'm on a windows computer, so I created the symlink with the following:

$ mklink "../../../Install Files/Documentation/some-document.pdf" "some-document.pdf"

My directory structure looks something like this:

  • docs/
    • book.toml
    • src/
      • SUMMARY.md
      • overview.md
      • some-document.pdf -> ../../../Install Files/Documentation/some-document.pdf

Now when I run the build server mdbook serve --open go to http://localhost:3000/overview.html, and click the link to some-document.pdf it will serve up a zero byte file... Which is kinda not helpful.


I believe what's happened is the HTML renderer copied the symlink as-is, meaning the output directory now contains a broken symlink. Because the output directory is meant to be copied around and standalone, we should be copying the actual file contents to the output directory.

One solution would be for the HTML renderer to run std::fs::canonicalize() first when copying files across (e.g. to resolve any symlinks), probably in mdbook::utils::copy_files_except_ext(). I'm not sure how much code depends on this function though.

@ehuss
Copy link
Contributor

ehuss commented May 4, 2020

It doesn't look like this is specific to Windows, is it? Symbolic links don't seem to work on unix, either. I think it might be something as simple as:

diff --git a/src/utils/fs.rs b/src/utils/fs.rs
index 570e84282..56740e479 100644
--- a/src/utils/fs.rs
+++ b/src/utils/fs.rs
@@ -113,7 +113,7 @@ pub fn copy_files_except_ext(

     for entry in fs::read_dir(from)? {
         let entry = entry?;
-        let metadata = entry.metadata()?;
+        let metadata = entry.path().metadata()?;

         // If the entry is a dir and the recursive option is enabled, call itself
         if metadata.is_dir() && recursive {

I think this was also reported at #1157.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants