microsoft/TypeScript

[project-references] Usage with esnext yields TS2307 Cannot find module project references

rosskevin opened this issue · 5 comments

TypeScript Version: 3.1.0-dev.20180831

Search Terms:
error TS2307: Cannot find module project references

Code
Reproduction: https://github.com/rosskevin/learn-a/tree/yarn-workspaces-TS2307

  "compilerOptions": {
    "outDir": "lib",
    "rootDir": "src",
    // leave imports as they are
    "module": "esnext",
    // do not transpile stuff like classes, async/await, ...
    "target": "esnext"
  }

Expected behavior:
Using esnext with project references should work just like commonjs.

Actual behavior:
Using esnext results in error TS2307: Cannot find module project

Playground Link:
Reproduction: https://github.com/rosskevin/learn-a/tree/yarn-workspaces-TS2307

Related Issues:
#25600

#26823 is a direct file import bug that may affect this. I will try to back out this difference and just prove the esnext part here.

Actually, this bug should stand on it's own, because it is broken with esnext usage and not broken with commonjs usage.

This is working as intended. You need to specify moduleResolution as node here if you intend to use the package from learna otherwise there is no way from tsconfig to know what you are saying. (Note you can use option --traceResolution to get information about how resolution takes place.
With classic resolution the files are looked only in the ancestor directories for .ts, .tsx and .d.ts So the error.

c:\temp\temp\learn-a>node c:\TypeScript\built\local\tsc.js -b packages\pkg2  --traceResolution

======== Resolving module '@ryancavanaugh/pkg1' from 'c:/temp/temp/learn-a/packages/pkg2/src/index.ts'. ========
Module resolution kind is not specified, using 'Classic'.
Directory 'c:/temp/temp/learn-a/packages/pkg2/src/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/temp/learn-a/packages/pkg2/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/temp/learn-a/packages/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/temp/learn-a/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/temp/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
======== Module name '@ryancavanaugh/pkg1' was not resolved. ========
packages/pkg2/src/index.ts:1:16 - error TS2307: Cannot find module '@ryancavanaugh/pkg1'.

1 import p1 from '@ryancavanaugh/pkg1';
                 ~~~~~~~~~~~~~~~~~~~~~

Changing tsconfig to use moduleResolution as node:

c:\temp\temp\learn-a>node c:\TypeScript\built\local\tsc.js -b packages\pkg2  --traceResolution
======== Resolving module '@ryancavanaugh/pkg1' from 'c:/temp/temp/learn-a/packages/pkg2/src/index.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module '@ryancavanaugh/pkg1' from 'node_modules' folder, target file type 'TypeScript'.
Directory 'c:/temp/temp/learn-a/packages/pkg2/src/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/temp/learn-a/packages/pkg2/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
Directory 'c:/temp/temp/learn-a/packages/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'ryancavanaugh__pkg1'
'package.json' has 'typings' field 'lib/index.d.ts' that references 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1/lib/index.d.ts'.
'package.json' does not have a 'typesVersions' field.
Found 'package.json' at 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1/package.json'. Package ID is '@ryancavanaugh/pkg1/lib/index.d.ts@3.0.2'.
File 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1.ts' does not exist.
File 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1.tsx' does not exist.
File 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1.d.ts' does not exist.
'package.json' has 'typings' field 'lib/index.d.ts' that references 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1/lib/index.d.ts'.
File 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1/lib/index.d.ts' exist - use it as a name resolution result.
Resolving real path for 'c:/temp/temp/learn-a/node_modules/@ryancavanaugh/pkg1/lib/index.d.ts', result 'c:/temp/temp/learn-a/packages/pkg1/lib/index.d.ts'.
======== Module name '@ryancavanaugh/pkg1' was successfully resolved to 'c:/temp/temp/learn-a/packages/pkg1/lib/index.d.ts'. ========

This is working as intended

This cannot be working as intended because that would mean projects references are not on par as a single projecttsc without them.

We build esnext libraries and consume them with ts-loader + babel + webpack. Without project-references I can build fine using tsc. With project references it fails. This sounds like a bug, not an intentional design decision. Also, as-is, we are using lerna + yarn workspaces + path aliases and it works fine.

I cannot believe that project-references is seeking to avoid feature parity with single project tsc. Please confirm that you want project-references to be limited by comparison.

Wait, I misunderstood, the default moduleResolution is not node?

Confirmed as working in (change to moduleResolution: node pushed):
https://github.com/rosskevin/learn-a/tree/yarn-workspaces-TS2307

Thanks @sheetalkamat