Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 26 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 !self.name.ends_with('/') && !self.name.ends_with(".sol") {
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 !s.ends_with('/') && !s.ends_with(".sol") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we extract these conditions to a helper function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in 49289cc

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

if !s.ends_with('/') {
if !s.ends_with('/') && !s.ends_with(".sol") {
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 !path.ends_with('/') && !path.ends_with(".sol") {
path.push('/');
}
if !name.ends_with('/') {
if !name.ends_with('/') && !name.ends_with(".sol") {
name.push('/');
}
Self { context, name, path }
Expand Down Expand Up @@ -423,4 +426,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