diff --git a/.changeset/mean-mice-tell.md b/.changeset/mean-mice-tell.md new file mode 100644 index 0000000000..c01d618b7e --- /dev/null +++ b/.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. diff --git a/packages/hardhat-core/src/internal/solidity/compiler/compiler-input.ts b/packages/hardhat-core/src/internal/solidity/compiler/compiler-input.ts index 045b8633a8..e1f2cadcdc 100644 --- a/packages/hardhat-core/src/internal/solidity/compiler/compiler-input.ts +++ b/packages/hardhat-core/src/internal/solidity/compiler/compiler-input.ts @@ -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, }; diff --git a/packages/hardhat-core/test/builtin-tasks/compile.ts b/packages/hardhat-core/test/builtin-tasks/compile.ts index 0c5c120e27..3be4a6ea0b 100644 --- a/packages/hardhat-core/test/builtin-tasks/compile.ts +++ b/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"; @@ -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); + } + }); + }); }); diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/.gitignore b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/.gitignore new file mode 100644 index 0000000000..e7f801166c --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/.gitignore @@ -0,0 +1,2 @@ +artifacts/ +cache/ diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/contracts/A.sol b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/contracts/A.sol new file mode 100644 index 0000000000..ad5f5e77cb --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/contracts/A.sol @@ -0,0 +1,6 @@ +pragma solidity ^0.7.0; + +import "dependency/contracts/Dep1.sol"; +import "dependency/contracts/Dep2.sol"; + +contract A {} diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/contracts/B.sol b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/contracts/B.sol new file mode 100644 index 0000000000..0289d130fc --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/contracts/B.sol @@ -0,0 +1,5 @@ +pragma solidity ^0.7.0; + +import "dependency/contracts/Dep1.sol"; + +contract B {} diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/hardhat.config.js b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/hardhat.config.js new file mode 100644 index 0000000000..38f2a55388 --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/hardhat.config.js @@ -0,0 +1,3 @@ +module.exports = { + solidity: "0.7.3", +}; diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/contracts/Dep1.sol b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/contracts/Dep1.sol new file mode 100644 index 0000000000..c3cbfee01f --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/contracts/Dep1.sol @@ -0,0 +1 @@ +pragma solidity ^0.7.0; diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/contracts/Dep2.sol b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/contracts/Dep2.sol new file mode 100644 index 0000000000..c3cbfee01f --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/contracts/Dep2.sol @@ -0,0 +1 @@ +pragma solidity ^0.7.0; diff --git a/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/package.json b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/package.json new file mode 100644 index 0000000000..2c30dd0e49 --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/consistent-build-info-names/node_modules/dependency/package.json @@ -0,0 +1,4 @@ +{ + "name": "dependency", + "version": "1.2.3" +}