Skip to content

Commit 3c87b5f

Browse files
authored
Merge pull request #5264 from NomicFoundation/hre-singleton
Add and test a singleton hre instance
2 parents a84cb1a + 4868bad commit 3c87b5f

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

v-next/hardhat/src/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { createHardhatRuntimeEnvironment } from "./hre.js";
1+
import { getHardhatRuntimeEnvironmentSingleton } from "./internal/hre-singleton.js";
22

33
// TODO:
4-
// - If the HRE was already initialized in the CLI, we should use that one.
54
// - Load the config from the file system.
6-
const hre = await createHardhatRuntimeEnvironment({});
5+
const hre = await getHardhatRuntimeEnvironmentSingleton({});
76

87
export const { config, tasks, globalArguments, hooks, interruptions } = hre;
98

v-next/hardhat/src/internal/cli/main.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { isAbsolute, resolve } from "node:path";
22

33
import {
44
buildGlobalParameterMap,
5-
createHardhatRuntimeEnvironment,
65
resolvePluginList,
76
} from "@nomicfoundation/hardhat-core";
87
import {
@@ -14,6 +13,7 @@ import { Task } from "@nomicfoundation/hardhat-core/types/tasks";
1413

1514
import "tsx"; // NOTE: This is important, it allows us to load .ts files form the CLI
1615
import { builtinPlugins } from "../builtin-plugins/index.js";
16+
import { getHardhatRuntimeEnvironmentSingleton } from "../hre-singleton.js";
1717

1818
export async function main(cliArguments: string[]) {
1919
const hreInitStart = performance.now();
@@ -67,6 +67,7 @@ export async function main(cliArguments: string[]) {
6767

6868
if (configPath === undefined) {
6969
// TODO: Find the closest config file
70+
// if HARDHAT_CONFIG exists, use it
7071
throw new Error("Missing --config");
7172
}
7273

@@ -83,10 +84,13 @@ export async function main(cliArguments: string[]) {
8384
usedCliArguments,
8485
);
8586

86-
const hre = await createHardhatRuntimeEnvironment(
87+
const hre = await getHardhatRuntimeEnvironmentSingleton(
8788
userConfig,
8889
userProvidedGlobalArguments,
89-
{ resolvedPlugins, globalParameterMap },
90+
{
91+
resolvedPlugins,
92+
globalParameterMap,
93+
},
9094
);
9195

9296
const hreInitEnd = performance.now();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { HardhatRuntimeEnvironment } from "../types/hre.js";
2+
3+
import { createHardhatRuntimeEnvironment } from "@nomicfoundation/hardhat-core";
4+
5+
let hre: HardhatRuntimeEnvironment;
6+
7+
/**
8+
* This function returns a singleton instance of the Hardhat Runtime Environment.
9+
*
10+
* It exists so that the CLI and the programmatic API are always using the same HRE instance.
11+
*
12+
* Note: Only the params of the first call are used.
13+
*/
14+
export async function getHardhatRuntimeEnvironmentSingleton(
15+
...params: Parameters<typeof createHardhatRuntimeEnvironment>
16+
): Promise<HardhatRuntimeEnvironment> {
17+
if (hre === undefined) {
18+
hre = await createHardhatRuntimeEnvironment(...params);
19+
}
20+
21+
return hre;
22+
}

v-next/hardhat/test/hre/index.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import assert from "node:assert/strict";
2+
import { describe, it } from "node:test";
3+
4+
import { createHardhatRuntimeEnvironment } from "../../src/hre.js";
5+
import { builtinPlugins } from "../../src/internal/builtin-plugins/index.js";
6+
import { getHardhatRuntimeEnvironmentSingleton } from "../../src/internal/hre-singleton.js";
7+
8+
describe("HRE", () => {
9+
describe("createHardhatRuntimeEnvironment", () => {
10+
it("should include the built-in plugins", async () => {
11+
const hre = await createHardhatRuntimeEnvironment({});
12+
13+
assert.deepEqual(hre.config.plugins, builtinPlugins);
14+
});
15+
});
16+
17+
describe("getHardhatRuntimeEnvironmentSingleton", () => {
18+
it("should return the same instance", async () => {
19+
const hre1 = await getHardhatRuntimeEnvironmentSingleton({
20+
plugins: [{ id: "custom task" }],
21+
});
22+
const hre2 = await getHardhatRuntimeEnvironmentSingleton({});
23+
24+
assert.deepEqual(
25+
hre1.config.plugins.find((p) => p.id === "custom task"),
26+
{ id: "custom task" },
27+
);
28+
assert.deepEqual(
29+
hre2.config.plugins.find((p) => p.id === "custom task"),
30+
{ id: "custom task" },
31+
);
32+
assert.deepEqual(hre1, hre2);
33+
});
34+
});
35+
});

v-next/hardhat/test/index.ts

-5
This file was deleted.

0 commit comments

Comments
 (0)