Cron handlers resolve void/env to its browser build in Cloudflare deploys

Aperta
#717 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
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript, vite
Ambito
build-system

Direzione di ricerca

Read the deploy wrapper code in dist/wrapper-*.mjs, especially the block that writes .void/handlers/handlers.ts and runs Vite build(), then inspect .void/handlers/out/__void_handlers.js after deployment. Confirm which export of void/env the handler bundle resolves and compare it with the Worker build. Done when deployed cron and queue handlers can read server environment keys, while the existing development and Vitest behavior still works.

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

Descrizione

What happens

In a deployed Cloudflare Worker, a cron job that reads a server value through void/env throws:

env: Key 'FOO' is not exposed to the client. Void only exposes VITE_* schema keys to browser code. Either rename the key in env.ts with that prefix, or move this access to a server module.

The same code works under void dev (POST /__void/scheduled) and in Vitest with @cloudflare/vitest-pool-workers, so tests do not catch it. Request handlers in the same Worker read the same key without trouble.

Versions: void 1.3.0 with @tanstack/react-start 1.169.4, D1, direct Cloudflare deploy (void deploy --platform cloudflare). The handler bundling code below is unchanged in 1.6.1.

Reproduce

  1. env.ts: export default defineEnv({ FOO: z.string() }), and set FOO as a Worker secret.

  2. crons/foo.ts:

    import { defineScheduled } from "void";
    import { env } from "void/env";
    
    export const cron = "0 * * * *";
    
    export default defineScheduled(async () => {
      console.log(env.FOO);
    });
    
  3. Deploy, then let the schedule fire, or call POST /__void/scheduled with a CRON_SECRET. The run fails with the error above; the HTTP route returns {"error":"AggregateError: Scheduled jobs failed"}.

Likely cause

The deploy wrapper bundles cron and queue handlers separately (.void/handlers/handlers.ts → .void/handlers/out/__void_handlers.js) with Vite's build() in library mode (dist/wrapper-*.mjs, the block that writes handlers.ts). That build sets no ssr option or resolve conditions, so Vite resolves packages with its default client conditions. void/env's browser export wins, and the bundle inlines dist/runtime/env-public-client.mjs instead of the workerd/worker build, env-public.mjs. The generated bundle contains the client module's "is not exposed to the client" message.

Queue consumers go through the same handler bundle, so they are probably affected too.

Possible fix

Build the handlers bundle with Worker resolve conditions (for example resolve.conditions: ["workerd", "worker", "import", "module", "default"], or as an SSR build targeting webworker). Or alias void/env to the server build, as the build already aliases void to void/handler.

Workaround

In code that cron jobs run, read values from the Worker's bindings with import { env } from "cloudflare:workers", validated with the same schema.

Lingua principale
TypeScript
Stelle
627
Fork
66
Merge medio
14h 17m
PR unite (30g)
200

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 voidzero-dev/void

Tutte le issue di voidzero-dev/void

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.