Skip to content

Commit

Permalink
Fix accidental ESM-mode directory module lookup in package non-root (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch authored Jan 30, 2025
1 parent 0652664 commit 019fec8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3118,7 +3118,7 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
packageInfo,
);
if (
!pathAndExtension && packageInfo
!pathAndExtension && !rest && packageInfo
// eslint-disable-next-line no-restricted-syntax
&& (packageInfo.contents.packageJsonContent.exports === undefined || packageInfo.contents.packageJsonContent.exports === null)
&& state.features & NodeResolutionFeatures.EsmMode
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/nodeModulesNoDirectoryModule.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/index.ts(2,8): error TS2307: Cannot find module 'i-have-a-dir-and-main/dist/dir' or its corresponding type declarations.


==== /node_modules/i-have-a-dir-and-main/package.json (0 errors) ====
{
"name": "i-have-a-dir-and-main",
"version": "1.0.0",
"type": "module",
"main": "dist/index.js"
}

==== /node_modules/i-have-a-dir-and-main/dist/index.d.ts (0 errors) ====
export declare const a = 1;

==== /node_modules/i-have-a-dir-and-main/dist/dir/index.d.ts (0 errors) ====
export declare const b = 2;

==== /package.json (0 errors) ====
{ "type": "module" }

==== /index.ts (1 errors) ====
import 'i-have-a-dir-and-main' // ok
import 'i-have-a-dir-and-main/dist/dir' // error
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'i-have-a-dir-and-main/dist/dir' or its corresponding type declarations.

26 changes: 26 additions & 0 deletions tests/cases/conformance/node/nodeModulesNoDirectoryModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @noUncheckedSideEffectImports: true
// @strict: true
// @module: node16
// @noEmit: true
// @noTypesAndSymbols: true

// @Filename: /node_modules/i-have-a-dir-and-main/package.json
{
"name": "i-have-a-dir-and-main",
"version": "1.0.0",
"type": "module",
"main": "dist/index.js"
}

// @Filename: /node_modules/i-have-a-dir-and-main/dist/index.d.ts
export declare const a = 1;

// @Filename: /node_modules/i-have-a-dir-and-main/dist/dir/index.d.ts
export declare const b = 2;

// @Filename: /package.json
{ "type": "module" }

// @Filename: /index.ts
import 'i-have-a-dir-and-main' // ok
import 'i-have-a-dir-and-main/dist/dir' // error

0 comments on commit 019fec8

Please sign in to comment.