Repository navigation
fix(zod-openapi): export local z binding so OpenAPI patch is not tree-shaken - #2069
Conversation
🦋 Changeset detectedLatest commit: 7f32313 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Addresses changeset-bot on honojs#2069 so the fix is published in the next version. Co-authored-by: Cursor <[email protected]>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2069 +/- ##
=======================================
Coverage 92.23% 92.23%
=======================================
Files 116 116
Lines 4122 4123 +1
Branches 1077 1077
=======================================
+ Hits 3802 3803 +1
Misses 285 285
Partials 35 35
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This PR is not acceptable. The exported |
|
Thanks @yusukebe — you're right, `export const z` dropped the TypeScript namespace. Fixed in the latest commit: keep the local value binding (so esbuild splitting cannot elide the `extendZodWithOpenApi` side effect), and merge a namespace so `z.infer` / `z.input` / `z.output` work again: ```ts Verified with `tsc --strict` against the built `dist` types. Happy to adjust if you'd rather take a different approach to #2051. |
…undling
`export { z }` re-exports the binding imported from zod, so a bundler may
resolve `import { z } from '@hono/zod-openapi'` straight to zod and drop the
edge to this module. Under esbuild code splitting the schema chunk then depends
on zod's chunk alone and can evaluate before the chunk holding
`extendZodWithOpenApi(z)`, leaving `.openapi()` undefined at runtime while
types, tests and the build all pass (honojs#2051).
The value has to be a binding local to this module for that edge to survive.
`export const z = zod` does that but carries only the value side, so
`z.infer<typeof Schema>` stops resolving for consumers — the regression raised
on the first revision of this change, and the reason the issue was left open as
a documentation note.
An alias declaration is a binding local to the module *and* keeps every meaning
of its target, so both hold:
export import z = zodModule.z
It compiles to `var z = zodModule.z` in `dist/index.mjs` and survives
declaration bundling as `import z = zodModule.z` in `dist/index.d.mts`. The
patch call takes `zodModule.z` directly, since it precedes the alias binding.
Verified against the built package: a consumer resolving `dist/index.d.mts`
type-checks `z.infer`, `z.input`, `z.output`, `z.TypeOf`, `z.ZodType`,
`z.ZodString`, `z.ZodObject` and `z.core.$ZodIssue`, all of which fail on the
`export const z` form; `z.string().openapi()` is a function when imported from
`dist/index.mjs`. 111 package tests and `tsc -b` pass.
Fixes honojs#2051
0312f25 to
7f32313
Compare
|
You were right, and the follow-up I pushed after your comment was still wrong — it restored The constraint is real: the value has to be a binding local to this module or the import edge disappears, and export import z = zodModule.zAn alias is local to the module and carries every meaning of its target, so both constraints hold at once. In the built package:
The patch call takes The whole change is now two lines plus a comment; the internal Verified against the built artifacts rather than the source — a consumer resolving z.infer<typeof Schema> z.input<…> z.output<…> z.TypeOf<…>
z.ZodType z.ZodString z.ZodObject<…> z.core.$ZodIssueEach of those fails on the If you'd still rather resolve #2051 as a README note about the bundler hazard, I'm happy to send that instead — just say the word. |
|
Interesting! Looks good. Thanks! |
* revert(zod-openapi): export local z binding Fixes #2078 This reverts commit fc0377c. Original commit title that has been reverted: fix(zod-openapi): export local z binding so OpenAPI patch is not tree-shaken (#2069) This commit is being reverted because its use-case is not yet supported by `rolldown-plugin-dts` which is a subdependency of `tsdown` in this project. Its impact is that the zod types have decraded and is resulting in types resulting in `any` which causes the typescript compiler to error in strict mode Signed-off-by: Kamaal Farah <[email protected]> * chore(zod-openapi): add revert changeset **Summary** Prepare a patch release for the reverted local z binding export. **Changes** Record the zod-openapi revert for a patch release because the local binding degrades zod types to any when tsdown builds the package. Signed-off-by: Kamaal Farah <[email protected]> --------- Signed-off-by: Kamaal Farah <[email protected]>
Summary
@hono/zod-openapicalledextendZodWithOpenApi(z)then re-exported the same zod import binding.splitting: true,import { z } from '@hono/zod-openapi'can resolve straight tozod, dropping this module — so schemas throw...openapi is not a function(issue [@hono/zod-openapi] pass-throughzre-export breaks.openapi()under esbuild code splitting #2051).const z = zod(still after the patch) so the side effect is not elidable.zalongside that value export (TS2395/TS2440), and annotate the export for--isolatedDeclarations.Test plan
pnpm --filter @hono/zod-openapi typecheckpnpm --filter @hono/zod-openapi build@hono/zod-openapiFixes #2051