TypeScript: Building composite project with resolveJsonModule fails
TypeScript Version: 3.0.0-rc
Search Terms: resolveJsonModule, composite project, TS6307
Code
Repro at https://github.com/strax/ts-3-json-bug-repro
Expected behavior:
With composite: true and resolveJsonModule: true set in tsconfig.json, JSON modules are resolved in the compilation like they are when the composite compiler option is not set.
Actual behavior:
Running tsc gives the following error:
error TS6307: File '/src/hello.json' is not in project file list. Projects must list all files or use an 'include' pattern.
In addition, trying to include src/hello.json in the files property fails with
error TS6054: File '/src/hello.json' has unsupported extension. The only supported extensions are '.ts', '.tsx', '.d.ts'.
Playground Link:
Related Issues:
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 17
- Comments: 24 (7 by maintainers)
Commits related to this issue
- Support resolveJsonModule option when files contain the json file Fixes #25636 — committed to microsoft/TypeScript by sheetalkamat 6 years ago
- Allow files to be included by `*.json` pattern in include of tsconfig Fixes #25636 — committed to microsoft/TypeScript by sheetalkamat 6 years ago
- Enable composite builds. Few additional tricks are required: 1) Due to resolveJsonModule being true, we need to explicitly add json files to "include" (https://github.com/Microsoft/TypeScript/issues/... — committed to sr-2020/nodejs-monorepo by aeremin 5 years ago
- :wrench: include json file, ref from https://github.com/microsoft/TypeScript/issues/25636 — committed to TsaiTsaiChieh/never-stray by TsaiTsaiChieh 2 years ago
This works for me:
This is still an issue
I was spending time fiddling around with this until I realized it was a TS issue.
It is confusing and not intuitive to have to write something like
@sheetalkamat Thank you for the comment, I see your point.
Let me explain my use case. In LoopBack, we have a concept of a data source - a class providing access to a database, web service, etc. An application (a LoopBack/TypeScript project) can have multiple datasources. Each datasource is implemented in two files: a JSON file with the configuration (to make it easy to edit programmatically) and a TypeScript file exporting the datasource class.
The TypeScript code in
src/datasources/db.datasource.ts(full version):JSON config in
src/datasources/db.datasource.json(full version):Relevant parts of tsconfig (we are not using project references yet):
Now back to the topic of this issue:
When the project is not composite and compiler is running in the “classic” mode (without
-b), it correctly understands thatdb.datasource.tsis importingdb.datasource.jsonand therefore the JSON file needs to be included in the compilation too.I find it confusing that composite/build mode changes this behavior.
When the project is switched to composite mode (the only change being adding
composite: truetocompilerConfig), the compilation starts failing with errorTS6307. Despite this error, the actual JSON file is correctly copied to the output directory.I find it confusing that the compiler understands that a JSON file is imported and thus needs to be copied to the output directory, yet it complains that it was not explicitly listed in tsconfig’s
filessection.I find the requirement to explicitly add all JSON files one by one as a poor user experience. For applications (projects) containing many JSON files, the
tsconfigcontent can become large and difficult to maintain.If my first two arguments above are not convincing enough and/or there are other reasons explaining why JSON files must be treated differently in composite vs. non-composite projects: Can we at least allow JSON files to be specified via an explicit pattern in
include, e.g.src/datasources/*.datasource.json? By explicit I mean that the pattern must match JSON files only (i.e. the pattern string must end with.json).**/*src/foo.ts**/*src/data.jsonsrc/*.jsonsrc/foo.tssrc/*.jsonsrc/data.jsonWe do not want to add json files in compilation accidently and want users to explicitly specify them instead of using the include/exclude pattern.
I’m experiencing the same issue my project is setup as followed:
My main entry configuration file is
src/tsconfig.jsonwhich only have references to other sub projects localized insrc/main,src/workerandsrc/utilsand in themainproject I’m trying to access thesrc/consts.jsonfile and I’m getting the error:Although I’ve listed it explicitly through
filesinsrc/main/tsconfig@strax @sheetalkamat @mhegazy Is this problem fixed in TypeScript 3.1.1?
I have tried to use TypeScript 3.1.1 in the repro project, TypeScript gives the following error:
It seems to this problem is still exist.
this still an issue 2021 … why this been closed?
@trusktr’s solution worked, but I had to restart my IDE.
Why is this issue closed?
@sheetalkamat We ran into a similar issue as @cockscomb.
tsccomplains about json files not in project file list no matter how we tweaktsconfig.json:None of the following works:
"include": ["**/*"]"include": ["**/*.json", "**/*.ts"]Do we have to use
filesto include json files?@cockscomb you need to have files clause to have the json file(
"files": ["src/hello.json"]in the tsconfig file). The issue here was it reported unsupported extension (which is not what you are seeing) In your cc: @RyanCavanaughwe should allow
.jsonin files list if--resolveJsonModulesis enabled.There is still the issue of JSON files in
node_modulesnot working.@zerubeus & anyone reading this in the future - I just checked after running into this issue now, it seem like @TsaiTsaiChieh 's PR above fixes the issue by allowing a glob pattern in tsconfig’s include property:
include: [“**/*.json”]
https://github.com/TsaiTsaiChieh/never-stray/commit/ac22f38c00a0a7f997ee3ca1a7c9eb6b90feee50
Confirmed working on typescript 4.5.4
Just add this to the root of
tsconfig.json: