Skip to content

Commit b9be9bc

Browse files
committed
Migrate changes for ExternArgs from root to crates mdbook-core and mdbook-driver.
1 parent 19859b0 commit b9be9bc

File tree

9 files changed

+90
-1473
lines changed

9 files changed

+90
-1473
lines changed

Cargo.lock

Lines changed: 66 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rust-version = "1.88.0" # Keep in sync with installation.md and .github/workflow
2727
[workspace.dependencies]
2828
anyhow = "1.0.100"
2929
axum = "0.8.6"
30+
cargo-manifest = "0.19.1"
3031
clap = { version = "4.5.51", features = ["cargo", "wrap_help"] }
3132
clap_complete = "4.5.60"
3233
ego-tree = "0.10.0"

crates/mdbook-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ rust-version.workspace = true
99

1010
[dependencies]
1111
anyhow.workspace = true
12+
cargo-manifest.workspace = true
1213
regex.workspace = true
1314
serde.workspace = true
1415
serde_json.workspace = true

crates/mdbook-core/src/utils/extern_args.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Get "compiler" args from cargo
22
33
use crate::errors::*;
4-
use anyhow::anyhow;
4+
use anyhow::{Context, anyhow, bail};
55
use cargo_manifest::{Edition, Manifest, MaybeInherited::Local};
6-
use log::{debug, info};
76
use std::fs;
87
use std::fs::File;
98
use std::io::prelude::*;
109
use std::path::{Path, PathBuf};
1110
use std::process::Command;
11+
use tracing::{debug, info};
1212

1313
/// Get the arguments needed to invoke rustc so it can find external crates
1414
/// when invoked by rustdoc to compile doctests.
@@ -23,8 +23,8 @@ use std::process::Command;
2323
/// Example:
2424
/// ```rust
2525
///
26-
/// use mdbook::utils::extern_args::ExternArgs;
27-
/// # use mdbook::errors::*;
26+
/// use mdbook_core::utils::extern_args::ExternArgs;
27+
/// # use mdbook_core::errors::*;
2828
///
2929
/// # fn main() -> Result<()> {
3030
/// // Get cargo to say what the compiler args need to be...
@@ -86,7 +86,7 @@ impl ExternArgs {
8686
.expect("doctest Cargo.toml must include a [package] section");
8787

8888
self.crate_name = package.name.replace('-', "_"); // maybe cargo shouldn't allow packages to include non-identifier characters?
89-
// in any case, this won't work when default crate doesn't have package name (which I'm sure cargo allows somehow or another)
89+
// in any case, this won't work when default crate doesn't have package name (which I'm sure cargo allows somehow or another)
9090
self.edition = if let Some(Local(edition)) = package.edition {
9191
my_display_edition(edition)
9292
} else {
@@ -174,7 +174,8 @@ impl ExternArgs {
174174
if dep_arg.ends_with(".rmeta") {
175175
debug!(
176176
"Build referenced {}, converted to .rlib hoping that actual file will be there in time.",
177-
dep_arg);
177+
dep_arg
178+
);
178179
dep_arg = dep_arg.replace(".rmeta", ".rlib");
179180
}
180181
self.extern_list.push(dep_arg);
@@ -200,7 +201,9 @@ impl ExternArgs {
200201
}
201202

202203
if self.extern_list.is_empty() || self.lib_list.is_empty() {
203-
bail!("Couldn't extract -L or --extern args from Cargo, is current directory == cargo project root?");
204+
bail!(
205+
"Couldn't extract -L or --extern args from Cargo, is current directory == cargo project root?"
206+
);
204207
}
205208

206209
debug!(

crates/mdbook-core/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use anyhow::Error;
44
use std::fmt::Write;
55
use tracing::error;
66

7+
pub mod extern_args;
78
pub mod fs;
89
mod html;
910
mod toml_ext;

crates/mdbook-driver/src/mdbook.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use anyhow::{Context, Error, Result, bail};
88
use indexmap::IndexMap;
99
use mdbook_core::book::{Book, BookItem, BookItems};
1010
use mdbook_core::config::{Config, RustEdition};
11-
use mdbook_core::utils::fs;
11+
use mdbook_core::utils::{extern_args, fs};
1212
use mdbook_html::HtmlHandlebars;
1313
use mdbook_preprocessor::{Preprocessor, PreprocessorContext};
1414
use mdbook_renderer::{RenderContext, Renderer};
@@ -271,6 +271,13 @@ impl MDBook {
271271
let (book, _) = self.preprocess_book(&TestRenderer)?;
272272

273273
let color_output = std::io::stderr().is_terminal();
274+
// get extra args we'll need for rustdoc, if config points to a cargo project
275+
276+
let mut extern_args = extern_args::ExternArgs::new();
277+
if let Some(manifest) = &self.config.rust.manifest {
278+
extern_args.load(&self.root.join(manifest))?;
279+
}
280+
274281
let mut failed = false;
275282
for item in book.iter() {
276283
if let BookItem::Chapter(ref ch) = *item {
@@ -298,7 +305,8 @@ impl MDBook {
298305
cmd.current_dir(temp_dir.path())
299306
.arg(chapter_path)
300307
.arg("--test")
301-
.args(&library_args);
308+
.args(&library_args)
309+
.args(extern_args.get_args());
302310

303311
if let Some(edition) = self.config.rust.edition {
304312
match edition {

0 commit comments

Comments
 (0)