Skip to content

Commit bf2d771

Browse files
committed
feat: added isolated config option to build profile
1 parent f18b1ea commit bf2d771

File tree

5 files changed

+77
-31
lines changed

5 files changed

+77
-31
lines changed

.changeset/tricky-toys-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"hardhat": patch
3+
---
4+
5+
Added `isolated` config option to the build profile configuration (https://github.com/NomicFoundation/hardhat/issues/7125)

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,3 @@ export const DEFAULT_BUILD_PROFILES = [
66
"solidity-tests",
77
"javascript-tests",
88
] as const;
9-
10-
export function shouldMergeCompilationJobs(buildProfile: string): boolean {
11-
return buildProfile !== "production";
12-
}

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

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import type { CompileCache } from "./cache.js";
22
import type { DependencyGraphImplementation } from "./dependency-graph.js";
33
import type { Artifact } from "../../../../types/artifacts.js";
4-
import type { SolcConfig, SolidityConfig } from "../../../../types/config.js";
4+
import type {
5+
SolcConfig,
6+
SolidityBuildProfileConfig,
7+
SolidityConfig,
8+
} from "../../../../types/config.js";
59
import type { HookManager } from "../../../../types/hooks.js";
610
import type {
711
SolidityBuildSystem,
@@ -45,10 +49,7 @@ import debug from "debug";
4549
import pMap from "p-map";
4650

4751
import { FileBuildResultType } from "../../../../types/solidity/build-system.js";
48-
import {
49-
DEFAULT_BUILD_PROFILE,
50-
shouldMergeCompilationJobs,
51-
} from "../build-profiles.js";
52+
import { DEFAULT_BUILD_PROFILE } from "../build-profiles.js";
5253

5354
import {
5455
getArtifactsDeclarationFile,
@@ -127,6 +128,11 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
127128

128129
await this.#downloadConfiguredCompilers(options?.quiet);
129130

131+
const buildProfileName = options?.buildProfile ?? DEFAULT_BUILD_PROFILE;
132+
const buildProfile = this.#getBuildProfile(buildProfileName);
133+
134+
const isolated = this.#isIsolated(buildProfile.isolated, options?.isolated);
135+
130136
const compilationJobsResult = await this.getCompilationJobs(
131137
rootFilePaths,
132138
options,
@@ -210,7 +216,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
210216
indexedIndividualJobs,
211217
compilationResult,
212218
emitArtifactsResult,
213-
options,
219+
isolated,
214220
);
215221
}),
216222
);
@@ -299,21 +305,13 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
299305
);
300306

301307
const buildProfileName = options?.buildProfile ?? DEFAULT_BUILD_PROFILE;
302-
303-
if (this.#options.solidityConfig.profiles[buildProfileName] === undefined) {
304-
throw new HardhatError(
305-
HardhatError.ERRORS.CORE.SOLIDITY.BUILD_PROFILE_NOT_FOUND,
306-
{
307-
buildProfileName,
308-
},
309-
);
310-
}
308+
const buildProfile = this.#getBuildProfile(buildProfileName);
311309

312310
log(`Using build profile ${buildProfileName}`);
313311

314312
const solcConfigSelector = new SolcConfigSelector(
315313
buildProfileName,
316-
this.#options.solidityConfig.profiles[buildProfileName],
314+
buildProfile,
317315
dependencyGraph,
318316
);
319317

@@ -388,6 +386,8 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
388386
// Select which files to compile
389387
const rootFilesToCompile: Set<string> = new Set();
390388

389+
const isolated = this.#isIsolated(buildProfile.isolated, options?.isolated);
390+
391391
for (const [rootFile, compilationJob] of indexedIndividualJobs.entries()) {
392392
const jobHash = await compilationJob.getBuildId();
393393
const cacheResult = this.#compileCache[rootFile];
@@ -397,7 +397,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
397397
options?.force === true ||
398398
cacheResult === undefined ||
399399
cacheResult.jobHash !== jobHash ||
400-
cacheResult.isolated !== options?.isolated
400+
cacheResult.isolated !== isolated
401401
) {
402402
rootFilesToCompile.add(rootFile);
403403
continue;
@@ -424,10 +424,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
424424
}
425425
}
426426

427-
if (
428-
options?.isolated !== true &&
429-
shouldMergeCompilationJobs(buildProfileName)
430-
) {
427+
if (!isolated) {
431428
// non-isolated mode
432429
log(`Merging compilation jobs`);
433430

@@ -803,6 +800,34 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
803800
this.#downloadedCompilers = true;
804801
}
805802

803+
#getBuildProfile(
804+
buildProfileNameOption?: string,
805+
): SolidityBuildProfileConfig {
806+
const buildProfileName = buildProfileNameOption ?? DEFAULT_BUILD_PROFILE;
807+
const buildProfile =
808+
this.#options.solidityConfig.profiles[buildProfileName];
809+
810+
if (buildProfile === undefined) {
811+
throw new HardhatError(
812+
HardhatError.ERRORS.CORE.SOLIDITY.BUILD_PROFILE_NOT_FOUND,
813+
{
814+
buildProfileName,
815+
},
816+
);
817+
}
818+
819+
return buildProfile;
820+
}
821+
822+
#isIsolated(
823+
isolatedBuildProfile: boolean,
824+
isolatedOption?: boolean,
825+
): boolean {
826+
// NOTE: Run in isolated mode if it has been explicitly requested or the build profile demands it
827+
// TODO: Consider allowing overriding the build profile's isolated mode via options
828+
return isolatedBuildProfile || isolatedOption === true;
829+
}
830+
806831
#getAllCompilerVersions(): Set<string> {
807832
return new Set(
808833
Object.values(this.#options.solidityConfig.profiles)
@@ -854,7 +879,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
854879
indexedIndividualJobs: Map<string, CompilationJob>,
855880
result: CompilationResult,
856881
emitArtifactsResult: EmitArtifactsResult,
857-
options: BuildOptions | undefined,
882+
isolated: boolean,
858883
): Promise<void> {
859884
const rootFilePaths = result.compilationJob.dependencyGraph
860885
.getRoots()
@@ -887,7 +912,7 @@ export class SolidityBuildSystemImplementation implements SolidityBuildSystem {
887912

888913
this.#compileCache[rootFilePath] = {
889914
jobHash,
890-
isolated: options?.isolated === true,
915+
isolated,
891916
artifactPaths,
892917
buildInfoPath: emitArtifactsResult.buildInfoPath,
893918
buildInfoOutputPath: emitArtifactsResult.buildInfoOutputPath,

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ function resolveSolidityConfig(
213213
settings: {},
214214
})),
215215
overrides: {},
216+
isolated: false,
216217
},
217218
},
218219
npmFilesToBuild: [],
@@ -230,6 +231,7 @@ function resolveSolidityConfig(
230231
},
231232
],
232233
overrides: {},
234+
isolated: solidityConfig.isolated ?? false,
233235
},
234236
},
235237
npmFilesToBuild: solidityConfig.npmFilesToBuild ?? [],
@@ -257,6 +259,7 @@ function resolveSolidityConfig(
257259
},
258260
),
259261
),
262+
isolated: solidityConfig.isolated ?? false,
260263
},
261264
},
262265
npmFilesToBuild: solidityConfig.npmFilesToBuild ?? [],
@@ -269,6 +272,8 @@ function resolveSolidityConfig(
269272
for (const [profileName, profile] of Object.entries(
270273
solidityConfig.profiles,
271274
)) {
275+
const isolated = profile.isolated ?? profileName === "production";
276+
272277
if ("version" in profile) {
273278
profiles[profileName] = {
274279
compilers: [
@@ -278,6 +283,7 @@ function resolveSolidityConfig(
278283
},
279284
],
280285
overrides: {},
286+
isolated,
281287
};
282288
continue;
283289
}
@@ -300,12 +306,20 @@ function resolveSolidityConfig(
300306
},
301307
),
302308
),
309+
isolated,
303310
};
304311
}
305312

306313
for (const profile of DEFAULT_BUILD_PROFILES) {
307314
if (!(profile in profiles)) {
308-
profiles[profile] = profiles.default;
315+
if (profile === "production") {
316+
profiles[profile] = {
317+
...profiles.default,
318+
isolated: true,
319+
};
320+
} else {
321+
profiles[profile] = profiles.default;
322+
}
309323
}
310324
}
311325

v-next/hardhat/src/internal/builtin-plugins/solidity/type-extensions.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ declare module "../../../types/config.js" {
1515
settings?: any;
1616
}
1717

18-
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- This could be an extension point
19-
export interface SingleVersionSolcUserConfig extends SolcUserConfig {}
18+
export interface CommonSolcUserConfig {
19+
isolated?: boolean;
20+
}
21+
22+
export interface SingleVersionSolcUserConfig
23+
extends CommonSolcUserConfig,
24+
SolcUserConfig {}
2025

21-
export interface MultiVersionSolcUserConfig {
26+
export interface MultiVersionSolcUserConfig extends CommonSolcUserConfig {
2227
compilers: SolcUserConfig[];
2328
overrides?: Record<string, SolcUserConfig>;
2429
}
@@ -55,6 +60,7 @@ declare module "../../../types/config.js" {
5560
export interface SolidityBuildProfileConfig {
5661
compilers: SolcConfig[];
5762
overrides: Record<string, SolcConfig>;
63+
isolated: boolean;
5864
}
5965

6066
export interface SolidityConfig {

0 commit comments

Comments
 (0)