Fallback to top-level config.toml if not present in current directory

This also preserves the behavior where x.py will only give a hard error on a missing config file
if it was configured through `--config` or RUST_BOOTSTRAP_CONFIG.
It also removes the top-level fallback for everything except the default path.
This commit is contained in:
Joshua Nelson 2022-03-03 19:48:26 -06:00
parent 85ce7fdfa2
commit 4d56f09650
2 changed files with 24 additions and 11 deletions

View File

@ -1233,16 +1233,18 @@ def bootstrap(help_triggered):
build.verbose = args.verbose build.verbose = args.verbose
build.clean = args.clean build.clean = args.clean
# Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then fallback to `config.toml` (if it # Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`,
# exists). # then `config.toml` in the root directory.
toml_path = args.config or os.getenv('RUST_BOOTSTRAP_CONFIG') toml_path = args.config or os.getenv('RUST_BOOTSTRAP_CONFIG')
if not toml_path and os.path.exists('config.toml'): using_default_path = toml_path is None
if using_default_path:
toml_path = 'config.toml' toml_path = 'config.toml'
if toml_path:
if not os.path.exists(toml_path): if not os.path.exists(toml_path):
toml_path = os.path.join(build.rust_root, toml_path) toml_path = os.path.join(build.rust_root, toml_path)
# Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
# but not if `config.toml` hasn't been created.
if not using_default_path or os.path.exists(toml_path):
with open(toml_path) as config: with open(toml_path) as config:
build.config_toml = config.read() build.config_toml = config.read()

View File

@ -647,7 +647,8 @@ impl Config {
let get_toml = |file: &Path| { let get_toml = |file: &Path| {
use std::process; use std::process;
let contents = t!(fs::read_to_string(file), "`include` config not found"); let contents =
t!(fs::read_to_string(file), format!("config file {} not found", file.display()));
match toml::from_str(&contents) { match toml::from_str(&contents) {
Ok(table) => table, Ok(table) => table,
Err(err) => { Err(err) => {
@ -657,14 +658,24 @@ impl Config {
} }
}; };
// check --config first, then `$RUST_BOOTSTRAP_CONFIG` first, then `config.toml` // Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
let toml_path = flags let toml_path = flags
.config .config
.clone() .clone()
.or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from)) .or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
.unwrap_or_else(|| PathBuf::from("config.toml")); let using_default_path = toml_path.is_none();
let mut toml = let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
if toml_path.exists() { get_toml(&toml_path) } else { TomlConfig::default() }; if using_default_path && !toml_path.exists() {
toml_path = config.src.join(toml_path);
}
// Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
// but not if `config.toml` hasn't been created.
let mut toml = if !using_default_path || toml_path.exists() {
get_toml(&toml_path)
} else {
TomlConfig::default()
};
if let Some(include) = &toml.profile { if let Some(include) = &toml.profile {
let mut include_path = config.src.clone(); let mut include_path = config.src.clone();