Skip to content

Suppress resolvedUsingTsExtension during loadModuleFromDirectory #52189

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
merged 5 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export interface ModuleResolutionState {
requestContainingDirectory: string | undefined;
reportDiagnostic: DiagnosticReporter;
isConfigLookup: boolean;
candidateIsFromPackageJsonField: boolean;
}

/** Just the fields that we use for module resolution.
Expand Down Expand Up @@ -526,6 +527,7 @@ export function resolveTypeReferenceDirective(typeReferenceDirectiveName: string
requestContainingDirectory: containingDirectory,
reportDiagnostic: diag => void diagnostics.push(diag),
isConfigLookup: false,
candidateIsFromPackageJsonField: false,
};
let resolved = primaryLookup();
let primary = true;
Expand Down Expand Up @@ -1652,6 +1654,7 @@ function nodeModuleNameResolverWorker(features: NodeResolutionFeatures, moduleNa
requestContainingDirectory: containingDirectory,
reportDiagnostic: diag => void diagnostics.push(diag),
isConfigLookup,
candidateIsFromPackageJsonField: false,
};

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

function tryExtension(ext: string, resolvedUsingTsExtension?: boolean): PathAndExtension | undefined {
const path = tryFile(candidate + ext, onlyRecordFailures, state);
return path === undefined ? undefined : { path, ext, resolvedUsingTsExtension };
return path === undefined ? undefined : { path, ext, resolvedUsingTsExtension: !state.candidateIsFromPackageJsonField && resolvedUsingTsExtension };
}
}

Expand Down Expand Up @@ -2111,6 +2114,7 @@ export function getTemporaryModuleResolutionState(packageJsonInfoCache: PackageJ
requestContainingDirectory: undefined,
reportDiagnostic: noop,
isConfigLookup: false,
candidateIsFromPackageJsonField: false,
};
}

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

Expand Down Expand Up @@ -2935,6 +2942,7 @@ export function classicNameResolver(moduleName: string, containingFile: string,
requestContainingDirectory: containingDirectory,
reportDiagnostic: diag => void diagnostics.push(diag),
isConfigLookup: false,
candidateIsFromPackageJsonField: false,
};

const resolved =
Expand Down Expand Up @@ -3015,6 +3023,7 @@ export function loadModuleFromGlobalCache(moduleName: string, projectName: strin
requestContainingDirectory: undefined,
reportDiagnostic: diag => void diagnostics.push(diag),
isConfigLookup: false,
candidateIsFromPackageJsonField: false,
};
const resolved = loadModuleFromImmediateNodeModulesDirectory(Extensions.Declaration, moduleName, globalCache, state, /*typesScopeOnly*/ false, /*cache*/ undefined, /*redirectedReference*/ undefined);
return createResolvedModuleWithFailedLookupLocations(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/resolutionCandidateFromPackageJsonField1.ts] ////

//// [package.json]
{
"name": "@angular/core",
"typings": "index.d.ts"
}

//// [index.ts]
export {};

//// [test.ts]
import "@angular/core";


//// [index.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//// [test.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("@angular/core");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/@angular/core/index.ts ===

export {};

=== tests/cases/compiler/@angular/core/testing/test.ts ===

import "@angular/core";

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/@angular/core/index.ts ===

export {};

=== tests/cases/compiler/@angular/core/testing/test.ts ===

import "@angular/core";

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/compiler/test.ts(1,19): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.


==== tests/cases/compiler/tsconfig.json (0 errors) ====
{
"compilerOptions": {
"paths": {
"foo/*": ["./dist/*"],
"baz/*.ts": ["./types/*.d.ts"]
}
}
}

==== tests/cases/compiler/dist/bar.ts (0 errors) ====
export const a = 1234;

==== tests/cases/compiler/types/main.d.ts (0 errors) ====
export const b: string;

==== tests/cases/compiler/test.ts (1 errors) ====
import { a } from "foo/bar.ts";
~~~~~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
import { b } from "baz/main.ts";

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/resolutionCandidateFromPackageJsonField2.ts] ////

//// [bar.ts]
export const a = 1234;

//// [main.d.ts]
export const b: string;

//// [test.ts]
import { a } from "foo/bar.ts";
import { b } from "baz/main.ts";


//// [bar.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
exports.a = 1234;
//// [test.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/compiler/dist/bar.ts ===
export const a = 1234;
>a : Symbol(a, Decl(bar.ts, 0, 12))

=== tests/cases/compiler/types/main.d.ts ===
export const b: string;
>b : Symbol(b, Decl(main.d.ts, 0, 12))

=== tests/cases/compiler/test.ts ===
import { a } from "foo/bar.ts";
>a : Symbol(a, Decl(test.ts, 0, 8))

import { b } from "baz/main.ts";
>b : Symbol(b, Decl(test.ts, 1, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/dist/bar.ts ===
export const a = 1234;
>a : 1234
>1234 : 1234

=== tests/cases/compiler/types/main.d.ts ===
export const b: string;
>b : string

=== tests/cases/compiler/test.ts ===
import { a } from "foo/bar.ts";
>a : 1234

import { b } from "baz/main.ts";
>b : string

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/compiler/test.ts(1,19): error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.


==== tests/cases/compiler/tsconfig.json (0 errors) ====
{
"compilerOptions": {
"paths": {
"foo/*": ["./dist/*"],
"baz/*.ts": ["./types/*.d.ts"]
}
}
}

==== tests/cases/compiler/dist/bar.ts (0 errors) ====
export const a = 1234;

==== tests/cases/compiler/types/main.d.ts (0 errors) ====
export const b: string;

==== tests/cases/compiler/test.ts (1 errors) ====
import { a } from "foo/bar.ts";
~~~~~~~~~~~~
!!! error TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
import { b } from "baz/main.ts";

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/resolutionCandidateFromPackageJsonField2.ts] ////

//// [bar.ts]
export const a = 1234;

//// [main.d.ts]
export const b: string;

//// [test.ts]
import { a } from "foo/bar.ts";
import { b } from "baz/main.ts";


//// [bar.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.a = void 0;
exports.a = 1234;
//// [test.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
=== tests/cases/compiler/dist/bar.ts ===
export const a = 1234;
>a : Symbol(a, Decl(bar.ts, 0, 12))

=== tests/cases/compiler/types/main.d.ts ===
export const b: string;
>b : Symbol(b, Decl(main.d.ts, 0, 12))

=== tests/cases/compiler/test.ts ===
import { a } from "foo/bar.ts";
>a : Symbol(a, Decl(test.ts, 0, 8))

import { b } from "baz/main.ts";
>b : Symbol(b, Decl(test.ts, 1, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/compiler/dist/bar.ts ===
export const a = 1234;
>a : 1234
>1234 : 1234

=== tests/cases/compiler/types/main.d.ts ===
export const b: string;
>b : string

=== tests/cases/compiler/test.ts ===
import { a } from "foo/bar.ts";
>a : 1234

import { b } from "baz/main.ts";
>b : string

20 changes: 20 additions & 0 deletions tests/cases/compiler/resolutionCandidateFromPackageJsonField1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @filename: tsconfig.json
{
"compilerOptions": {
"paths": {
"@angular/*": ["./@angular/*"]
}
}
}

// @filename: @angular/core/package.json
{
"name": "@angular/core",
"typings": "index.d.ts"
}

// @filename: @angular/core/index.ts
export {};

// @filename: @angular/core/testing/test.ts
import "@angular/core";
21 changes: 21 additions & 0 deletions tests/cases/compiler/resolutionCandidateFromPackageJsonField2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @moduleResolution: node10,bundler

// @filename: tsconfig.json
{
"compilerOptions": {
"paths": {
"foo/*": ["./dist/*"],
"baz/*.ts": ["./types/*.d.ts"]
}
}
}

// @filename: dist/bar.ts
export const a = 1234;

// @filename: types/main.d.ts
export const b: string;

// @filename: test.ts
import { a } from "foo/bar.ts";
import { b } from "baz/main.ts";