Doesn't handle tsconfig.json with comments

Open
#1 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
typescript, webpack
Domain
build-system

Research direction

Start by locating the source corresponding to lib/index.js and trace the two tsconfig.json reads shown in the issue. Review TypeScript's readConfigFile behavior and the supplied workaround, then verify that project-reference configs containing comments no longer produce a JSON parse error. Done means both alias-generation paths handle commented configs while preserving error handling.

Written by the indexing model from the issue text.

Description

The JSON parsing of tsconfig.json files doesn't seem to having comments in them, and errors with a JSON parse error.

Here's the patch I'm using to work around it (with patch-package):

diff --git a/node_modules/@microsoft/webpack-project-references-alias/lib/index.js b/node_modules/@microsoft/webpack-project-references-alias/lib/index.js
index f88fe3a..1d33585 100644
--- a/node_modules/@microsoft/webpack-project-references-alias/lib/index.js
+++ b/node_modules/@microsoft/webpack-project-references-alias/lib/index.js
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
 const fs = require("fs");
 const path = require("path");
 const functional_1 = require("./functional");
+const typescript = require("typescript");
+
 function getAliasForProject(project) {
     if (typeof project === "undefined") {
         project = process.cwd();
@@ -33,7 +35,7 @@ const getReferencedProjectsRecursive = functional_1.memoize((tsConfigPath) => {
 function getReferencedProjects(tsConfigPath) {
     var _a;
     const projectDir = path.dirname(tsConfigPath);
-    const config = require(tsConfigPath);
+    const config = readConfigFile(tsConfigPath)
     const references = (_a = config.references) === null || _a === void 0 ? void 0 : _a.map(o => o.path).map(p => path.join(projectDir, p)).map(resolveTsConfig);
     return (references !== null && references !== void 0 ? references : []);
 }
@@ -62,7 +64,7 @@ function tryGetPackageInfo(dir) {
 }
 function getAliasFor(tsConfigPath) {
     const projectRootDir = path.dirname(tsConfigPath);
-    const config = require(tsConfigPath);
+    const config = readConfigFile(tsConfigPath);
     const hasExplicitOutDir = typeof config.compilerOptions.outDir !== "undefined";
     const rootDir = path.join(projectRootDir, config.compilerOptions.rootDir || "");
     const outDir = hasExplicitOutDir
@@ -82,3 +84,6 @@ function getAliasFor(tsConfigPath) {
     alias[outDir] = rootDir;
     return alias;
 }
+function readConfigFile(tsConfigPath) {
+  return typescript.readConfigFile(tsConfigPath, path => fs.readFileSync(path, 'utf-8')).config
+}

It totally ignores errors which is naive, but good enough as a temporary workaround.

Dominant language
TypeScript
Stars
22
Forks
7
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.