Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/mdbook-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
//! # run().unwrap()
//! ```

use crate::static_regex;
use crate::utils::{TomlExt, fs, log_backtrace};
use anyhow::{Context, Error, Result, bail};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -149,15 +150,23 @@ impl Config {
pub fn update_from_env(&mut self) -> Result<()> {
debug!("Updating the config from environment variables");

static_regex!(
VALID_KEY,
r"^(:?book|build|rust|output|preprocessor)(:?$|\.)"
);

let overrides =
env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value)));

for (key, value) in overrides {
if key == "log" {
// MDBOOK_LOG is used to control logging.
trace!("{} => {}", key, value);
if !VALID_KEY.is_match(&key) {
// Ignore environment variables for other top-level things.
// This allows users to set things like `MDBOOK_VERSION` or
// `MDBOOK_DOWNLOAD_URL` for their own scripts and not
// interfere with how the config is loaded.
continue;
}
trace!("{} => {}", key, value);
let parsed_value = serde_json::from_str(&value)
.unwrap_or_else(|_| serde_json::Value::String(value.to_string()));

Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ unknown field `title`, expected `edition`
fn env_invalid_config_key() {
BookTest::from_dir("config/empty").run("build", |cmd| {
cmd.env("MDBOOK_FOO", "testing")
.expect_failure()
.expect_stdout(str![[""]])
.expect_stderr(str![[r#"
ERROR invalid key `foo`
INFO Book building has started
INFO Running the html backend
INFO HTML book written to `[ROOT]/book`

"#]]);
});
Expand Down