Skip to content

Commit a166f21

Browse files
committed
Add config.rust.package-dir and fix cargo output parser (maybe not for the last time)
1 parent 25a3fb3 commit a166f21

File tree

3 files changed

+67
-12
lines changed

3 files changed

+67
-12
lines changed

src/book/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,13 @@ impl MDBook {
306306

307307
let color_output = std::io::stderr().is_terminal();
308308

309-
// get extra args we'll need for rustdoc
310-
// assumes current working directory is project root, eventually
311-
// pick up manifest directory from some config.
309+
// get extra args we'll need for rustdoc, if config points to a cargo project.
312310

313311
let mut extern_args = ExternArgs::new();
314-
extern_args.load(&std::env::current_dir()?)?;
315-
312+
if let Some(package_dir) = &self.config.rust.package_dir {
313+
extern_args.load(&package_dir)?;
314+
}
315+
316316
let mut failed = false;
317317
for item in book.iter() {
318318
if let BookItem::Chapter(ref ch) = *item {

src/config.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,8 @@ impl Default for BuildConfig {
497497
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
498498
#[serde(default, rename_all = "kebab-case")]
499499
pub struct RustConfig {
500+
/// Path to a Cargo package
501+
pub package_dir: Option<PathBuf>,
500502
/// Rust edition used in playground
501503
pub edition: Option<RustEdition>,
502504
}
@@ -798,6 +800,9 @@ mod tests {
798800
create-missing = false
799801
use-default-preprocessors = true
800802
803+
[rust]
804+
package-dir = "."
805+
801806
[output.html]
802807
theme = "./themedir"
803808
default-theme = "rust"
@@ -839,7 +844,10 @@ mod tests {
839844
use_default_preprocessors: true,
840845
extra_watch_dirs: Vec::new(),
841846
};
842-
let rust_should_be = RustConfig { edition: None };
847+
let rust_should_be = RustConfig {
848+
package_dir: Some(PathBuf::from(".")),
849+
edition: None,
850+
};
843851
let playground_should_be = Playground {
844852
editable: true,
845853
copyable: true,
@@ -918,6 +926,7 @@ mod tests {
918926
assert_eq!(got.book, book_should_be);
919927

920928
let rust_should_be = RustConfig {
929+
package_dir: None,
921930
edition: Some(RustEdition::E2015),
922931
};
923932
let got = Config::from_str(src).unwrap();
@@ -937,6 +946,7 @@ mod tests {
937946
"#;
938947

939948
let rust_should_be = RustConfig {
949+
package_dir: None,
940950
edition: Some(RustEdition::E2018),
941951
};
942952

@@ -957,6 +967,7 @@ mod tests {
957967
"#;
958968

959969
let rust_should_be = RustConfig {
970+
package_dir: None,
960971
edition: Some(RustEdition::E2021),
961972
};
962973

@@ -1356,4 +1367,19 @@ mod tests {
13561367
false
13571368
);
13581369
}
1370+
1371+
1372+
/* todo -- make this test fail, as it should
1373+
#[test]
1374+
#[should_panic(expected = "Invalid configuration file")]
1375+
// invalid key in config file should really generate an error...
1376+
fn invalid_rust_setting() {
1377+
let src = r#"
1378+
[rust]
1379+
foo = "bar"
1380+
"#;
1381+
1382+
Config::from_str(src).unwrap();
1383+
}
1384+
*/
13591385
}

src/utils/extern_args.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Get "compiler" args from cargo
22
33
use crate::errors::*;
4-
use log::info;
4+
use log::{info, warn};
55
use std::fs::File;
66
use std::path::{Path, PathBuf};
77
use std::process::Command;
@@ -29,7 +29,7 @@ use std::process::Command;
2929
/// extern_args.load(&proj_root)?;
3030
///
3131
/// // then, when actually invoking rustdoc or some other compiler-like tool...
32-
///
32+
///
3333
/// assert!(extern_args.get_args().iter().any(|e| e == "-L")); // args contains "-L".to_string()
3434
/// assert!(extern_args.get_args().iter().any(|e| e == "--extern"));
3535
/// # Ok(())
@@ -86,6 +86,7 @@ impl ExternArgs {
8686

8787
/// Parse response stdout+stderr response from `cargo build`
8888
/// into arguments we can use to invoke rustdoc.
89+
/// Stop at first line that traces a compiler invocation.
8990
///
9091
/// >>> This parser is broken, doesn't handle arg values with embedded spaces (single quoted).
9192
/// Fortunately, the args we care about (so far) don't have those kinds of values.
@@ -105,9 +106,15 @@ impl ExternArgs {
105106
_ => {}
106107
}
107108
}
109+
110+
return Ok(());
108111
};
109112
}
110113

114+
if self.suffix_args.len() < 1 {
115+
warn!("Couldn't extract --extern args from Cargo, is current directory == cargo project root?");
116+
}
117+
111118
Ok(())
112119
}
113120

@@ -123,12 +130,34 @@ mod test {
123130

124131
#[test]
125132
fn parse_response_parses_string() -> Result<()> {
126-
let resp = std::fs::read_to_string("tests/t1.txt")?;
133+
let test_str = r###"
134+
Fresh unicode-ident v1.0.14
135+
Fresh cfg-if v1.0.0
136+
Fresh memchr v2.7.4
137+
Fresh autocfg v1.4.0
138+
Fresh version_check v0.9.5
139+
--- clip ---
140+
Fresh bytecount v0.6.8
141+
Fresh leptos_router v0.7.0
142+
Fresh leptos_meta v0.7.0
143+
Fresh console_error_panic_hook v0.1.7
144+
Fresh mdbook-keeper v0.5.0
145+
Dirty leptos-book v0.1.0 (/home/bobhy/src/localdep/book): the file `src/lib.rs` has changed (1733758773.052514835s, 10h 32m 29s after last build at 1733720824.458358565s)
146+
Compiling leptos-book v0.1.0 (/home/bobhy/src/localdep/book)
147+
Running `/home/bobhy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name leptos_book --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type cdylib --crate-type rlib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values("hydrate", "ssr"))' -C metadata=2eec49d479de095c --out-dir /home/bobhy/src/localdep/book/target/debug/deps -C incremental=/home/bobhy/src/localdep/book/target/debug/incremental -L dependency=/home/bobhy/src/localdep/book/target/debug/deps --extern console_error_panic_hook=/home/bobhy/src/localdep/book/target/debug/deps/libconsole_error_panic_hook-d34cf0116774f283.rlib --extern http=/home/bobhy/src/localdep/book/target/debug/deps/libhttp-d4d503240b7a6b18.rlib --extern leptos=/home/bobhy/src/localdep/book/target/debug/deps/libleptos-1dabf2e09ca58f3d.rlib --extern leptos_meta=/home/bobhy/src/localdep/book/target/debug/deps/libleptos_meta-df8ce1704acca063.rlib --extern leptos_router=/home/bobhy/src/localdep/book/target/debug/deps/libleptos_router-df109cd2ee44b2a0.rlib --extern mdbook_keeper_lib=/home/bobhy/src/localdep/book/target/debug/deps/libmdbook_keeper_lib-f4016aaf2c5da5f2.rlib --extern thiserror=/home/bobhy/src/localdep/book/target/debug/deps/libthiserror-acc5435cdf9551fe.rlib --extern wasm_bindgen=/home/bobhy/src/localdep/book/target/debug/deps/libwasm_bindgen-89a7b1dccd9668ae.rlib`
148+
Running `/home/bobhy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc --crate-name leptos_book --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --check-cfg 'cfg(docsrs)' --check-cfg 'cfg(feature, values("hydrate", "ssr"))' -C metadata=24fbc99376c5eff3 -C extra-filename=-24fbc99376c5eff3 --out-dir /home/bobhy/src/localdep/book/target/debug/deps -C incremental=/home/bobhy/src/localdep/book/target/debug/incremental -L dependency=/home/bobhy/src/localdep/book/target/debug/deps --extern console_error_panic_hook=/home/bobhy/src/localdep/book/target/debug/deps/libconsole_error_panic_hook-d34cf0116774f283.rlib --extern http=/home/bobhy/src/localdep/book/target/debug/deps/libhttp-d4d503240b7a6b18.rlib --extern leptos=/home/bobhy/src/localdep/book/target/debug/deps/libleptos-1dabf2e09ca58f3d.rlib --extern leptos_book=/home/bobhy/src/localdep/book/target/debug/deps/libleptos_book.rlib --extern leptos_meta=/home/bobhy/src/localdep/book/target/debug/deps/libleptos_meta-df8ce1704acca063.rlib --extern leptos_router=/home/bobhy/src/localdep/book/target/debug/deps/libleptos_router-df109cd2ee44b2a0.rlib --extern mdbook_keeper_lib=/home/bobhy/src/localdep/book/target/debug/deps/libmdbook_keeper_lib-f4016aaf2c5da5f2.rlib --extern thiserror=/home/bobhy/src/localdep/book/target/debug/deps/libthiserror-acc5435cdf9551fe.rlib --extern wasm_bindgen=/home/bobhy/src/localdep/book/target/debug/deps/libwasm_bindgen-89a7b1dccd9668ae.rlib`
149+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.60s
150+
151+
"###;
152+
127153
let mut ea = ExternArgs::new();
128-
ea.parse_response(&resp)?;
154+
ea.parse_response(&test_str)?;
155+
156+
let args = ea.get_args();
157+
assert_eq!(18, args.len());
129158

130-
let sfx = ea.get_args();
131-
assert!(sfx.len() > 0);
159+
assert_eq!(1, args.iter().filter(|i| *i == "-L").count());
160+
assert_eq!(8, args.iter().filter(|i| *i == "--extern").count());
132161

133162
Ok(())
134163
}

0 commit comments

Comments
 (0)