Skip to content

Commit

Permalink
Merge pull request #674 from axodotdev/fix/infinite-mdbooks
Browse files Browse the repository at this point in the history
fix: prevent mdbook from recursing infinitely
  • Loading branch information
liv authored Dec 14, 2023
2 parents 19fdf72 + 50bd412 commit 9f08d0b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ pub enum OrandaError {
#[source]
details: mdbook::errors::Error,
},

#[error("Can't build mdbook because book output directory {dest_path} is under book source directory {src_path}")]
#[diagnostic(help(
"Make sure that your book source does not contain your book output directory, as that will lead to infinite recursion. Change either the `src` setting or the `build_dir` setting in your book.toml."
))]
MdbookBuildRecursive { src_path: String, dest_path: String },

#[error("We found a potential {kind} project at {manifest_path} but there was an issue")]
#[diagnostic(severity = "warn")]
BrokenProject {
Expand Down
21 changes: 21 additions & 0 deletions src/site/mdbook.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axoasset::LocalAsset;
use camino::{Utf8Path, Utf8PathBuf};
use mdbook::MDBook;
use std::path::PathBuf;

use crate::config::MdBookConfig;
use crate::data::workspaces::WorkspaceData;
Expand Down Expand Up @@ -205,6 +206,17 @@ pub fn build_mdbook(
let book_dir = mdbook_dir(workspace, book_cfg)?;
let mut md = load_mdbook(&book_dir)?;

// Check if we'd be infinitely recursing due to mdbook's build output directory being
// in the same directory as its source (This Can Happen!)
let book_src = homogenize_path(&md.source_dir());
let book_dest = homogenize_path(&md.config.build.build_dir.clone());
if book_dest.starts_with(&book_src) {
return Err(OrandaError::MdbookBuildRecursive {
src_path: book_src.display().to_string(),
dest_path: book_dest.display().to_string(),
});
}

// If custom theme is enabled, set that up
let custom_theme = custom_theme(book_cfg, oranda_theme);
let theme_dir = custom_theme_dir(book_cfg, dist)?;
Expand Down Expand Up @@ -271,6 +283,15 @@ pub fn load_mdbook(book_dir: &Utf8Path) -> Result<MDBook> {
Ok(md)
}

/// Homogenizes a relative path to start with ./
pub fn homogenize_path(path: &PathBuf) -> PathBuf {
if path.is_relative() && !path.starts_with("./") {
return PathBuf::from(".").join(path);
}

path.to_owned()
}

/// Initialize a directory with our custom theme files
///
/// Note that these files assume you will also call [`add_custom_syntax_theme_to_output`][]
Expand Down

0 comments on commit 9f08d0b

Please sign in to comment.