Skip to content

feat(config): warn on unknown config keys in foundry.toml #10621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ repository.workspace = true
workspace = true

[dependencies]
serde_ignored = "0.1"
foundry-block-explorers = { workspace = true, features = ["foundry-compilers"] }
foundry-compilers = { workspace = true, features = ["svm-solc"] }

Expand Down Expand Up @@ -56,6 +57,7 @@ path-slash = "0.2"
similar-asserts.workspace = true
figment = { workspace = true, features = ["test"] }
tempfile.workspace = true
tracing-test = "0.2"

[features]
isolate-by-default = []
isolate-by-default = []
68 changes: 67 additions & 1 deletion crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ use std::{
str::FromStr,
};

use tracing::warn;

mod macros;

pub mod utils;
Expand Down Expand Up @@ -640,7 +642,30 @@ impl Config {
Self::from_provider(provider)
}

/// Read `foundry.toml`, collect any unknown keys, and log a warning.
fn warn_unknown_keys() -> Result<(), ExtractConfigError> {
let path = Path::new(Self::FILE_NAME);

// if the file exists & is readable, deserialize and collect ignored keys
if let Ok(raw) = fs::read_to_string(path) {
let mut ignored = Vec::new();
let deserializer = toml::Deserializer::new(&raw);

// collect every ignored field name
let _: Result<Self, _> =
serde_ignored::deserialize(deserializer, |field| ignored.push(field.to_string()));

// if we found any, log a single warning with a list
if !ignored.is_empty() {
warn!("Found unknown config keys in {}: {}", Self::FILE_NAME, ignored.join(", "));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use sh_warn and output message. We already have collect_warnings so should use that one

pub fn collect_warnings(&self) -> Result<Vec<Warning>, Error> {

Would be great to address also the inline config, that is #5371

}
}
Ok(())
}

fn from_figment(figment: Figment) -> Result<Self, ExtractConfigError> {
Self::warn_unknown_keys()?;

let mut config = figment.extract::<Self>().map_err(ExtractConfigError::new)?;
config.profile = figment.profile().clone();

Expand Down Expand Up @@ -2580,8 +2605,9 @@ mod tests {
};
use similar_asserts::assert_eq;
use soldeer_core::remappings::RemappingsLocation;
use std::{fs::File, io::Write};
use std::{env, fs::File, io::Write};
use tempfile::tempdir;
use tracing_test::traced_test;
use NamedChain::Moonbeam;

// Helper function to clear `__warnings` in config, since it will be populated during loading
Expand Down Expand Up @@ -4977,4 +5003,44 @@ mod tests {
Ok(())
});
}

#[traced_test]
#[test]
fn warn_unknown_keys_logs_warning_for_unknown_keys() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are good but would be great to have an e2e test too where we run forge for a project with unknown config keys defined in foundry.toml and inline and we assert proper warnings displayed

let dir = tempdir().expect("failed to create temp dir");
let path = dir.path().join("foundry.toml");
fs::write(&path, "optimizor = false\n").expect("Failed to write foundry.toml");

let old_dir = env::current_dir().expect("failed to get current dir");
env::set_current_dir(&dir).expect("failed to set current dir");

let _ = Config::warn_unknown_keys();

assert!(
logs_contain("Found unknown config keys") && logs_contain("optimizor"),
"Expected a warning log containing 'optimizor'"
);

env::set_current_dir(old_dir).expect("failed to restore dir");
}

#[traced_test]
#[test]
fn warn_unknown_keys_logs_nothing_for_valid_keys() {
let dir = tempdir().expect("failed to create temp dir");
let path = dir.path().join("foundry.toml");
fs::write(&path, "optimizer = false\n").expect("Failed to write foundry.toml");

let old_dir = env::current_dir().expect("failed to get current dir");
env::set_current_dir(&dir).expect("failed to set current dir");

let _ = Config::warn_unknown_keys();

assert!(
!logs_contain("Found unknown config keys"),
"Did not expect a warning for only valid keys"
);

env::set_current_dir(old_dir).expect("failed to restore dir");
}
}
Loading