Skip to content

Commit

Permalink
fix(remappings): project autoremappings should respect config (#9466)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy authored Dec 4, 2024
1 parent 2c9719e commit 25c978a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
12 changes: 7 additions & 5 deletions crates/config/src/providers/remappings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
foundry_toml_dirs, remappings_from_env_var, remappings_from_newline, utils::get_dir_remapping,
Config,
};
use crate::{foundry_toml_dirs, remappings_from_env_var, remappings_from_newline, Config};
use figment::{
value::{Dict, Map},
Error, Figment, Metadata, Profile, Provider,
Expand Down Expand Up @@ -39,7 +36,12 @@ impl Remappings {
pub fn with_figment(mut self, figment: &Figment) -> Self {
let mut add_project_remapping = |path: &str| {
if let Ok(path) = figment.find_value(path) {
if let Some(remapping) = path.into_string().and_then(get_dir_remapping) {
if let Some(path) = path.into_string() {
let remapping = Remapping {
context: None,
name: format!("{path}/"),
path: format!("{path}/"),
};
self.project_paths.push(remapping);
}
}
Expand Down
33 changes: 33 additions & 0 deletions crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,36 @@ contract MyScript is BaseScript {
pretty_err(&lib_toml_file, fs::write(&lib_toml_file, lib_config.to_string_pretty().unwrap()));
cmd.forge_fuse().args(["build"]).assert_success();
});

// Tests that project remappings use config paths.
// For `src=src/contracts` config, remapping should be `src/contracts/ = src/contracts/`.
// For `src=src` config, remapping should be `src/ = src/`.
// <https://github.com/foundry-rs/foundry/issues/9454>
forgetest!(test_project_remappings, |prj, cmd| {
foundry_test_utils::util::initialize(prj.root());
let config = Config {
src: "src/contracts".into(),
remappings: vec![Remapping::from_str("contracts/=src/contracts/").unwrap().into()],
..Default::default()
};
prj.write_config(config);

// Add Counter.sol in `src/contracts` project dir.
let src_dir = &prj.root().join("src/contracts");
pretty_err(src_dir, fs::create_dir_all(src_dir));
pretty_err(
src_dir.join("Counter.sol"),
fs::write(src_dir.join("Counter.sol"), "contract Counter{}"),
);
prj.add_test(
"CounterTest.sol",
r#"
import "contracts/Counter.sol";
contract CounterTest {
}
"#,
)
.unwrap();
cmd.forge_fuse().args(["build"]).assert_success();
});

0 comments on commit 25c978a

Please sign in to comment.