Skip to content

Commit

Permalink
Add forge config --fix (foundry-rs#2325)
Browse files Browse the repository at this point in the history
And fixes duplicate warnings emitted for implicit profiles

Signed-off-by: Julian Popescu <[email protected]>
  • Loading branch information
jpopesculian authored Jul 14, 2022
1 parent 75aa708 commit 44b861b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
8 changes: 7 additions & 1 deletion cli/src/cmd/forge/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::cmd::{forge::build::BuildArgs, utils::Cmd};
use clap::Parser;
use foundry_common::evm::EvmArgs;
use foundry_config::{figment::Figment, Config};
use foundry_config::{figment::Figment, fix::fix_tomls, Config};

foundry_config::impl_figment_convert!(ConfigArgs, opts, evm_opts);

Expand All @@ -14,6 +14,8 @@ pub struct ConfigArgs {
json: bool,
#[clap(help = "prints basic set of currently set config values", long)]
basic: bool,
#[clap(help = "attempts to fix any configuration warnings", long)]
fix: bool,
// support nested build arguments
#[clap(flatten)]
opts: BuildArgs,
Expand All @@ -25,6 +27,10 @@ impl Cmd for ConfigArgs {
type Output = ();

fn run(self) -> eyre::Result<Self::Output> {
if self.fix {
fix_tomls();
return Ok(())
}
let figment: Figment = From::from(&self);
let config = Config::from_provider(figment);
let s = if self.basic {
Expand Down
32 changes: 27 additions & 5 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,14 @@ impl Config {
toml_provider: impl Provider,
profile: Profile,
) -> Figment {
// provide warnings for unknown sections in toml provider
for unknown_key in toml_provider.data().unwrap_or_default().keys().filter(|k| {
k != &Config::PROFILE_SECTION && !Config::STANDALONE_SECTIONS.iter().any(|s| s == k)
}) {
let src =
toml_provider.metadata().source.map(|src| format!(" in {src}")).unwrap_or_default();
config_warn!("Unknown section [{unknown_key}] found{src}. This notation for profiles has been deprecated and may result in the profile not being registered in future versions. Please use [profile.{profile}] instead or run `forge config --fix`.");
}
// use [profile.<profile>] as [<profile>]
let mut profiles = vec![Config::DEFAULT_PROFILE];
if profile != Config::DEFAULT_PROFILE {
Expand Down Expand Up @@ -2160,11 +2168,6 @@ impl<P: Provider> Provider for OptionalStrictProfileProvider<P> {
}
fn data(&self) -> Result<Map<Profile, Dict>, Error> {
let mut figment = Figment::from(&self.provider);
if let Some(profile) = figment.profiles().find(|p| self.profiles.contains(p)) {
let src =
self.provider.metadata().source.map(|src| format!(" in {src}")).unwrap_or_default();
config_warn!("Implied profile [{profile}] found{src}. This notation has been deprecated and may result in the profile not being registered in future versions. Please use [profile.{profile}] instead.");
}
for profile in &self.profiles {
figment = figment.merge(UnwrapProfileProvider::new(
&self.provider,
Expand Down Expand Up @@ -3313,6 +3316,25 @@ mod tests {
);
}

#[test]
fn test_implicit_profile_loads() {
figment::Jail::expect_with(|jail| {
jail.create_file(
"foundry.toml",
r#"
[default]
src = 'my-src'
out = 'my-out'
"#,
)?;
let loaded = Config::load().sanitized();
assert_eq!(loaded.src.file_name().unwrap(), "my-src");
assert_eq!(loaded.out.file_name().unwrap(), "my-out");

Ok(())
});
}

// a test to print the config, mainly used to update the example config in the README
#[test]
#[ignore]
Expand Down

0 comments on commit 44b861b

Please sign in to comment.