From 0395e08a2f8b7042a656867f2ac13420cfe87fec Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Thu, 7 Jul 2022 07:21:55 -0700 Subject: [PATCH 1/3] feat(hardhat-forge): build info path config (#84) Allow the `build_info_path` config option to be parsed and passed through the hardhat plugin. It will correctly create the debug files that point to the build info files dynamically based on the configured build info path. The tests that fetch the build info objects still pass, they look up the path from the debug file to the build info file. --- .changeset/late-rivers-applaud.md | 5 +++++ packages/hardhat-forge/src/forge/build/build.ts | 4 ++++ packages/hardhat-forge/src/forge/config/config.ts | 1 + packages/hardhat-forge/src/index.ts | 7 ++++--- .../test/fixture-projects/hardhat-project/foundry.toml | 1 + 5 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 .changeset/late-rivers-applaud.md diff --git a/.changeset/late-rivers-applaud.md b/.changeset/late-rivers-applaud.md new file mode 100644 index 0000000..1f5f462 --- /dev/null +++ b/.changeset/late-rivers-applaud.md @@ -0,0 +1,5 @@ +--- +"@foundry-rs/hardhat-forge": patch +--- + +Add build_info_path config diff --git a/packages/hardhat-forge/src/forge/build/build.ts b/packages/hardhat-forge/src/forge/build/build.ts index 4d60a4e..7cbfa5f 100644 --- a/packages/hardhat-forge/src/forge/build/build.ts +++ b/packages/hardhat-forge/src/forge/build/build.ts @@ -22,6 +22,7 @@ export declare interface ForgeBuildArgs extends CompilerArgs, ProjectPathArgs { offline?: boolean; viaIr?: boolean; buildInfo?: boolean; + buildInfoPath?: string; } /** * @@ -79,6 +80,9 @@ export function buildArgs(args: ForgeBuildArgs): string[] { if (args.buildInfo === true) { allArgs.push("--build-info"); } + if (typeof args.buildInfoPath === "string") { + allArgs.push("--build-info-path", args.buildInfoPath); + } allArgs.push(...compilerArgs(args)); allArgs.push(...projectPathsArgs(args)); diff --git a/packages/hardhat-forge/src/forge/config/config.ts b/packages/hardhat-forge/src/forge/config/config.ts index 7cce59d..aedbc0c 100644 --- a/packages/hardhat-forge/src/forge/config/config.ts +++ b/packages/hardhat-forge/src/forge/config/config.ts @@ -64,4 +64,5 @@ export declare interface FoundryConfig { revert_strings?: any; sparse_mode?: boolean; build_info?: boolean; + build_info_path?: string; } diff --git a/packages/hardhat-forge/src/index.ts b/packages/hardhat-forge/src/index.ts index f47ec72..6b8c35f 100644 --- a/packages/hardhat-forge/src/index.ts +++ b/packages/hardhat-forge/src/index.ts @@ -17,9 +17,10 @@ extendEnvironment((hre: HardhatRuntimeEnvironment) => { (hre as any).artifacts = lazyObject(() => { const config = spawnConfigSync(); const outDir = path.join(hre.config.paths.root, config.out); - // the build info directory is not currently configurable, - // it will always be placed in out/build-info - const buildInfoDir = path.join(outDir, "build-info"); + const buildInfoDir = + typeof config.build_info_path === "string" + ? config.build_info_path + : path.join(outDir, "build-info"); const artifacts = new ForgeArtifacts( hre.config.paths.root, diff --git a/packages/hardhat-forge/test/fixture-projects/hardhat-project/foundry.toml b/packages/hardhat-forge/test/fixture-projects/hardhat-project/foundry.toml index 776a766..3ffa250 100644 --- a/packages/hardhat-forge/test/fixture-projects/hardhat-project/foundry.toml +++ b/packages/hardhat-forge/test/fixture-projects/hardhat-project/foundry.toml @@ -5,5 +5,6 @@ libs = ['lib'] extra_output = ['devdoc', 'userdoc', 'metadata', 'storageLayout'] build_info = true +build_info_path = 'build-info' # See more config options https://github.com/foundry-rs/foundry/tree/master/config From f2d3db98a63e3d2cc3b2a1107ef062c2de5d8607 Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Sun, 24 Jul 2022 03:57:24 -0700 Subject: [PATCH 2/3] hardhat-forge: parse string metadata (#85) * hardhat-forge: failing test It should be generating metadata as an object, not as a string. Its a JSON string that is double stringified. * hardhat-forge: parse build info metadata Deserialize the build info compiler output as an object because ethers-solc serializes it as a string. Hardhat plugins expect it to be an object and not a string. * chore: add changeset foundry-rs/hardhat-forge --- .changeset/lazy-news-hide.md | 5 +++++ packages/hardhat-forge/src/forge/artifacts.ts | 10 ++++++++++ packages/hardhat-forge/test/project.test.ts | 1 + 3 files changed, 16 insertions(+) create mode 100644 .changeset/lazy-news-hide.md diff --git a/.changeset/lazy-news-hide.md b/.changeset/lazy-news-hide.md new file mode 100644 index 0000000..fda7a9a --- /dev/null +++ b/.changeset/lazy-news-hide.md @@ -0,0 +1,5 @@ +--- +"@foundry-rs/hardhat-forge": patch +--- + +Deserialize compiler output metadata in buildinfo diff --git a/packages/hardhat-forge/src/forge/artifacts.ts b/packages/hardhat-forge/src/forge/artifacts.ts index ada90dc..fd88945 100644 --- a/packages/hardhat-forge/src/forge/artifacts.ts +++ b/packages/hardhat-forge/src/forge/artifacts.ts @@ -143,6 +143,16 @@ export class ForgeArtifacts implements IArtifacts { } const buildInfo = fsExtra.readJsonSync(buildInfoPath) as BuildInfo; + + // Handle ethers-solc serializing the metadata as a string + // when hardhat serializes it as an object + for (const contract of Object.values(buildInfo.output.contracts)) { + for (const output of Object.values(contract)) { + if (typeof (output as any).metadata === "string") { + (output as any).metadata = JSON.parse((output as any).metadata); + } + } + } return buildInfo; } diff --git a/packages/hardhat-forge/test/project.test.ts b/packages/hardhat-forge/test/project.test.ts index a8fec84..5bdacfe 100644 --- a/packages/hardhat-forge/test/project.test.ts +++ b/packages/hardhat-forge/test/project.test.ts @@ -78,6 +78,7 @@ describe("Integration tests", function () { assert.exists(contract.abi); assert.exists((contract as any).devdoc); assert.exists((contract as any).metadata); + assert.typeOf((contract as any).metadata, "object"); assert.exists((contract as any).storageLayout); assert.exists((contract as any).userdoc); assert.exists(contract.evm); From ec00b641545687916bf5779260fb20fbceffead0 Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Sun, 24 Jul 2022 03:57:24 -0700 Subject: [PATCH 3/3] hardhat-forge: parse string metadata (#85) * hardhat-forge: failing test It should be generating metadata as an object, not as a string. Its a JSON string that is double stringified. * hardhat-forge: parse build info metadata Deserialize the build info compiler output as an object because ethers-solc serializes it as a string. Hardhat plugins expect it to be an object and not a string. * chore: add changeset foundry-rs/hardhat-forge --- .changeset/lazy-news-hide.md | 5 +++++ packages/hardhat-forge/src/forge/artifacts.ts | 10 ++++++++++ packages/hardhat-forge/test/project.test.ts | 1 + 3 files changed, 16 insertions(+) create mode 100644 .changeset/lazy-news-hide.md diff --git a/.changeset/lazy-news-hide.md b/.changeset/lazy-news-hide.md new file mode 100644 index 0000000..fda7a9a --- /dev/null +++ b/.changeset/lazy-news-hide.md @@ -0,0 +1,5 @@ +--- +"@foundry-rs/hardhat-forge": patch +--- + +Deserialize compiler output metadata in buildinfo diff --git a/packages/hardhat-forge/src/forge/artifacts.ts b/packages/hardhat-forge/src/forge/artifacts.ts index ada90dc..fd88945 100644 --- a/packages/hardhat-forge/src/forge/artifacts.ts +++ b/packages/hardhat-forge/src/forge/artifacts.ts @@ -143,6 +143,16 @@ export class ForgeArtifacts implements IArtifacts { } const buildInfo = fsExtra.readJsonSync(buildInfoPath) as BuildInfo; + + // Handle ethers-solc serializing the metadata as a string + // when hardhat serializes it as an object + for (const contract of Object.values(buildInfo.output.contracts)) { + for (const output of Object.values(contract)) { + if (typeof (output as any).metadata === "string") { + (output as any).metadata = JSON.parse((output as any).metadata); + } + } + } return buildInfo; } diff --git a/packages/hardhat-forge/test/project.test.ts b/packages/hardhat-forge/test/project.test.ts index a8fec84..5bdacfe 100644 --- a/packages/hardhat-forge/test/project.test.ts +++ b/packages/hardhat-forge/test/project.test.ts @@ -78,6 +78,7 @@ describe("Integration tests", function () { assert.exists(contract.abi); assert.exists((contract as any).devdoc); assert.exists((contract as any).metadata); + assert.typeOf((contract as any).metadata, "object"); assert.exists((contract as any).storageLayout); assert.exists((contract as any).userdoc); assert.exists(contract.evm);