Skip to content

Commit c901bd2

Browse files
committed
Use "global" instead of "singleton" for the shared HRE instance
1 parent 76823a8 commit c901bd2

File tree

6 files changed

+68
-63
lines changed

6 files changed

+68
-63
lines changed

v-next/hardhat/src/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import type { UserInterruptionManager } from "./types/user-interruptions.js";
77

88
import { resolveHardhatConfigPath } from "./config.js";
99
import { createHardhatRuntimeEnvironment } from "./hre.js";
10-
import { importUserConfig } from "./internal/helpers/config-loading.js";
1110
import {
12-
getHardhatRuntimeEnvironmentSingleton,
13-
setHardhatRuntimeEnvironmentSingleton,
14-
} from "./internal/hre-singleton.js";
11+
getGlobalHardhatRuntimeEnvironment,
12+
setGlobalHardhatRuntimeEnvironment,
13+
} from "./internal/global-hre-instance.js";
14+
import { importUserConfig } from "./internal/helpers/config-loading.js";
1515

1616
let maybeHre: HardhatRuntimeEnvironment | undefined =
17-
getHardhatRuntimeEnvironmentSingleton();
17+
getGlobalHardhatRuntimeEnvironment();
1818

1919
if (maybeHre === undefined) {
2020
/* eslint-disable no-restricted-syntax -- Allow top-level await here */
@@ -24,7 +24,7 @@ if (maybeHre === undefined) {
2424
maybeHre = await createHardhatRuntimeEnvironment(userConfig);
2525
/* eslint-enable no-restricted-syntax */
2626

27-
setHardhatRuntimeEnvironmentSingleton(maybeHre);
27+
setGlobalHardhatRuntimeEnvironment(maybeHre);
2828
}
2929

3030
const hre: HardhatRuntimeEnvironment = maybeHre;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import { kebabToCamelCase } from "@ignored/hardhat-vnext-utils/string";
2929
import { resolveHardhatConfigPath } from "../../config.js";
3030
import { createHardhatRuntimeEnvironment } from "../../hre.js";
3131
import { builtinPlugins } from "../builtin-plugins/index.js";
32+
import { setGlobalHardhatRuntimeEnvironment } from "../global-hre-instance.js";
3233
import { importUserConfig } from "../helpers/config-loading.js";
33-
import { setHardhatRuntimeEnvironmentSingleton } from "../hre-singleton.js";
3434

3535
import { printErrorMessages } from "./error-handler.js";
3636
import { getGlobalHelpString } from "./helpers/getGlobalHelpString.js";
@@ -92,7 +92,7 @@ export async function main(
9292
);
9393

9494
// This must be the first time we set it, otherwise we let it crash
95-
setHardhatRuntimeEnvironmentSingleton(hre);
95+
setGlobalHardhatRuntimeEnvironment(hre);
9696

9797
const taskOrId = parseTask(cliArguments, usedCliArguments, hre);
9898

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { HardhatRuntimeEnvironment } from "../types/hre.js";
2+
3+
import { assertHardhatInvariant } from "@ignored/hardhat-vnext-errors";
4+
5+
let hre: HardhatRuntimeEnvironment | undefined;
6+
7+
/**
8+
* This function returns a global instance of the Hardhat Runtime Environment
9+
* if it was already initialized.
10+
*
11+
* It exists so that the CLI and the programmatic API (as in `import "hardhat"`)
12+
* are always using the same HRE instance.
13+
*/
14+
export function getGlobalHardhatRuntimeEnvironment():
15+
| HardhatRuntimeEnvironment
16+
| undefined {
17+
return hre;
18+
}
19+
20+
/**
21+
* Sets the global instance of the Hardhat Runtime Environment.
22+
*/
23+
export function setGlobalHardhatRuntimeEnvironment(
24+
newHre: HardhatRuntimeEnvironment,
25+
): void {
26+
assertHardhatInvariant(
27+
hre === undefined,
28+
"The global instances of the HRE is already set",
29+
);
30+
hre = newHre;
31+
}
32+
33+
/**
34+
* This function resets the global instance of the Hardhat Runtime Environment,
35+
* so that it can be reinitialized.
36+
*
37+
* It should be used only in tests.
38+
*/
39+
export function resetGlobalHardhatRuntimeEnvironment(): void {
40+
hre = undefined;
41+
}

v-next/hardhat/src/internal/hre-singleton.ts

-36
This file was deleted.

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

+17-17
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { resolveHardhatConfigPath } from "../../src/config.js";
77
import { createHardhatRuntimeEnvironment } from "../../src/hre.js";
88
import { builtinPlugins } from "../../src/internal/builtin-plugins/index.js";
99
import {
10-
getHardhatRuntimeEnvironmentSingleton,
11-
resetHardhatRuntimeEnvironmentSingleton,
12-
setHardhatRuntimeEnvironmentSingleton,
13-
} from "../../src/internal/hre-singleton.js";
10+
getGlobalHardhatRuntimeEnvironment,
11+
resetGlobalHardhatRuntimeEnvironment,
12+
setGlobalHardhatRuntimeEnvironment,
13+
} from "../../src/internal/global-hre-instance.js";
1414
import { useFixtureProject } from "../helpers/project.js";
1515

1616
describe("HRE", () => {
1717
afterEach(() => {
18-
resetHardhatRuntimeEnvironmentSingleton();
18+
resetGlobalHardhatRuntimeEnvironment();
1919
});
2020

2121
describe("createHardhatRuntimeEnvironment", () => {
@@ -26,30 +26,30 @@ describe("HRE", () => {
2626
});
2727
});
2828

29-
describe("getHardhatRuntimeEnvironmentSingleton", () => {
30-
it("Should return undefined if it wasn't set", () => {
31-
assert.equal(getHardhatRuntimeEnvironmentSingleton(), undefined);
32-
assert.equal(getHardhatRuntimeEnvironmentSingleton(), undefined);
29+
describe("getGlobalHardhatRuntimeEnvironment", () => {
30+
it("Should return undefined if the global instance isn't set", () => {
31+
assert.equal(getGlobalHardhatRuntimeEnvironment(), undefined);
32+
assert.equal(getGlobalHardhatRuntimeEnvironment(), undefined);
3333
});
3434

3535
it("should return the same instance after it's set", async () => {
3636
const hre = await createHardhatRuntimeEnvironment({});
37-
setHardhatRuntimeEnvironmentSingleton(hre);
37+
setGlobalHardhatRuntimeEnvironment(hre);
3838

39-
const hre1 = getHardhatRuntimeEnvironmentSingleton();
40-
const hre2 = getHardhatRuntimeEnvironmentSingleton();
39+
const hre1 = getGlobalHardhatRuntimeEnvironment();
40+
const hre2 = getGlobalHardhatRuntimeEnvironment();
4141

4242
assert.ok(hre1 === hre, "The instances are not the same");
4343
assert.ok(hre2 === hre, "The instances are not the same");
4444
});
4545

46-
it("should include the builtin plugins", async () => {
46+
it("should include the builtin plugins when initialized using createHardhatRuntimeEnvironment", async () => {
4747
const hre = await createHardhatRuntimeEnvironment({});
48-
setHardhatRuntimeEnvironmentSingleton(hre);
49-
const singletonHre = getHardhatRuntimeEnvironmentSingleton();
48+
setGlobalHardhatRuntimeEnvironment(hre);
49+
const globalHre = getGlobalHardhatRuntimeEnvironment();
5050

51-
assert.ok(singletonHre === hre, "The instances are not the same");
52-
assert.deepEqual(singletonHre.config.plugins, builtinPlugins);
51+
assert.ok(globalHre === hre, "The instances are not the same");
52+
assert.deepEqual(globalHre.config.plugins, builtinPlugins);
5353
});
5454
});
5555

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
parseTask,
3333
parseTaskArguments,
3434
} from "../../../src/internal/cli/main.js";
35-
import { resetHardhatRuntimeEnvironmentSingleton } from "../../../src/internal/hre-singleton.js";
35+
import { resetGlobalHardhatRuntimeEnvironment } from "../../../src/internal/global-hre-instance.js";
3636
import { getHardhatVersion } from "../../../src/internal/utils/package.js";
3737
import { useFixtureProject } from "../../helpers/project.js";
3838

@@ -83,7 +83,7 @@ async function getTasksAndSubtaskResults(
8383
describe("main", function () {
8484
describe("main", function () {
8585
afterEach(function () {
86-
resetHardhatRuntimeEnvironmentSingleton();
86+
resetGlobalHardhatRuntimeEnvironment();
8787
});
8888

8989
describe("version", function () {

0 commit comments

Comments
 (0)