Skip to content

Commit

Permalink
Cherry-pick PR #49327 into release-4.7 (#49329)
Browse files Browse the repository at this point in the history
Component commits:
79957c1 Fix index fallback of CJS package from ESM-mode import when `main` is present but does not resolve

Co-authored-by: Andrew Branch <[email protected]>
  • Loading branch information
TypeScript Bot and andrewbranch authored Jun 1, 2022
1 parent 6e0328a commit b24b05b
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2413,8 +2413,8 @@ namespace ts {
);
if (
!pathAndExtension && packageInfo
&& packageInfo.packageJsonContent.exports === undefined
&& packageInfo.packageJsonContent.main === undefined
// eslint-disable-next-line no-null/no-null
&& (packageInfo.packageJsonContent.exports === undefined || packageInfo.packageJsonContent.exports === null)
&& state.features & NodeResolutionFeatures.EsmMode
) {
// EsmMode disables index lookup in `loadNodeModuleFromDirectoryWorker` generally, however non-relative package resolutions still assume
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/index.cts(4,21): error TS2307: Cannot find module 'dedent4' or its corresponding type declarations.
/index.mts(4,21): error TS2307: Cannot find module 'dedent4' or its corresponding type declarations.


==== /node_modules/@types/dedent/package.json (0 errors) ====
{ "name": "@types/dedent", "version": "1.0.0", "main": "" }

==== /node_modules/@types/dedent2/package.json (0 errors) ====
{ "name": "@types/dedent2", "version": "1.0.0", "main": "asdfasdfasdf" }

==== /node_modules/@types/dedent3/package.json (0 errors) ====
{ "name": "@types/dedent3", "version": "1.0.0", "main": "asdfasdfasdf", "exports": null }

==== /node_modules/@types/dedent4/package.json (0 errors) ====
{ "name": "@types/dedent4", "version": "1.0.0", "main": "asdfasdfasdf", "exports": "./asdfasdfasdf" }

==== /node_modules/@types/dedent/index.d.ts (0 errors) ====
export {};

==== /node_modules/@types/dedent2/index.d.ts (0 errors) ====
export {};

==== /node_modules/@types/dedent3/index.d.ts (0 errors) ====
export {};

==== /node_modules/@types/dedent4/index.d.ts (0 errors) ====
export {};

==== /index.mts (1 errors) ====
import dedent from "dedent";
import dedent2 from "dedent2";
import dedent3 from "dedent3";
import dedent4 from "dedent4"; // Error
~~~~~~~~~
!!! error TS2307: Cannot find module 'dedent4' or its corresponding type declarations.

==== /index.cts (1 errors) ====
import dedent from "dedent";
import dedent2 from "dedent2";
import dedent3 from "dedent3";
import dedent4 from "dedent4"; // Error
~~~~~~~~~
!!! error TS2307: Cannot find module 'dedent4' or its corresponding type declarations.

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

//// [package.json]
{ "name": "@types/dedent", "version": "1.0.0", "main": "" }

//// [package.json]
{ "name": "@types/dedent2", "version": "1.0.0", "main": "asdfasdfasdf" }

//// [package.json]
{ "name": "@types/dedent3", "version": "1.0.0", "main": "asdfasdfasdf", "exports": null }

//// [package.json]
{ "name": "@types/dedent4", "version": "1.0.0", "main": "asdfasdfasdf", "exports": "./asdfasdfasdf" }

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

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

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

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

//// [index.mts]
import dedent from "dedent";
import dedent2 from "dedent2";
import dedent3 from "dedent3";
import dedent4 from "dedent4"; // Error

//// [index.cts]
import dedent from "dedent";
import dedent2 from "dedent2";
import dedent3 from "dedent3";
import dedent4 from "dedent4"; // Error


//// [index.mjs]
export {};
//// [index.cjs]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== /node_modules/@types/dedent/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/dedent2/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/dedent3/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/dedent4/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /index.mts ===
import dedent from "dedent";
>dedent : Symbol(dedent, Decl(index.mts, 0, 6))

import dedent2 from "dedent2";
>dedent2 : Symbol(dedent2, Decl(index.mts, 1, 6))

import dedent3 from "dedent3";
>dedent3 : Symbol(dedent3, Decl(index.mts, 2, 6))

import dedent4 from "dedent4"; // Error
>dedent4 : Symbol(dedent4, Decl(index.mts, 3, 6))

=== /index.cts ===
import dedent from "dedent";
>dedent : Symbol(dedent, Decl(index.cts, 0, 6))

import dedent2 from "dedent2";
>dedent2 : Symbol(dedent2, Decl(index.cts, 1, 6))

import dedent3 from "dedent3";
>dedent3 : Symbol(dedent3, Decl(index.cts, 2, 6))

import dedent4 from "dedent4"; // Error
>dedent4 : Symbol(dedent4, Decl(index.cts, 3, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
=== /node_modules/@types/dedent/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/dedent2/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/dedent3/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/dedent4/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /index.mts ===
import dedent from "dedent";
>dedent : typeof dedent

import dedent2 from "dedent2";
>dedent2 : typeof dedent2

import dedent3 from "dedent3";
>dedent3 : typeof dedent3

import dedent4 from "dedent4"; // Error
>dedent4 : any

=== /index.cts ===
import dedent from "dedent";
>dedent : typeof dedent

import dedent2 from "dedent2";
>dedent2 : typeof dedent2

import dedent3 from "dedent3";
>dedent3 : typeof dedent3

import dedent4 from "dedent4"; // Error
>dedent4 : any

37 changes: 37 additions & 0 deletions tests/cases/compiler/nodeNextImportModeImplicitIndexResolution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @module: nodenext

// @Filename: /node_modules/@types/dedent/package.json
{ "name": "@types/dedent", "version": "1.0.0", "main": "" }

// @Filename: /node_modules/@types/dedent2/package.json
{ "name": "@types/dedent2", "version": "1.0.0", "main": "asdfasdfasdf" }

// @Filename: /node_modules/@types/dedent3/package.json
{ "name": "@types/dedent3", "version": "1.0.0", "main": "asdfasdfasdf", "exports": null }

// @Filename: /node_modules/@types/dedent4/package.json
{ "name": "@types/dedent4", "version": "1.0.0", "main": "asdfasdfasdf", "exports": "./asdfasdfasdf" }

// @Filename: /node_modules/@types/dedent/index.d.ts
export {};

// @Filename: /node_modules/@types/dedent2/index.d.ts
export {};

// @Filename: /node_modules/@types/dedent3/index.d.ts
export {};

// @Filename: /node_modules/@types/dedent4/index.d.ts
export {};

// @Filename: /index.mts
import dedent from "dedent";
import dedent2 from "dedent2";
import dedent3 from "dedent3";
import dedent4 from "dedent4"; // Error

// @Filename: /index.cts
import dedent from "dedent";
import dedent2 from "dedent2";
import dedent3 from "dedent3";
import dedent4 from "dedent4"; // Error

0 comments on commit b24b05b

Please sign in to comment.