-
Notifications
You must be signed in to change notification settings - Fork 2k
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
zarkk01
wants to merge
8
commits into
foundry-rs:master
Choose a base branch
from
zarkk01:warn-unknown-config-keys
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4480293
feat(config): warn on unknown config keys in foundry.toml
zarkk01 07ca54d
Merge branch 'master' into warn-unknown-config-keys
zarkk01 395148f
fix(config): log warning for unknown config keys and add test
zarkk01 1cb99a5
feat(config): log warning for unknown config keys and add test
zarkk01 75e4ad4
feat(config): log warning for unknown config keys and add test
zarkk01 6b0483f
Merge branch 'master' into warn-unknown-config-keys
zarkk01 6f83d52
Merge branch 'master' into warn-unknown-config-keys
grandizzy 2259716
Merge branch 'master' into warn-unknown-config-keys
zarkk01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,8 @@ use std::{ | |
str::FromStr, | ||
}; | ||
|
||
use tracing::warn; | ||
|
||
mod macros; | ||
|
||
pub mod utils; | ||
|
@@ -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(", ")); | ||
} | ||
} | ||
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(); | ||
|
||
|
@@ -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 | ||
|
@@ -4977,4 +5003,44 @@ mod tests { | |
Ok(()) | ||
}); | ||
} | ||
|
||
#[traced_test] | ||
#[test] | ||
fn warn_unknown_keys_logs_warning_for_unknown_keys() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 onefoundry/crates/config/src/providers/warnings.rs
Line 41 in 66fe662
Would be great to address also the inline config, that is #5371