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

オープン
#717 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
200

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

voidzero-dev/void のほかの issue

voidzero-dev/void の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。