question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

tsdx silently fails parsing of tsconfig files containing comments, trailing commas, etc. that TSC accepts

See original GitHub issue

Current Behavior

tsc supports tsconfig.json files that contain comments, trailing commas, and other features beyond standard JSON.

Current tsdx, however, parses tsconfig.json using fs.readJson which ends up calling JSON.parse which will throw an exception if faced with comments, trailing commas, and other non-standard JSON content. Making things worse, the exception is silently ignored, which makes it really hard to figure out why config settings are being ignored. I had to step through tsdx code to figure out what was going on.

https://github.com/jaredpalmer/tsdx/blob/4f6de1083393057903a1837fb6658f8058ee832f/src/createRollupConfig.ts#L46-L49

Luckily this JSON is only used once in tsdx (only in createRollupConfig.ts) and that JSON is only currently used for one setting: esModuleInterop. But #468 is about to use it too for processing declarationDir.

BTW, tsdx is in good company with this problem: Create React App had the same problem.

There’s also an extends feature in tsconfig which allows configuration inheritance across multiple tsconfig files. This isn’t supported by tsdx either. See #484.

Expected behavior

  1. tsdx should accept the same tsconfig.json syntax that does.
  2. Syntax errors (or other unexpected errors) reading tsconfig.json should not be silently ignored.

Suggested solution(s)

For 1) Luckily, the nice people in TypeScript-land have exported their tsconfig parser. From https://github.com/facebook/create-react-app/issues/6865#issuecomment-485962203:

Unfortunately it looks like typescript uses a hand-written parser. I traced it down to parseJsonText which is defined in src/compiler/parser.ts. Ahahaha.

If we’re allowed to hook into the typescript package, it looks like they provide public access to their parse function.

Note that there are a whole set of parse functions defined. Hopefully one of those would meet tsdx’s needs. Ideally it’d also support the extends keyword in tsconfig.json which is also currently unsupported by tsdx.

for 2) I’d suggest white-listing ignorable exceptions (e.g. file not found?) and reporting the rest to the user. In particular, any syntax errors should be reported to the user and should fail the build.

Additional context

Your environment

Software Version(s)
TSDX tsdx@0.12.3
TypeScript typescript@3.7.5
Browser n/a (this is a build issue)
npm/Yarn yarn@1.21.1
Node v12.13.1
Operating System MacOS Catalina 10.15.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
agilgur5commented, Mar 10, 2020

@allcontributors please add @justingrant for bugs, ideas

0reactions
allcontributors[bot]commented, Mar 10, 2020

@agilgur5

I’ve put up a pull request to add @justingrant! 🎉

Read more comments on GitHub >

github_iconTop Results From Across the Web

215 - Stack Overflow
Parsing error : Cannot read file 'd:\test\testproject\tsconfig.app.eslint.json'.
Read more >
tsconfig.json isn't strict JSON, what now? : r/typescript - Reddit
Any tools that are "doing it right" shouldn't be parsing the config file themselves, so I'd say go nuts with comments and trailing...
Read more >
PhpStorm 2020.3 Release Notes | Knowledge Base - YouTrack
Feature WI‑55626 Make parameter info available during indexing Bug WI‑54938 Twig: local variable scope Bug WI‑54053 Change parameter type: update children Feature WI‑56151 Provide `#` attribute...
Read more >
@netlify/esbuild-freebsd-arm64 | Yarn - Package Manager
The /* @__PURE__ */ comment annotation can be added to function calls to indicate that they are side-effect free. These annotations are passed...
Read more >
TypeScript
For each of those packages, TypeScript will first check whether package.json contains a "tsconfig" field, and if it does, TypeScript will try to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found