Skip to content

Commit

Permalink
Resolve typeReferenceDirective as fileOrDirectory instead of just dir…
Browse files Browse the repository at this point in the history
…ectory when its not default typeRoots

Fixes #37708
Bug 2 part of the issue
  • Loading branch information
sheetalkamat committed Dec 1, 2022
1 parent 7f58b18 commit 1c2318d
Show file tree
Hide file tree
Showing 31 changed files with 69 additions and 29 deletions.
14 changes: 11 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,18 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
}
return firstDefined(typeRoots, typeRoot => {
const candidate = combinePaths(typeRoot, typeReferenceDirectiveName);
const candidateDirectory = getDirectoryPath(candidate);
const directoryExists = directoryProbablyExists(candidateDirectory, host);
const directoryExists = directoryProbablyExists(typeRoot, host);
if (!directoryExists && traceEnabled) {
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, candidateDirectory);
trace(host, Diagnostics.Directory_0_does_not_exist_skipping_all_lookups_in_it, typeRoot);
}
if (options.typeRoots) {
// Custom typeRoots resolve as file or directory just like we do modules
const resolvedFromFile = loadModuleFromFile(Extensions.Declaration, candidate, !directoryExists, moduleResolutionState);
if (resolvedFromFile) {
const packageDirectory = parseNodeModuleFromPath(resolvedFromFile.path);
const packageInfo = packageDirectory ? getPackageJsonInfo(packageDirectory, /*onlyRecordFailures*/ false, moduleResolutionState) : undefined;
return resolvedTypeScriptOnly(withPackageId(packageInfo, resolvedFromFile));
}
}
return resolvedTypeScriptOnly(
loadNodeModuleFromDirectory(Extensions.Declaration, candidate,
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/library-reference-1.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'jquery', containing file '/src/consumer.ts', root directory 'types'. ========",
"Resolving with primary search path 'types'.",
"File 'types/jquery.d.ts' does not exist.",
"File 'types/jquery/package.json' does not exist.",
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for 'types/jquery/index.d.ts', result '/src/types/jquery/index.d.ts'.",
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/library-reference-10.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'jquery', containing file '/foo/consumer.ts', root directory './types'. ========",
"Resolving with primary search path './types'.",
"File './types/jquery.d.ts' does not exist.",
"Found 'package.json' at './types/jquery/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' has 'typings' field 'jquery.d.ts' that references 'types/jquery/jquery.d.ts'.",
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/library-reference-13.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
"Resolving with primary search path '/a/types'.",
"File '/a/types/jquery.d.ts' does not exist.",
"File '/a/types/jquery/package.json' does not exist.",
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/a/types/jquery/index.d.ts', result '/a/types/jquery/index.d.ts'.",
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/library-reference-14.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory '/a/types'. ========",
"Resolving with primary search path '/a/types'.",
"File '/a/types/jquery.d.ts' does not exist.",
"File '/a/types/jquery/package.json' does not exist.",
"File '/a/types/jquery/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/a/types/jquery/index.d.ts', result '/a/types/jquery/index.d.ts'.",
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/library-reference-15.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'jquery', containing file '/a/__inferred type names__.ts', root directory 'types'. ========",
"Resolving with primary search path 'types'.",
"File 'types/jquery.d.ts' does not exist.",
"File 'types/jquery/package.json' does not exist.",
"File 'types/jquery/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for 'types/jquery/index.d.ts', result '/a/types/jquery/index.d.ts'.",
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/library-reference-2.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'jquery', containing file '/consumer.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/jquery.d.ts' does not exist.",
"Found 'package.json' at '/types/jquery/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"'package.json' does not have a 'typings' field.",
Expand All @@ -10,6 +11,7 @@
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
"======== Resolving type reference directive 'jquery', containing file '/test/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/jquery.d.ts' does not exist.",
"File '/types/jquery/package.json' exists according to earlier cached lookups.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/library-reference-4.trace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'foo', containing file '/src/root.ts', root directory '/src'. ========",
"Resolving with primary search path '/src'.",
"File '/src/foo.d.ts' does not exist.",
"Looking up in 'node_modules' folder, initial location '/src'.",
"Directory '/src/node_modules' does not exist, skipping all lookups in it.",
"File '/node_modules/foo/package.json' does not exist.",
Expand All @@ -10,6 +11,7 @@
"======== Type reference directive 'foo' was successfully resolved to '/node_modules/foo/index.d.ts', primary: false. ========",
"======== Resolving type reference directive 'bar', containing file '/src/root.ts', root directory '/src'. ========",
"Resolving with primary search path '/src'.",
"File '/src/bar.d.ts' does not exist.",
"Looking up in 'node_modules' folder, initial location '/src'.",
"Directory '/src/node_modules' does not exist, skipping all lookups in it.",
"File '/node_modules/bar/package.json' does not exist.",
Expand All @@ -19,6 +21,7 @@
"======== Type reference directive 'bar' was successfully resolved to '/node_modules/bar/index.d.ts', primary: false. ========",
"======== Resolving type reference directive 'alpha', containing file '/node_modules/foo/index.d.ts', root directory '/src'. ========",
"Resolving with primary search path '/src'.",
"File '/src/alpha.d.ts' does not exist.",
"Looking up in 'node_modules' folder, initial location '/node_modules/foo'.",
"File '/node_modules/foo/node_modules/alpha/package.json' does not exist.",
"File '/node_modules/foo/node_modules/alpha.d.ts' does not exist.",
Expand All @@ -27,6 +30,7 @@
"======== Type reference directive 'alpha' was successfully resolved to '/node_modules/foo/node_modules/alpha/index.d.ts', primary: false. ========",
"======== Resolving type reference directive 'alpha', containing file '/node_modules/bar/index.d.ts', root directory '/src'. ========",
"Resolving with primary search path '/src'.",
"File '/src/alpha.d.ts' does not exist.",
"Looking up in 'node_modules' folder, initial location '/node_modules/bar'.",
"File '/node_modules/bar/node_modules/alpha/package.json' does not exist.",
"File '/node_modules/bar/node_modules/alpha.d.ts' does not exist.",
Expand Down
4 changes: 4 additions & 0 deletions tests/baselines/reference/library-reference-8.trace.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
[
"======== Resolving type reference directive 'alpha', containing file '/test/foo.ts', root directory '/test/types'. ========",
"Resolving with primary search path '/test/types'.",
"File '/test/types/alpha.d.ts' does not exist.",
"File '/test/types/alpha/package.json' does not exist.",
"File '/test/types/alpha/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/test/types/alpha/index.d.ts', result '/test/types/alpha/index.d.ts'.",
"======== Type reference directive 'alpha' was successfully resolved to '/test/types/alpha/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'beta', containing file '/test/foo.ts', root directory '/test/types'. ========",
"Resolving with primary search path '/test/types'.",
"File '/test/types/beta.d.ts' does not exist.",
"File '/test/types/beta/package.json' does not exist.",
"File '/test/types/beta/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/test/types/beta/index.d.ts', result '/test/types/beta/index.d.ts'.",
"======== Type reference directive 'beta' was successfully resolved to '/test/types/beta/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'beta', containing file '/test/types/alpha/index.d.ts', root directory '/test/types'. ========",
"Resolving with primary search path '/test/types'.",
"File '/test/types/beta.d.ts' does not exist.",
"File '/test/types/beta/package.json' does not exist according to earlier cached lookups.",
"File '/test/types/beta/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/test/types/beta/index.d.ts', result '/test/types/beta/index.d.ts'.",
"======== Type reference directive 'beta' was successfully resolved to '/test/types/beta/index.d.ts', primary: true. ========",
"======== Resolving type reference directive 'alpha', containing file '/test/types/beta/index.d.ts', root directory '/test/types'. ========",
"Resolving with primary search path '/test/types'.",
"File '/test/types/alpha.d.ts' does not exist.",
"File '/test/types/alpha/package.json' does not exist according to earlier cached lookups.",
"File '/test/types/alpha/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/test/types/alpha/index.d.ts', result '/test/types/alpha/index.d.ts'.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"======== Resolving type reference directive '@beep/boop', containing file '/a.ts', root directory 'types'. ========",
"Resolving with primary search path 'types'.",
"Directory 'types/@beep' does not exist, skipping all lookups in it.",
"Directory 'types' does not exist, skipping all lookups in it.",
"Looking up in 'node_modules' folder, initial location '/'.",
"Scoped package detected, looking in 'beep__boop'",
"File '/node_modules/@types/beep__boop/package.json' does not exist.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Resolution:: {
"isExternalLibraryImport": false
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json"
],
"affectingLocations": [],
Expand All @@ -34,7 +35,9 @@ Resolution:: {
"resolvedFileName": "/root/src/types/lib/typings/lib.d.ts",
"isExternalLibraryImport": false
},
"failedLookupLocations": [],
"failedLookupLocations": [
"/root/src/types/lib.d.ts"
],
"affectingLocations": [
"/root/src/types/lib/package.json"
],
Expand All @@ -55,6 +58,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand All @@ -81,6 +85,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib.d.ts"
Expand All @@ -105,6 +110,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down Expand Up @@ -134,6 +140,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down Expand Up @@ -40,6 +41,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down Expand Up @@ -72,6 +74,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down Expand Up @@ -102,6 +105,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down Expand Up @@ -137,6 +141,7 @@ Resolution:: {
"isExternalLibraryImport": true
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json",
"/root/src/types/lib/index.d.ts",
"/root/src/node_modules/lib/package.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Resolution:: {
"isExternalLibraryImport": false
},
"failedLookupLocations": [
"/root/src/types/lib.d.ts",
"/root/src/types/lib/package.json"
],
"affectingLocations": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Output::

======== Resolving type reference directive 'sometype', containing file '/src/packages/__inferred type names__.ts', root directory '/src/packages/typeroot1'. ========
Resolving with primary search path '/src/packages/typeroot1'.
File '/src/packages/typeroot1/sometype.d.ts' does not exist.
File '/src/packages/typeroot1/sometype/package.json' does not exist.
File '/src/packages/typeroot1/sometype/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/src/packages/typeroot1/sometype/index.d.ts', result '/src/packages/typeroot1/sometype/index.d.ts'.
Expand All @@ -56,6 +57,7 @@ Resolving real path for '/src/packages/typeroot1/sometype/index.d.ts', result '/

======== Resolving type reference directive 'sometype', containing file '/src/packages/__inferred type names__.ts', root directory '/src/packages/typeroot2'. ========
Resolving with primary search path '/src/packages/typeroot2'.
File '/src/packages/typeroot2/sometype.d.ts' does not exist.
File '/src/packages/typeroot2/sometype/package.json' does not exist.
File '/src/packages/typeroot2/sometype/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/src/packages/typeroot2/sometype/index.d.ts', result '/src/packages/typeroot2/sometype/index.d.ts'.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
=== /a.ts ===

a;
>a : Symbol(a, Decl(phaser.d.ts, 0, 13))

=== /node_modules/phaser/types/phaser.d.ts ===
declare const a: number;
>a : Symbol(a, Decl(phaser.d.ts, 0, 13))

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
[
"======== Resolving type reference directive 'phaser', containing file '/__inferred type names__.ts', root directory '/node_modules/phaser/types'. ========",
"Resolving with primary search path '/node_modules/phaser/types'.",
"Looking up in 'node_modules' folder, initial location '/'.",
"File '/node_modules/phaser/types/phaser.d.ts' exist - use it as a name resolution result.",
"File '/node_modules/phaser/package.json' does not exist.",
"File '/node_modules/phaser.d.ts' does not exist.",
"File '/node_modules/phaser/index.d.ts' does not exist.",
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
"======== Type reference directive 'phaser' was not resolved. ========"
"Resolving real path for '/node_modules/phaser/types/phaser.d.ts', result '/node_modules/phaser/types/phaser.d.ts'.",
"======== Type reference directive 'phaser' was successfully resolved to '/node_modules/phaser/types/phaser.d.ts', primary: true. ========"
]
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
=== /a.ts ===
a;
>a : any
>a : number

=== /node_modules/phaser/types/phaser.d.ts ===
declare const a: number;
>a : number

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'lib', containing file '/app.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/lib.d.ts' does not exist.",
"File '/types/lib/package.json' does not exist.",
"File '/types/lib/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/types/lib/index.d.ts', result '/types/lib/index.d.ts'.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'lib', containing file '/app.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/lib.d.ts' does not exist.",
"File '/types/lib/package.json' does not exist.",
"File '/types/lib/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/types/lib/index.d.ts', result '/types/lib/index.d.ts'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"======== Module name './mod1' was successfully resolved to '/mod1.ts'. ========",
"======== Resolving type reference directive 'lib', containing file '/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/lib.d.ts' does not exist.",
"File '/types/lib/package.json' does not exist.",
"File '/types/lib/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/types/lib/index.d.ts', result '/types/lib/index.d.ts'.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"======== Module name './mod1' was successfully resolved to '/mod1.ts'. ========",
"======== Resolving type reference directive 'lib', containing file '/mod1.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/lib.d.ts' does not exist.",
"File '/types/lib/package.json' does not exist.",
"File '/types/lib/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/types/lib/index.d.ts', result '/types/lib/index.d.ts'.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'lib', containing file '/app.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/lib.d.ts' does not exist.",
"File '/types/lib/package.json' does not exist.",
"File '/types/lib/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/types/lib/index.d.ts', result '/types/lib/index.d.ts'.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
"======== Resolving type reference directive 'lib', containing file '/__inferred type names__.ts', root directory '/types'. ========",
"Resolving with primary search path '/types'.",
"File '/types/lib.d.ts' does not exist.",
"File '/types/lib/package.json' does not exist.",
"File '/types/lib/index.d.ts' exist - use it as a name resolution result.",
"Resolving real path for '/types/lib/index.d.ts', result '/types/lib/index.d.ts'.",
Expand Down

0 comments on commit 1c2318d

Please sign in to comment.