From 46006dae2504a84645bb7fda16bcd74398c96f5f Mon Sep 17 00:00:00 2001 From: grandizzy Date: Tue, 5 Nov 2024 13:12:52 +0200 Subject: [PATCH 1/2] fix(remappings): explicit lib remappings should override detected --- crates/config/src/providers/remappings.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/config/src/providers/remappings.rs b/crates/config/src/providers/remappings.rs index 48e9c875f1ac..1f35a5ba925b 100644 --- a/crates/config/src/providers/remappings.rs +++ b/crates/config/src/providers/remappings.rs @@ -157,10 +157,6 @@ impl RemappingsProvider<'_> { // resolve that if self.auto_detect_remappings { let mut lib_remappings = BTreeMap::new(); - // find all remappings of from libs that use a foundry.toml - for r in self.lib_foundry_toml_remappings() { - insert_closest(&mut lib_remappings, r.context, r.name, r.path.into()); - } // use auto detection for all libs for r in self .lib_paths @@ -177,6 +173,12 @@ impl RemappingsProvider<'_> { insert_closest(&mut lib_remappings, r.context, r.name, r.path.into()); } + // Find all remappings from libs that use a foundry.toml + // Explicit remappings should override auto-detected remappings. + for r in self.lib_foundry_toml_remappings() { + lib_remappings.entry(r.context).or_default().insert(r.name, r.path.into()); + } + all_remappings.extend( lib_remappings .into_iter() From 7a7d1de4bf1a58e390429d657382661e1f52f297 Mon Sep 17 00:00:00 2001 From: grandizzy Date: Tue, 5 Nov 2024 16:23:54 +0200 Subject: [PATCH 2/2] Test --- crates/forge/tests/cli/config.rs | 36 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/forge/tests/cli/config.rs b/crates/forge/tests/cli/config.rs index f38dcf414a00..ca1845a5d137 100644 --- a/crates/forge/tests/cli/config.rs +++ b/crates/forge/tests/cli/config.rs @@ -573,26 +573,32 @@ forgetest_init!(can_detect_lib_foundry_toml, |prj, cmd| { // test remappings with closer paths are prioritised // so that `dep/=lib/a/src` will take precedent over `dep/=lib/a/lib/b/src` -forgetest_init!(can_prioritise_closer_lib_remappings, |prj, cmd| { - let config = cmd.config(); - - // create a new lib directly in the `lib` folder with conflicting remapping `forge-std/` - let mut config = config; - config.remappings = vec![Remapping::from_str("forge-std/=lib/forge-std/src/").unwrap().into()]; +forgetest!(can_prioritise_closer_lib_remappings, |prj, cmd| { + // `lib/dep1` has `my-dep/=path/to/dep` remappings. + // `lib/dep1/lib/dep2` has `my-dep/=path/to/dep` and `my-dep2/=path/to/dep` remappings. + // Resolved remappings should be `my-dep` to `lib/dep1` and `my-dep2` to `lib/dep1/lib/dep2`. + let mut config = cmd.config(); + config.remappings.push(Remapping::from_str("my-dep/=path/to/dep").unwrap().into()); let nested = prj.paths().libraries[0].join("dep1"); pretty_err(&nested, fs::create_dir_all(&nested)); let toml_file = nested.join("foundry.toml"); pretty_err(&toml_file, fs::write(&toml_file, config.to_string_pretty().unwrap())); - let config = cmd.config(); - let remappings = config.get_all_remappings().collect::>(); - similar_asserts::assert_eq!( - remappings, - vec![ - "dep1/=lib/dep1/src/".parse().unwrap(), - "forge-std/=lib/forge-std/src/".parse().unwrap() - ] - ); + let nested_lib = prj.paths().libraries[0].join("dep1").join("lib/dep2"); + pretty_err(&nested, fs::create_dir_all(&nested_lib)); + let toml_file = nested_lib.join("foundry.toml"); + config.remappings.push(Remapping::from_str("my-dep2/=path/to/dep").unwrap().into()); + pretty_err(&toml_file, fs::write(&toml_file, config.to_string_pretty().unwrap())); + + cmd.args(["remappings", "--pretty"]).assert_success().stdout_eq(str![[r#" +Global: +- dep1/=lib/dep1/src/ +- dep2/=lib/dep1/lib/dep2/src/ +- my-dep/=lib/dep1/path/to/dep/ +- my-dep2/=lib/dep1/lib/dep2/path/to/dep/ + + +"#]]); }); // Test that remappings within root of the project have priority over remappings of sub-projects.