Skip to content

Commit 1c2318d

Browse files
committed
Resolve typeReferenceDirective as fileOrDirectory instead of just directory when its not default typeRoots
Fixes #37708 Bug 2 part of the issue
1 parent 7f58b18 commit 1c2318d

31 files changed

+69
-29
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,18 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
549549
}
550550
return firstDefined(typeRoots, typeRoot => {
551551
const candidate = combinePaths(typeRoot, typeReferenceDirectiveName);
552-
const candidateDirectory = getDirectoryPath(candidate);
553-
const directoryExists = directoryProbablyExists(candidateDirectory, host);
552+
const directoryExists = directoryProbablyExists(typeRoot, host);
554553
if (!directoryExists && traceEnabled) {
555-
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, candidateDirectory);
554+
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, typeRoot);
555+
}
556+
if (options.typeRoots) {
557+
// Custom typeRoots resolve as file or directory just like we do modules
558+
const resolvedFromFile = loadModuleFromFile(Extensions.Declaration, candidate, !directoryExists, moduleResolutionState);
559+
if (resolvedFromFile) {
560+
const packageDirectory = parseNodeModuleFromPath(resolvedFromFile.path);
561+
const packageInfo = packageDirectory ? getPackageJsonInfo(packageDirectory, /*onlyRecordFailures*/ false, moduleResolutionState) : undefined;
562+
return resolvedTypeScriptOnly(withPackageId(packageInfo, resolvedFromFile));
563+
}
556564
}
557565
return resolvedTypeScriptOnly(
558566
loadNodeModuleFromDirectory(Extensions.Declaration, candidate,

tests/baselines/reference/library-reference-1.trace.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory 'types'. ========",
33
"Resolving with primary search path 'types'.",
4+
"File 'types/jquery.d.ts' does not exist.",
45
"File 'types/jquery/package.json' does not exist.",
56
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",
67
"Resolving real path for 'types/jquery/index.d.ts', result '/src/types/jquery/index.d.ts'.",

tests/baselines/reference/library-reference-10.trace.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/foo/consumer.ts', root directory './types'. ========",
33
"Resolving with primary search path './types'.",
4+
"File './types/jquery.d.ts' does not exist.",
45
"Found 'package.json' at './types/jquery/package.json'.",
56
"'package.json' does not have a 'typesVersions' field.",
67
"'package.json' has 'typings' field 'jquery.d.ts' that references 'types/jquery/jquery.d.ts'.",

tests/baselines/reference/library-reference-13.trace.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
33
"Resolving with primary search path '/a/types'.",
4+
"File '/a/types/jquery.d.ts' does not exist.",
45
"File '/a/types/jquery/package.json' does not exist.",
56
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",
67
"Resolving real path for '/a/types/jquery/index.d.ts', result '/a/types/jquery/index.d.ts'.",

tests/baselines/reference/library-reference-14.trace.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
33
"Resolving with primary search path '/a/types'.",
4+
"File '/a/types/jquery.d.ts' does not exist.",
45
"File '/a/types/jquery/package.json' does not exist.",
56
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",
67
"Resolving real path for '/a/types/jquery/index.d.ts', result '/a/types/jquery/index.d.ts'.",

tests/baselines/reference/library-reference-15.trace.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory 'types'. ========",
33
"Resolving with primary search path 'types'.",
4+
"File 'types/jquery.d.ts' does not exist.",
45
"File 'types/jquery/package.json' does not exist.",
56
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",
67
"Resolving real path for 'types/jquery/index.d.ts', result '/a/types/jquery/index.d.ts'.",

tests/baselines/reference/library-reference-2.trace.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/consumer.ts', root directory '/types'. ========",
33
"Resolving with primary search path '/types'.",
4+
"File '/types/jquery.d.ts' does not exist.",
45
"Found 'package.json' at '/types/jquery/package.json'.",
56
"'package.json' does not have a 'typesVersions' field.",
67
"'package.json' does not have a 'typings' field.",
@@ -10,6 +11,7 @@
1011
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
1112
"======== Resolving type reference directive 'jquery', containing file '/test/__inferred type names__.ts', root directory '/types'. ========",
1213
"Resolving with primary search path '/types'.",
14+
"File '/types/jquery.d.ts' does not exist.",
1315
"File '/types/jquery/package.json' exists according to earlier cached lookups.",
1416
"'package.json' does not have a 'typings' field.",
1517
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",

tests/baselines/reference/library-reference-4.trace.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[
22
"======== Resolving type reference directive 'foo', containing file '/src/root.ts', root directory '/src'. ========",
33
"Resolving with primary search path '/src'.",
4+
"File '/src/foo.d.ts' does not exist.",
45
"Looking up in 'node_modules' folder, initial location '/src'.",
56
"Directory '/src/node_modules' does not exist, skipping all lookups in it.",
67
"File '/node_modules/foo/package.json' does not exist.",
@@ -10,6 +11,7 @@
1011
"======== Type reference directive 'foo' was successfully resolved to '/node_modules/foo/index.d.ts', primary: false. ========",
1112
"======== Resolving type reference directive 'bar', containing file '/src/root.ts', root directory '/src'. ========",
1213
"Resolving with primary search path '/src'.",
14+
"File '/src/bar.d.ts' does not exist.",
1315
"Looking up in 'node_modules' folder, initial location '/src'.",
1416
"Directory '/src/node_modules' does not exist, skipping all lookups in it.",
1517
"File '/node_modules/bar/package.json' does not exist.",
@@ -19,6 +21,7 @@
1921
"======== Type reference directive 'bar' was successfully resolved to '/node_modules/bar/index.d.ts', primary: false. ========",
2022
"======== Resolving type reference directive 'alpha', containing file '/node_modules/foo/index.d.ts', root directory '/src'. ========",
2123
"Resolving with primary search path '/src'.",
24+
"File '/src/alpha.d.ts' does not exist.",
2225
"Looking up in 'node_modules' folder, initial location '/node_modules/foo'.",
2326
"File '/node_modules/foo/node_modules/alpha/package.json' does not exist.",
2427
"File '/node_modules/foo/node_modules/alpha.d.ts' does not exist.",
@@ -27,6 +30,7 @@
2730
"======== Type reference directive 'alpha' was successfully resolved to '/node_modules/foo/node_modules/alpha/index.d.ts', primary: false. ========",
2831
"======== Resolving type reference directive 'alpha', containing file '/node_modules/bar/index.d.ts', root directory '/src'. ========",
2932
"Resolving with primary search path '/src'.",
33+
"File '/src/alpha.d.ts' does not exist.",
3034
"Looking up in 'node_modules' folder, initial location '/node_modules/bar'.",
3135
"File '/node_modules/bar/node_modules/alpha/package.json' does not exist.",
3236
"File '/node_modules/bar/node_modules/alpha.d.ts' does not exist.",

tests/baselines/reference/library-reference-8.trace.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
[
22
"======== Resolving type reference directive 'alpha', containing file '/test/foo.ts', root directory '/test/types'. ========",
33
"Resolving with primary search path '/test/types'.",
4+
"File '/test/types/alpha.d.ts' does not exist.",
45
"File '/test/types/alpha/package.json' does not exist.",
56
"File '/test/types/alpha/index.d.ts' exist - use it as a name resolution result.",
67
"Resolving real path for '/test/types/alpha/index.d.ts', result '/test/types/alpha/index.d.ts'.",
78
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========",
89
"======== Resolving type reference directive 'beta', containing file '/test/foo.ts', root directory '/test/types'. ========",
910
"Resolving with primary search path '/test/types'.",
11+
"File '/test/types/beta.d.ts' does not exist.",
1012
"File '/test/types/beta/package.json' does not exist.",
1113
"File '/test/types/beta/index.d.ts' exist - use it as a name resolution result.",
1214
"Resolving real path for '/test/types/beta/index.d.ts', result '/test/types/beta/index.d.ts'.",
1315
"======== Type reference directive 'beta' was successfully resolved to '/test/types/beta/index.d.ts', primary: true. ========",
1416
"======== Resolving type reference directive 'beta', containing file '/test/types/alpha/index.d.ts', root directory '/test/types'. ========",
1517
"Resolving with primary search path '/test/types'.",
18+
"File '/test/types/beta.d.ts' does not exist.",
1619
"File '/test/types/beta/package.json' does not exist according to earlier cached lookups.",
1720
"File '/test/types/beta/index.d.ts' exist - use it as a name resolution result.",
1821
"Resolving real path for '/test/types/beta/index.d.ts', result '/test/types/beta/index.d.ts'.",
1922
"======== Type reference directive 'beta' was successfully resolved to '/test/types/beta/index.d.ts', primary: true. ========",
2023
"======== Resolving type reference directive 'alpha', containing file '/test/types/beta/index.d.ts', root directory '/test/types'. ========",
2124
"Resolving with primary search path '/test/types'.",
25+
"File '/test/types/alpha.d.ts' does not exist.",
2226
"File '/test/types/alpha/package.json' does not exist according to earlier cached lookups.",
2327
"File '/test/types/alpha/index.d.ts' exist - use it as a name resolution result.",
2428
"Resolving real path for '/test/types/alpha/index.d.ts', result '/test/types/alpha/index.d.ts'.",

tests/baselines/reference/library-reference-scoped-packages.trace.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
"======== Resolving type reference directive '@beep/boop', containing file '/a.ts', root directory 'types'. ========",
33
"Resolving with primary search path 'types'.",
4-
"Directory 'types/@beep' does not exist, skipping all lookups in it.",
4+
"Directory 'types' does not exist, skipping all lookups in it.",
55
"Looking up in 'node_modules' folder, initial location '/'.",
66
"Scoped package detected, looking in 'beep__boop'",
77
"File '/node_modules/@types/beep__boop/package.json' does not exist.",

0 commit comments

Comments
 (0)