fix(solc): prefere dapptools style remappings (#713)

This commit is contained in:
Matthias Seitz 2021-12-19 21:34:06 +01:00 committed by GitHub
parent ef13818e82
commit 0d2fc53541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 19 deletions

View File

@ -135,11 +135,16 @@ impl Remapping {
/// are unified into `@aave` by looking at their common ancestor, the root of this subdirectory /// are unified into `@aave` by looking at their common ancestor, the root of this subdirectory
/// (`@aave`) /// (`@aave`)
pub fn find_many(root: impl AsRef<Path>) -> Vec<Remapping> { pub fn find_many(root: impl AsRef<Path>) -> Vec<Remapping> {
/// prioritize ("a", "1/2") over ("a", "1/2/3") /// 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) { fn insert_higher_path(
mappings: &mut HashMap<String, PathBuf>,
key: String,
path: PathBuf,
force: bool,
) {
match mappings.entry(key) { match mappings.entry(key) {
Entry::Occupied(mut e) => { Entry::Occupied(mut e) => {
if e.get().components().count() > path.components().count() { if force || e.get().components().count() > path.components().count() {
e.insert(path); e.insert(path);
} }
} }
@ -181,7 +186,27 @@ impl Remapping {
'outer: for (name, path) in scan_children(dir_path) { 'outer: for (name, path) in scan_children(dir_path) {
let mut p = path.as_path(); let mut p = path.as_path();
let mut first_parent = true; 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() { 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 parent == dir_path {
if !simplified { if !simplified {
// handle trailing src,lib,contracts dir in cases like // handle trailing src,lib,contracts dir in cases like
@ -191,18 +216,12 @@ impl Remapping {
&mut remappings, &mut remappings,
format!("{}/", root_name), format!("{}/", root_name),
path, path,
false,
); );
simplified = true; simplified = true;
} }
continue 'outer 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; first_parent = false;
p = parent; p = parent;
} }
@ -261,7 +280,6 @@ fn scan_children(root: &Path) -> HashMap<String, PathBuf> {
} }
} }
} }
remappings remappings
} }
@ -336,6 +354,10 @@ mod tests {
"repo1/lib/ds-math/src/contract.sol", "repo1/lib/ds-math/src/contract.sol",
"repo1/lib/ds-math/lib/ds-test/src/", "repo1/lib/ds-math/lib/ds-test/src/",
"repo1/lib/ds-math/lib/ds-test/src/test.sol", "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[..]); mkdir_or_touch(tmp_dir_path, &paths[..]);
@ -354,14 +376,12 @@ mod tests {
}, },
Remapping { Remapping {
name: "ds-test/".to_string(), 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( path: to_str(
tmp_dir_path tmp_dir_path.join("guni-lev").join("lib").join("ds-test").join("demo"),
.join("repo1")
.join("lib")
.join("ds-math")
.join("lib")
.join("ds-test")
.join("src"),
), ),
}, },
]; ];
@ -432,7 +452,6 @@ mod tests {
}, },
]; ];
expected.sort_unstable(); expected.sort_unstable();
assert_eq!(remappings, expected); assert_eq!(remappings, expected);
} }
} }