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

[WIP] add esm support #1994

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/hardhat-core/src/internal/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async function main() {
const showSolidityConfigWarnings = taskName === TASK_COMPILE;

const ctx = HardhatContext.createHardhatContext();
const config = loadConfigAndTasks(hardhatArguments, {
const config = await loadConfigAndTasks(hardhatArguments, {
showSolidityConfigWarnings,
});

Expand Down
16 changes: 10 additions & 6 deletions packages/hardhat-core/src/internal/core/config/config-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ import { DEFAULT_SOLC_VERSION } from "./default-config";

const log = debug("hardhat:core:config");

function importCsjOrEsModule(filePath: string): any {
const imported = require(filePath);
return imported.default !== undefined ? imported.default : imported;
function importCsjOrEsModule(filePath: string): any | Promise<any> {
try {
const imported = require(filePath);
return imported.default !== undefined ? imported.default : imported;
} catch (err) {
return import(filePath);
}
}

export function resolveConfigPath(configPath: string | undefined) {
Expand All @@ -36,10 +40,10 @@ export function resolveConfigPath(configPath: string | undefined) {
return configPath;
}

export function loadConfigAndTasks(
export async function loadConfigAndTasks(
hardhatArguments?: Partial<HardhatArguments>,
{ showSolidityConfigWarnings } = { showSolidityConfigWarnings: false }
): HardhatConfig {
): Promise<HardhatConfig> {
let configPath =
hardhatArguments !== undefined ? hardhatArguments.config : undefined;

Expand All @@ -63,7 +67,7 @@ export function loadConfigAndTasks(

try {
require("../tasks/builtin-tasks");
userConfig = importCsjOrEsModule(configPath);
userConfig = await importCsjOrEsModule(configPath);
} catch (e) {
analyzeModuleNotFoundError(e, configPath);

Expand Down
27 changes: 14 additions & 13 deletions packages/hardhat-core/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ if (!HardhatContext.isCreated()) {
loadTsNode();
}

const config = loadConfigAndTasks(hardhatArguments);

const env = new Environment(
config,
hardhatArguments,
ctx.tasksDSL.getTaskDefinitions(),
ctx.extendersManager.getExtenders(),
ctx.experimentalHardhatNetworkMessageTraceHooks
);

ctx.setHardhatRuntimeEnvironment(env);

env.injectToGlobal();
// TODO: Implication is this becoming async?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will lead to a race condition. Same for packages/hardhat-core/src/internal/lib/hardhat-lib.ts

loadConfigAndTasks(hardhatArguments).then((config) => {
const env = new Environment(
config,
hardhatArguments,
ctx.tasksDSL.getTaskDefinitions(),
ctx.extendersManager.getExtenders(),
ctx.experimentalHardhatNetworkMessageTraceHooks
);

ctx.setHardhatRuntimeEnvironment(env);

env.injectToGlobal();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("Config extensions", function () {
});

it("Should throw the right error when trying to modify the user config", function () {
expectHardhatError(
expectHardhatErrorAsync(
() => loadConfigAndTasks(),
ERRORS.GENERAL.USER_CONFIG_MODIFIED
);
Expand Down
54 changes: 27 additions & 27 deletions packages/hardhat-core/test/internal/core/config/config-loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("config loading", function () {

it("Should throw the right error", function () {
expectHardhatError(
() => loadConfigAndTasks(),
async () => await loadConfigAndTasks(),
ERRORS.GENERAL.INVALID_CONFIG
);
});
Expand All @@ -65,8 +65,8 @@ describe("config loading", function () {
resetHardhatContext();
});

it("should accept a relative path from the CWD", function () {
const config = loadConfigAndTasks({ config: "config.js" });
it("should accept a relative path from the CWD", async function () {
const config = await loadConfigAndTasks({ config: "config.js" });

assert.equal(
config.paths.configFile,
Expand All @@ -76,7 +76,7 @@ describe("config loading", function () {

it("should accept an absolute path", async function () {
const fixtureDir = await getFixtureProjectPath("custom-config-file");
const config = loadConfigAndTasks({
const config = await loadConfigAndTasks({
config: path.join(fixtureDir, "config.js"),
});

Expand Down Expand Up @@ -114,11 +114,11 @@ describe("config loading", function () {
resetHardhatContext();
});

it("should remove everything from global state after loading", function () {
it("should remove everything from global state after loading", async function () {
const globalAsAny: any = global;

HardhatContext.createHardhatContext();
loadConfigAndTasks();
await loadConfigAndTasks();

assert.isUndefined(globalAsAny.subtask);
assert.isUndefined(globalAsAny.task);
Expand All @@ -128,13 +128,13 @@ describe("config loading", function () {
resetHardhatContext();

HardhatContext.createHardhatContext();
loadConfigAndTasks();
await loadConfigAndTasks();

assert.isUndefined(globalAsAny.subtask);
assert.isUndefined(globalAsAny.task);
assert.isUndefined(globalAsAny.types);
assert.isUndefined(globalAsAny.extendEnvironment);
resetHardhatContext();
await resetHardhatContext();
});
});

Expand All @@ -150,7 +150,7 @@ describe("config loading", function () {
});

it("should accept a relative path from the CWD", function () {
expectHardhatError(
expectHardhatErrorAsync(
() => loadConfigAndTasks(),
ERRORS.GENERAL.LIB_IMPORTED_FROM_THE_CONFIG
);
Expand All @@ -168,10 +168,10 @@ describe("config loading", function () {
resetHardhatContext();
});

it("should re-throw the error", function () {
it("should re-throw the error", async function () {
let errorThrown;
try {
loadConfigAndTasks();
await loadConfigAndTasks();
} catch (e) {
errorThrown = e;
}
Expand All @@ -195,10 +195,10 @@ describe("config loading", function () {
resetHardhatContext();
});

it("should re-throw the error", function () {
it("should re-throw the error", async function () {
let errorThrown;
try {
loadConfigAndTasks();
await loadConfigAndTasks();
} catch (e) {
errorThrown = e;
}
Expand All @@ -222,10 +222,10 @@ describe("config loading", function () {
resetHardhatContext();
});

it("should re-throw the error", function () {
it("should re-throw the error", async function () {
let errorThrown;
try {
loadConfigAndTasks();
await loadConfigAndTasks();
} catch (e) {
errorThrown = e;
}
Expand All @@ -250,7 +250,7 @@ describe("config loading", function () {
});

it("should indicate the plugin and the missing dependency", function () {
expectHardhatError(
expectHardhatErrorAsync(
() => loadConfigAndTasks(),
ERRORS.PLUGINS.MISSING_DEPENDENCIES,
"Plugin some-plugin requires the following dependencies to be installed: some-dependency"
Expand All @@ -270,7 +270,7 @@ describe("config loading", function () {
});

it("should indicate the plugin and the missing dependencies", function () {
expectHardhatError(
expectHardhatErrorAsync(
() => loadConfigAndTasks(),
ERRORS.PLUGINS.MISSING_DEPENDENCIES,
"Plugin some-plugin requires the following dependencies to be installed: some-dependency, some-other-dependency"
Expand All @@ -290,7 +290,7 @@ describe("config loading", function () {
});

it("should indicate the buidler plugin", function () {
expectHardhatError(
expectHardhatErrorAsync(
() => loadConfigAndTasks(),
ERRORS.PLUGINS.BUIDLER_PLUGIN,
`You are using some-buidler-plugin, which is a Buidler plugin. Use the equivalent
Expand Down Expand Up @@ -329,7 +329,7 @@ Hardhat plugin instead.`
// We run this twice to make sure that the cache is cleaned properly
for (let i = 0; i < 2; i++) {
HardhatContext.createHardhatContext();
loadConfigAndTasks();
await loadConfigAndTasks();
const ctx = HardhatContext.getHardhatContext();

const files = ctx.getFilesLoadedDuringConfig();
Expand Down Expand Up @@ -371,8 +371,8 @@ Hardhat plugin instead.`
resetHardhatContext();
});

it("should emit a warning if there's no configured solidity", function () {
const config = loadConfigAndTasks(
it("should emit a warning if there's no configured solidity", async function () {
const config = await loadConfigAndTasks(
{
config: "config-without-solidity.js",
},
Expand All @@ -388,8 +388,8 @@ Hardhat plugin instead.`
assert.equal(config.solidity.compilers[0].version, DEFAULT_SOLC_VERSION);
});

it("should emit a warning if the solc version is too new", function () {
loadConfigAndTasks(
it("should emit a warning if the solc version is too new", async function () {
await loadConfigAndTasks(
{
config: "unsupported-new-solc.js",
},
Expand All @@ -400,8 +400,8 @@ Hardhat plugin instead.`
assert.include(consoleWarnStub.args[0][0], "is not fully supported yet");
});

it("should emit a warning if there are multiple unsupported versions", function () {
loadConfigAndTasks(
it("should emit a warning if there are multiple unsupported versions", async function () {
await loadConfigAndTasks(
{
config: "multiple-unsupported-solc.js",
},
Expand All @@ -412,8 +412,8 @@ Hardhat plugin instead.`
assert.include(consoleWarnStub.args[0][0], "are not fully supported yet");
});

it("should emit a warning if there is an unsupported version in an override", function () {
loadConfigAndTasks(
it("should emit a warning if there is an unsupported version in an override", async function () {
await loadConfigAndTasks(
{
config: "unsupported-solc-in-override.js",
},
Expand Down