Skip to content

ESM mode nonrelative imports should assume index.js entrypoints even … #47854

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 1 commit into from
Feb 11, 2022
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
20 changes: 18 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,13 @@ namespace ts {
onlyRecordFailures = true;
}
}
return loadNodeModuleFromDirectory(extensions, candidate, onlyRecordFailures, state, considerPackageJson);
// esm mode relative imports shouldn't do any directory lookups (either inside `package.json`
// files or implicit `index.js`es). This is a notable depature from cjs norms, where `./foo/pkg`
// could have been redirected by `./foo/pkg/package.json` to an arbitrary location!
if (!(state.features & NodeResolutionFeatures.EsmMode)) {
return loadNodeModuleFromDirectory(extensions, candidate, onlyRecordFailures, state, considerPackageJson);
}
return undefined;
}

/*@internal*/
Expand Down Expand Up @@ -2178,7 +2184,7 @@ namespace ts {
if (packageInfo && packageInfo.packageJsonContent.exports && state.features & NodeResolutionFeatures.Exports) {
return loadModuleFromExports(packageInfo, extensions, combinePaths(".", rest), state, cache, redirectedReference)?.value;
}
const pathAndExtension =
let pathAndExtension =
loadModuleFromFile(extensions, candidate, onlyRecordFailures, state) ||
loadNodeModuleFromDirectoryWorker(
extensions,
Expand All @@ -2188,6 +2194,16 @@ namespace ts {
packageInfo && packageInfo.packageJsonContent,
packageInfo && packageInfo.versionPaths
);
if (
!pathAndExtension && packageInfo
&& packageInfo.packageJsonContent.exports === undefined
&& packageInfo.packageJsonContent.main === undefined
&& state.features & NodeResolutionFeatures.EsmMode
) {
// EsmMode disables index lookup in `loadNodeModuleFromDirectoryWorker` generally, however non-relative package resolutions still assume
// a default `index.js` entrypoint if no `main` or `exports` are present
pathAndExtension = loadModuleFromFile(extensions, combinePaths(candidate, "index.js"), onlyRecordFailures, state);
}
return withPackageId(packageInfo, pathAndExtension);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests/cases/compiler/index.ts(2,31): error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node12' or 'nodenext'. Consider adding an extension to the import path.
tests/cases/compiler/index.ts(3,31): error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node12' or 'nodenext'. Consider adding an extension to the import path.


==== tests/cases/compiler/node_modules/pkg/package.json (0 errors) ====
{
"name": "pkg",
"version": "0.0.1"
}
==== tests/cases/compiler/node_modules/pkg/index.d.ts (0 errors) ====
export const item = 4;
==== tests/cases/compiler/pkg/package.json (0 errors) ====
{
"private": true
}
==== tests/cases/compiler/pkg/index.d.ts (0 errors) ====
export const item = 4;
==== tests/cases/compiler/package.json (0 errors) ====
{
"type": "module",
"private": true
}
==== tests/cases/compiler/index.ts (2 errors) ====
import { item } from "pkg"; // should work (`index.js` is assumed to be the entrypoint for packages found via nonrelative import)
import { item as item2 } from "./pkg"; // shouldn't work (`index.js` is _not_ assumed to be the entrypoint for packages found via relative import)
~~~~~~~
!!! error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node12' or 'nodenext'. Consider adding an extension to the import path.
import { item as item3 } from "./node_modules/pkg" // _even if they're in a node_modules folder_
~~~~~~~~~~~~~~~~~~~~
!!! error TS2834: Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node12' or 'nodenext'. Consider adding an extension to the import path.

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

//// [package.json]
{
"name": "pkg",
"version": "0.0.1"
}
//// [index.d.ts]
export const item = 4;
//// [package.json]
{
"private": true
}
//// [index.d.ts]
export const item = 4;
//// [package.json]
{
"type": "module",
"private": true
}
//// [index.ts]
import { item } from "pkg"; // should work (`index.js` is assumed to be the entrypoint for packages found via nonrelative import)
import { item as item2 } from "./pkg"; // shouldn't work (`index.js` is _not_ assumed to be the entrypoint for packages found via relative import)
import { item as item3 } from "./node_modules/pkg" // _even if they're in a node_modules folder_


//// [index.js]
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
export const item = 4;
>item : Symbol(item, Decl(index.d.ts, 0, 12))

=== tests/cases/compiler/pkg/index.d.ts ===
export const item = 4;
>item : Symbol(item, Decl(index.d.ts, 0, 12))

=== tests/cases/compiler/index.ts ===
import { item } from "pkg"; // should work (`index.js` is assumed to be the entrypoint for packages found via nonrelative import)
>item : Symbol(item, Decl(index.ts, 0, 8))

import { item as item2 } from "./pkg"; // shouldn't work (`index.js` is _not_ assumed to be the entrypoint for packages found via relative import)
>item2 : Symbol(item2, Decl(index.ts, 1, 8))

import { item as item3 } from "./node_modules/pkg" // _even if they're in a node_modules folder_
>item3 : Symbol(item3, Decl(index.ts, 2, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/node_modules/pkg/index.d.ts ===
export const item = 4;
>item : 4
>4 : 4

=== tests/cases/compiler/pkg/index.d.ts ===
export const item = 4;
>item : 4
>4 : 4

=== tests/cases/compiler/index.ts ===
import { item } from "pkg"; // should work (`index.js` is assumed to be the entrypoint for packages found via nonrelative import)
>item : 4

import { item as item2 } from "./pkg"; // shouldn't work (`index.js` is _not_ assumed to be the entrypoint for packages found via relative import)
>item : any
>item2 : any

import { item as item3 } from "./node_modules/pkg" // _even if they're in a node_modules folder_
>item : any
>item3 : any

23 changes: 23 additions & 0 deletions tests/cases/compiler/nodeNextImportModeImplicitIndexResolution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @module: nodenext
// @filename: node_modules/pkg/package.json
{
"name": "pkg",
"version": "0.0.1"
}
// @filename: node_modules/pkg/index.d.ts
export const item = 4;
// @filename: pkg/package.json
{
"private": true
}
// @filename: pkg/index.d.ts
export const item = 4;
// @filename: package.json
{
"type": "module",
"private": true
}
// @filename: index.ts
import { item } from "pkg"; // should work (`index.js` is assumed to be the entrypoint for packages found via nonrelative import)
import { item as item2 } from "./pkg"; // shouldn't work (`index.js` is _not_ assumed to be the entrypoint for packages found via relative import)
import { item as item3 } from "./node_modules/pkg" // _even if they're in a node_modules folder_