Skip to content

Commit

Permalink
Enable user configurable tsconfig path
Browse files Browse the repository at this point in the history
  • Loading branch information
zoeyTM committed Oct 25, 2021
1 parent 8b88242 commit 759a8af
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 5 deletions.
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 @@ -121,7 +121,7 @@ async function main() {
}

if (willRunWithTypescript(hardhatArguments.config)) {
loadTsNode();
loadTsNode(hardhatArguments.tsconfig);
}

let taskName = parsedTaskName ?? TASK_HELP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function isTypescriptSupported() {
return cachedIsTypescriptSupported;
}

export function loadTsNode() {
export function loadTsNode(tsConfigPath?: string) {
try {
require.resolve("typescript");
} catch (error) {
Expand All @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-core/src/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if (!HardhatContext.isCreated()) {
}

if (willRunWithTypescript(hardhatArguments.config)) {
loadTsNode();
loadTsNode(hardhatArguments.tsconfig);
}

const config = loadConfigAndTasks(hardhatArguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
39 changes: 39 additions & 0 deletions packages/hardhat-core/test/internal/core/typescript-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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")
);
});
});
});

0 comments on commit 759a8af

Please sign in to comment.