Skip to content

Refactor module resolution Extensions, fix lookup priorities #51471

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
4 changes: 2 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4547,7 +4547,7 @@
"category": "Message",
"code": 6094
},
"Loading module as file / folder, candidate module location '{0}', target file type '{1}'.": {
"Loading module as file / folder, candidate module location '{0}', target file types: {1}.": {
"category": "Message",
"code": 6095
},
Expand All @@ -4559,7 +4559,7 @@
"category": "Message",
"code": 6097
},
"Loading module '{0}' from 'node_modules' folder, target file type '{1}'.": {
"Loading module '{0}' from 'node_modules' folder, target file types: {1}.": {
"category": "Message",
"code": 6098
},
Expand Down
323 changes: 171 additions & 152 deletions src/compiler/moduleNameResolver.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7776,6 +7776,8 @@ const allSupportedExtensions: readonly Extension[][] = [[Extension.Ts, Extension
const allSupportedExtensionsWithJson: readonly Extension[][] = [...allSupportedExtensions, [Extension.Json]];
/** @internal */
export const supportedDeclarationExtensions: readonly Extension[] = [Extension.Dts, Extension.Dcts, Extension.Dmts];
/** @internal */
export const supportedTSImplementationExtensions: readonly Extension[] = [Extension.Ts, Extension.Cts, Extension.Mts, Extension.Tsx];

/** @internal */
export function getSupportedExtensions(options?: CompilerOptions): readonly Extension[][];
Expand Down
6 changes: 3 additions & 3 deletions src/testRunner/unittests/reuseProgramStructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ describe("unittests:: Reuse program structure:: General", () => {
[
"======== Resolving module 'a' from 'file1.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'a' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"File 'node_modules/a/package.json' does not exist.",
"File 'node_modules/a.ts' does not exist.",
"File 'node_modules/a.tsx' does not exist.",
Expand All @@ -285,7 +285,7 @@ describe("unittests:: Reuse program structure:: General", () => {
"File 'node_modules/@types/a/package.json' does not exist.",
"File 'node_modules/@types/a.d.ts' does not exist.",
"File 'node_modules/@types/a/index.d.ts' does not exist.",
"Loading module 'a' from 'node_modules' folder, target file type 'JavaScript'.",
"Loading module 'a' from 'node_modules' folder, target file types: JavaScript.",
"File 'node_modules/a/package.json' does not exist according to earlier cached lookups.",
"File 'node_modules/a.js' does not exist.",
"File 'node_modules/a.jsx' does not exist.",
Expand All @@ -307,7 +307,7 @@ describe("unittests:: Reuse program structure:: General", () => {
[
"======== Resolving module 'a' from 'file1.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'a' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"File 'node_modules/a/package.json' does not exist.",
"File 'node_modules/a.ts' does not exist.",
"File 'node_modules/a.tsx' does not exist.",
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/cachedModuleResolution1.trace.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
Expand All @@ -12,7 +12,7 @@
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========",
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Resolution for module 'foo' was found in cache from location '/a/b/c'.",
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========"
]
4 changes: 2 additions & 2 deletions tests/baselines/reference/cachedModuleResolution2.trace.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
"File '/a/b/node_modules/foo.ts' does not exist.",
"File '/a/b/node_modules/foo.tsx' does not exist.",
Expand All @@ -10,7 +10,7 @@
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========",
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
"Resolution for module 'foo' was found in cache from location '/a/b/c'.",
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/cachedModuleResolution5.trace.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
Expand All @@ -12,7 +12,7 @@
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========",
"======== Resolving module 'foo' from '/a/b/lib.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Resolution for module 'foo' was found in cache from location '/a/b'.",
"======== Module name 'foo' was successfully resolved to '/a/b/node_modules/foo.d.ts'. ========"
]
6 changes: 3 additions & 3 deletions tests/baselines/reference/cachedModuleResolution6.trace.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Loading module 'foo' from 'node_modules' folder, target file type 'JavaScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: JavaScript.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
Expand All @@ -18,7 +18,7 @@
"======== Module name 'foo' was not resolved. ========",
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Resolution for module 'foo' was found in cache from location '/a/b/c'.",
"======== Module name 'foo' was not resolved. ========"
]
6 changes: 3 additions & 3 deletions tests/baselines/reference/cachedModuleResolution7.trace.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"Loading module 'foo' from 'node_modules' folder, target file type 'JavaScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: JavaScript.",
"Directory '/a/b/c/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/node_modules' does not exist, skipping all lookups in it.",
"Directory '/node_modules' does not exist, skipping all lookups in it.",
"======== Module name 'foo' was not resolved. ========",
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
"Explicitly specified module resolution kind: 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Directory '/a/b/c/d/e/node_modules' does not exist, skipping all lookups in it.",
"Directory '/a/b/c/d/node_modules' does not exist, skipping all lookups in it.",
"Resolution for module 'foo' was found in cache from location '/a/b/c'.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"======== Resolving module 'foo/use' from '/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo/use' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo/use' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/foo/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/foo/use.ts' does not exist.",
Expand All @@ -11,7 +11,7 @@
"======== Module name 'foo/use' was successfully resolved to '/node_modules/foo/use.d.ts' with Package ID 'foo/use.d.ts@1.2.3'. ========",
"======== Resolving module 'a' from '/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'a' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"File '/node_modules/a/package.json' does not exist.",
"File '/node_modules/a.ts' does not exist.",
"File '/node_modules/a.tsx' does not exist.",
Expand All @@ -23,15 +23,15 @@
"======== Module name 'a' was successfully resolved to '/node_modules/a/index.d.ts'. ========",
"======== Resolving module './index' from '/node_modules/foo/use.d.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module as file / folder, candidate module location '/node_modules/foo/index', target file type 'TypeScript'.",
"Loading module as file / folder, candidate module location '/node_modules/foo/index', target file types: TypeScript, Declaration.",
"File '/node_modules/foo/index.ts' does not exist.",
"File '/node_modules/foo/index.tsx' does not exist.",
"File '/node_modules/foo/index.d.ts' exist - use it as a name resolution result.",
"File '/node_modules/foo/package.json' exists according to earlier cached lookups.",
"======== Module name './index' was successfully resolved to '/node_modules/foo/index.d.ts' with Package ID 'foo/index.d.ts@1.2.3'. ========",
"======== Resolving module 'foo' from '/node_modules/a/index.d.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/a/node_modules/foo/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/a/node_modules/foo.ts' does not exist.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
"======== Resolving module '@foo/bar/use' from '/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module '@foo/bar/use' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module '@foo/bar/use' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/@foo/bar/use.ts' does not exist.",
Expand All @@ -11,7 +11,7 @@
"======== Module name '@foo/bar/use' was successfully resolved to '/node_modules/@foo/bar/use.d.ts' with Package ID '@foo/bar/use.d.ts@1.2.3'. ========",
"======== Resolving module 'a' from '/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module 'a' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"File '/node_modules/a/package.json' does not exist.",
"File '/node_modules/a.ts' does not exist.",
"File '/node_modules/a.tsx' does not exist.",
Expand All @@ -23,15 +23,15 @@
"======== Module name 'a' was successfully resolved to '/node_modules/a/index.d.ts'. ========",
"======== Resolving module './index' from '/node_modules/@foo/bar/use.d.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module as file / folder, candidate module location '/node_modules/@foo/bar/index', target file type 'TypeScript'.",
"Loading module as file / folder, candidate module location '/node_modules/@foo/bar/index', target file types: TypeScript, Declaration.",
"File '/node_modules/@foo/bar/index.ts' does not exist.",
"File '/node_modules/@foo/bar/index.tsx' does not exist.",
"File '/node_modules/@foo/bar/index.d.ts' exist - use it as a name resolution result.",
"File '/node_modules/@foo/bar/package.json' exists according to earlier cached lookups.",
"======== Module name './index' was successfully resolved to '/node_modules/@foo/bar/index.d.ts' with Package ID '@foo/bar/index.d.ts@1.2.3'. ========",
"======== Resolving module '@foo/bar' from '/node_modules/a/index.d.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module '@foo/bar' from 'node_modules' folder, target file type 'TypeScript'.",
"Loading module '@foo/bar' from 'node_modules' folder, target file types: TypeScript, Declaration.",
"Found 'package.json' at '/node_modules/a/node_modules/@foo/bar/package.json'.",
"'package.json' does not have a 'typesVersions' field.",
"File '/node_modules/a/node_modules/@foo/bar.ts' does not exist.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /project/a.js ===

export default "a.js";

=== /project/a.js.js ===

export default "a.js.js";

=== /project/dir/index.ts ===

export default "dir/index.ts";

=== /project/dir.js ===

export default "dir.js";

=== /project/b.ts ===
import a from "./a.js";
>a : Symbol(a, Decl(b.ts, 0, 6))

import dir from "./dir";
>dir : Symbol(dir, Decl(b.ts, 1, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /project/a.js ===

export default "a.js";

=== /project/a.js.js ===

export default "a.js.js";

=== /project/dir/index.ts ===

export default "dir/index.ts";

=== /project/dir.js ===

export default "dir.js";

=== /project/b.ts ===
import a from "./a.js";
>a : "a.js"

import dir from "./dir";
>dir : "dir.js"

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /project/a.js ===

export default "a.js";

=== /project/a.js.js ===

export default "a.js.js";

=== /project/dir/index.ts ===

export default "dir/index.ts";

=== /project/dir.js ===

export default "dir.js";

=== /project/b.ts ===
import a from "./a.js";
>a : Symbol(a, Decl(b.ts, 0, 6))

import dir from "./dir";
>dir : Symbol(dir, Decl(b.ts, 1, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /project/a.js ===

export default "a.js";

=== /project/a.js.js ===

export default "a.js.js";

=== /project/dir/index.ts ===

export default "dir/index.ts";

=== /project/dir.js ===

export default "dir.js";

=== /project/b.ts ===
import a from "./a.js";
>a : "a.js"

import dir from "./dir";
>dir : "dir/index.ts"

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /project/a.js ===

export default "a.js";

=== /project/a.js.js ===

export default "a.js.js";

=== /project/dir/index.ts ===

export default "dir/index.ts";

=== /project/dir.js ===

export default "dir.js";

=== /project/b.ts ===
import a from "./a.js";
>a : Symbol(a, Decl(b.ts, 0, 6))

import dir from "./dir";
>dir : Symbol(dir, Decl(b.ts, 1, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== /project/a.js ===

export default "a.js";

=== /project/a.js.js ===

export default "a.js.js";

=== /project/dir/index.ts ===

export default "dir/index.ts";

=== /project/dir.js ===

export default "dir.js";

=== /project/b.ts ===
import a from "./a.js";
>a : "a.js"

import dir from "./dir";
>dir : "dir.js"

Loading