diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts index 807811926c..2866988aca 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity-test/helpers.ts @@ -1,12 +1,3 @@ -import type { - Artifact as HardhatArtifact, - BuildInfo, -} from "../../../types/artifacts.js"; -import type { - CompilationJobCreationError, - FailedFileBuildResult, - FileBuildResult, -} from "../../../types/solidity/build-system.js"; import type { ArtifactId as EdrArtifactId, Artifact as EdrArtifact, @@ -14,89 +5,6 @@ import type { import path from "node:path"; -import { HardhatError } from "@ignored/hardhat-vnext-errors"; -import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs"; - -import { FileBuildResultType } from "../../../types/solidity/build-system.js"; - -type SolidityBuildResults = - | Map - | CompilationJobCreationError; -type SuccessfulSolidityBuildResults = Map< - string, - Exclude ->; - -/** - * This function asserts that the given Solidity build results are successful. - * It throws a HardhatError if the build results indicate that the compilation - * job failed. - */ -export function throwIfSolidityBuildFailed( - results: SolidityBuildResults, -): asserts results is SuccessfulSolidityBuildResults { - if ("reason" in results) { - throw new HardhatError( - HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR, - { - reason: results.formattedReason, - rootFilePath: results.rootFilePath, - buildProfile: results.buildProfile, - }, - ); - } - - const sucessful = [...results.values()].every( - ({ type }) => - type === FileBuildResultType.CACHE_HIT || - type === FileBuildResultType.BUILD_SUCCESS, - ); - - if (!sucessful) { - throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED); - } -} - -/** - * This function returns the artifacts generated during the compilation associated - * with the given Solidity build results. It relies on the fact that each successful - * build result has a corresponding artifact generated property. - */ -export async function getArtifacts( - results: SuccessfulSolidityBuildResults, - artifactsRootPath: string, -): Promise { - const artifacts: EdrArtifact[] = []; - - for (const [source, result] of results.entries()) { - for (const artifactPath of result.contractArtifactsGenerated) { - const artifact: HardhatArtifact = await readJsonFile(artifactPath); - const buildInfo: BuildInfo = await readJsonFile( - path.join(artifactsRootPath, "build-info", `${result.buildId}.json`), - ); - - const id = { - name: artifact.contractName, - solcVersion: buildInfo.solcVersion, - source, - }; - - const contract = { - abi: JSON.stringify(artifact.abi), - bytecode: artifact.bytecode, - deployedBytecode: artifact.deployedBytecode, - }; - - artifacts.push({ - id, - contract, - }); - } - } - - return artifacts; -} - /** * This function returns the test suite ids associated with the given artifacts. * The test suite ID is the relative path of the test file, relative to the diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts index 23e5070ccd..ad7703b74a 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity-test/task-action.ts @@ -9,12 +9,12 @@ import { getAllFilesMatching } from "@ignored/hardhat-vnext-utils/fs"; import { createNonClosingWriter } from "@ignored/hardhat-vnext-utils/stream"; import { shouldMergeCompilationJobs } from "../solidity/build-profiles.js"; - import { getArtifacts, - getTestSuiteIds, throwIfSolidityBuildFailed, -} from "./helpers.js"; +} from "../solidity/build-results.js"; + +import { getTestSuiteIds } from "./helpers.js"; import { testReporter } from "./reporter.js"; import { run } from "./runner.js"; diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity/build-results.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity/build-results.ts new file mode 100644 index 0000000000..40520c4ba8 --- /dev/null +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity/build-results.ts @@ -0,0 +1,95 @@ +import type { + Artifact as HardhatArtifact, + BuildInfo, +} from "../../../types/artifacts.js"; +import type { Artifact as EdrArtifact } from "@ignored/edr"; + +import path from "node:path"; + +import { HardhatError } from "@ignored/hardhat-vnext-errors"; +import { readJsonFile } from "@ignored/hardhat-vnext-utils/fs"; + +import { + FileBuildResultType, + type CompilationJobCreationError, + type FailedFileBuildResult, + type FileBuildResult, +} from "../../../types/solidity.js"; + +type SolidityBuildResults = + | Map + | CompilationJobCreationError; +type SuccessfulSolidityBuildResults = Map< + string, + Exclude +>; + +/** + * This function asserts that the given Solidity build results are successful. + * It throws a HardhatError if the build results indicate that the compilation + * job failed. + */ +export function throwIfSolidityBuildFailed( + results: SolidityBuildResults, +): asserts results is SuccessfulSolidityBuildResults { + if ("reason" in results) { + throw new HardhatError( + HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR, + { + reason: results.formattedReason, + rootFilePath: results.rootFilePath, + buildProfile: results.buildProfile, + }, + ); + } + + const sucessful = [...results.values()].every( + ({ type }) => + type === FileBuildResultType.CACHE_HIT || + type === FileBuildResultType.BUILD_SUCCESS, + ); + + if (!sucessful) { + throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED); + } +} + +/** + * This function returns the artifacts generated during the compilation associated + * with the given Solidity build results. It relies on the fact that each successful + * build result has a corresponding artifact generated property. + */ +export async function getArtifacts( + results: SuccessfulSolidityBuildResults, + artifactsRootPath: string, +): Promise { + const artifacts: EdrArtifact[] = []; + + for (const [source, result] of results.entries()) { + for (const artifactPath of result.contractArtifactsGenerated) { + const artifact: HardhatArtifact = await readJsonFile(artifactPath); + const buildInfo: BuildInfo = await readJsonFile( + path.join(artifactsRootPath, "build-info", `${result.buildId}.json`), + ); + + const id = { + name: artifact.contractName, + solcVersion: buildInfo.solcVersion, + source, + }; + + const contract = { + abi: JSON.stringify(artifact.abi), + bytecode: artifact.bytecode, + deployedBytecode: artifact.deployedBytecode, + }; + + artifacts.push({ + id, + contract, + }); + } + } + + return artifacts; +} diff --git a/v-next/hardhat/src/internal/builtin-plugins/solidity/tasks/compile.ts b/v-next/hardhat/src/internal/builtin-plugins/solidity/tasks/compile.ts index 0bb657db05..b4e341f25d 100644 --- a/v-next/hardhat/src/internal/builtin-plugins/solidity/tasks/compile.ts +++ b/v-next/hardhat/src/internal/builtin-plugins/solidity/tasks/compile.ts @@ -1,10 +1,9 @@ import type { NewTaskActionFunction } from "../../../../types/tasks.js"; -import { HardhatError } from "@ignored/hardhat-vnext-errors"; import { resolveFromRoot } from "@ignored/hardhat-vnext-utils/path"; -import { FileBuildResultType } from "../../../../types/solidity.js"; import { shouldMergeCompilationJobs } from "../build-profiles.js"; +import { throwIfSolidityBuildFailed } from "../build-results.js"; import { isNpmRootPath } from "../build-system/root-paths-utils.js"; interface CompileActionArguments { @@ -37,26 +36,7 @@ const compileAction: NewTaskActionFunction = async ( quiet, }); - if ("reason" in results) { - throw new HardhatError( - HardhatError.ERRORS.SOLIDITY.COMPILATION_JOB_CREATION_ERROR, - { - reason: results.formattedReason, - rootFilePath: results.rootFilePath, - buildProfile: results.buildProfile, - }, - ); - } - - const sucessful = [...results.values()].every( - ({ type }) => - type === FileBuildResultType.CACHE_HIT || - type === FileBuildResultType.BUILD_SUCCESS, - ); - - if (!sucessful) { - throw new HardhatError(HardhatError.ERRORS.SOLIDITY.BUILD_FAILED); - } + throwIfSolidityBuildFailed(results); // If we recompiled the entire project we cleanup the artifacts if (files.length === 0) {