Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

Commit 0d2fc53

Browse files
authored
fix(solc): prefere dapptools style remappings (#713)
1 parent ef13818 commit 0d2fc53

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

Diff for: ethers-solc/src/remappings.rs

+38-19
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,16 @@ impl Remapping {
135135
/// are unified into `@aave` by looking at their common ancestor, the root of this subdirectory
136136
/// (`@aave`)
137137
pub fn find_many(root: impl AsRef<Path>) -> Vec<Remapping> {
138-
/// prioritize ("a", "1/2") over ("a", "1/2/3")
139-
fn insert_higher_path(mappings: &mut HashMap<String, PathBuf>, key: String, path: PathBuf) {
138+
/// prioritize ("a", "1/2") over ("a", "1/2/3") or if `force` set
139+
fn insert_higher_path(
140+
mappings: &mut HashMap<String, PathBuf>,
141+
key: String,
142+
path: PathBuf,
143+
force: bool,
144+
) {
140145
match mappings.entry(key) {
141146
Entry::Occupied(mut e) => {
142-
if e.get().components().count() > path.components().count() {
147+
if force || e.get().components().count() > path.components().count() {
143148
e.insert(path);
144149
}
145150
}
@@ -181,7 +186,27 @@ impl Remapping {
181186
'outer: for (name, path) in scan_children(dir_path) {
182187
let mut p = path.as_path();
183188
let mut first_parent = true;
189+
190+
// check for dapptools style mappings like `ds-test/` : `lib/ds-test/src`
191+
if path.ends_with(DAPPTOOLS_CONTRACTS_DIR) || path.ends_with(DAPPTOOLS_LIB_DIR)
192+
{
193+
insert_higher_path(&mut remappings, name, path, true);
194+
continue
195+
}
196+
197+
// traverse the path back to the current depth 1 root and check if it can be
198+
// simplified
184199
while let Some(parent) = p.parent() {
200+
// check for dapptools style mappings like `ds-test/` : `lib/ds-test/src`
201+
if parent.ends_with(DAPPTOOLS_CONTRACTS_DIR) ||
202+
parent.ends_with(DAPPTOOLS_LIB_DIR)
203+
{
204+
// end early since we reached the higher up dapptools style barrier:
205+
// `name: demo`, `path: guni-lev/lib/ds-test/demo` and `parent:
206+
// guni-lev/lib/`
207+
insert_higher_path(&mut remappings, name, path, false);
208+
continue 'outer
209+
}
185210
if parent == dir_path {
186211
if !simplified {
187212
// handle trailing src,lib,contracts dir in cases like
@@ -191,18 +216,12 @@ impl Remapping {
191216
&mut remappings,
192217
format!("{}/", root_name),
193218
path,
219+
false,
194220
);
195221
simplified = true;
196222
}
197223
continue 'outer
198224
}
199-
if parent.ends_with(DAPPTOOLS_CONTRACTS_DIR) ||
200-
parent.ends_with(DAPPTOOLS_LIB_DIR)
201-
{
202-
// end early
203-
insert_higher_path(&mut remappings, name, path);
204-
continue 'outer
205-
}
206225
first_parent = false;
207226
p = parent;
208227
}
@@ -261,7 +280,6 @@ fn scan_children(root: &Path) -> HashMap<String, PathBuf> {
261280
}
262281
}
263282
}
264-
265283
remappings
266284
}
267285

@@ -336,6 +354,10 @@ mod tests {
336354
"repo1/lib/ds-math/src/contract.sol",
337355
"repo1/lib/ds-math/lib/ds-test/src/",
338356
"repo1/lib/ds-math/lib/ds-test/src/test.sol",
357+
"guni-lev/lib/ds-test/src/",
358+
"guni-lev/lib/ds-test/src/test.sol",
359+
"guni-lev/lib/ds-test/demo/",
360+
"guni-lev/lib/ds-test/demo/demo.sol",
339361
];
340362
mkdir_or_touch(tmp_dir_path, &paths[..]);
341363

@@ -354,14 +376,12 @@ mod tests {
354376
},
355377
Remapping {
356378
name: "ds-test/".to_string(),
379+
path: to_str(tmp_dir_path.join("guni-lev").join("lib").join("ds-test").join("src")),
380+
},
381+
Remapping {
382+
name: "demo/".to_string(),
357383
path: to_str(
358-
tmp_dir_path
359-
.join("repo1")
360-
.join("lib")
361-
.join("ds-math")
362-
.join("lib")
363-
.join("ds-test")
364-
.join("src"),
384+
tmp_dir_path.join("guni-lev").join("lib").join("ds-test").join("demo"),
365385
),
366386
},
367387
];
@@ -432,7 +452,6 @@ mod tests {
432452
},
433453
];
434454
expected.sort_unstable();
435-
436455
assert_eq!(remappings, expected);
437456
}
438457
}

0 commit comments

Comments
 (0)