diff --git a/crates/mdbook-core/src/config.rs b/crates/mdbook-core/src/config.rs index 3368e5f6a8..b2c6862c30 100644 --- a/crates/mdbook-core/src/config.rs +++ b/crates/mdbook-core/src/config.rs @@ -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}; @@ -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())); diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index e44649a84e..641243c103 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -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` "#]]); });