Skip to content

Commit b3eca68

Browse files
authored
Suppress resolvedUsingTsExtension during loadModuleFromDirectory (#52189)
1 parent 3da4886 commit b3eca68

14 files changed

+243
-1
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ export interface ModuleResolutionState {
259259
requestContainingDirectory: string | undefined;
260260
reportDiagnostic: DiagnosticReporter;
261261
isConfigLookup: boolean;
262+
candidateIsFromPackageJsonField: boolean;
262263
}
263264

264265
/** Just the fields that we use for module resolution.
@@ -526,6 +527,7 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
526527
requestContainingDirectory: containingDirectory,
527528
reportDiagnostic: diag => void diagnostics.push(diag),
528529
isConfigLookup: false,
530+
candidateIsFromPackageJsonField: false,
529531
};
530532
let resolved = primaryLookup();
531533
let primary = true;
@@ -1652,6 +1654,7 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
16521654
requestContainingDirectory: containingDirectory,
16531655
reportDiagnostic: diag => void diagnostics.push(diag),
16541656
isConfigLookup,
1657+
candidateIsFromPackageJsonField: false,
16551658
};
16561659

16571660
if (traceEnabled && getEmitModuleResolutionKind(compilerOptions) >= ModuleResolutionKind.Node16 && getEmitModuleResolutionKind(compilerOptions) <= ModuleResolutionKind.NodeNext) {
@@ -1948,7 +1951,7 @@ function tryAddingExtensions(candidate: string, extensions: Extensions, original
19481951

19491952
function tryExtension(ext: string, resolvedUsingTsExtension?: boolean): PathAndExtension | undefined {
19501953
const path = tryFile(candidate + ext, onlyRecordFailures, state);
1951-
return path === undefined ? undefined : { path, ext, resolvedUsingTsExtension };
1954+
return path === undefined ? undefined : { path, ext, resolvedUsingTsExtension: !state.candidateIsFromPackageJsonField && resolvedUsingTsExtension };
19521955
}
19531956
}
19541957

@@ -2111,6 +2114,7 @@ export function getTemporaryModuleResolutionState(packageJsonInfoCache: PackageJ
21112114
requestContainingDirectory: undefined,
21122115
reportDiagnostic: noop,
21132116
isConfigLookup: false,
2117+
candidateIsFromPackageJsonField: false,
21142118
};
21152119
}
21162120

@@ -2232,11 +2236,14 @@ function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: st
22322236
// (technically it only emits a deprecation warning in esm packages right now, but that's probably
22332237
// enough to mean we don't need to support it)
22342238
const features = state.features;
2239+
const candidateIsFromPackageJsonField = state.candidateIsFromPackageJsonField;
2240+
state.candidateIsFromPackageJsonField = true;
22352241
if (jsonContent?.type !== "module") {
22362242
state.features &= ~NodeResolutionFeatures.EsmMode;
22372243
}
22382244
const result = nodeLoadModuleByRelativeName(expandedExtensions, candidate, onlyRecordFailures, state, /*considerPackageJson*/ false);
22392245
state.features = features;
2246+
state.candidateIsFromPackageJsonField = candidateIsFromPackageJsonField;
22402247
return result;
22412248
};
22422249

@@ -2946,6 +2953,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
29462953
requestContainingDirectory: containingDirectory,
29472954
reportDiagnostic: diag => void diagnostics.push(diag),
29482955
isConfigLookup: false,
2956+
candidateIsFromPackageJsonField: false,
29492957
};
29502958

29512959
const resolved =
@@ -3026,6 +3034,7 @@ export function loadModuleFromGlobalCache(moduleName: string, projectName: strin
30263034
requestContainingDirectory: undefined,
30273035
reportDiagnostic: diag => void diagnostics.push(diag),
30283036
isConfigLookup: false,
3037+
candidateIsFromPackageJsonField: false,
30293038
};
30303039
const resolved = loadModuleFromImmediateNodeModulesDirectory(Extensions.Declaration, moduleName, globalCache, state, /*typesScopeOnly*/ false, /*cache*/ undefined, /*redirectedReference*/ undefined);
30313040
return createResolvedModuleWithFailedLookupLocations(
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/resolutionCandidateFromPackageJsonField1.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "@angular/core",
6+
"typings": "index.d.ts"
7+
}
8+
9+
//// [index.ts]
10+
export {};
11+
12+
//// [test.ts]
13+
import "@angular/core";
14+
15+
16+
//// [index.js]
17+
"use strict";
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
//// [test.js]
20+
"use strict";
21+
Object.defineProperty(exports, "__esModule", { value: true });
22+
require("@angular/core");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/@angular/core/index.ts ===
2+
3+
export {};
4+
5+
=== tests/cases/compiler/@angular/core/testing/test.ts ===
6+
7+
import "@angular/core";
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/@angular/core/index.ts ===
2+
3+
export {};
4+
5+
=== tests/cases/compiler/@angular/core/testing/test.ts ===
6+
7+
import "@angular/core";
8+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests/cases/compiler/test.ts(1,19): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
2+
3+
4+
==== tests/cases/compiler/tsconfig.json (0 errors) ====
5+
{
6+
"compilerOptions": {
7+
"paths": {
8+
"foo/*": ["./dist/*"],
9+
"baz/*.ts": ["./types/*.d.ts"]
10+
}
11+
}
12+
}
13+
14+
==== tests/cases/compiler/dist/bar.ts (0 errors) ====
15+
export const a = 1234;
16+
17+
==== tests/cases/compiler/types/main.d.ts (0 errors) ====
18+
export const b: string;
19+
20+
==== tests/cases/compiler/test.ts (1 errors) ====
21+
import { a } from "foo/bar.ts";
22+
~~~~~~~~~~~~
23+
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
24+
import { b } from "baz/main.ts";
25+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/resolutionCandidateFromPackageJsonField2.ts] ////
2+
3+
//// [bar.ts]
4+
export const a = 1234;
5+
6+
//// [main.d.ts]
7+
export const b: string;
8+
9+
//// [test.ts]
10+
import { a } from "foo/bar.ts";
11+
import { b } from "baz/main.ts";
12+
13+
14+
//// [bar.js]
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
exports.a = void 0;
18+
exports.a = 1234;
19+
//// [test.js]
20+
"use strict";
21+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/dist/bar.ts ===
2+
export const a = 1234;
3+
>a : Symbol(a, Decl(bar.ts, 0, 12))
4+
5+
=== tests/cases/compiler/types/main.d.ts ===
6+
export const b: string;
7+
>b : Symbol(b, Decl(main.d.ts, 0, 12))
8+
9+
=== tests/cases/compiler/test.ts ===
10+
import { a } from "foo/bar.ts";
11+
>a : Symbol(a, Decl(test.ts, 0, 8))
12+
13+
import { b } from "baz/main.ts";
14+
>b : Symbol(b, Decl(test.ts, 1, 8))
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/dist/bar.ts ===
2+
export const a = 1234;
3+
>a : 1234
4+
>1234 : 1234
5+
6+
=== tests/cases/compiler/types/main.d.ts ===
7+
export const b: string;
8+
>b : string
9+
10+
=== tests/cases/compiler/test.ts ===
11+
import { a } from "foo/bar.ts";
12+
>a : 1234
13+
14+
import { b } from "baz/main.ts";
15+
>b : string
16+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
tests/cases/compiler/test.ts(1,19): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
2+
3+
4+
==== tests/cases/compiler/tsconfig.json (0 errors) ====
5+
{
6+
"compilerOptions": {
7+
"paths": {
8+
"foo/*": ["./dist/*"],
9+
"baz/*.ts": ["./types/*.d.ts"]
10+
}
11+
}
12+
}
13+
14+
==== tests/cases/compiler/dist/bar.ts (0 errors) ====
15+
export const a = 1234;
16+
17+
==== tests/cases/compiler/types/main.d.ts (0 errors) ====
18+
export const b: string;
19+
20+
==== tests/cases/compiler/test.ts (1 errors) ====
21+
import { a } from "foo/bar.ts";
22+
~~~~~~~~~~~~
23+
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
24+
import { b } from "baz/main.ts";
25+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/resolutionCandidateFromPackageJsonField2.ts] ////
2+
3+
//// [bar.ts]
4+
export const a = 1234;
5+
6+
//// [main.d.ts]
7+
export const b: string;
8+
9+
//// [test.ts]
10+
import { a } from "foo/bar.ts";
11+
import { b } from "baz/main.ts";
12+
13+
14+
//// [bar.js]
15+
"use strict";
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
exports.a = void 0;
18+
exports.a = 1234;
19+
//// [test.js]
20+
"use strict";
21+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/dist/bar.ts ===
2+
export const a = 1234;
3+
>a : Symbol(a, Decl(bar.ts, 0, 12))
4+
5+
=== tests/cases/compiler/types/main.d.ts ===
6+
export const b: string;
7+
>b : Symbol(b, Decl(main.d.ts, 0, 12))
8+
9+
=== tests/cases/compiler/test.ts ===
10+
import { a } from "foo/bar.ts";
11+
>a : Symbol(a, Decl(test.ts, 0, 8))
12+
13+
import { b } from "baz/main.ts";
14+
>b : Symbol(b, Decl(test.ts, 1, 8))
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/dist/bar.ts ===
2+
export const a = 1234;
3+
>a : 1234
4+
>1234 : 1234
5+
6+
=== tests/cases/compiler/types/main.d.ts ===
7+
export const b: string;
8+
>b : string
9+
10+
=== tests/cases/compiler/test.ts ===
11+
import { a } from "foo/bar.ts";
12+
>a : 1234
13+
14+
import { b } from "baz/main.ts";
15+
>b : string
16+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// @filename: tsconfig.json
2+
{
3+
"compilerOptions": {
4+
"paths": {
5+
"@angular/*": ["./@angular/*"]
6+
}
7+
}
8+
}
9+
10+
// @filename: @angular/core/package.json
11+
{
12+
"name": "@angular/core",
13+
"typings": "index.d.ts"
14+
}
15+
16+
// @filename: @angular/core/index.ts
17+
export {};
18+
19+
// @filename: @angular/core/testing/test.ts
20+
import "@angular/core";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @moduleResolution: node10,bundler
2+
3+
// @filename: tsconfig.json
4+
{
5+
"compilerOptions": {
6+
"paths": {
7+
"foo/*": ["./dist/*"],
8+
"baz/*.ts": ["./types/*.d.ts"]
9+
}
10+
}
11+
}
12+
13+
// @filename: dist/bar.ts
14+
export const a = 1234;
15+
16+
// @filename: types/main.d.ts
17+
export const b: string;
18+
19+
// @filename: test.ts
20+
import { a } from "foo/bar.ts";
21+
import { b } from "baz/main.ts";

0 commit comments

Comments
 (0)