Skip to content

Commit 0d075d2

Browse files
committed
Address new PR comments
1 parent 62a09b9 commit 0d075d2

File tree

5 files changed

+68
-35
lines changed

5 files changed

+68
-35
lines changed

.changeset/good-eels-buy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"hardhat": patch
33
---
44

5-
Add `downloadCompilers` and `getCompiler` hooks for extensible custom compiler support
5+
Add `SolidityHooks#downloadCompilers` and `SolidityHooks#getCompiler` hooks for extensible custom compiler support ([#8009](https://github.com/NomicFoundation/hardhat/pull/8009))

v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/build-system.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import pMap from "p-map";
5858

5959
import { FileBuildResultType } from "../../../../types/solidity/build-system.js";
6060
import { DEFAULT_BUILD_PROFILE } from "../build-profiles.js";
61+
import { getSolcCompilerForConfig } from "../solidity-hooks.js";
6162

6263
import {
6364
getArtifactsDeclarationFile,
@@ -91,20 +92,6 @@ export function isSolcSolidityCompilerConfig(
9192
return config.type === undefined || config.type === "solc";
9293
}
9394

94-
/**
95-
* Resolves the preferWasm setting for a given compiler config, falling back
96-
* to the build profile's preferWasm if not set on the compiler.
97-
*/
98-
function resolvePreferWasm(
99-
compilerConfig: SolidityCompilerConfig,
100-
buildProfilePreferWasm: boolean,
101-
): boolean {
102-
if (isSolcSolidityCompilerConfig(compilerConfig)) {
103-
return compilerConfig.preferWasm ?? buildProfilePreferWasm;
104-
}
105-
return false;
106-
}
107-
10895
// Compiler warnings to suppress from build output.
10996
// Each rule specifies a warning message and the source file it applies to.
11097
// This allows suppressing known warnings from internal files (e.g., console.sol)
@@ -511,10 +498,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
511498
"getCompiler",
512499
[compilerConfig],
513500
async (_context, cfg) =>
514-
getCompiler(cfg.version, {
515-
preferWasm: resolvePreferWasm(cfg, buildProfile.preferWasm),
516-
compilerPath: cfg.path,
517-
}),
501+
getSolcCompilerForConfig(cfg, buildProfile.preferWasm),
518502
);
519503
longVersion = compiler.longVersion;
520504
longVersionMap.set(compilerConfig.version, longVersion);
@@ -758,10 +742,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
758742
"getCompiler",
759743
[runnableCompilationJob.solcConfig],
760744
async (_context, cfg) =>
761-
getCompiler(cfg.version, {
762-
preferWasm: resolvePreferWasm(cfg, buildProfile.preferWasm),
763-
compilerPath: cfg.path,
764-
}),
745+
getSolcCompilerForConfig(cfg, buildProfile.preferWasm),
765746
);
766747

767748
log(
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import type { SolidityHooks } from "../../../../types/hooks.js";
22

3-
import { isSolcConfig } from "../build-system/build-system.js";
4-
import { downloadSolcCompilers } from "../build-system/compiler/index.js";
3+
import { downloadSolcCompilersHandler } from "../solidity-hooks.js";
54

65
export default async (): Promise<Partial<SolidityHooks>> => ({
76
downloadCompilers: async (_context, compilerConfigs, quiet) => {
8-
const solcVersions = new Set(
9-
compilerConfigs.filter(isSolcConfig).map((c) => c.version),
10-
);
11-
12-
if (solcVersions.size > 0) {
13-
await downloadSolcCompilers(solcVersions, quiet);
14-
}
7+
await downloadSolcCompilersHandler(compilerConfigs, quiet);
158
},
169
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { SolidityCompilerConfig } from "../../../types/config.js";
2+
import type { Compiler } from "../../../types/solidity.js";
3+
4+
import { isSolcSolidityCompilerConfig } from "./build-system/build-system.js";
5+
import {
6+
downloadSolcCompilers,
7+
getCompiler,
8+
} from "./build-system/compiler/index.js";
9+
10+
/**
11+
* Downloads solc compilers for the given configs, filtering out non-solc types.
12+
* This is the default implementation of the `downloadCompilers` hook handler.
13+
*/
14+
export async function downloadSolcCompilersHandler(
15+
compilerConfigs: SolidityCompilerConfig[],
16+
quiet: boolean,
17+
): Promise<void> {
18+
const solcVersions = new Set(
19+
compilerConfigs.filter(isSolcSolidityCompilerConfig).map((c) => c.version),
20+
);
21+
22+
if (solcVersions.size > 0) {
23+
await downloadSolcCompilers(solcVersions, quiet);
24+
}
25+
}
26+
27+
/**
28+
* Resolves the preferWasm setting for a given compiler config, falling back
29+
* to the build profile's preferWasm if not set on the compiler.
30+
*/
31+
export function resolvePreferWasm(
32+
compilerConfig: SolidityCompilerConfig,
33+
buildProfilePreferWasm: boolean,
34+
): boolean {
35+
if (isSolcSolidityCompilerConfig(compilerConfig)) {
36+
return compilerConfig.preferWasm ?? buildProfilePreferWasm;
37+
}
38+
return false;
39+
}
40+
41+
/**
42+
* Creates a solc Compiler for the given config. This is the default
43+
* implementation used as the fallback in the `getCompiler` hook chain.
44+
*/
45+
export async function getSolcCompilerForConfig(
46+
compilerConfig: SolidityCompilerConfig,
47+
buildProfilePreferWasm: boolean,
48+
): Promise<Compiler> {
49+
return getCompiler(compilerConfig.version, {
50+
preferWasm: resolvePreferWasm(compilerConfig, buildProfilePreferWasm),
51+
compilerPath: compilerConfig.path,
52+
});
53+
}

v-next/hardhat/test/internal/builtin-plugins/solidity/hooks.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/consistent-type-assertions -- We use `as any` casts
2+
for non-solc compiler configs in tests because "solx" is not registered in the base type system. */
13
import type {
24
ConfigurationVariableResolver,
35
HardhatConfig,
@@ -464,7 +466,7 @@ describe("solidity - hooks", () => {
464466
solidity: {
465467
compilers: [
466468
{ version: "0.8.23" },
467-
{ type: "solx", version: "0.8.23" },
469+
{ type: "solx", version: "0.8.23" } as any,
468470
],
469471
},
470472
});
@@ -482,7 +484,7 @@ describe("solidity - hooks", () => {
482484
"Should include solc configs (type undefined)",
483485
);
484486
assert.equal(
485-
capturedConfigs.filter((c) => c.type === "solx").length > 0,
487+
capturedConfigs.filter((c) => (c as any).type === "solx").length > 0,
486488
true,
487489
"Should include non-solc configs (type solx)",
488490
);
@@ -498,7 +500,11 @@ describe("solidity - hooks", () => {
498500
solidity: {
499501
compilers: [
500502
{ version: "0.8.23" },
501-
{ type: "solx", version: "0.8.23", path: "/mock/path/to/solx" },
503+
{
504+
type: "solx",
505+
version: "0.8.23",
506+
path: "/mock/path/to/solx",
507+
} as any,
502508
],
503509
},
504510
});

0 commit comments

Comments
 (0)