Skip to content

Commit

Permalink
Merge pull request #3305 from NomicFoundation/buildinfo-names-are-not…
Browse files Browse the repository at this point in the history
…-deterministic-hh-1230

Sort files when creating the solc input
  • Loading branch information
fvictorio committed Nov 1, 2022
2 parents c2bd001 + c6420a3 commit 6cb5e0b
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/mean-mice-tell.md
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fixed an issue that was causing build-info file names to not be deterministic.
Expand Up @@ -4,7 +4,13 @@ export function getInputFromCompilationJob(
compilationJob: CompilationJob
): CompilerInput {
const sources: { [sourceName: string]: { content: string } } = {};
for (const file of compilationJob.getResolvedFiles()) {

// we sort the files so that we always get the same compilation input
const resolvedFiles = compilationJob
.getResolvedFiles()
.sort((a, b) => a.sourceName.localeCompare(b.sourceName));

for (const file of resolvedFiles) {
sources[file.sourceName] = {
content: file.content.rawContent,
};
Expand Down
27 changes: 27 additions & 0 deletions packages/hardhat-core/test/builtin-tasks/compile.ts
@@ -1,4 +1,5 @@
import { assert } from "chai";
import ci from "ci-info";
import * as fsExtra from "fs-extra";
import * as path from "path";

Expand Down Expand Up @@ -817,4 +818,30 @@ Read about compiler configuration at https://hardhat.org/config
});
});
});

describe("project where two contracts import the same dependency", function () {
useFixtureProject("consistent-build-info-names");
useEnvironment();

it("should always produce the same build-info name", async function () {
await this.env.run("compile");

const buildInfos = getBuildInfos();
assert.lengthOf(buildInfos, 1);

const expectedBuildInfoName = buildInfos[0];

const runs = ci.isCI ? 10 : 100;

for (let i = 0; i < runs; i++) {
await this.env.run("clean");
await this.env.run("compile");

const newBuildInfos = getBuildInfos();
assert.lengthOf(newBuildInfos, 1);

assert.equal(newBuildInfos[0], expectedBuildInfoName);
}
});
});
});
@@ -0,0 +1,2 @@
artifacts/
cache/
@@ -0,0 +1,6 @@
pragma solidity ^0.7.0;

import "dependency/contracts/Dep1.sol";
import "dependency/contracts/Dep2.sol";

contract A {}
@@ -0,0 +1,5 @@
pragma solidity ^0.7.0;

import "dependency/contracts/Dep1.sol";

contract B {}
@@ -0,0 +1,3 @@
module.exports = {
solidity: "0.7.3",
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6cb5e0b

Please sign in to comment.