Skip to content

When resolving type reference from custom typeRoot localtion use mangled scope name if the package is scoped name #53166

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

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@ function getOriginalAndResolvedFileName(fileName: string, host: ModuleResolution
};
}

function getCandidateFromTypeRoot(typeRoot: string, typeReferenceDirectiveName: string, moduleResolutionState: ModuleResolutionState) {
const nameForLookup = endsWith(typeRoot, "/node_modules/@types") || endsWith(typeRoot, "/node_modules/@types/") ?
mangleScopedPackageNameWithTrace(typeReferenceDirectiveName, moduleResolutionState) :
typeReferenceDirectiveName;
return combinePaths(typeRoot, nameForLookup);
}

/**
* @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown.
* This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups
Expand Down Expand Up @@ -619,7 +626,7 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
trace(host, Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", "));
}
return firstDefined(typeRoots, typeRoot => {
const candidate = combinePaths(typeRoot, typeReferenceDirectiveName);
const candidate = getCandidateFromTypeRoot(typeRoot, typeReferenceDirectiveName, moduleResolutionState);
const directoryExists = directoryProbablyExists(typeRoot, host);
if (!directoryExists && traceEnabled) {
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, typeRoot);
Expand Down Expand Up @@ -3084,7 +3091,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
function resolveFromTypeRoot(moduleName: string, state: ModuleResolutionState) {
if (!state.compilerOptions.typeRoots) return;
for (const typeRoot of state.compilerOptions.typeRoots) {
const candidate = combinePaths(typeRoot, moduleName);
const candidate = getCandidateFromTypeRoot(typeRoot, moduleName, state);
const directoryExists = directoryProbablyExists(typeRoot, state.host);
if (!directoryExists && state.traceEnabled) {
trace(state.host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, typeRoot);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/a.ts(2,30): error TS2307: Cannot find module '@mangled/typescache' or its corresponding type declarations.
/a.ts(4,36): error TS2307: Cannot find module '@mangled/nodemodulescache' or its corresponding type declarations.
/a.ts(5,30): error TS2307: Cannot find module '@scoped/attypescache' or its corresponding type declarations.


==== /a.ts (3 errors) ====
import { typesCache } from "@scoped/typescache";
import { mangledTypes } from "@mangled/typescache";
~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module '@mangled/typescache' or its corresponding type declarations.
import { nodeModulesCache } from "@scoped/nodemodulescache";
import { mangledNodeModules } from "@mangled/nodemodulescache";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module '@mangled/nodemodulescache' or its corresponding type declarations.
import { atTypesCache } from "@scoped/attypescache";
~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module '@scoped/attypescache' or its corresponding type declarations.
import { mangledAtTypesCache } from "@mangled/attypescache";

==== /a/types/dummy/index.d.ts (0 errors) ====
export const dummy: number;

==== /a/types/@scoped/typescache/index.d.ts (0 errors) ====
export const typesCache: number;

==== /a/types/mangled__typescache/index.d.ts (0 errors) ====
export const mangledTypes: number;

==== /a/node_modules/@scoped/nodemodulescache/index.d.ts (0 errors) ====
export const nodeModulesCache: number;

==== /a/node_modules/mangled__nodemodulescache/index.d.ts (0 errors) ====
export const mangledNodeModules: number;

==== /a/node_modules/@types/@scoped/attypescache/index.d.ts (0 errors) ====
export const atTypesCache: number;

==== /a/node_modules/@types/mangled__attypescache/index.d.ts (0 errors) ====
export const mangledAtTypesCache: number;


Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [tests/cases/compiler/moduleResolutionAsTypeReferenceDirectiveScoped.ts] ////

//// [index.d.ts]
export const dummy: number;

//// [index.d.ts]
export const typesCache: number;

//// [index.d.ts]
export const mangledTypes: number;

//// [index.d.ts]
export const nodeModulesCache: number;

//// [index.d.ts]
export const mangledNodeModules: number;

//// [index.d.ts]
export const atTypesCache: number;

//// [index.d.ts]
export const mangledAtTypesCache: number;


//// [a.ts]
import { typesCache } from "@scoped/typescache";
import { mangledTypes } from "@mangled/typescache";
import { nodeModulesCache } from "@scoped/nodemodulescache";
import { mangledNodeModules } from "@mangled/nodemodulescache";
import { atTypesCache } from "@scoped/attypescache";
import { mangledAtTypesCache } from "@mangled/attypescache";


//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== /a.ts ===
import { typesCache } from "@scoped/typescache";
>typesCache : Symbol(typesCache, Decl(a.ts, 0, 8))

import { mangledTypes } from "@mangled/typescache";
>mangledTypes : Symbol(mangledTypes, Decl(a.ts, 1, 8))

import { nodeModulesCache } from "@scoped/nodemodulescache";
>nodeModulesCache : Symbol(nodeModulesCache, Decl(a.ts, 2, 8))

import { mangledNodeModules } from "@mangled/nodemodulescache";
>mangledNodeModules : Symbol(mangledNodeModules, Decl(a.ts, 3, 8))

import { atTypesCache } from "@scoped/attypescache";
>atTypesCache : Symbol(atTypesCache, Decl(a.ts, 4, 8))

import { mangledAtTypesCache } from "@mangled/attypescache";
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(a.ts, 5, 8))

=== /a/types/dummy/index.d.ts ===
export const dummy: number;
>dummy : Symbol(dummy, Decl(index.d.ts, 0, 12))

=== /a/types/@scoped/typescache/index.d.ts ===
export const typesCache: number;
>typesCache : Symbol(typesCache, Decl(index.d.ts, 0, 12))

=== /a/node_modules/@scoped/nodemodulescache/index.d.ts ===
export const nodeModulesCache: number;
>nodeModulesCache : Symbol(nodeModulesCache, Decl(index.d.ts, 0, 12))

=== /a/node_modules/@types/mangled__attypescache/index.d.ts ===
export const mangledAtTypesCache: number;
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 12))


Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[
"======== Resolving module '@scoped/typescache' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'Node10'.",
"Loading module '@scoped/typescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'scoped__typescache'",
"File '/a/types/@scoped/typescache.d.ts' does not exist.",
"File '/a/types/@scoped/typescache/package.json' does not exist.",
"File '/a/types/@scoped/typescache/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/a/types/@scoped/typescache/index.d.ts', result '/a/types/@scoped/typescache/index.d.ts'.",
"======== Module name '@scoped/typescache' was successfully resolved to '/a/types/@scoped/typescache/index.d.ts'. ========",
"======== Resolving module '@mangled/typescache' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'Node10'.",
"Loading module '@mangled/typescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'mangled__typescache'",
"Scoped package detected, looking in 'mangled__typescache'",
"File '/a/node_modules/@types/mangled__typescache.d.ts' does not exist.",
"Loading module '@mangled/typescache' from 'node_modules' folder, target file types: JavaScript.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@mangled/typescache' was not resolved. ========",
"======== Resolving module '@scoped/nodemodulescache' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'Node10'.",
"Loading module '@scoped/nodemodulescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'scoped__nodemodulescache'",
"File '/a/types/@scoped/nodemodulescache.d.ts' does not exist.",
"File '/a/node_modules/@scoped/nodemodulescache.d.ts' does not exist.",
"File '/a/node_modules/@scoped/nodemodulescache/package.json' does not exist.",
"File '/a/node_modules/@scoped/nodemodulescache/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/a/node_modules/@scoped/nodemodulescache/index.d.ts', result '/a/node_modules/@scoped/nodemodulescache/index.d.ts'.",
"======== Module name '@scoped/nodemodulescache' was successfully resolved to '/a/node_modules/@scoped/nodemodulescache/index.d.ts'. ========",
"======== Resolving module '@mangled/nodemodulescache' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'Node10'.",
"Loading module '@mangled/nodemodulescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'mangled__nodemodulescache'",
"Scoped package detected, looking in 'mangled__nodemodulescache'",
"File '/a/node_modules/@types/mangled__nodemodulescache.d.ts' does not exist.",
"Loading module '@mangled/nodemodulescache' from 'node_modules' folder, target file types: JavaScript.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@mangled/nodemodulescache' was not resolved. ========",
"======== Resolving module '@scoped/attypescache' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'Node10'.",
"Loading module '@scoped/attypescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'scoped__attypescache'",
"File '/a/types/@scoped/attypescache.d.ts' does not exist.",
"File '/a/node_modules/@scoped/attypescache.d.ts' does not exist.",
"Scoped package detected, looking in 'scoped__attypescache'",
"File '/a/node_modules/@types/scoped__attypescache.d.ts' does not exist.",
"Loading module '@scoped/attypescache' from 'node_modules' folder, target file types: JavaScript.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name '@scoped/attypescache' was not resolved. ========",
"======== Resolving module '@mangled/attypescache' from '/a.ts'. ========",
"Module resolution kind is not specified, using 'Node10'.",
"Loading module '@mangled/attypescache' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Scoped package detected, looking in 'mangled__attypescache'",
"Scoped package detected, looking in 'mangled__attypescache'",
"File '/a/node_modules/@types/mangled__attypescache.d.ts' does not exist.",
"File '/a/node_modules/@types/mangled__attypescache/package.json' does not exist.",
"File '/a/node_modules/@types/mangled__attypescache/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/a/node_modules/@types/mangled__attypescache/index.d.ts', result '/a/node_modules/@types/mangled__attypescache/index.d.ts'.",
"======== Module name '@mangled/attypescache' was successfully resolved to '/a/node_modules/@types/mangled__attypescache/index.d.ts'. ========",
"======== Resolving type reference directive 'dummy', containing file '/__inferred type names__.ts', root directory '/a/types,/a/node_modules,/a/node_modules/@types'. ========",
"Resolving with primary search path '/a/types, /a/node_modules, /a/node_modules/@types'.",
"File '/a/types/dummy.d.ts' does not exist.",
"File '/a/types/dummy/package.json' does not exist.",
"File '/a/types/dummy/index.d.ts' exists - use it as a name resolution result.",
"Resolving real path for '/a/types/dummy/index.d.ts', result '/a/types/dummy/index.d.ts'.",
"======== Type reference directive 'dummy' was successfully resolved to '/a/types/dummy/index.d.ts', primary: true. ========"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== /a.ts ===
import { typesCache } from "@scoped/typescache";
>typesCache : number

import { mangledTypes } from "@mangled/typescache";
>mangledTypes : any

import { nodeModulesCache } from "@scoped/nodemodulescache";
>nodeModulesCache : number

import { mangledNodeModules } from "@mangled/nodemodulescache";
>mangledNodeModules : any

import { atTypesCache } from "@scoped/attypescache";
>atTypesCache : any

import { mangledAtTypesCache } from "@mangled/attypescache";
>mangledAtTypesCache : number

=== /a/types/dummy/index.d.ts ===
export const dummy: number;
>dummy : number

=== /a/types/@scoped/typescache/index.d.ts ===
export const typesCache: number;
>typesCache : number

=== /a/node_modules/@scoped/nodemodulescache/index.d.ts ===
export const nodeModulesCache: number;
>nodeModulesCache : number

=== /a/node_modules/@types/mangled__attypescache/index.d.ts ===
export const mangledAtTypesCache: number;
>mangledAtTypesCache : number


Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
error TS2688: Cannot find type definition file for '@mangled/nodemodulescache'.
The file is in the program because:
Entry point of type library '@mangled/nodemodulescache' specified in compilerOptions
error TS2688: Cannot find type definition file for '@mangled/typescache'.
The file is in the program because:
Entry point of type library '@mangled/typescache' specified in compilerOptions
error TS2688: Cannot find type definition file for '@scoped/attypescache'.
The file is in the program because:
Entry point of type library '@scoped/attypescache' specified in compilerOptions
/a.ts(4,1): error TS2304: Cannot find name 'mangledNodeModules'.
/a.ts(5,1): error TS2552: Cannot find name 'atTypesCache'. Did you mean 'typesCache'?


!!! error TS2688: Cannot find type definition file for '@mangled/nodemodulescache'.
!!! error TS2688: The file is in the program because:
!!! error TS2688: Entry point of type library '@mangled/nodemodulescache' specified in compilerOptions
!!! error TS2688: Cannot find type definition file for '@mangled/typescache'.
!!! error TS2688: The file is in the program because:
!!! error TS2688: Entry point of type library '@mangled/typescache' specified in compilerOptions
!!! error TS2688: Cannot find type definition file for '@scoped/attypescache'.
!!! error TS2688: The file is in the program because:
!!! error TS2688: Entry point of type library '@scoped/attypescache' specified in compilerOptions
==== /a.ts (2 errors) ====
typesCache;
mangledAtTypesCache;
nodeModulesCache;
mangledNodeModules;
~~~~~~~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'mangledNodeModules'.
atTypesCache;
~~~~~~~~~~~~
!!! error TS2552: Cannot find name 'atTypesCache'. Did you mean 'typesCache'?
!!! related TS2728 /types/@scoped/typescache/index.d.ts:1:15: 'typesCache' is declared here.
mangledAtTypesCache;
==== /types/@scoped/typescache/index.d.ts (0 errors) ====
declare const typesCache: number;

==== /types/mangled__typescache/index.d.ts (0 errors) ====
declare const mangledTypes: number;

==== /node_modules/@scoped/nodemodulescache/index.d.ts (0 errors) ====
declare const nodeModulesCache: number;

==== /node_modules/mangled__nodemodulescache/index.d.ts (0 errors) ====
declare const mangledNodeModules: number;

==== /node_modules/@types/@scoped/attypescache/index.d.ts (0 errors) ====
declare const atTypesCache: number;

==== /node_modules/@types/mangled__attypescache/index.d.ts (0 errors) ====
declare const mangledAtTypesCache: number;

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/typeReferenceDirectiveScopedPackageCustomTypeRoot.ts] ////

//// [index.d.ts]
declare const typesCache: number;

//// [index.d.ts]
declare const mangledTypes: number;

//// [index.d.ts]
declare const nodeModulesCache: number;

//// [index.d.ts]
declare const mangledNodeModules: number;

//// [index.d.ts]
declare const atTypesCache: number;

//// [index.d.ts]
declare const mangledAtTypesCache: number;

//// [a.ts]
typesCache;
mangledAtTypesCache;
nodeModulesCache;
mangledNodeModules;
atTypesCache;
mangledAtTypesCache;

//// [a.js]
typesCache;
mangledAtTypesCache;
nodeModulesCache;
mangledNodeModules;
atTypesCache;
mangledAtTypesCache;
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== /a.ts ===
typesCache;
>typesCache : Symbol(typesCache, Decl(index.d.ts, 0, 13))

mangledAtTypesCache;
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 13))

nodeModulesCache;
>nodeModulesCache : Symbol(nodeModulesCache, Decl(index.d.ts, 0, 13))

mangledNodeModules;
atTypesCache;
mangledAtTypesCache;
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 13))

=== /types/@scoped/typescache/index.d.ts ===
declare const typesCache: number;
>typesCache : Symbol(typesCache, Decl(index.d.ts, 0, 13))

=== /node_modules/@scoped/nodemodulescache/index.d.ts ===
declare const nodeModulesCache: number;
>nodeModulesCache : Symbol(nodeModulesCache, Decl(index.d.ts, 0, 13))

=== /node_modules/@types/mangled__attypescache/index.d.ts ===
declare const mangledAtTypesCache: number;
>mangledAtTypesCache : Symbol(mangledAtTypesCache, Decl(index.d.ts, 0, 13))

Loading