zod 4 toJSONSchema() throws a deferred SC2011 in finalize (Map entries spread)

Aperta
#3,605 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

Start by reproducing with the minimal index.ts (a z.object calling toJSONSchema()) and the scriptc build command from the issue, then read the failing line 295 of node_modules/zod/v4/core/to-json-schema.js, which spreads ctx.seen.entries() in finalize. The payload does not name the compiler source that emits SC2011, so the first step is locating where that diagnostic is raised for untyped Map entries. Done means the probe prints the expected draft 2020-12 JSON Schema without the SC2011 error.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

toJSONSchema() from zod 4.4.3 compiles, but throws at run time with a deferred SC2011 inside zod's finalize.

index.ts:

import { z } from "zod";

console.log(JSON.stringify(z.object({ a: z.string() }).toJSONSchema()));

With [email protected] installed and a tsconfig.json of {"compilerOptions":{"target":"ES2022","module":"NodeNext","moduleResolution":"NodeNext","strict":true}}, scriptc build index.ts -o probe --optimization dev --node-islands=off builds (0.4.10 and 0.4.11). Running ./probe:

scriptc: not compiled: values of type 'any' have no native representation at this site (instantiating 'finalize' with ({ counter: number; cycles: unknown; external: unknown; io: unknown; metadataR...)) [SC2011 at node_modules/zod/v4/core/to-json-schema.js:295]
Uncaught Error: values of type 'any' have no native representation at this site (instantiating 'finalize' with ...) [SC2011 at node_modules/zod/v4/core/to-json-schema.js:295]
    at finalize (node_modules/zod/v4/core/to-json-schema.js:295:5)

Node prints {"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"a":{"type":"string"}},"required":["a"],"additionalProperties":false}.

Line 295 of to-json-schema.js is

for (const entry of [...ctx.seen.entries()].reverse()) {

where ctx.seen is a Map built in initializeContext (seen: new Map()), keyed by schema with untyped entry objects. The same happens through schema["~standard"].jsonSchema.input(...) and z.toJSONSchema(schema), which all reach finalize.

Programs that hand zod schemas to the MCP SDK (tool listings) or build structured-output prompts with z.toJSONSchema hit this.

Lingua principale
TypeScript
Stelle
6.6k
Fork
216
Merge medio
9h 35m
PR unite (30g)
775

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vercel-labs/scriptc

Tutte le issue di vercel-labs/scriptc

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.