Skip to content

Fix incorrect usage of package.json types/main in package subdirectories - #60696

Merged
Andrew Branch (andrewbranch) merged 1 commit into
microsoft:mainfrom
andrewbranch:bug/module-resolution-package-json-field-subdir
Dec 6, 2024
Merged

Fix incorrect usage of package.json types/main in package subdirectories#60696
Andrew Branch (andrewbranch) merged 1 commit into
microsoft:mainfrom
andrewbranch:bug/module-resolution-package-json-field-subdir

Conversation

@andrewbranch

Copy link
Copy Markdown
Member

Fixes a bug I noticed randomly

The content of moduleResolution_packageJson_yesAtPackageRoot.ts, for easier reviewing:

// @noImplicitReferences: true
// @traceResolution: true

// @Filename: /node_modules/foo/bar/index.js
not read

// @Filename: /node_modules/foo/package.json
{ "name": "foo", "version": "1.2.3", "types": "types.d.ts" }

// @Filename: /node_modules/foo/types.d.ts
export const x = 0;

// @Filename: /a.ts
import { x } from "foo/bar";

You can see the resolution of "foo/bar" incorrectly looking for /node_modules/foo/bar/types.d.ts due to package.json content in /node_modules/foo. (It doesn’t change the final resolution in these tests because /node_modules/foo/bar/types.d.ts doesn’t exist.) Presumably this problem only escaped detection for ~10 years because whatever value was in package.json "types" usually is either "index.d.ts", which would get looked up in subdirectories anyway, or is a path that doesn’t exist when resolved relative to the subdirectory path.

@andrewbranch

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) test top400

@typescript-bot

TypeScript Bot (typescript-bot) commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started

@andrewbranch

Copy link
Copy Markdown
Member Author

TypeScript Bot (@typescript-bot) test top400

@typescript-bot

TypeScript Bot (typescript-bot) commented Dec 6, 2024

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results

@typescript-bot

Copy link
Copy Markdown
Contributor

Andrew Branch (@andrewbranch) Here are the results of running the top 400 repos with tsc comparing main and refs/pull/60696/merge:

Everything looks good!

@andrewbranch
Andrew Branch (andrewbranch) merged commit 421f5c5 into microsoft:main Dec 6, 2024
@andrewbranch
Andrew Branch (andrewbranch) deleted the bug/module-resolution-package-json-field-subdir branch December 6, 2024 21:47
@andrewbranch Andrew Branch (andrewbranch) added the Breaking Change Would introduce errors in existing code label Jan 3, 2025
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team Breaking Change Would introduce errors in existing code For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants