nx: nx tries to parse JSONC files as JSON which causes commands to fail
I created a “hello-world”-style NX repository using the following command:
npx create-nx-workspace isaacscript --preset=ts
After that, the only modification I made was to add more content to the extensions.json file.
After that, I ran the following command:
nx generate @nrwl/js:library --name=isaac-typescript-definitions --buildable --publishable --importPath="isaac-typescript-definitions"
It fails, with the following error message:
Cannot parse .vscode/extensions.json: ValueExpected in JSON at position 440
The “extensions.json” file is a JSONC file. JSONC is a format that supports comments and trailing commas. Thus, I believe that the error message is related to NX is trying to parse the JSONC file as a JSON file.
Since it is common for “extensions.json”, “settings.json”, and so on to contain JSONC features, this bug should be fixed!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 17 (6 by maintainers)
The issue is still relevant and is a somewhat annoying flaw in nx, especially since it affects brand-new users who are onboarding new existing monorepos.
What sucks about this issue is that you run into very early. I had just set up my Nx workspace, made a couple changes to migrate existing/working tsconfigs, and then tried to run my first Nx command, and it failed with a really obscure error (see OP). When I visited the file, there were no visible errors (my editor shows errors in tsconfigs). This was not a pleasant “first taste” of Nx, and many others are likely to run into it. It’s an important issue because it affects new adopters of Nx, especially since Nx sells itself as “works with any project”.
Upon further investigation, it seems that the problem is much worse than what I outlined in the OP. Building anything with NX also fails, because it isn’t able to parse trailing commas in a tsconfig.json file.
For example, running:
Gives the following error:
(Position 530 is the trailing comma.)
The original error for parsing JSONC files have been fixed. We’ve also moved TS/JS inspection (for building Nx graph) to Rust, so the original issue of reading
tsconfig.base.jsonisn’t relevant.I tested this on the latest version of Nx, and could not reproduce. If you run into any problems, please open a new issue with reproduction steps. Thanks!
I mean, it’s easy enough to avoid by taking the trailing commas out of tsconfig files. I don’t love it either, but not a deal breaker for a far better monorepo experience.
If you use vscode, the prettier formatter will strip them as a workaround:
I just ran into this issue too. They are using the
jsonc-parser, which has an option forallowTrailingComma(I don’t think jsonc allows trailing commas by default). The options are passed as the 3rd parameter here:https://github.com/nrwl/nx/blob/master/packages/nx/src/utils/json.ts#L49
It would be nice if trailing commas were allowed by default, especially when reading tsconfig files.
I just concluded the same as @Zamiell has mentioned with trailing comma in a tsconfig.json file.
To give some context to my issue, it failed from the 3rd
pathsentry due to the comma in the array element: