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): project autoremappings should respect config #9466

Merged
merged 2 commits into from
Dec 4, 2024
Merged
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
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();
});
Loading