Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and test a singleton hre instance #5264

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions v-next/hardhat/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { createHardhatRuntimeEnvironment } from "./hre.js";
import { getHRE } from "./internal/hre-singleton.js";

// TODO:
// - If the HRE was already initialized in the CLI, we should use that one.
// - Load the config from the file system.
const hre = await createHardhatRuntimeEnvironment({});
const hre = await getHRE({});

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

Expand Down
12 changes: 6 additions & 6 deletions v-next/hardhat/src/internal/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isAbsolute, resolve } from "node:path";

import {
buildGlobalParameterMap,
createHardhatRuntimeEnvironment,
resolvePluginList,
} from "@nomicfoundation/hardhat-core";
import {
Expand All @@ -14,6 +13,7 @@ import { Task } from "@nomicfoundation/hardhat-core/types/tasks";

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

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

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

Expand All @@ -83,11 +84,10 @@ export async function main(cliArguments: string[]) {
usedCliArguments,
);

const hre = await createHardhatRuntimeEnvironment(
userConfig,
userProvidedGlobalArguments,
{ resolvedPlugins, globalParameterMap },
);
const hre = await getHRE(userConfig, userProvidedGlobalArguments, {
resolvedPlugins,
globalParameterMap,
});

const hreInitEnd = performance.now();
console.log("Time to initialize the HRE (ms):", hreInitEnd - hreInitStart);
Expand Down
20 changes: 20 additions & 0 deletions v-next/hardhat/src/internal/hre-singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { HardhatRuntimeEnvironment } from "../types/hre.js";

import { createHardhatRuntimeEnvironment } from "@nomicfoundation/hardhat-core";

let hre: HardhatRuntimeEnvironment;

/**
* This function returns a singleton instance of the Hardhat Runtime Environment.
*
* It exists so that the CLI and the programmatic API are always using the same HRE instance.
*/
export async function getHRE(
zoeyTM marked this conversation as resolved.
Show resolved Hide resolved
...params: Parameters<typeof createHardhatRuntimeEnvironment>
): Promise<HardhatRuntimeEnvironment> {
if (hre === undefined) {
zoeyTM marked this conversation as resolved.
Show resolved Hide resolved
hre = await createHardhatRuntimeEnvironment(...params);
}

return hre;
}
33 changes: 33 additions & 0 deletions v-next/hardhat/test/hre/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import assert from "node:assert";
zoeyTM marked this conversation as resolved.
Show resolved Hide resolved
import { describe, it } from "node:test";

import { createHardhatRuntimeEnvironment } from "../../src/hre.js";
import { builtinPlugins } from "../../src/internal/builtin-plugins/index.js";
import { getHRE } from "../../src/internal/hre-singleton.js";

describe("HRE", () => {
describe("createHardhatRuntimeEnvironment", () => {
it("should include the built-in plugins", async () => {
const hre = await createHardhatRuntimeEnvironment({});

assert.deepStrictEqual(hre.config.plugins, builtinPlugins);
});
});

describe("getHRE", () => {
it("should return the same instance", async () => {
const hre1 = await getHRE({ plugins: [{ id: "custom task" }] });
const hre2 = await getHRE({});

assert.deepEqual(
hre1.config.plugins.find((p) => p.id === "custom task"),
{ id: "custom task" },
);
assert.deepEqual(
hre2.config.plugins.find((p) => p.id === "custom task"),
{ id: "custom task" },
);
assert.deepStrictEqual(hre1, hre2);
});
});
});
5 changes: 0 additions & 5 deletions v-next/hardhat/test/index.ts

This file was deleted.

Loading