Skip to content

fix(zod-openapi): export local z binding so OpenAPI patch is not tree-shaken - #2069

Merged
yusukebe merged 1 commit into
honojs:mainfrom
edenbuilds:fix/zod-openapi-z-reexport
Aug 8, 2026
Merged

yusukebe merged 1 commit into
honojs:mainfrom
edenbuilds:fix/zod-openapi-z-reexport

Conversation

@edenbuilds

@edenbuilds edenbuilds commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • @hono/zod-openapi called extendZodWithOpenApi(z) then re-exported the same zod import binding.
  • With esbuild splitting: true, import { z } from '@hono/zod-openapi' can resolve straight to zod, dropping this module — so schemas throw ...openapi is not a function (issue [@hono/zod-openapi] pass-through z re-export breaks .openapi() under esbuild code splitting #2051).
  • Export a local const z = zod (still after the patch) so the side effect is not elidable.
  • Follow-up: avoid importing type z alongside that value export (TS2395/TS2440), and annotate the export for --isolatedDeclarations.

Test plan

  • pnpm --filter @hono/zod-openapi typecheck
  • pnpm --filter @hono/zod-openapi build
  • Changeset for @hono/zod-openapi

Fixes #2051

@changeset-bot

changeset-bot Bot commented Aug 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7f32313

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/zod-openapi Patch

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

edenbuilds added a commit to edenbuilds/middleware that referenced this pull request Aug 6, 2026
Addresses changeset-bot on honojs#2069 so the fix is published in the next version.

Co-authored-by: Cursor <[email protected]>
@codecov

codecov Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.23%. Comparing base (f1e3b86) to head (7f32313).
⚠️ Report is 1 commits behind head on main.

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           
Flag Coverage Δ
standard-validator 95.65% <ø> (ø)
zod-openapi 95.55% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@yusukebe

yusukebe commented Aug 7, 2026

Copy link
Copy Markdown
Member

This PR is not acceptable.

The exported z lost its namespace. So the following throws the error:

const Schema = z.object({ name: z.string() })
type User = z.infer<typeof Schema> // ✘ TS2503: Cannot find namespace 'z'

@edenbuilds

Copy link
Copy Markdown
Contributor Author

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
const Schema = z.object({ name: z.string() })
type User = z.infer // OK
```

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
@edenbuilds
edenbuilds force-pushed the fix/zod-openapi-z-reexport branch from 0312f25 to 7f32313 Compare August 7, 2026 19:52
@edenbuilds

Copy link
Copy Markdown
Contributor Author

You were right, and the follow-up I pushed after your comment was still wrong — it restored z.infer by merging a hand-written namespace, which only covered three aliases. z.ZodType, z.ZodString, z.ZodObject and z.core all still broke. Sorry for the noise; I've replaced the whole thing.

The constraint is real: the value has to be a binding local to this module or the import edge disappears, and export const z carries only the value side. @salpers reached the same conclusion in #2051 and asked whether there was a z.infer-preserving approach nobody had tried. There is one — an alias declaration:

export import z = zodModule.z

An alias is local to the module and carries every meaning of its target, so both constraints hold at once. In the built package:

  • dist/index.mjs → var z = zodModule.z — a local binding, so the edge to this module survives
  • dist/index.d.mts → import z = zodModule.z — the namespace survives declaration bundling

The patch call takes zodModule.z directly, since it precedes the alias binding.

The whole change is now two lines plus a comment; the internal z.input<…> / z.output<…> type positions are untouched, because z is a full namespace again.

Verified against the built artifacts rather than the source — a consumer resolving dist/index.d.mts type-checks all of:

z.infer<typeof Schema>   z.input<…>   z.output<…>   z.TypeOf<…>
z.ZodType   z.ZodString   z.ZodObject<…>   z.core.$ZodIssue

Each of those fails on the export const z form, including your exact repro. z.string().openapi() is a function when imported from dist/index.mjs. 111 package tests and tsc -b pass.

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.

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe

yusukebe commented Aug 8, 2026

Copy link
Copy Markdown
Member

@edenbuilds

Interesting! Looks good. Thanks!

@yusukebe
yusukebe merged commit fc0377c into honojs:main Aug 8, 2026
99 checks passed
@github-actions github-actions Bot mentioned this pull request Aug 8, 2026
yusukebe pushed a commit that referenced this pull request Aug 13, 2026
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[@hono/zod-openapi] pass-through z re-export breaks .openapi() under esbuild code splitting

2 participants