Repository navigation
TypeScript: compilerOptions.types not respected when using project references #27372
Description
Activity
From Oliver Joseph Ash (@OliverJAsh) on September 26, 2018 14:26
Actually, this might not be confined to
compilerOptions.types. I've also noticed other compiler options likenoImplicitReturnsare not respected.This is caused by
src/__tests__being placed in the inferred project, instead of thetsconfigproject it belongs to. Here's the tsserver.log and relevant project section:Info 125 [11:15:0.406] Project '/dev/null/inferredProject1*' (Inferred) 0 Info 125 [11:15:0.406] Files (21) /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es5.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2016.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.dom.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.dom.iterable.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.webworker.importscripts.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.scripthost.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.core.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.collection.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.generator.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.iterable.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.promise.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.proxy.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.reflect.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.symbol.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2016.array.include.d.ts /Path/jest-ts-project-references/node_modules/typescript/lib/lib.es2016.full.d.ts /Path/jest-ts-project-references/src/sum.ts /Path/jest-ts-project-references/src/__tests__/sum.ts /Path/jest-ts-project-references/node_modules/@types/jest/index.d.tstscgets the project associations right when runsheetalkamat commented
on Sep 26, 2018 MemberMore actionsAt moment this is working as expected since the referenced projects are not loaded as part of file open. Thus since _tests/sum.ts does not belong to tsconfig.json so inferred project is created.
Here we could check if the file belongs to any of the referenced project but that would be additional work on each file open (if file didn't belong to the main project) so we should add this feature with careful thought.
cc: Ryan Cavanaugh (@RyanCavanaugh) Another instance of need to unload/load project on demand (if/when we do this)- addedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Oct 1, 2018 - added a commit that references this issue
on Oct 11, 2018 - added a commit that references this issue
on Nov 2, 2018 Ran into this issue and having trouble working around it. I thought I'd describe our use case here to both bump this issue and see if there's any solution that might work for us.
Our runtime loads special JS/TS files from a flat file structure which have specific globals available within their file scope. I'll call these "entrypoint" files. Entrypoints are not allowed to "export" things. They run in their own scope. Each entrypoint must define an initialization function called "init" in the file scope. It is very often the case that a project has multiple of these files. The TypeScript compiler recognizes these files as script files instead of modules. The consequence of this is that TypeScript sees multiple declarations of the same identifier (ex: for the initialization function) in the same scope (the global scope is shared across the script files in the same project) and emits a compiler error.
An idea for a possible solution is to separate the compilation of each of these entrypoint files so that we have more control over what's included in each compilation. The compiler provides us a way to do this efficiently with project references. We can separate each of the entrypoints into their own TypeScript project and put all of the standard ES modules into a separate project that can be referenced from each of the other projects.
As an example, if we have
entrypoint1.tsandentrypoint2.ts, we'd createtsconfig.entrypoint1.jsonandtsconfig.entrypoint2.json. Then if our project has other ES modules (module1.ts,module2.ts, etc), we can create atsconfig.modules.jsonand add a project reference to this from the entrypoint projects. Then we have a "solution" tsconfig.json at the root which has references to all of these.Notice how this graph is created:
modules / \ entrypoint1 entrypoint2entrypoint1andentrypoint2depend onmodules, but neither is included in the other's compilation, so the declarations inentrypoint1shouldn't conflict with those inentrypoint2. This all works fine and when runningtschere, we get no errors! Unfortunately, while testing this out, I ran into this issue with the tsserver. It doesn't seem to respect project references astscdoes. While I've confirmed that setting up a project like this works perfectly fine when compiling withtscon the command line, VSCode treats each file as if it doesn't have an associated tsconfig at all... This idiosyncrasy between tsserver and tsc makes project references effectively unusable for us.It currently seems like there is no way to solve this problem with the following constraints:
- no
export - consistent behavior between language server and compiler
- no change to the file structure of the original projects
- no
sheetalkamat commented
on Mar 10, 2020 MemberMore actions#37239 should help with this
Reacted by Joey Watts, Guilherme Tod and Rob Palmer- addedFix AvailableA PR has been opened for this issueA PR has been opened for this issueand removedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Mar 10, 2020 - added a commit that references this issue
on Feb 1, 2022
From Oliver Joseph Ash (@OliverJAsh) on September 26, 2018 13:23
Steps to Reproduce:
Full reproduction case
tsconfig.jsonthat has project references, where the referenced TS projects specify a customcompilerOptions.typesExpected: VSCode should respect
compilerOption.types.Actual: VSCode appears to ignore
compilerOption.types.For example, I have three TS projects:
tsconfig-src.jsonwhich should not havejesttypes (compilerOption.typesis set to[])tsconfig-tests.jsonwhich should havejesttypes (compilerOption.typesis set to["jest"])tsconfig.jsonwhichreferencesboth of the aboveThe following file is part of the
tsconfig-src.jsonproject:When I run
tsc -b -f tsconfig.json, I get the expected error:(
testis a global type provided by thejesttypes.)However, VSCode has no error. Instead, the types appear to be available, even when they shouldn't:
You can try this for yourself with the provided full reproduction case.
Copied from original issue: microsoft/vscode#59466