Skip to content

Commit 7adfa8d

Browse files
author
Andy
authored
Merge pull request #16023 from Microsoft/mrjs
In path mapping module resolution, try loading from path as directory even if it has an extension
2 parents 8b15e2b + 3690926 commit 7adfa8d

7 files changed

+115
-4
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -653,11 +653,13 @@ namespace ts {
653653
if (state.traceEnabled) {
654654
trace(state.host, Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path);
655655
}
656-
// A path mapping may have a ".ts" extension; in contrast to an import, which should omit it.
657-
const tsExtension = tryGetExtensionFromPath(candidate);
658-
if (tsExtension !== undefined) {
656+
// A path mapping may have an extension, in contrast to an import, which should omit it.
657+
const extension = tryGetExtensionFromPath(candidate);
658+
if (extension !== undefined) {
659659
const path = tryFile(candidate, failedLookupLocations, /*onlyRecordFailures*/ false, state);
660-
return path && { path, extension: tsExtension };
660+
if (path !== undefined) {
661+
return { path, extension };
662+
}
661663
}
662664

663665
return loader(extensions, candidate, failedLookupLocations, !directoryProbablyExists(getDirectoryPath(candidate), state.host), state);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/pathMappingBasedModuleResolution_withExtensionInName.ts] ////
2+
3+
//// [index.d.ts]
4+
export const x: number;
5+
6+
//// [index.d.ts]
7+
export const y: number;
8+
9+
//// [a.ts]
10+
import { x } from "zone.js";
11+
import { y } from "zone.tsx";
12+
13+
14+
//// [a.js]
15+
"use strict";
16+
exports.__esModule = true;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== /a.ts ===
2+
import { x } from "zone.js";
3+
>x : Symbol(x, Decl(a.ts, 0, 8))
4+
5+
import { y } from "zone.tsx";
6+
>y : Symbol(y, Decl(a.ts, 1, 8))
7+
8+
=== /foo/zone.js/index.d.ts ===
9+
export const x: number;
10+
>x : Symbol(x, Decl(index.d.ts, 0, 12))
11+
12+
=== /foo/zone.tsx/index.d.ts ===
13+
export const y: number;
14+
>y : Symbol(y, Decl(index.d.ts, 0, 12))
15+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
"======== Resolving module 'zone.js' from '/a.ts'. ========",
3+
"Module resolution kind is not specified, using 'NodeJs'.",
4+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'zone.js'.",
5+
"'paths' option is specified, looking for a pattern to match module name 'zone.js'.",
6+
"Module name 'zone.js', matched pattern '*'.",
7+
"Trying substitution 'foo/*', candidate module location: 'foo/zone.js'.",
8+
"File '/foo/zone.js' does not exist.",
9+
"Loading module as file / folder, candidate module location '/foo/zone.js', target file type 'TypeScript'.",
10+
"File '/foo/zone.js.ts' does not exist.",
11+
"File '/foo/zone.js.tsx' does not exist.",
12+
"File '/foo/zone.js.d.ts' does not exist.",
13+
"File name '/foo/zone.js' has a '.js' extension - stripping it.",
14+
"File '/foo/zone.ts' does not exist.",
15+
"File '/foo/zone.tsx' does not exist.",
16+
"File '/foo/zone.d.ts' does not exist.",
17+
"File '/foo/zone.js/package.json' does not exist.",
18+
"File '/foo/zone.js/index.ts' does not exist.",
19+
"File '/foo/zone.js/index.tsx' does not exist.",
20+
"File '/foo/zone.js/index.d.ts' exist - use it as a name resolution result.",
21+
"======== Module name 'zone.js' was successfully resolved to '/foo/zone.js/index.d.ts'. ========",
22+
"======== Resolving module 'zone.tsx' from '/a.ts'. ========",
23+
"Module resolution kind is not specified, using 'NodeJs'.",
24+
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'zone.tsx'.",
25+
"'paths' option is specified, looking for a pattern to match module name 'zone.tsx'.",
26+
"Module name 'zone.tsx', matched pattern '*'.",
27+
"Trying substitution 'foo/*', candidate module location: 'foo/zone.tsx'.",
28+
"File '/foo/zone.tsx' does not exist.",
29+
"Loading module as file / folder, candidate module location '/foo/zone.tsx', target file type 'TypeScript'.",
30+
"File '/foo/zone.tsx.ts' does not exist.",
31+
"File '/foo/zone.tsx.tsx' does not exist.",
32+
"File '/foo/zone.tsx.d.ts' does not exist.",
33+
"File '/foo/zone.tsx/package.json' does not exist.",
34+
"File '/foo/zone.tsx/index.ts' does not exist.",
35+
"File '/foo/zone.tsx/index.tsx' does not exist.",
36+
"File '/foo/zone.tsx/index.d.ts' exist - use it as a name resolution result.",
37+
"======== Module name 'zone.tsx' was successfully resolved to '/foo/zone.tsx/index.d.ts'. ========"
38+
]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== /a.ts ===
2+
import { x } from "zone.js";
3+
>x : number
4+
5+
import { y } from "zone.tsx";
6+
>y : number
7+
8+
=== /foo/zone.js/index.d.ts ===
9+
export const x: number;
10+
>x : number
11+
12+
=== /foo/zone.tsx/index.d.ts ===
13+
export const y: number;
14+
>y : number
15+

tests/baselines/reference/pathMappingBasedModuleResolution_withExtension_failedLookup.trace.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
"Module name 'foo', matched pattern 'foo'.",
77
"Trying substitution 'foo/foo.ts', candidate module location: 'foo/foo.ts'.",
88
"File '/foo/foo.ts' does not exist.",
9+
"Loading module as file / folder, candidate module location '/foo/foo.ts', target file type 'TypeScript'.",
910
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
1011
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1112
"'baseUrl' option is set to '/', using this value to resolve non-relative module name 'foo'.",
1213
"'paths' option is specified, looking for a pattern to match module name 'foo'.",
1314
"Module name 'foo', matched pattern 'foo'.",
1415
"Trying substitution 'foo/foo.ts', candidate module location: 'foo/foo.ts'.",
1516
"File '/foo/foo.ts' does not exist.",
17+
"Loading module as file / folder, candidate module location '/foo/foo.ts', target file type 'JavaScript'.",
1618
"Loading module 'foo' from 'node_modules' folder, target file type 'JavaScript'.",
1719
"Directory '/node_modules' does not exist, skipping all lookups in it.",
1820
"======== Module name 'foo' was not resolved. ========"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// @traceResolution: true
2+
// @module: commonjs
3+
// @noImplicitReferences: true
4+
5+
// @filename: /tsconfig.json
6+
{
7+
"compilerOptions": {
8+
"baseUrl": ".",
9+
"paths": {
10+
"*": ["foo/*"]
11+
}
12+
}
13+
}
14+
15+
// @filename: /foo/zone.js/index.d.ts
16+
export const x: number;
17+
18+
// @filename: /foo/zone.tsx/index.d.ts
19+
export const y: number;
20+
21+
// @filename: /a.ts
22+
import { x } from "zone.js";
23+
import { y } from "zone.tsx";

0 commit comments

Comments
 (0)