Title: Module resolution: tsc unexpectedly rewrites the import path when exports field contains multiple entries resolving to the same file · Issue #56290 · microsoft/TypeScript · GitHub
Open Graph Title: Module resolution: tsc unexpectedly rewrites the import path when exports field contains multiple entries resolving to the same file · Issue #56290 · microsoft/TypeScript
X Title: Module resolution: tsc unexpectedly rewrites the import path when exports field contains multiple entries resolving to the same file · Issue #56290 · microsoft/TypeScript
Description: Demo Repo https://github.com/sodatea/ts-exports-rewrite Which of the following problems are you reporting? Something else more complicated which I'll explain in more detail Demonstrate the defect described above with a code sample. In th...
Open Graph Description: Demo Repo https://github.com/sodatea/ts-exports-rewrite Which of the following problems are you reporting? Something else more complicated which I'll explain in more detail Demonstrate the defect d...
X Description: Demo Repo https://github.com/sodatea/ts-exports-rewrite Which of the following problems are you reporting? Something else more complicated which I'll explain in more detail Demonstrate the defe...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/56290
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Module resolution: tsc unexpectedly rewrites the import path when exports field contains multiple entries resolving to the same file","articleBody":"### Demo Repo\r\n\r\nhttps://github.com/sodatea/ts-exports-rewrite\r\n\r\n### Which of the following problems are you reporting?\r\n\r\nSomething else more complicated which I'll explain in more detail\r\n\r\n### Demonstrate the defect described above with a code sample.\r\n\r\nIn the reproduction repo, the `parse.ts` file *indirectly depends on* the `lru-cache` module's type by importing the `cache.ts` file:\r\n```ts\r\nimport { createCache } from './cache'\r\nexport const parseCache = createCache\u003c{}>()\r\n```\r\nAfter compilation, in `parse.d.ts`, we would normally expect a `import('lru-cache').LRUCache` expression.\r\nHowever, it actually becomes `import(\"lru-cache/min\").LRUCache`.\r\nOn the other hand, the compiled `cache.d.ts` file still imports from `lru-cache`, not `lru-cache/min`.\r\n\r\nThis seems to be related to `lru-cache@10.0.1`'s exports field: https://unpkg.com/browse/lru-cache@10.0.1/package.json#L34\r\nIt contains 2 entries, `./min` and `.`, both pointing to the same file. TypeScript picks the first entry.\r\n\r\nIf I modify the `exports` field to make `.` appear before `./min`, then the compiled declaration file would use `import(\"lru-cache\")` as expected.\r\n\r\nWhile this isn't a very severe bug and has easy workarounds, this is quite unexpected.\r\nBecause `lru-cache/min` is an entry only available in `exports` field, this compilation result breaks the compatibility with older build tools. And it is quite hard to debug, because no documentation ever mentioned that the order of keys in the `exports` field would make a difference.\r\n\r\n### Run `tsc --showConfig` and paste its output here\r\n\r\n```\r\n{\r\n \"compilerOptions\": {\r\n \"outDir\": \"./temp\",\r\n \"target\": \"esnext\",\r\n \"module\": \"esnext\",\r\n \"moduleResolution\": \"bundler\",\r\n \"esModuleInterop\": true,\r\n \"declaration\": true,\r\n \"emitDeclarationOnly\": true,\r\n \"types\": []\r\n },\r\n \"files\": [\r\n \"./cache.ts\",\r\n \"./parse.ts\"\r\n ],\r\n \"include\": [\r\n \"cache.ts\",\r\n \"parse.ts\"\r\n ],\r\n \"exclude\": [\r\n \"temp\"\r\n ]\r\n}\r\n```\r\n\r\n### Run `tsc --traceResolution` and paste its output here\r\n\r\n```\r\n======== Resolving module 'lru-cache' from '/Users/haoqun/Reproductions/ts-exports-rewrite/cache.ts'. ========\r\nExplicitly specified module resolution kind: 'Bundler'.\r\nResolving in CJS mode with conditions 'import', 'types'.\r\nFound 'package.json' at '/Users/haoqun/Reproductions/ts-exports-rewrite/package.json'.\r\nLoading module 'lru-cache' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.\r\nSearching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.\r\nFound 'package.json' at '/Users/haoqun/Reproductions/ts-exports-rewrite/node_modules/lru-cache/package.json'.\r\nEntering conditional exports.\r\nMatched 'exports' condition 'import'.\r\nEntering conditional exports.\r\nMatched 'exports' condition 'types'.\r\nUsing 'exports' subpath '.' with target './dist/mjs/index.d.ts'.\r\nFile '/Users/haoqun/Reproductions/ts-exports-rewrite/node_modules/lru-cache/dist/mjs/index.d.ts' exists - use it as a name resolution result.\r\nResolved under condition 'types'.\r\nExiting conditional exports.\r\nResolved under condition 'import'.\r\nExiting conditional exports.\r\nResolving real path for '/Users/haoqun/Reproductions/ts-exports-rewrite/node_modules/lru-cache/dist/mjs/index.d.ts', result '/Users/haoqun/Reproductions/ts-exports-rewrite/node_modules/lru-cache/dist/mjs/index.d.ts'.\r\n======== Module name 'lru-cache' was successfully resolved to '/Users/haoqun/Reproductions/ts-exports-rewrite/node_modules/lru-cache/dist/mjs/index.d.ts' with Package ID 'lru-cache/dist/mjs/index.d.ts@10.0.1'. ========\r\n======== Resolving module './cache' from '/Users/haoqun/Reproductions/ts-exports-rewrite/parse.ts'. ========\r\nExplicitly specified module resolution kind: 'Bundler'.\r\nResolving in CJS mode with conditions 'import', 'types'.\r\nLoading module as file / folder, candidate module location '/Users/haoqun/Reproductions/ts-exports-rewrite/cache', target file types: TypeScript, JavaScript, Declaration, JSON.\r\nFile '/Users/haoqun/Reproductions/ts-exports-rewrite/cache.ts' exists - use it as a name resolution result.\r\n======== Module name './cache' was successfully resolved to '/Users/haoqun/Reproductions/ts-exports-rewrite/cache.ts'. ========\r\n```\r\n\r\n### Paste the `package.json` of the *importing* module, if it exists\r\n\r\n```\r\n{\r\n \"name\": \"ts-exports-rewrite\",\r\n \"version\": \"1.0.0\",\r\n \"description\": \"\",\r\n \"main\": \"index.js\",\r\n \"scripts\": {\r\n \"build\": \"tsc\"\r\n },\r\n \"keywords\": [],\r\n \"author\": \"\",\r\n \"license\": \"ISC\",\r\n \"dependencies\": {\r\n \"lru-cache\": \"10.0.1\",\r\n \"typescript\": \"^5.2.2\"\r\n }\r\n}\r\n```\r\n\r\n### Paste the `package.json` of the *target* module, if it exists\r\n\r\n```\r\n{\r\n \"name\": \"lru-cache\",\r\n \"description\": \"A cache object that deletes the least-recently-used items.\",\r\n \"version\": \"10.0.1\",\r\n \"author\": \"Isaac Z. Schlueter \u003ci@izs.me>\",\r\n \"keywords\": [\r\n \"mru\",\r\n \"lru\",\r\n \"cache\"\r\n ],\r\n \"sideEffects\": false,\r\n \"scripts\": {\r\n \"build\": \"npm run prepare\",\r\n \"preprepare\": \"rm -rf dist\",\r\n \"prepare\": \"tsc -p tsconfig.json && tsc -p tsconfig-esm.json\",\r\n \"postprepare\": \"bash fixup.sh\",\r\n \"pretest\": \"npm run prepare\",\r\n \"presnap\": \"npm run prepare\",\r\n \"test\": \"c8 tap\",\r\n \"snap\": \"c8 tap\",\r\n \"preversion\": \"npm test\",\r\n \"postversion\": \"npm publish\",\r\n \"prepublishOnly\": \"git push origin --follow-tags\",\r\n \"format\": \"prettier --write .\",\r\n \"typedoc\": \"typedoc --tsconfig tsconfig-esm.json ./src/*.ts\",\r\n \"benchmark-results-typedoc\": \"bash scripts/benchmark-results-typedoc.sh\",\r\n \"prebenchmark\": \"npm run prepare\",\r\n \"benchmark\": \"make -C benchmark\",\r\n \"preprofile\": \"npm run prepare\",\r\n \"profile\": \"make -C benchmark profile\"\r\n },\r\n \"main\": \"./dist/cjs/index.js\",\r\n \"module\": \"./dist/mjs/index.js\",\r\n \"exports\": {\r\n \"./min\": {\r\n \"import\": {\r\n \"types\": \"./dist/mjs/index.d.ts\",\r\n \"default\": \"./dist/mjs/index.min.js\"\r\n },\r\n \"require\": {\r\n \"types\": \"./dist/cjs/index.d.ts\",\r\n \"default\": \"./dist/cjs/index.min.js\"\r\n }\r\n },\r\n \".\": {\r\n \"import\": {\r\n \"types\": \"./dist/mjs/index.d.ts\",\r\n \"default\": \"./dist/mjs/index.js\"\r\n },\r\n \"require\": {\r\n \"types\": \"./dist/cjs/index.d.ts\",\r\n \"default\": \"./dist/cjs/index.js\"\r\n }\r\n }\r\n },\r\n \"repository\": \"git://github.com/isaacs/node-lru-cache.git\",\r\n \"devDependencies\": {\r\n \"@size-limit/preset-small-lib\": \"^7.0.8\",\r\n \"@types/node\": \"^20.2.5\",\r\n \"@types/tap\": \"^15.0.6\",\r\n \"benchmark\": \"^2.1.4\",\r\n \"c8\": \"^7.11.2\",\r\n \"clock-mock\": \"^1.0.6\",\r\n \"esbuild\": \"^0.17.11\",\r\n \"eslint-config-prettier\": \"^8.5.0\",\r\n \"marked\": \"^4.2.12\",\r\n \"mkdirp\": \"^2.1.5\",\r\n \"prettier\": \"^2.6.2\",\r\n \"size-limit\": \"^7.0.8\",\r\n \"tap\": \"^16.3.4\",\r\n \"ts-node\": \"^10.9.1\",\r\n \"tslib\": \"^2.4.0\",\r\n \"typedoc\": \"^0.24.6\",\r\n \"typescript\": \"^5.0.4\"\r\n },\r\n \"license\": \"ISC\",\r\n \"files\": [\r\n \"dist\"\r\n ],\r\n \"engines\": {\r\n \"node\": \"14 || >=16.14\"\r\n },\r\n \"prettier\": {\r\n \"semi\": false,\r\n \"printWidth\": 70,\r\n \"tabWidth\": 2,\r\n \"useTabs\": false,\r\n \"singleQuote\": true,\r\n \"jsxSingleQuote\": false,\r\n \"bracketSameLine\": true,\r\n \"arrowParens\": \"avoid\",\r\n \"endOfLine\": \"lf\"\r\n },\r\n \"tap\": {\r\n \"coverage\": false,\r\n \"node-arg\": [\r\n \"--expose-gc\",\r\n \"-r\",\r\n \"ts-node/register\"\r\n ],\r\n \"ts\": false\r\n },\r\n \"size-limit\": [\r\n {\r\n \"path\": \"./dist/mjs/index.js\"\r\n }\r\n ]\r\n}\r\n```\r\n\r\n### Any other comments can go here\r\n\r\nHave a nice day!","author":{"url":"https://github.com/haoqunjiang","@type":"Person","name":"haoqunjiang"},"datePublished":"2023-11-02T08:47:38Z","url":"https://github.com/microsoft/TypeScript/issues/56290"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:64dde7b5-2107-c9a5-ea28-afd8fb80f2e8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E7A2:104CA3:970DBB:CD5ACD:6B053CEA |
| html-safe-nonce | 62c649a99da2d9b3adc0009254463a193bb2a606af1cf825bb93f3dd3866e2ef |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFN0EyOjEwNENBMzo5NzBEQkI6Q0Q1QUNEOjZCMDUzQ0VBIiwidmlzaXRvcl9pZCI6IjUyNzc1MzA5NjA0MjEwMTA2NjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 08dc782e83d6247156f39a40f6f60aa98af7b9b44cbafa752a5a48b08f1443ce |
| hovercard-subject-tag | issue:1973775720 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/56290/issue_layout |
| twitter:image | https://opengraph.githubassets.com/30dd30ff5edd93ca7a05ed3de4a0252f5981dc1c886dd5c69e5bea41260661a8/microsoft/TypeScript/issues/56290 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/30dd30ff5edd93ca7a05ed3de4a0252f5981dc1c886dd5c69e5bea41260661a8/microsoft/TypeScript/issues/56290 |
| og:image:alt | Demo Repo https://github.com/sodatea/ts-exports-rewrite Which of the following problems are you reporting? Something else more complicated which I'll explain in more detail Demonstrate the defect d... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | haoqunjiang |
| hostname | github.com |
| expected-hostname | github.com |
| None | 854d880890b5e847b3fce9d1b31f032107baeddeebad2aa379c48aa4f1451f6b |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 220e980097d94ce616d5d11fbd5316d208e3fc93 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width