Skip to content

fix(test): stop Vite's SSR transform from dropping zod's z export - #491

Open
yange-siegenia wants to merge 1 commit into
QuackbackIO:mainfrom
yange-siegenia:fix/vitest-zod-ssr-transform
Open

fix(test): stop Vite's SSR transform from dropping zod's z export#491
yange-siegenia wants to merge 1 commit into
QuackbackIO:mainfrom
yange-siegenia:fix/vitest-zod-ssr-transform

Conversation

@yange-siegenia

Copy link
Copy Markdown

On a clean checkout with the committed lockfile, bun run test fails to run essentially the whole suite. Every module that declares a zod schema throws on load, which is most of the server.

Cause

zod 4.4.3's entry point is:

import * as z from "./v4/classic/external.js";
export * from "./v4/classic/external.js";
export { z };
export default z;

Vite's SSR transform loses the z binding in that specific combination — a namespace import that is re-exported by name alongside an export * of the same module. import { z } from 'zod' therefore arrives as undefined, while import * as z from 'zod' still works.

Minimal reproduction:

import { z } from 'zod'
it('z is defined', () => {
  expect(typeof z?.string).toBe('function')  // fails: z is undefined
})

Fix

Add zod to the SSR optimizer's include list so esbuild pre-bundles it, which sidesteps the SSR transform and re-exports it correctly.

Two other plausible fixes were tried and do not work, which is why the comment records them:

  • externalizing zod so Node loads it natively — still fails
  • removing the SSR optimizer entirely — still fails

So this is specific to the SSR transform rather than to the optimizer being enabled.

Verification

Confirmed against main (currently ce3d245): the reproduction above fails without this change and passes with it.

Full suite against a real pgvector/pgvector:pg17 Postgres with migrations applied: 13,946 passing. The handful of remaining failures all pass when their files are run in isolation — they are pre-existing parallelism flakes under full-suite CPU contention, in auth components and email routing, unrelated to this change.

I hit this while working on something unrelated in a fork, and it looked worth sending upstream on its own.

The suite could not run at all. zod's entry does `import * as z from
'./v4/classic/external.js'`, then both `export *` from that same module
and `export { z }`. Vite's SSR transform loses the `z` binding in that
combination, so `import { z } from 'zod'` arrives as undefined and every
module that declares a schema throws on load -- which is nearly all of
them.

Pre-bundling zod with esbuild sidesteps the SSR transform and re-exports
it correctly. The two other plausible fixes were tried and do not work:
externalizing zod so Node loads it natively still fails, and so does
removing the SSR optimizer entirely, so this is specific to the transform
rather than to the optimizer being on.

Verified against a pgvector Postgres: 13946 passing. The five remaining
failures pass in isolation and are parallelism flakes that predate this.
@CLAassistant

CLAassistant commented Sep 4, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

2 participants