Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module resolution: typeRoots key conflict in cache #58367

Closed
egalli opened this issue Apr 29, 2024 · 3 comments
Closed

Module resolution: typeRoots key conflict in cache #58367

egalli opened this issue Apr 29, 2024 · 3 comments
Labels
Not a Defect This behavior is one of several equally-correct options

Comments

@egalli
Copy link

egalli commented Apr 29, 2024

Demo Repo

https://github.com/egalli/type-root-test

Which of the following problems are you reporting?

Something else more complicated which I'll explain in more detail

Demonstrate the defect described above with a code sample.

src/index.ts

export const A: AType = "typeA";
// error TS2552: Cannot find name 'BType'. Did you mean 'AType'?
export const B: BType = "typeB"; 

Run tsc --showConfig and paste its output here

{
    "compilerOptions": {
        "outDir": "./dist",
        "typeRoots": [
            "./@pkg1/a",
            "./@pkg2/b"
        ]
    },
    "files": [
        "./src/index.ts"
    ],
    "exclude": [
        "@pkg1",
        "@pkg2"
    ]
}

Run tsc --traceResolution and paste its output here

File 'C:/workspace/ts-test/src/package.json' does not exist.
Found 'package.json' at 'C:/workspace/ts-test/package.json'.
======== Resolving type reference directive 'dist', containing file 'C:/workspace/ts-test/__inferred type names__.ts', root directory 'C:/workspace/ts-test/@pkg1/a,C:/workspace/ts-test/@pkg2/b'. ========
Resolving with primary search path 'C:/workspace/ts-test/@pkg1/a, C:/workspace/ts-test/@pkg2/b'.
File 'C:/workspace/ts-test/@pkg1/a/dist.d.ts' does not exist.
File 'C:/workspace/ts-test/@pkg1/a/dist/package.json' does not exist.
File 'C:/workspace/ts-test/@pkg1/a/dist/index.d.ts' exists - use it as a name resolution result.
Resolving real path for 'C:/workspace/ts-test/@pkg1/a/dist/index.d.ts', result 'C:/workspace/ts-test/@pkg1/a/dist/index.d.ts'.
======== Type reference directive 'dist' was successfully resolved to 'C:/workspace/ts-test/@pkg1/a/dist/index.d.ts', primary: true. ========
File 'C:/workspace/ts-test/@pkg1/a/dist/package.json' does not exist according to earlier cached lookups.
Found 'package.json' at 'C:/workspace/ts-test/@pkg1/a/package.json'.
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist.
Found 'package.json' at 'C:/workspace/ts-test/node_modules/typescript/package.json'.
======== Resolving module '@typescript/lib-es5' from 'C:/workspace/ts-test/__lib_node_modules_lookup_lib.es5.d.ts__.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory 'C:/workspace/ts-test/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-es5'
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-es5'
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-es5'
Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: JavaScript.
Searching all ancestor node_modules directories for fallback extensions: JavaScript.
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@typescript/lib-es5' was not resolved. ========
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups.
File 'C:/workspace/ts-test/node_modules/typescript/package.json' exists according to earlier cached lookups.
======== Resolving module '@typescript/lib-decorators' from 'C:/workspace/ts-test/__lib_node_modules_lookup_lib.decorators.d.ts__.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module '@typescript/lib-decorators' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory 'C:/workspace/ts-test/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-decorators'
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-decorators'
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-decorators'
Loading module '@typescript/lib-decorators' from 'node_modules' folder, target file types: JavaScript.
Searching all ancestor node_modules directories for fallback extensions: JavaScript.
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@typescript/lib-decorators' was not resolved. ========
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups.
File 'C:/workspace/ts-test/node_modules/typescript/package.json' exists according to earlier cached lookups.
======== Resolving module '@typescript/lib-decorators/legacy' from 'C:/workspace/ts-test/__lib_node_modules_lookup_lib.decorators.legacy.d.ts__.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module '@typescript/lib-decorators/legacy' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory 'C:/workspace/ts-test/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-decorators/legacy'
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-decorators/legacy'
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-decorators/legacy'
Loading module '@typescript/lib-decorators/legacy' from 'node_modules' folder, target file types: JavaScript.
Searching all ancestor node_modules directories for fallback extensions: JavaScript.
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@typescript/lib-decorators/legacy' was not resolved. ========
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups.
File 'C:/workspace/ts-test/node_modules/typescript/package.json' exists according to earlier cached lookups.
======== Resolving module '@typescript/lib-dom' from 'C:/workspace/ts-test/__lib_node_modules_lookup_lib.dom.d.ts__.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory 'C:/workspace/ts-test/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-dom'
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-dom'
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-dom'
Loading module '@typescript/lib-dom' from 'node_modules' folder, target file types: JavaScript.
Searching all ancestor node_modules directories for fallback extensions: JavaScript.
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@typescript/lib-dom' was not resolved. ========
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups.
File 'C:/workspace/ts-test/node_modules/typescript/package.json' exists according to earlier cached lookups.
======== Resolving module '@typescript/lib-webworker/importscripts' from 'C:/workspace/ts-test/__lib_node_modules_lookup_lib.webworker.importscripts.d.ts__.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module '@typescript/lib-webworker/importscripts' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory 'C:/workspace/ts-test/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-webworker/importscripts'
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-webworker/importscripts'
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-webworker/importscripts'
Loading module '@typescript/lib-webworker/importscripts' from 'node_modules' folder, target file types: JavaScript.
Searching all ancestor node_modules directories for fallback extensions: JavaScript.
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@typescript/lib-webworker/importscripts' was not resolved. ========
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups.
File 'C:/workspace/ts-test/node_modules/typescript/package.json' exists according to earlier cached lookups.
======== Resolving module '@typescript/lib-scripthost' from 'C:/workspace/ts-test/__lib_node_modules_lookup_lib.scripthost.d.ts__.ts'. ========
Explicitly specified module resolution kind: 'Node10'.
Loading module '@typescript/lib-scripthost' from 'node_modules' folder, target file types: TypeScript, Declaration.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory 'C:/workspace/ts-test/node_modules/@types' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-scripthost'
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-scripthost'
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'typescript__lib-scripthost'
Loading module '@typescript/lib-scripthost' from 'node_modules' folder, target file types: JavaScript.
Searching all ancestor node_modules directories for fallback extensions: JavaScript.
Directory 'C:/workspace/node_modules' does not exist, skipping all lookups in it.
Directory 'C:/node_modules' does not exist, skipping all lookups in it.
======== Module name '@typescript/lib-scripthost' was not resolved. ========
File 'C:/workspace/ts-test/node_modules/typescript/lib/package.json' does not exist according to earlier cached lookups.
File 'C:/workspace/ts-test/node_modules/typescript/package.json' exists according to earlier cached lookups.
src/index.ts:4:17 - error TS2552: Cannot find name 'BType'. Did you mean 'AType'?

4 export const B: BType = "typeB";
                  ~~~~~


Found 1 error in src/index.ts:4

Paste the package.json of the importing module, if it exists

{
  "name": "type-root-test",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "keywords": [],
  "author": "",
  "license": "Unlicense",
  "devDependencies": {
    "typescript": "^5.5.0-dev.20240429"
  }
}

Paste the package.json of the target module, if it exists

@pkg1/a/package.json

{
    "name": "@pkg1/a",
    "types": "dist/index.d.ts"
}

@pkg2/b/package.json

{
    "name": "@pkg2/b",
    "types": "dist/index.d.ts"
}

Any other comments can go here

There seems to be an issue when adding multiple typeRoots that have folders with the exact same name ("dist" in the example repo). The issue seems to be a that these folders will have the same to the key in the cache in the src/compiler/program.ts:loadWithModeAwareCache function.

Context for this issues
I'm a developer work on enabling the WebNN W3C specification in a project that requires both WebNN and WebGPU. If the WebNN CG creates a @webnn/types project based on the existing @webgpu/types, and developers include both typeRoots (i.e. "typeRoots": ["./node_modules/@webgpu/types", "./node_modules/@webnn/types"]), developers would run into this issue.

@egalli egalli changed the title Module resolution: Module resolution: typeRoots key conflict in cache Apr 29, 2024
@RyanCavanaugh RyanCavanaugh added the Not a Defect This behavior is one of several equally-correct options label May 1, 2024
@RyanCavanaugh
Copy link
Member

This isn't really a situation suitable for using typeRoots to solve.

typeRoots tells TS that when resolving a type directive, each root should be consulted.

Separately, if types is not specified, then the types list is inferred from whatever directory names are in the typeRoots

So you have, effectively, types: ["dist"], which resolves to "./@pkg1/a/dist", and the resolution of dist is done. Nothing else happens.

You should add these folders to include or files if you want them both in your program.

@egalli
Copy link
Author

egalli commented May 1, 2024

When using types and the base project needs to override the default typeRoots, does that mean they they would need to add ./node_modules to the typeRoots. i.e.

{
    "compilerOptions": {
        "typeRoots": [
            /* other project specific type roots */
            "./node_modules/"
        ],
        "types": [
            "@webgpu/types",
            "@webnn/types"
        ]
    }
}

Still, it seems kind of unexpected that different folders with the same name, but on different typeRoots, would be cache as the same module.

Thanks for all the help!

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not a Defect This behavior is one of several equally-correct options
Projects
None yet
Development

No branches or pull requests

3 participants