Skip to content

Commit

Permalink
Merge pull request NomicFoundation#1992 from nomiclabs/tsconfig
Browse files Browse the repository at this point in the history
Enable user configurable tsconfig path
  • Loading branch information
zoeyTM authored Oct 26, 2021
2 parents e07a319 + 503229d commit 8250554
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 1,751 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-suits-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Enable user configurable tsconfig path
2 changes: 1 addition & 1 deletion docs/getting-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ GLOBAL OPTIONS:
--max-memory The maximum amount of memory that Hardhat can use.
--network The network to connect to.
--show-stack-traces Show stack traces.
--tsconfig Reserved hardhat argument -- Has no effect.
--tsconfig A TypeScript config file.
--verbose Enables Hardhat verbose logging
--version Shows hardhat's version.
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/create-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ GLOBAL OPTIONS:
--max-memory The maximum amount of memory that Hardhat can use.
--network The network to connect to.
--show-stack-traces Show stack traces.
--tsconfig Reserved hardhat argument -- Has no effect.
--tsconfig A TypeScript config file.
--verbose Enables Hardhat verbose logging
--version Shows hardhat's version.
Expand Down
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 @@
{}
2 changes: 1 addition & 1 deletion packages/hardhat-core/test/internal/cli/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const coreParams = [
name: "--max-memory",
},
{
description: "Reserved hardhat argument -- Has no effect.",
description: "A TypeScript config file.",
name: "--tsconfig",
},
verboseParam,
Expand Down
40 changes: 40 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,8 @@ 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 { expectHardhatError } from "../../helpers/errors";
import { ERRORS } from "../../../src/internal/core/errors-list";

describe("Typescript support", function () {
describe("strict typescript config", function () {
Expand Down Expand Up @@ -58,3 +60,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 () {
expectHardhatError(
() => require("../../../src/internal/lib/hardhat-lib"),
ERRORS.ARGUMENTS.INVALID_ENV_VAR_VALUE
);
});
});

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")
);
});
});
});
Loading

0 comments on commit 8250554

Please sign in to comment.