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

未關閉
#717 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
48/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
typescript, vite
領域
build-system

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
TypeScript
星號
627
分支
66
平均合併
14 小時 17 分鐘
30 天內合併 PR
200

環境準備

這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

voidzero-dev/void 的其他 Issue

查看 voidzero-dev/void 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。