Skip to content

Commit cfe6b69

Browse files
committed
Try file if matched pattern specifies the extension instead of all the time
Fixes #39743
1 parent 634688f commit cfe6b69

14 files changed

+172
-15
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ namespace ts {
13771377
trace(state.host, Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path);
13781378
}
13791379
// A path mapping may have an extension, in contrast to an import, which should omit it.
1380-
const extension = tryGetExtensionFromPath(candidate);
1380+
const extension = tryGetExtensionFromPath(subst);
13811381
if (extension !== undefined) {
13821382
const path = tryFile(candidate, onlyRecordFailures, state);
13831383
if (path !== undefined) {

tests/baselines/reference/moduleResolutionWithExtensions_withPaths.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error TS6504: File '/node_modules/foo/lib/test.js' is a JavaScript file. Did you
44
error TS6504: File '/relative.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
55
The file is in the program because:
66
Matched by include pattern '**/*' in '/tsconfig.json'
7-
/test.ts(1,22): error TS7016: Could not find a declaration file for module 'foo/test.js'. '/node_modules/foo/lib/test.js' implicitly has an 'any' type.
87

98

109
!!! error TS6504: File '/node_modules/foo/lib/test.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
@@ -46,10 +45,8 @@ error TS6504: File '/relative.js' is a JavaScript file. Did you mean to enable t
4645
export declare function relative(): void;
4746

4847

49-
==== /test.ts (1 errors) ====
48+
==== /test.ts (0 errors) ====
5049
import { test } from "foo/test.js";
51-
~~~~~~~~~~~~~
52-
!!! error TS7016: Could not find a declaration file for module 'foo/test.js'. '/node_modules/foo/lib/test.js' implicitly has an 'any' type.
5350
import { test as test2 } from "foo/test";
5451
import { relative } from "./relative.js";
5552
import { relative as relative2 } from "./relative";

tests/baselines/reference/moduleResolutionWithExtensions_withPaths.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { test } from "foo/test.js";
1212
>test : Symbol(test, Decl(test.ts, 0, 8))
1313

1414
import { test as test2 } from "foo/test";
15-
>test : Symbol(test2, Decl(test.d.ts, 0, 0))
15+
>test : Symbol(test, Decl(test.d.ts, 0, 0))
1616
>test2 : Symbol(test2, Decl(test.ts, 1, 8))
1717

1818
import { relative } from "./relative.js";

tests/baselines/reference/moduleResolutionWithExtensions_withPaths.trace.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55
"'paths' option is specified, looking for a pattern to match module name 'foo/test.js'.",
66
"Module name 'foo/test.js', matched pattern 'foo/*'.",
77
"Trying substitution 'node_modules/foo/lib/*', candidate module location: 'node_modules/foo/lib/test.js'.",
8-
"File '/node_modules/foo/lib/test.js' exist - use it as a name resolution result.",
9-
"======== Module name 'foo/test.js' was successfully resolved to '/node_modules/foo/lib/test.js'. ========",
8+
"Loading module as file / folder, candidate module location '/node_modules/foo/lib/test.js', target file type 'TypeScript'.",
9+
"File '/node_modules/foo/lib/test.js.ts' does not exist.",
10+
"File '/node_modules/foo/lib/test.js.tsx' does not exist.",
11+
"File '/node_modules/foo/lib/test.js.d.ts' does not exist.",
12+
"File name '/node_modules/foo/lib/test.js' has a '.js' extension - stripping it.",
13+
"File '/node_modules/foo/lib/test.ts' does not exist.",
14+
"File '/node_modules/foo/lib/test.tsx' does not exist.",
15+
"File '/node_modules/foo/lib/test.d.ts' exist - use it as a name resolution result.",
16+
"File '/node_modules/foo/package.json' does not exist.",
17+
"======== Module name 'foo/test.js' was successfully resolved to '/node_modules/foo/lib/test.d.ts'. ========",
1018
"======== Resolving module 'foo/test' from '/test.ts'. ========",
1119
"Explicitly specified module resolution kind: 'NodeJs'.",
1220
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/test'.",

tests/baselines/reference/moduleResolutionWithExtensions_withPaths.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export declare function relative(): void;
99

1010
=== /test.ts ===
1111
import { test } from "foo/test.js";
12-
>test : any
12+
>test : () => void
1313

1414
import { test as test2 } from "foo/test";
1515
>test : () => void

tests/baselines/reference/pathMappingBasedModuleResolution_withExtensionInName.trace.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"'paths' option is specified, looking for a pattern to match module name 'zone.js'.",
66
"Module name 'zone.js', matched pattern '*'.",
77
"Trying substitution 'foo/*', candidate module location: 'foo/zone.js'.",
8-
"File '/foo/zone.js' does not exist.",
98
"Loading module as file / folder, candidate module location '/foo/zone.js', target file type 'TypeScript'.",
109
"File '/foo/zone.js.ts' does not exist.",
1110
"File '/foo/zone.js.tsx' does not exist.",
@@ -25,7 +24,6 @@
2524
"'paths' option is specified, looking for a pattern to match module name 'zone.tsx'.",
2625
"Module name 'zone.tsx', matched pattern '*'.",
2726
"Trying substitution 'foo/*', candidate module location: 'foo/zone.tsx'.",
28-
"File '/foo/zone.tsx' does not exist.",
2927
"Loading module as file / folder, candidate module location '/foo/zone.tsx', target file type 'TypeScript'.",
3028
"File '/foo/zone.tsx.ts' does not exist.",
3129
"File '/foo/zone.tsx.tsx' does not exist.",

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_MapedToNodeModules.trace.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,37 @@
55
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.js'.",
66
"Module name 'foo/bar/foobar.js', matched pattern '*'.",
77
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.js'.",
8+
"Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.js', target file type 'TypeScript'.",
9+
"File '/node_modules/foo/bar/foobar.js.ts' does not exist.",
10+
"File '/node_modules/foo/bar/foobar.js.tsx' does not exist.",
11+
"File '/node_modules/foo/bar/foobar.js.d.ts' does not exist.",
12+
"File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.",
13+
"File '/node_modules/foo/bar/foobar.ts' does not exist.",
14+
"File '/node_modules/foo/bar/foobar.tsx' does not exist.",
15+
"File '/node_modules/foo/bar/foobar.d.ts' does not exist.",
16+
"Directory '/node_modules/foo/bar/foobar.js' does not exist, skipping all lookups in it.",
17+
"Trying substitution 'src/types', candidate module location: 'src/types'.",
18+
"Loading module as file / folder, candidate module location '/src/types', target file type 'TypeScript'.",
19+
"Loading module 'foo/bar/foobar.js' from 'node_modules' folder, target file type 'TypeScript'.",
20+
"File '/node_modules/foo/package.json' does not exist.",
21+
"File '/node_modules/foo/bar/foobar.js.ts' does not exist.",
22+
"File '/node_modules/foo/bar/foobar.js.tsx' does not exist.",
23+
"File '/node_modules/foo/bar/foobar.js.d.ts' does not exist.",
24+
"File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.",
25+
"File '/node_modules/foo/bar/foobar.ts' does not exist.",
26+
"File '/node_modules/foo/bar/foobar.tsx' does not exist.",
27+
"File '/node_modules/foo/bar/foobar.d.ts' does not exist.",
28+
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
29+
"File name '/node_modules/@types/foo/bar/foobar.js' has a '.js' extension - stripping it.",
30+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.js'.",
31+
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.js'.",
32+
"Module name 'foo/bar/foobar.js', matched pattern '*'.",
33+
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.js'.",
34+
"Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.js', target file type 'JavaScript'.",
35+
"File '/node_modules/foo/bar/foobar.js.js' does not exist.",
36+
"File '/node_modules/foo/bar/foobar.js.jsx' does not exist.",
37+
"File name '/node_modules/foo/bar/foobar.js' has a '.js' extension - stripping it.",
838
"File '/node_modules/foo/bar/foobar.js' exist - use it as a name resolution result.",
39+
"File '/node_modules/foo/package.json' does not exist.",
940
"======== Module name 'foo/bar/foobar.js' was successfully resolved to '/node_modules/foo/bar/foobar.js'. ========"
1041
]

tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/a.ts(1,20): error TS7042: Module 'foo/bar/foobar.json' was resolved to '/node_modules/foo/bar/foobar.json', but '--resolveJsonModule' is not used.
1+
/a.ts(1,20): error TS2732: Cannot find module 'foo/bar/foobar.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
22

33

44
==== /tsconfig.json (0 errors) ====
@@ -16,7 +16,7 @@
1616
==== /a.ts (1 errors) ====
1717
import foobar from "foo/bar/foobar.json";
1818
~~~~~~~~~~~~~~~~~~~~~
19-
!!! error TS7042: Module 'foo/bar/foobar.json' was resolved to '/node_modules/foo/bar/foobar.json', but '--resolveJsonModule' is not used.
19+
!!! error TS2732: Cannot find module 'foo/bar/foobar.json'. Consider using '--resolveJsonModule' to import module with '.json' extension.
2020

2121
==== /node_modules/foo/bar/foobar.json (0 errors) ====
2222
{ "a": 10 }

tests/baselines/reference/requireOfJsonFileWithoutResolveJsonModuleAndPathMapping.trace.json

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.json'.",
66
"Module name 'foo/bar/foobar.json', matched pattern '*'.",
77
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.json'.",
8-
"File '/node_modules/foo/bar/foobar.json' exist - use it as a name resolution result.",
9-
"======== Module name 'foo/bar/foobar.json' was successfully resolved to '/node_modules/foo/bar/foobar.json'. ========"
8+
"Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'TypeScript'.",
9+
"File '/node_modules/foo/bar/foobar.json.ts' does not exist.",
10+
"File '/node_modules/foo/bar/foobar.json.tsx' does not exist.",
11+
"File '/node_modules/foo/bar/foobar.json.d.ts' does not exist.",
12+
"Directory '/node_modules/foo/bar/foobar.json' does not exist, skipping all lookups in it.",
13+
"Trying substitution 'src/types', candidate module location: 'src/types'.",
14+
"Loading module as file / folder, candidate module location '/src/types', target file type 'TypeScript'.",
15+
"Loading module 'foo/bar/foobar.json' from 'node_modules' folder, target file type 'TypeScript'.",
16+
"File '/node_modules/foo/package.json' does not exist.",
17+
"File '/node_modules/foo/bar/foobar.json.ts' does not exist.",
18+
"File '/node_modules/foo/bar/foobar.json.tsx' does not exist.",
19+
"File '/node_modules/foo/bar/foobar.json.d.ts' does not exist.",
20+
"Directory '/node_modules/@types' does not exist, skipping all lookups in it.",
21+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo/bar/foobar.json'.",
22+
"'paths' option is specified, looking for a pattern to match module name 'foo/bar/foobar.json'.",
23+
"Module name 'foo/bar/foobar.json', matched pattern '*'.",
24+
"Trying substitution 'node_modules/*', candidate module location: 'node_modules/foo/bar/foobar.json'.",
25+
"Loading module as file / folder, candidate module location '/node_modules/foo/bar/foobar.json', target file type 'JavaScript'.",
26+
"File '/node_modules/foo/bar/foobar.json.js' does not exist.",
27+
"File '/node_modules/foo/bar/foobar.json.jsx' does not exist.",
28+
"Directory '/node_modules/foo/bar/foobar.json' does not exist, skipping all lookups in it.",
29+
"Trying substitution 'src/types', candidate module location: 'src/types'.",
30+
"Loading module as file / folder, candidate module location '/src/types', target file type 'JavaScript'.",
31+
"Loading module 'foo/bar/foobar.json' from 'node_modules' folder, target file type 'JavaScript'.",
32+
"File '/node_modules/foo/package.json' does not exist.",
33+
"File '/node_modules/foo/bar/foobar.json.js' does not exist.",
34+
"File '/node_modules/foo/bar/foobar.json.jsx' does not exist.",
35+
"======== Module name 'foo/bar/foobar.json' was not resolved. ========"
1036
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [tests/cases/compiler/requireOfJsonFile_PathMapping.ts] ////
2+
3+
//// [foobar.json]
4+
{ "a": 10 }
5+
6+
//// [a.ts]
7+
import foobar from "foo/bar/foobar.json";
8+
9+
10+
//// [/bin/a.js]
11+
"use strict";
12+
exports.__esModule = true;

0 commit comments

Comments
 (0)