From 759a8af1d79c9205ce5719c329736214f62c6331 Mon Sep 17 00:00:00 2001 From: morgansliman Date: Mon, 25 Oct 2021 10:56:35 -0400 Subject: [PATCH] Enable user configurable tsconfig path --- packages/hardhat-core/src/internal/cli/cli.ts | 2 +- .../internal/core/params/hardhat-params.ts | 4 +- .../src/internal/core/typescript-support.ts | 6 ++- packages/hardhat-core/src/register.ts | 2 +- .../typescript-project/test/tsconfig.json | 1 + .../test/internal/core/typescript-support.ts | 39 +++++++++++++++++++ 6 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 packages/hardhat-core/test/fixture-projects/typescript-project/test/tsconfig.json diff --git a/packages/hardhat-core/src/internal/cli/cli.ts b/packages/hardhat-core/src/internal/cli/cli.ts index f09328a1be..83a5d19a7f 100755 --- a/packages/hardhat-core/src/internal/cli/cli.ts +++ b/packages/hardhat-core/src/internal/cli/cli.ts @@ -121,7 +121,7 @@ async function main() { } if (willRunWithTypescript(hardhatArguments.config)) { - loadTsNode(); + loadTsNode(hardhatArguments.tsconfig); } let taskName = parsedTaskName ?? TASK_HELP; diff --git a/packages/hardhat-core/src/internal/core/params/hardhat-params.ts b/packages/hardhat-core/src/internal/core/params/hardhat-params.ts index e541899abf..19234878cd 100644 --- a/packages/hardhat-core/src/internal/core/params/hardhat-params.ts +++ b/packages/hardhat-core/src/internal/core/params/hardhat-params.ts @@ -78,8 +78,8 @@ export const HARDHAT_PARAM_DEFINITIONS: HardhatParamDefinitions = { tsconfig: { name: "tsconfig", defaultValue: undefined, - description: "Reserved hardhat argument -- Has no effect.", - type: types.string, + description: "A TypeScript config file.", + type: types.inputFile, isOptional: true, isFlag: false, isVariadic: false, diff --git a/packages/hardhat-core/src/internal/core/typescript-support.ts b/packages/hardhat-core/src/internal/core/typescript-support.ts index 6c27a858e8..f9c1c7e44c 100644 --- a/packages/hardhat-core/src/internal/core/typescript-support.ts +++ b/packages/hardhat-core/src/internal/core/typescript-support.ts @@ -38,7 +38,7 @@ export function isTypescriptSupported() { return cachedIsTypescriptSupported; } -export function loadTsNode() { +export function loadTsNode(tsConfigPath?: string) { try { require.resolve("typescript"); } catch (error) { @@ -58,6 +58,10 @@ export function loadTsNode() { return; } + if (tsConfigPath !== undefined) { + process.env.TS_NODE_PROJECT = tsConfigPath; + } + // See: https://github.com/nomiclabs/hardhat/issues/265 if (process.env.TS_NODE_FILES === undefined) { process.env.TS_NODE_FILES = "true"; diff --git a/packages/hardhat-core/src/register.ts b/packages/hardhat-core/src/register.ts index f3858837de..f1c13713f5 100644 --- a/packages/hardhat-core/src/register.ts +++ b/packages/hardhat-core/src/register.ts @@ -36,7 +36,7 @@ if (!HardhatContext.isCreated()) { } if (willRunWithTypescript(hardhatArguments.config)) { - loadTsNode(); + loadTsNode(hardhatArguments.tsconfig); } const config = loadConfigAndTasks(hardhatArguments); diff --git a/packages/hardhat-core/test/fixture-projects/typescript-project/test/tsconfig.json b/packages/hardhat-core/test/fixture-projects/typescript-project/test/tsconfig.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/packages/hardhat-core/test/fixture-projects/typescript-project/test/tsconfig.json @@ -0,0 +1 @@ +{} diff --git a/packages/hardhat-core/test/internal/core/typescript-support.ts b/packages/hardhat-core/test/internal/core/typescript-support.ts index 39b29807fe..13454eb28a 100644 --- a/packages/hardhat-core/test/internal/core/typescript-support.ts +++ b/packages/hardhat-core/test/internal/core/typescript-support.ts @@ -5,6 +5,7 @@ import { TASK_TEST_GET_TEST_FILES } from "../../../src/builtin-tasks/task-names" import { resetHardhatContext } from "../../../src/internal/reset"; import { useEnvironment } from "../../helpers/environment"; import { useFixtureProject } from "../../helpers/project"; +import { HardhatError } from "../../../src/internal/core/errors"; describe("Typescript support", function () { describe("strict typescript config", function () { @@ -58,3 +59,41 @@ describe("Typescript support", function () { }); }); }); + +describe("tsconfig param", function () { + useFixtureProject("typescript-project"); + describe("When setting an incorrect tsconfig file", function () { + beforeEach(() => { + process.env.HARDHAT_TSCONFIG = "non-existent.ts"; + }); + + afterEach(() => { + delete process.env.HARDHAT_TSCONFIG; + resetHardhatContext(); + }); + + it("should fail to load hardhat", function () { + assert.throws( + () => require("../../../src/internal/lib/hardhat-lib"), + HardhatError + ); + }); + }); + + describe("When setting a correct tsconfig file", function () { + beforeEach(() => { + process.env.HARDHAT_TSCONFIG = "./test/tsconfig.json"; + }); + + afterEach(() => { + delete process.env.HARDHAT_TSCONFIG; + resetHardhatContext(); + }); + + it("should load hardhat", function () { + assert.doesNotThrow(() => + require("../../../src/internal/lib/hardhat-lib") + ); + }); + }); +});