Skip to content
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
41 changes: 35 additions & 6 deletions crates/artifacts/solc/src/remappings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ impl fmt::Display for Remapping {
}
s.push(':');
}
let name =
if !self.name.ends_with('/') { format!("{}/", self.name) } else { self.name.clone() };
let name = if needs_trailing_slash(&self.name) {
format!("{}/", self.name)
} else {
self.name.clone()
};
s.push_str(&{
#[cfg(target_os = "windows")]
{
Expand All @@ -153,7 +156,7 @@ impl fmt::Display for Remapping {
}
});

if !s.ends_with('/') {
if needs_trailing_slash(&s) {
s.push('/');
}
f.write_str(&s)
Expand Down Expand Up @@ -241,7 +244,7 @@ impl fmt::Display for RelativeRemapping {
}
});

if !s.ends_with('/') {
if needs_trailing_slash(&s) {
s.push('/');
}
f.write_str(&s)
Expand All @@ -252,10 +255,10 @@ impl From<RelativeRemapping> for Remapping {
fn from(r: RelativeRemapping) -> Self {
let RelativeRemapping { context, mut name, path } = r;
let mut path = path.relative().display().to_string();
if !path.ends_with('/') {
if needs_trailing_slash(&path) {
path.push('/');
}
if !name.ends_with('/') {
if needs_trailing_slash(&name) {
name.push('/');
}
Self { context, name, path }
Expand Down Expand Up @@ -341,6 +344,15 @@ impl<'de> Deserialize<'de> for RelativeRemapping {
}
}

/// Helper to determine if name or path of a remapping needs trailing slash.
/// Returns false if it already ends with a slash or if remapping is a solidity file.
/// Used to preserve name and path of single file remapping, see
/// <https://github.com/foundry-rs/foundry/issues/6706>
/// <https://github.com/foundry-rs/foundry/issues/8499>
fn needs_trailing_slash(name_or_path: &str) -> bool {
!name_or_path.ends_with('/') && !name_or_path.ends_with(".sol")
}

#[cfg(test)]
mod tests {
pub use super::*;
Expand Down Expand Up @@ -423,4 +435,21 @@ mod tests {
);
assert_eq!(remapping.to_string(), "oz/=a/b/c/d/".to_string());
}

// <https://github.com/foundry-rs/foundry/issues/6706#issuecomment-3141270852>
#[test]
fn can_preserve_single_sol_file_remapping() {
let remapping = "@my-lib/B.sol=lib/my-lib/B.sol";
let remapping = Remapping::from_str(remapping).unwrap();

assert_eq!(
remapping,
Remapping {
context: None,
name: "@my-lib/B.sol".to_string(),
path: "lib/my-lib/B.sol".to_string()
}
);
assert_eq!(remapping.to_string(), "@my-lib/B.sol=lib/my-lib/B.sol".to_string());
}
}
42 changes: 41 additions & 1 deletion crates/compilers/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,12 @@ impl<L> ProjectPathsConfig<L> {
})
.find_map(|r| {
import.strip_prefix(&r.name).ok().map(|stripped_import| {
let lib_path = Path::new(&r.path).join(stripped_import);
let lib_path =
if stripped_import.as_os_str().is_empty() && r.path.ends_with(".sol") {
r.path.clone().into()
} else {
Path::new(&r.path).join(stripped_import)
};

// we handle the edge case where the path of a remapping ends with "contracts"
// (`<name>/=.../contracts`) and the stripped import also starts with
Expand Down Expand Up @@ -1196,4 +1201,39 @@ mod tests {
dependency.join("A.sol")
);
}

#[test]
fn can_resolve_single_file_mapped_import() {
let dir = tempfile::tempdir().unwrap();
let mut config = ProjectPathsConfig::builder().root(dir.path()).build::<()>().unwrap();
config.create_all().unwrap();

fs::write(
config.sources.join("A.sol"),
r#"pragma solidity ^0.8.0; import "@my-lib/B.sol"; contract A is B {}"#,
)
.unwrap();

let dependency = config.root.join("my-lib");
fs::create_dir(&dependency).unwrap();
fs::write(dependency.join("B.sol"), r"pragma solidity ^0.8.0; contract B {}").unwrap();

config.remappings.push(Remapping {
context: None,
name: "@my-lib/B.sol".into(),
path: "my-lib/B.sol".into(),
});

// Test that single file import / remapping resolves to file.
assert!(config
.resolve_import_and_include_paths(
&config.sources,
Path::new("@my-lib/B.sol"),
&mut Default::default(),
)
.unwrap()
.to_str()
.unwrap()
.ends_with("my-lib/B.sol"));
}
}
Loading