Skip to content

Commit 49289cc

Browse files
committed
add helper fn
1 parent ca2b54d commit 49289cc

File tree

1 file changed

+14
-5
lines changed
  • crates/artifacts/solc/src/remappings

1 file changed

+14
-5
lines changed

crates/artifacts/solc/src/remappings/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ impl fmt::Display for Remapping {
138138
}
139139
s.push(':');
140140
}
141-
let name = if !self.name.ends_with('/') && !self.name.ends_with(".sol") {
141+
let name = if needs_trailing_slash(&self.name) {
142142
format!("{}/", self.name)
143143
} else {
144144
self.name.clone()
@@ -156,7 +156,7 @@ impl fmt::Display for Remapping {
156156
}
157157
});
158158

159-
if !s.ends_with('/') && !s.ends_with(".sol") {
159+
if needs_trailing_slash(&s) {
160160
s.push('/');
161161
}
162162
f.write_str(&s)
@@ -244,7 +244,7 @@ impl fmt::Display for RelativeRemapping {
244244
}
245245
});
246246

247-
if !s.ends_with('/') && !s.ends_with(".sol") {
247+
if needs_trailing_slash(&s) {
248248
s.push('/');
249249
}
250250
f.write_str(&s)
@@ -255,10 +255,10 @@ impl From<RelativeRemapping> for Remapping {
255255
fn from(r: RelativeRemapping) -> Self {
256256
let RelativeRemapping { context, mut name, path } = r;
257257
let mut path = path.relative().display().to_string();
258-
if !path.ends_with('/') && !path.ends_with(".sol") {
258+
if needs_trailing_slash(&path) {
259259
path.push('/');
260260
}
261-
if !name.ends_with('/') && !name.ends_with(".sol") {
261+
if needs_trailing_slash(&name) {
262262
name.push('/');
263263
}
264264
Self { context, name, path }
@@ -344,6 +344,15 @@ impl<'de> Deserialize<'de> for RelativeRemapping {
344344
}
345345
}
346346

347+
/// Helper to determine if name or path of a remapping needs trailing slash.
348+
/// Returns false if it already ends with a slash or if remapping is a solidity file.
349+
/// Used to preserve name and path of single file remapping, see
350+
/// <https://github.com/foundry-rs/foundry/issues/6706>
351+
/// <https://github.com/foundry-rs/foundry/issues/8499>
352+
fn needs_trailing_slash(name_or_path: &str) -> bool {
353+
!name_or_path.ends_with('/') && !name_or_path.ends_with(".sol")
354+
}
355+
347356
#[cfg(test)]
348357
mod tests {
349358
pub use super::*;

0 commit comments

Comments
 (0)