Skip to content

Commit d145258

Browse files
committed
dapp: remappings: add flag for legacy compat
1 parent fb0cbe0 commit d145258

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/dapp-tests/integration/tests.sh

+11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ dapp_remappings() {
6060

6161
dapp_remappings
6262

63+
# tests dapp remappings on a large legacy project with many implicit imports
64+
dapp_remappings_compat() {
65+
REV="bc6d7657f0f5190f65051543199e1b47bf29932b"
66+
TMPDIR=$(mktemp -d)
67+
git clone https://github.com/dapp-org/tinlake-tests "$TMPDIR"
68+
export DAPP_ALLOW_IMPLICIT_IMPORTS=yes
69+
(cd "$TMPDIR" && git checkout "$REV" && dapp update && dapp --use solc:0.7.6 build)
70+
}
71+
72+
dapp_remappings_compat
73+
6374
test_hevm_symbolic() {
6475
solc --bin-runtime -o . --overwrite factor.sol
6576
# should find counterexample

src/dapp/default.nix

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{ lib, stdenv, fetchFromGitHub, makeWrapper, glibcLocales
2-
, coreutils, git, gnused, gnumake, hevm, jshon, jq, nix
3-
, nodejs, perl, python3, seth, shellcheck, solc, tre, dapptoolsSrc }:
2+
, coreutils, findutils, ripgrep, git, gnused, gnumake, hevm
3+
, jshon, jq, nix, nodejs, perl, python3, seth, shellcheck, solc
4+
, tre, dapptoolsSrc }:
45

56
stdenv.mkDerivation rec {
67
name = "dapp-${version}";
@@ -16,7 +17,7 @@ stdenv.mkDerivation rec {
1617
postInstall =
1718
let
1819
path = lib.makeBinPath [
19-
coreutils git gnused gnumake hevm jshon jq nix nodejs perl seth solc tre python3
20+
coreutils findutils git gnused gnumake hevm jshon jq nix nodejs perl seth solc tre python3 ripgrep
2021
];
2122
in
2223
''

src/dapp/libexec/dapp/dapp-remappings

+31-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,20 @@ function buildDependencyTree(prefix) {
2525

2626
// walk tree and build remappings
2727
function buildRemappings(pkg) {
28-
const remappings = pkg.deps.map(dep => {
29-
return `${pkg.path}/:${dep.name}/=${dep.path}/`
30-
})
31-
return pkg.deps.map(buildRemappings).concat(remappings).flat()
28+
const paths = mapHashes(pkg)
29+
30+
let implicits = []
31+
if (process.env.DAPP_ALLOW_IMPLICIT_IMPORTS == "yes") {
32+
const directs = pkg.deps.map(p => p.name)
33+
// get the transitive deps with exactly one version that are not direct deps
34+
const uniques = Object.entries(pkg.deps.reduce(buildVersionMap, {}))
35+
.filter(([name, vs]) => vs.size == 1 && !directs.includes(name))
36+
// build remappings that allow importing these deps from `pkg`
37+
implicits = uniques.map(([name, v]) => `${pkg.path}/:${name}/=${paths[([...v][0])]}/`)
38+
}
39+
40+
const remappings = pkg.deps.map(dep => `${pkg.path}/:${dep.name}/=${dep.path}/`)
41+
return pkg.deps.map(buildRemappings).concat(remappings).concat(implicits).flat()
3242
}
3343

3444
// walk tree and rewrite paths so that all packages with the same hash have the same path
@@ -47,6 +57,21 @@ function mapHashes(pkg) {
4757
return pkg.deps.reduce(go, { [pkg.hash]: pkg.path })
4858
}
4959

60+
// folds over a dependency tree with map as the accumlator and builds a mapping
61+
// from package name to a set of discovered package verions
62+
function buildVersionMap(map, pkg) {
63+
const update = (map, dep) => {
64+
map[dep.name] == undefined
65+
? map[dep.name] = new Set([dep.hash])
66+
: map[dep.name].add(dep.hash)
67+
return map
68+
}
69+
70+
return pkg.deps.reduce((versions, dep) => {
71+
return update(versions, dep)
72+
}, update(map, pkg))
73+
}
74+
5075
// strip the leading `.` or `./` from a path
5176
function normalize(path) {
5277
return path.replace(/^\.\//, "").replace(/^\//, "")
@@ -56,9 +81,9 @@ function normalize(path) {
5681
// availalbe (because it's faster), or falls back to a sha256sum of the directory contents if needed
5782
function hash(dir) {
5883
if (ls(dir).includes(".git")) {
59-
return run("git", ["-C", dir, "rev-parse", "HEAD"])
84+
return run("git", ["-C", dir, "rev-parse", "HEAD"]).trim()
6085
} else {
61-
return run("bash", ["-c", `rg --files ${dir} | sort | xargs sha256sum | sha256sum`])
86+
return run("bash", ["-c", `rg --files ${dir} | sort | xargs sha256sum | sha256sum | cut -d' ' -f1`]).trim()
6287
}
6388
}
6489

0 commit comments

Comments
 (0)