Skip to content
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

fix(remappings): explicit lib remappings should override detected #9263

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions crates/config/src/providers/remappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
36 changes: 21 additions & 15 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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.
Expand Down
Loading