Skip to content

Commit 46d70d7

Browse files
authored
When resolving type reference from custom typeRoot localtion use mangled scope name if the package is scoped name (#53166)
1 parent 42d2339 commit 46d70d7

13 files changed

+487
-2
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,13 @@ function getOriginalAndResolvedFileName(fileName: string, host: ModuleResolution
484484
};
485485
}
486486

487+
function getCandidateFromTypeRoot(typeRoot: string, typeReferenceDirectiveName: string, moduleResolutionState: ModuleResolutionState) {
488+
const nameForLookup = endsWith(typeRoot, "/node_modules/@types") || endsWith(typeRoot, "/node_modules/@types/") ?
489+
mangleScopedPackageNameWithTrace(typeReferenceDirectiveName, moduleResolutionState) :
490+
typeReferenceDirectiveName;
491+
return combinePaths(typeRoot, nameForLookup);
492+
}
493+
487494
/**
488495
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
489496
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
@@ -619,7 +626,7 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
619626
trace(host, Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", "));
620627
}
621628
return firstDefined(typeRoots, typeRoot => {
622-
const candidate = combinePaths(typeRoot, typeReferenceDirectiveName);
629+
const candidate = getCandidateFromTypeRoot(typeRoot, typeReferenceDirectiveName, moduleResolutionState);
623630
const directoryExists = directoryProbablyExists(typeRoot, host);
624631
if (!directoryExists && traceEnabled) {
625632
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, typeRoot);
@@ -3084,7 +3091,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
30843091
function resolveFromTypeRoot(moduleName: string, state: ModuleResolutionState) {
30853092
if (!state.compilerOptions.typeRoots) return;
30863093
for (const typeRoot of state.compilerOptions.typeRoots) {
3087-
const candidate = combinePaths(typeRoot, moduleName);
3094+
const candidate = getCandidateFromTypeRoot(typeRoot, moduleName, state);
30883095
const directoryExists = directoryProbablyExists(typeRoot, state.host);
30893096
if (!directoryExists && state.traceEnabled) {
30903097
trace(state.host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, typeRoot);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/a.ts(2,30): error TS2307: Cannot find module '@mangled/typescache' or its corresponding type declarations.
2+
/a.ts(4,36): error TS2307: Cannot find module '@mangled/nodemodulescache' or its corresponding type declarations.
3+
/a.ts(5,30): error TS2307: Cannot find module '@scoped/attypescache' or its corresponding type declarations.
4+
5+
6+
==== /a.ts (3 errors) ====
7+
import { typesCache } from "@scoped/typescache";
8+
import { mangledTypes } from "@mangled/typescache";
9+
~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS2307: Cannot find module '@mangled/typescache' or its corresponding type declarations.
11+
import { nodeModulesCache } from "@scoped/nodemodulescache";
12+
import { mangledNodeModules } from "@mangled/nodemodulescache";
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
!!! error TS2307: Cannot find module '@mangled/nodemodulescache' or its corresponding type declarations.
15+
import { atTypesCache } from "@scoped/attypescache";
16+
~~~~~~~~~~~~~~~~~~~~~~
17+
!!! error TS2307: Cannot find module '@scoped/attypescache' or its corresponding type declarations.
18+
import { mangledAtTypesCache } from "@mangled/attypescache";
19+
20+
==== /a/types/dummy/index.d.ts (0 errors) ====
21+
export const dummy: number;
22+
23+
==== /a/types/@scoped/typescache/index.d.ts (0 errors) ====
24+
export const typesCache: number;
25+
26+
==== /a/types/mangled__typescache/index.d.ts (0 errors) ====
27+
export const mangledTypes: number;
28+
29+
==== /a/node_modules/@scoped/nodemodulescache/index.d.ts (0 errors) ====
30+
export const nodeModulesCache: number;
31+
32+
==== /a/node_modules/mangled__nodemodulescache/index.d.ts (0 errors) ====
33+
export const mangledNodeModules: number;
34+
35+
==== /a/node_modules/@types/@scoped/attypescache/index.d.ts (0 errors) ====
36+
export const atTypesCache: number;
37+
38+
==== /a/node_modules/@types/mangled__attypescache/index.d.ts (0 errors) ====
39+
export const mangledAtTypesCache: number;
40+
41+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [tests/cases/compiler/moduleResolutionAsTypeReferenceDirectiveScoped.ts] ////
2+
3+
//// [index.d.ts]
4+
export const dummy: number;
5+
6+
//// [index.d.ts]
7+
export const typesCache: number;
8+
9+
//// [index.d.ts]
10+
export const mangledTypes: number;
11+
12+
//// [index.d.ts]
13+
export const nodeModulesCache: number;
14+
15+
//// [index.d.ts]
16+
export const mangledNodeModules: number;
17+
18+
//// [index.d.ts]
19+
export const atTypesCache: number;
20+
21+
//// [index.d.ts]
22+
export const mangledAtTypesCache: number;
23+
24+
25+
//// [a.ts]
26+
import { typesCache } from "@scoped/typescache";
27+
import { mangledTypes } from "@mangled/typescache";
28+
import { nodeModulesCache } from "@scoped/nodemodulescache";
29+
import { mangledNodeModules } from "@mangled/nodemodulescache";
30+
import { atTypesCache } from "@scoped/attypescache";
31+
import { mangledAtTypesCache } from "@mangled/attypescache";
32+
33+
34+
//// [a.js]
35+
"use strict";
36+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== /a.ts ===
2+
import { typesCache } from "@scoped/typescache";
3+
>typesCache : Symbol(typesCache, Decl(a.ts, 0, 8))
4+
5+
import { mangledTypes } from "@mangled/typescache";
6+
>mangledTypes : Symbol(mangledTypes, Decl(a.ts, 1, 8))
7+
8+
import { nodeModulesCache } from "@scoped/nodemodulescache";
9+
>nodeModulesCache : Symbol(nodeModulesCache, Decl(a.ts, 2, 8))
10+
11+
import { mangledNodeModules } from "@mangled/nodemodulescache";
12+
>mangledNodeModules : Symbol(mangledNodeModules, Decl(a.ts, 3, 8))
13+
14+
import { atTypesCache } from "@scoped/attypescache";
15+
>atTypesCache : Symbol(atTypesCache, Decl(a.ts, 4, 8))
16+
17+
import { mangledAtTypesCache } from "@mangled/attypescache";
18+
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(a.ts, 5, 8))
19+
20+
=== /a/types/dummy/index.d.ts ===
21+
export const dummy: number;
22+
>dummy : Symbol(dummy, Decl(index.d.ts, 0, 12))
23+
24+
=== /a/types/@scoped/typescache/index.d.ts ===
25+
export const typesCache: number;
26+
>typesCache : Symbol(typesCache, Decl(index.d.ts, 0, 12))
27+
28+
=== /a/node_modules/@scoped/nodemodulescache/index.d.ts ===
29+
export const nodeModulesCache: number;
30+
>nodeModulesCache : Symbol(nodeModulesCache, Decl(index.d.ts, 0, 12))
31+
32+
=== /a/node_modules/@types/mangled__attypescache/index.d.ts ===
33+
export const mangledAtTypesCache: number;
34+
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 12))
35+
36+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
[
2+
"======== Resolving module '@scoped/typescache' from '/a.ts'. ========",
3+
"Module resolution kind is not specified, using 'Node10'.",
4+
"Loading module '@scoped/typescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
5+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
6+
"Scoped package detected, looking in 'scoped__typescache'",
7+
"File '/a/types/@scoped/typescache.d.ts' does not exist.",
8+
"File '/a/types/@scoped/typescache/package.json' does not exist.",
9+
"File '/a/types/@scoped/typescache/index.d.ts' exists - use it as a name resolution result.",
10+
"Resolving real path for '/a/types/@scoped/typescache/index.d.ts', result '/a/types/@scoped/typescache/index.d.ts'.",
11+
"======== Module name '@scoped/typescache' was successfully resolved to '/a/types/@scoped/typescache/index.d.ts'. ========",
12+
"======== Resolving module '@mangled/typescache' from '/a.ts'. ========",
13+
"Module resolution kind is not specified, using 'Node10'.",
14+
"Loading module '@mangled/typescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
15+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
16+
"Scoped package detected, looking in 'mangled__typescache'",
17+
"Scoped package detected, looking in 'mangled__typescache'",
18+
"File '/a/node_modules/@types/mangled__typescache.d.ts' does not exist.",
19+
"Loading module '@mangled/typescache' from 'node_modules' folder, target file types: JavaScript.",
20+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
21+
"======== Module name '@mangled/typescache' was not resolved. ========",
22+
"======== Resolving module '@scoped/nodemodulescache' from '/a.ts'. ========",
23+
"Module resolution kind is not specified, using 'Node10'.",
24+
"Loading module '@scoped/nodemodulescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
25+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
26+
"Scoped package detected, looking in 'scoped__nodemodulescache'",
27+
"File '/a/types/@scoped/nodemodulescache.d.ts' does not exist.",
28+
"File '/a/node_modules/@scoped/nodemodulescache.d.ts' does not exist.",
29+
"File '/a/node_modules/@scoped/nodemodulescache/package.json' does not exist.",
30+
"File '/a/node_modules/@scoped/nodemodulescache/index.d.ts' exists - use it as a name resolution result.",
31+
"Resolving real path for '/a/node_modules/@scoped/nodemodulescache/index.d.ts', result '/a/node_modules/@scoped/nodemodulescache/index.d.ts'.",
32+
"======== Module name '@scoped/nodemodulescache' was successfully resolved to '/a/node_modules/@scoped/nodemodulescache/index.d.ts'. ========",
33+
"======== Resolving module '@mangled/nodemodulescache' from '/a.ts'. ========",
34+
"Module resolution kind is not specified, using 'Node10'.",
35+
"Loading module '@mangled/nodemodulescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
36+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
37+
"Scoped package detected, looking in 'mangled__nodemodulescache'",
38+
"Scoped package detected, looking in 'mangled__nodemodulescache'",
39+
"File '/a/node_modules/@types/mangled__nodemodulescache.d.ts' does not exist.",
40+
"Loading module '@mangled/nodemodulescache' from 'node_modules' folder, target file types: JavaScript.",
41+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
42+
"======== Module name '@mangled/nodemodulescache' was not resolved. ========",
43+
"======== Resolving module '@scoped/attypescache' from '/a.ts'. ========",
44+
"Module resolution kind is not specified, using 'Node10'.",
45+
"Loading module '@scoped/attypescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
46+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
47+
"Scoped package detected, looking in 'scoped__attypescache'",
48+
"File '/a/types/@scoped/attypescache.d.ts' does not exist.",
49+
"File '/a/node_modules/@scoped/attypescache.d.ts' does not exist.",
50+
"Scoped package detected, looking in 'scoped__attypescache'",
51+
"File '/a/node_modules/@types/scoped__attypescache.d.ts' does not exist.",
52+
"Loading module '@scoped/attypescache' from 'node_modules' folder, target file types: JavaScript.",
53+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
54+
"======== Module name '@scoped/attypescache' was not resolved. ========",
55+
"======== Resolving module '@mangled/attypescache' from '/a.ts'. ========",
56+
"Module resolution kind is not specified, using 'Node10'.",
57+
"Loading module '@mangled/attypescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
58+
"Directory '/node_modules' does not exist, skipping all lookups in it.",
59+
"Scoped package detected, looking in 'mangled__attypescache'",
60+
"Scoped package detected, looking in 'mangled__attypescache'",
61+
"File '/a/node_modules/@types/mangled__attypescache.d.ts' does not exist.",
62+
"File '/a/node_modules/@types/mangled__attypescache/package.json' does not exist.",
63+
"File '/a/node_modules/@types/mangled__attypescache/index.d.ts' exists - use it as a name resolution result.",
64+
"Resolving real path for '/a/node_modules/@types/mangled__attypescache/index.d.ts', result '/a/node_modules/@types/mangled__attypescache/index.d.ts'.",
65+
"======== Module name '@mangled/attypescache' was successfully resolved to '/a/node_modules/@types/mangled__attypescache/index.d.ts'. ========",
66+
"======== Resolving type reference directive 'dummy', containing file '/__inferred type names__.ts', root directory '/a/types,/a/node_modules,/a/node_modules/@types'. ========",
67+
"Resolving with primary search path '/a/types, /a/node_modules, /a/node_modules/@types'.",
68+
"File '/a/types/dummy.d.ts' does not exist.",
69+
"File '/a/types/dummy/package.json' does not exist.",
70+
"File '/a/types/dummy/index.d.ts' exists - use it as a name resolution result.",
71+
"Resolving real path for '/a/types/dummy/index.d.ts', result '/a/types/dummy/index.d.ts'.",
72+
"======== Type reference directive 'dummy' was successfully resolved to '/a/types/dummy/index.d.ts', primary: true. ========"
73+
]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== /a.ts ===
2+
import { typesCache } from "@scoped/typescache";
3+
>typesCache : number
4+
5+
import { mangledTypes } from "@mangled/typescache";
6+
>mangledTypes : any
7+
8+
import { nodeModulesCache } from "@scoped/nodemodulescache";
9+
>nodeModulesCache : number
10+
11+
import { mangledNodeModules } from "@mangled/nodemodulescache";
12+
>mangledNodeModules : any
13+
14+
import { atTypesCache } from "@scoped/attypescache";
15+
>atTypesCache : any
16+
17+
import { mangledAtTypesCache } from "@mangled/attypescache";
18+
>mangledAtTypesCache : number
19+
20+
=== /a/types/dummy/index.d.ts ===
21+
export const dummy: number;
22+
>dummy : number
23+
24+
=== /a/types/@scoped/typescache/index.d.ts ===
25+
export const typesCache: number;
26+
>typesCache : number
27+
28+
=== /a/node_modules/@scoped/nodemodulescache/index.d.ts ===
29+
export const nodeModulesCache: number;
30+
>nodeModulesCache : number
31+
32+
=== /a/node_modules/@types/mangled__attypescache/index.d.ts ===
33+
export const mangledAtTypesCache: number;
34+
>mangledAtTypesCache : number
35+
36+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error TS2688: Cannot find type definition file for '@mangled/nodemodulescache'.
2+
The file is in the program because:
3+
Entry point of type library '@mangled/nodemodulescache' specified in compilerOptions
4+
error TS2688: Cannot find type definition file for '@mangled/typescache'.
5+
The file is in the program because:
6+
Entry point of type library '@mangled/typescache' specified in compilerOptions
7+
error TS2688: Cannot find type definition file for '@scoped/attypescache'.
8+
The file is in the program because:
9+
Entry point of type library '@scoped/attypescache' specified in compilerOptions
10+
/a.ts(4,1): error TS2304: Cannot find name 'mangledNodeModules'.
11+
/a.ts(5,1): error TS2552: Cannot find name 'atTypesCache'. Did you mean 'typesCache'?
12+
13+
14+
!!! error TS2688: Cannot find type definition file for '@mangled/nodemodulescache'.
15+
!!! error TS2688: The file is in the program because:
16+
!!! error TS2688: Entry point of type library '@mangled/nodemodulescache' specified in compilerOptions
17+
!!! error TS2688: Cannot find type definition file for '@mangled/typescache'.
18+
!!! error TS2688: The file is in the program because:
19+
!!! error TS2688: Entry point of type library '@mangled/typescache' specified in compilerOptions
20+
!!! error TS2688: Cannot find type definition file for '@scoped/attypescache'.
21+
!!! error TS2688: The file is in the program because:
22+
!!! error TS2688: Entry point of type library '@scoped/attypescache' specified in compilerOptions
23+
==== /a.ts (2 errors) ====
24+
typesCache;
25+
mangledAtTypesCache;
26+
nodeModulesCache;
27+
mangledNodeModules;
28+
~~~~~~~~~~~~~~~~~~
29+
!!! error TS2304: Cannot find name 'mangledNodeModules'.
30+
atTypesCache;
31+
~~~~~~~~~~~~
32+
!!! error TS2552: Cannot find name 'atTypesCache'. Did you mean 'typesCache'?
33+
!!! related TS2728 /types/@scoped/typescache/index.d.ts:1:15: 'typesCache' is declared here.
34+
mangledAtTypesCache;
35+
==== /types/@scoped/typescache/index.d.ts (0 errors) ====
36+
declare const typesCache: number;
37+
38+
==== /types/mangled__typescache/index.d.ts (0 errors) ====
39+
declare const mangledTypes: number;
40+
41+
==== /node_modules/@scoped/nodemodulescache/index.d.ts (0 errors) ====
42+
declare const nodeModulesCache: number;
43+
44+
==== /node_modules/mangled__nodemodulescache/index.d.ts (0 errors) ====
45+
declare const mangledNodeModules: number;
46+
47+
==== /node_modules/@types/@scoped/attypescache/index.d.ts (0 errors) ====
48+
declare const atTypesCache: number;
49+
50+
==== /node_modules/@types/mangled__attypescache/index.d.ts (0 errors) ====
51+
declare const mangledAtTypesCache: number;
52+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/typeReferenceDirectiveScopedPackageCustomTypeRoot.ts] ////
2+
3+
//// [index.d.ts]
4+
declare const typesCache: number;
5+
6+
//// [index.d.ts]
7+
declare const mangledTypes: number;
8+
9+
//// [index.d.ts]
10+
declare const nodeModulesCache: number;
11+
12+
//// [index.d.ts]
13+
declare const mangledNodeModules: number;
14+
15+
//// [index.d.ts]
16+
declare const atTypesCache: number;
17+
18+
//// [index.d.ts]
19+
declare const mangledAtTypesCache: number;
20+
21+
//// [a.ts]
22+
typesCache;
23+
mangledAtTypesCache;
24+
nodeModulesCache;
25+
mangledNodeModules;
26+
atTypesCache;
27+
mangledAtTypesCache;
28+
29+
//// [a.js]
30+
typesCache;
31+
mangledAtTypesCache;
32+
nodeModulesCache;
33+
mangledNodeModules;
34+
atTypesCache;
35+
mangledAtTypesCache;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
=== /a.ts ===
2+
typesCache;
3+
>typesCache : Symbol(typesCache, Decl(index.d.ts, 0, 13))
4+
5+
mangledAtTypesCache;
6+
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 13))
7+
8+
nodeModulesCache;
9+
>nodeModulesCache : Symbol(nodeModulesCache, Decl(index.d.ts, 0, 13))
10+
11+
mangledNodeModules;
12+
atTypesCache;
13+
mangledAtTypesCache;
14+
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 13))
15+
16+
=== /types/@scoped/typescache/index.d.ts ===
17+
declare const typesCache: number;
18+
>typesCache : Symbol(typesCache, Decl(index.d.ts, 0, 13))
19+
20+
=== /node_modules/@scoped/nodemodulescache/index.d.ts ===
21+
declare const nodeModulesCache: number;
22+
>nodeModulesCache : Symbol(nodeModulesCache, Decl(index.d.ts, 0, 13))
23+
24+
=== /node_modules/@types/mangled__attypescache/index.d.ts ===
25+
declare const mangledAtTypesCache: number;
26+
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 13))
27+

0 commit comments

Comments
 (0)