Skip to content

Commit

Permalink
fix(windows): normalize paths with normpath instead of `std::fs::ca…
Browse files Browse the repository at this point in the history
…nonicalize()` (#84)

On Windows, `std::fs::canonicalize()` [produces a weird type of path
that cannot be used with
`Command::current_dir()`](rust-lang/rust#42869).
This switches to using the
[`normpath`](https://docs.rs/normpath/latest/normpath/trait.PathExt.html#tymethod.normalize)
crate, which should avoid these issues.
  • Loading branch information
max-heller authored Apr 6, 2024
1 parent 1f61e56 commit 319c7ba
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 14 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ env_logger = "0.11.0"
genawaiter = { version = "0.99.1", default-features = false }
log = "0.4.0"
mdbook = { version = "0.4.35", default-features = false }
normpath = "1.0.0, <1.2.0" # 1.2.0 raises MSRV to 1.74
once_cell = "1.0.0"
pulldown-cmark = { version = "0.10.0", default-features = false }
pulldown-cmark-to-cmark = "13.0.0"
Expand Down
8 changes: 5 additions & 3 deletions src/book.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::{fs, path::PathBuf};

use normpath::PathExt;

pub struct Book<'book> {
pub book: &'book mdbook::book::Book,
pub root: PathBuf,
Expand All @@ -9,11 +11,11 @@ pub struct Book<'book> {

impl<'book> Book<'book> {
pub fn new(ctx: &'book mdbook::renderer::RenderContext) -> anyhow::Result<Self> {
let root = ctx.root.canonicalize()?;
let source_dir = ctx.source_dir().canonicalize()?;
let root = ctx.root.normalize()?.into_path_buf();
let source_dir = ctx.source_dir().normalize()?.into_path_buf();

fs::create_dir_all(&ctx.destination)?;
let destination = ctx.destination.canonicalize()?;
let destination = ctx.destination.normalize()?.into_path_buf();

Ok(Self {
book: &ctx.book,
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ mod tests {
};

use mdbook::{BookItem, Renderer as _};
use normpath::PathExt;
use once_cell::sync::Lazy;
use regex::Regex;
use tempfile::{tempfile, TempDir};
Expand Down Expand Up @@ -372,7 +373,7 @@ mod tests {
writeln!(&mut logs, "{err:#}").unwrap()
}

let root = self.book.root.canonicalize().unwrap();
let root = self.book.root.normalize().unwrap().into_path_buf();
let re = Regex::new(&format!(
r"(?P<root>{})|(?P<line>line\s+\d+)|(?P<page>page\s+\d+)",
root.display()
Expand Down Expand Up @@ -566,7 +567,7 @@ mod tests {
insta::assert_snapshot!(book, @r###"
├─ log output
│ INFO mdbook::book: Running the pandoc backend
│ WARN mdbook_pandoc::preprocess: Unable to normalize link 'foobarbaz' in chapter 'Getting Started': Unable to canonicalize path: $ROOT/src/foobarbaz: No such file or directory (os error 2)
│ WARN mdbook_pandoc::preprocess: Unable to normalize link 'foobarbaz' in chapter 'Getting Started': Unable to normalize path: $ROOT/src/foobarbaz: No such file or directory (os error 2)
│ WARN mdbook_pandoc: Unable to resolve one or more relative links within the book, consider setting the `hosted-html` option in `[output.pandoc]`
│ INFO mdbook_pandoc::pandoc::renderer: Wrote output to book/markdown/book.md
├─ markdown/book.md
Expand Down
3 changes: 2 additions & 1 deletion src/pandoc/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
};

use anyhow::Context as _;
use normpath::PathExt;
use tempfile::NamedTempFile;

use crate::{
Expand Down Expand Up @@ -219,7 +220,7 @@ impl Renderer {
include_str!("filters/annotate-tables-with-column-widths.lua")
)?;
pandoc.arg("--lua-filter");
pandoc.arg(filter.path().canonicalize()?);
pandoc.arg(filter.path().normalize()?.as_path());
_filter_tempfile_guard = filter.into_temp_path();
} else {
log::warn!(
Expand Down
12 changes: 7 additions & 5 deletions src/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use mdbook::{
book::{BookItems, Chapter},
BookItem,
};
use normpath::PathExt;
use once_cell::sync::Lazy;
use pulldown_cmark::{CodeBlockKind, CowStr, HeadingLevel, LinkType};
use regex::Regex;
Expand Down Expand Up @@ -366,20 +367,21 @@ impl<'book> Preprocessor<'book> {
/// - Uniquely corresponds to the file at the original path
fn normalize_path(&self, path: &Path) -> anyhow::Result<NormalizedPath> {
let absolute_path = path
.canonicalize()
.normalize()
.or_else(|err| match path.extension() {
Some(extension) if extension == "html" => {
if path.file_stem().is_some_and(|name| name == "index") {
if let Ok(path) = path.with_file_name("README.md").canonicalize() {
if let Ok(path) = path.with_file_name("README.md").normalize() {
return Ok(path);
}
}
path.with_extension("md").canonicalize()
path.with_extension("md").normalize()
}
Some(extension) if extension == "md" => path.with_extension("html").canonicalize(),
Some(extension) if extension == "md" => path.with_extension("html").normalize(),
_ => Err(err),
})
.with_context(|| format!("Unable to canonicalize path: {}", path.display()))?;
.with_context(|| format!("Unable to normalize path: {}", path.display()))?
.into_path_buf();
let preprocessed_relative_path = absolute_path
.strip_prefix(&self.ctx.book.source_dir)
.or_else(|_| absolute_path.strip_prefix(&self.preprocessed))
Expand Down

0 comments on commit 319c7ba

Please sign in to comment.