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(solc): prefere dapptools style remappings #713

Merged
merged 1 commit into from
Dec 19, 2021
Merged
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
57 changes: 38 additions & 19 deletions ethers-solc/src/remappings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ impl Remapping {
/// are unified into `@aave` by looking at their common ancestor, the root of this subdirectory
/// (`@aave`)
pub fn find_many(root: impl AsRef<Path>) -> Vec<Remapping> {
/// prioritize ("a", "1/2") over ("a", "1/2/3")
fn insert_higher_path(mappings: &mut HashMap<String, PathBuf>, key: String, path: PathBuf) {
/// prioritize ("a", "1/2") over ("a", "1/2/3") or if `force` set
fn insert_higher_path(
mappings: &mut HashMap<String, PathBuf>,
key: String,
path: PathBuf,
force: bool,
) {
match mappings.entry(key) {
Entry::Occupied(mut e) => {
if e.get().components().count() > path.components().count() {
if force || e.get().components().count() > path.components().count() {
e.insert(path);
}
}
Expand Down Expand Up @@ -181,7 +186,27 @@ impl Remapping {
'outer: for (name, path) in scan_children(dir_path) {
let mut p = path.as_path();
let mut first_parent = true;

// check for dapptools style mappings like `ds-test/` : `lib/ds-test/src`
if path.ends_with(DAPPTOOLS_CONTRACTS_DIR) || path.ends_with(DAPPTOOLS_LIB_DIR)
{
insert_higher_path(&mut remappings, name, path, true);
continue
}

// traverse the path back to the current depth 1 root and check if it can be
// simplified
while let Some(parent) = p.parent() {
// check for dapptools style mappings like `ds-test/` : `lib/ds-test/src`
if parent.ends_with(DAPPTOOLS_CONTRACTS_DIR) ||
parent.ends_with(DAPPTOOLS_LIB_DIR)
{
// end early since we reached the higher up dapptools style barrier:
// `name: demo`, `path: guni-lev/lib/ds-test/demo` and `parent:
// guni-lev/lib/`
insert_higher_path(&mut remappings, name, path, false);
continue 'outer
}
if parent == dir_path {
if !simplified {
// handle trailing src,lib,contracts dir in cases like
Expand All @@ -191,18 +216,12 @@ impl Remapping {
&mut remappings,
format!("{}/", root_name),
path,
false,
);
simplified = true;
}
continue 'outer
}
if parent.ends_with(DAPPTOOLS_CONTRACTS_DIR) ||
parent.ends_with(DAPPTOOLS_LIB_DIR)
{
// end early
insert_higher_path(&mut remappings, name, path);
continue 'outer
}
first_parent = false;
p = parent;
}
Expand Down Expand Up @@ -261,7 +280,6 @@ fn scan_children(root: &Path) -> HashMap<String, PathBuf> {
}
}
}

remappings
}

Expand Down Expand Up @@ -336,6 +354,10 @@ mod tests {
"repo1/lib/ds-math/src/contract.sol",
"repo1/lib/ds-math/lib/ds-test/src/",
"repo1/lib/ds-math/lib/ds-test/src/test.sol",
"guni-lev/lib/ds-test/src/",
"guni-lev/lib/ds-test/src/test.sol",
"guni-lev/lib/ds-test/demo/",
"guni-lev/lib/ds-test/demo/demo.sol",
];
mkdir_or_touch(tmp_dir_path, &paths[..]);

Expand All @@ -354,14 +376,12 @@ mod tests {
},
Remapping {
name: "ds-test/".to_string(),
path: to_str(tmp_dir_path.join("guni-lev").join("lib").join("ds-test").join("src")),
},
Remapping {
name: "demo/".to_string(),
path: to_str(
tmp_dir_path
.join("repo1")
.join("lib")
.join("ds-math")
.join("lib")
.join("ds-test")
.join("src"),
tmp_dir_path.join("guni-lev").join("lib").join("ds-test").join("demo"),
),
},
];
Expand Down Expand Up @@ -432,7 +452,6 @@ mod tests {
},
];
expected.sort_unstable();

assert_eq!(remappings, expected);
}
}