diff --git a/src/cli/mod.rs b/src/cli/mod.rs index c4585158a..f8bc3397c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -275,6 +275,7 @@ pub mod tests { use super::*; pub fn cli_run(args: &Vec) -> Result<()> { + Config::reset(); *env::ARGS.write().unwrap() = args.clone(); STDOUT.lock().unwrap().clear(); STDERR.lock().unwrap().clear(); diff --git a/src/config/mod.rs b/src/config/mod.rs index bb39581f8..a59190ba4 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -13,10 +13,10 @@ use rayon::prelude::*; pub use settings::{Settings, SettingsPartial}; +use crate::cli::Cli; use crate::config::config_file::legacy_version::LegacyVersionFile; use crate::config::config_file::rtx_toml::RtxToml; use crate::config::config_file::{ConfigFile, ConfigFileType}; - use crate::config::tracking::Tracker; use crate::file::display_path; use crate::plugins::core::{PluginMap, CORE_PLUGINS, EXPERIMENTAL_CORE_PLUGINS}; @@ -51,11 +51,12 @@ pub struct Config { impl Config { pub fn load() -> Result { + let cli_settings = Cli::new().settings(&env::ARGS.read().unwrap()); + Settings::add_partial(cli_settings); let global_config = load_rtxrc()?; + Settings::add_partial(global_config.settings()?); let config_filenames = load_config_filenames(&BTreeMap::new()); - let settings = Settings::default_builder() - .preloaded(global_config.settings()?) - .load()?; + let settings = Settings::default_builder().load()?; let plugins = load_plugins(&settings)?; let config_files = load_all_config_files( &settings, @@ -64,11 +65,10 @@ impl Config { &BTreeMap::new(), ConfigMap::new(), )?; - let mut settings = Settings::default_builder(); for cf in config_files.values() { - settings = settings.preloaded(cf.settings()?); + Settings::add_partial(cf.settings()?); } - let mut settings = settings.load()?; + let mut settings = Settings::default_builder().load()?; if settings.raw { settings.verbose = true; settings.jobs = 1; @@ -123,7 +123,6 @@ impl Config { Ok(config) } - pub fn get_shorthands(&self) -> &Shorthands { self.shorthands .get_or_init(|| get_shorthands(&self.settings)) @@ -241,6 +240,11 @@ impl Config { crate::runtime_symlinks::rebuild(self)?; Ok(()) } + + #[cfg(test)] + pub fn reset() { + Settings::reset(); + } } fn get_project_root(config_files: &ConfigMap) -> Option { diff --git a/src/config/settings.rs b/src/config/settings.rs index 86d02fdc7..f2999044d 100644 --- a/src/config/settings.rs +++ b/src/config/settings.rs @@ -1,17 +1,18 @@ use confique::env::parse::{list_by_colon, list_by_comma}; -use confique::{Builder, Config, Partial}; -use crate::cli::Cli; -use log::LevelFilter; -use serde_derive::{Deserialize, Serialize}; use std::collections::{BTreeMap, BTreeSet}; use std::fmt::{Debug, Display, Formatter}; use std::path::PathBuf; +use std::sync::{Once, RwLock}; + +use confique::{Builder, Config, Partial}; +use log::LevelFilter; +use serde_derive::{Deserialize, Serialize}; use crate::env; #[derive(Config, Debug, Clone)] -#[config(partial_attr(derive(Debug)))] +#[config(partial_attr(derive(Debug, Clone)))] pub struct Settings { #[config(env = "RTX_EXPERIMENTAL", default = false)] pub experimental: bool, @@ -54,26 +55,38 @@ impl Default for Settings { Settings::default_builder().load().unwrap() } } + +static PARTIALS: RwLock> = RwLock::new(Vec::new()); + impl Settings { - pub fn default_builder() -> Builder { - let mut p = SettingsPartial::empty(); - let args = env::ARGS.read().unwrap(); - if *env::CI { - p.yes = Some(true); - } - if *env::RTX_LOG_LEVEL < LevelFilter::Info { - p.verbose = Some(true); - } - for arg in &*args { - if arg == "--" { - break; + pub fn add_partial(partial: SettingsPartial) { + static ONCE: Once = Once::new(); + ONCE.call_once(|| { + let mut p = SettingsPartial::empty(); + if *env::CI { + p.yes = Some(true); + } + if *env::RTX_LOG_LEVEL < LevelFilter::Info { + p.verbose = Some(true); } - if arg == "--raw" { - p.raw = Some(true); + for arg in &*env::ARGS.read().unwrap() { + if arg == "--" { + break; + } + if arg == "--raw" { + p.raw = Some(true); + } } + PARTIALS.write().unwrap().push(p); + }); + PARTIALS.write().unwrap().push(partial); + } + pub fn default_builder() -> Builder { + let mut b = Self::builder(); + for partial in PARTIALS.read().unwrap().iter() { + b = b.preloaded(partial.clone()); } - let cli = Cli::new().settings(&args); - Self::builder().preloaded(cli).preloaded(p).env() + b.env() } pub fn to_index_map(&self) -> BTreeMap { @@ -129,6 +142,11 @@ impl Settings { map.insert("yes".into(), self.yes.to_string()); map } + + #[cfg(test)] + pub fn reset() { + PARTIALS.write().unwrap().clear(); + } } impl Display for Settings {