Skip to content

Commit 1187c34

Browse files
authored
fix(config): include root argument in deserialization (#704)
1 parent 28f6d91 commit 1187c34

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

cli/tests/cmd.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ forgetest_init!(can_override_config, |prj: TestProject, mut cmd: TestCommand| {
135135
assert_eq!(foundry_toml, file);
136136

137137
let config = forge_utils::load_config();
138-
let profile = Config::load();
138+
let profile = Config::load_with_root(prj.root());
139139
assert_eq!(config, profile.clone().sanitized());
140140

141141
// ensure remappings contain test
@@ -154,7 +154,6 @@ forgetest_init!(can_override_config, |prj: TestProject, mut cmd: TestCommand| {
154154
// remappings work
155155
let remappings_txt = prj.create_file("remappings.txt", "ds-test/=lib/ds-test/from-file/");
156156
let config = forge_utils::load_config();
157-
println!("{:?}", config.remappings);
158157
assert_eq!(
159158
format!("ds-test/={}/", prj.root().join("lib/ds-test/from-file").display()),
160159
Remapping::from(config.remappings[0].clone()).to_string()

config/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ethers_solc::{
2222
remappings::{RelativeRemapping, Remapping},
2323
EvmVersion, Project, ProjectPathsConfig, SolcConfig,
2424
};
25-
use figment::providers::Data;
25+
use figment::{providers::Data, value::Value};
2626
use inflector::Inflector;
2727

2828
// Macros useful for creating a figment.
@@ -166,11 +166,11 @@ pub struct Config {
166166
// "#
167167
pub solc_settings: Option<String>,
168168
/// The root path where the config detection started from, `Config::with_root`
169-
///
170-
/// **Note:** This field is never serialized nor deserialized. This is merely used to provided
171-
/// additional context.
172169
#[doc(hidden)]
173-
#[serde(skip)]
170+
// We're skipping serialization here, so it won't be included in the [`Config::to_string()`]
171+
// representation, but will be deserialized from the `Figment` so that forge commands can
172+
// override it.
173+
#[serde(rename = "root", default, skip_serializing)]
174174
pub __root: RootPath,
175175
/// PRIVATE: This structure may grow, As such, constructing this structure should
176176
/// _always_ be done using a public constructor or update syntax:
@@ -637,7 +637,8 @@ impl From<Config> for Figment {
637637
}
638638

639639
/// A helper wrapper around the root path used during Config detection
640-
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord)]
640+
#[derive(Debug, PartialEq, Eq, Hash, Clone, PartialOrd, Ord, Deserialize, Serialize)]
641+
#[serde(transparent)]
641642
pub struct RootPath(pub PathBuf);
642643

643644
impl Default for RootPath {
@@ -676,7 +677,11 @@ impl Provider for Config {
676677

677678
#[track_caller]
678679
fn data(&self) -> Result<Map<Profile, Dict>, figment::Error> {
679-
Serialized::defaults(self).data()
680+
let mut data = Serialized::defaults(self).data()?;
681+
if let Some(entry) = data.get_mut(&self.profile) {
682+
entry.insert("root".to_string(), Value::serialize(self.__root.clone())?);
683+
}
684+
Ok(data)
680685
}
681686

682687
fn profile(&self) -> Option<Profile> {
@@ -1461,6 +1466,7 @@ mod tests {
14611466
fn can_use_impl_figment_macro() {
14621467
#[derive(Default, Serialize)]
14631468
struct MyArgs {
1469+
#[serde(skip_serializing_if = "Option::is_none")]
14641470
root: Option<PathBuf>,
14651471
}
14661472
impl_figment_convert!(MyArgs);

config/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/// use foundry_config::figment::value::*;
2121
/// #[derive(Default, Serialize)]
2222
/// struct MyArgs {
23+
/// #[serde(skip_serializing_if = "Option::is_none")]
2324
/// root: Option<PathBuf>,
2425
/// }
2526
/// impl_figment_convert!(MyArgs);

0 commit comments

Comments
 (0)