mirror of
https://github.com/rust-lang/mdBook.git
synced 2025-12-27 07:54:20 -05:00
Ignore invalid top-level environment variable config keys
This changes it so that top-level environment variable config keys like `MDBOOK_FOO` are ignored instead of generating an error. It's just too inconvenient since it is common for users to set environment variables like `MDBOOK_VERSION` or whatever for their own scripts.
This commit is contained in:
@@ -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()));
|
||||
|
||||
|
||||
@@ -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`
|
||||
|
||||
"#]]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user