Repository navigation
Conversation
`packages/graphql-modules/package.json` has declared `exports["."].types` and
`exports["./*"].types` for years, but they never reach npm. `bob build` rewrites
the exports map for the published `dist/` directory via its `rewriteExports`,
which rebuilds every entry keeping only `require` and `import`:
newValue = {
require: newValue.require?.replace(`${distDir}/`, ""),
import: newValue.import?.replace(`${distDir}/`, ""),
};
Anything else in the condition object -- `types` included -- is dropped. Every
published tarball up to and including 3.1.2 therefore has an exports map with no
`types` condition, so the declarations are unreachable under `moduleResolution`
`bundler`, `node16` and `nodenext`:
error TS7016: Could not find a declaration file for module 'graphql-modules'.
'.../graphql-modules/index.mjs' implicitly has an 'any' type.
There are types at '.../graphql-modules/index.d.ts', but this result could
not be resolved when respecting package.json "exports".
Only the legacy `node10` resolver worked, because it reads the top-level
`typings` field and ignores `exports` entirely. That is the problem reported in
graphql-hive#2480, which was closed without it going away, and it still reproduces on the
latest release.
The conditions are reinstated in `scripts/fix-async-context.js`, which already
post-processes `dist/package.json` for exactly this class of loss -- its existing
comment reads "because bob removes the imports field". Doing it there needs no
bob upgrade, which matters because the v7 bump was tried in 0cc7337 and reverted
in 31be509. The restore is driven from the source package.json, so it is a no-op
if `types` is ever dropped there, and it leaves the condition first.
Verified end to end rather than by inspection:
- before: `yarn build` then reading dist/package.json -> no `types` condition
- after: `yarn build` -> `{ types, require, import }` on both `.` and `./*`,
both targets present in dist
- packed the built dist with `npm pack`, installed the tarball into a fresh
project on TypeScript 5.9.3 with `module: preserve` +
`moduleResolution: bundler`, and `tsc` goes from exit 2 (TS7016) to exit 0
`yarn test` is 89 passed / 1 failed, and that one failure
(tests/memory-leak.spec.ts) reproduces on an unmodified checkout of master, so
it is not from this change.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the problem reported in #2480 and detailed in #2686.
The defect
packages/graphql-modules/package.jsonhas declared thetypesconditions foryears:
They never reach npm.
bob buildrewrites the exports map for the publisheddist/directory inutils/rewrite-exports.js, and that function rebuildsevery entry keeping only two keys:
Anything else in the condition object —
typesincluded — is dropped. So everypublished tarball up to and including 3.1.2 ships:
which makes the declarations unreachable under
moduleResolutionbundler,node16andnodenext:Only the legacy
node10resolver works, because it reads the top-leveltypingsfield and ignoresexportsentirely. That is why #2480 could beclosed without the problem going away, and why it still reproduces on the
latest release.
The fix
The conditions are reinstated in
scripts/fix-async-context.js, which alreadypost-processes
dist/package.jsonfor exactly this class of loss — its existingcomment reads "because bob removes the
importsfield". Putting it there:0cc7337and reverted in31be509;package.json, so it is a no-op iftypesis everdropped there, and it cannot invent a path of its own;
typesfirst in the condition object, which is the documented order.Happy to move it into a dedicated script, or to rename that script now that it
does two things, if you'd prefer.
Verification
Measured end to end, not by inspecting the JSON:
yarn buildonmaster, then readdist/package.jsontypesconditionyarn buildwith this change{ types, require, import }on both.and./*, both targets present indistnpm packthatdist, install the tarball into a fresh project (TypeScript 5.9.3,module: preserve,moduleResolution: bundler), runtscyarn testis 89 passed / 1 failed. The one failure,packages/graphql-modules/tests/memory-leak.spec.ts, reproduces on anunmodified checkout of
master, so it is not from this change.Note on ordering
The usual guidance is that
typesmust be listed first. For this package itturns out not to be what binds — I checked both orderings against the published
tarball, and
typesfirst andtypeslast both resolve; only its absencefails. This PR lists it first anyway, as the convention.