From 4e3b120e754474a506842aaab63d8e4d5ddbc906 Mon Sep 17 00:00:00 2001 From: SHANNWD <167665311+SHANNWD@users.noreply.github.com> Date: Fri, 4 Dec 2026 17:43:50 +0530 Subject: [PATCH 1/2] fix(db): use binding name for local dev/preview D1 when database_id is omitted (#1030) --- packages/void/src/plugin-cloudflare-config.ts | 13 ++++ .../void/src/plugin-cloudflare-preview.ts | 3 +- packages/void/src/plugin-cloudflare.ts | 2 + packages/void/test/unit/local-d1.test.ts | 73 +++++++++++++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/packages/void/src/plugin-cloudflare-config.ts b/packages/void/src/plugin-cloudflare-config.ts index 8472275a4f..fbc979cd02 100644 --- a/packages/void/src/plugin-cloudflare-config.ts +++ b/packages/void/src/plugin-cloudflare-config.ts @@ -184,6 +184,19 @@ export function restoreLocalContainerBindings(worker: CloudflareConfig['worker'] } } +/** Give D1 bindings without an ID their binding name locally, matching Wrangler and `local-d1.ts`. */ +export function restoreLocalD1Bindings(worker: Pick): void { + const env = worker.env; + if (!env) { + return; + } + for (const [name, binding] of Object.entries(env)) { + if (binding.type === 'd1' && !binding.id) { + env[name] = { ...binding, id: name }; + } + } +} + export function nativeAuxiliaryWorkers( workers: Array | undefined, root: string, diff --git a/packages/void/src/plugin-cloudflare-preview.ts b/packages/void/src/plugin-cloudflare-preview.ts index 07713d7bd2..8d236fc330 100644 --- a/packages/void/src/plugin-cloudflare-preview.ts +++ b/packages/void/src/plugin-cloudflare-preview.ts @@ -3,7 +3,7 @@ import { getRootConfigPath, getWorkerConfigPath } from '@cloudflare/build-output import type { CloudflareConfig } from '@cloudflare/vite-plugin'; import { dirname, join } from 'pathslash'; import type { Plugin } from 'vite'; -import { addLocalQueueProducers } from './plugin-cloudflare-config.ts'; +import { addLocalQueueProducers, restoreLocalD1Bindings } from './plugin-cloudflare-config.ts'; import type { LocalQueueProducers } from './plugin-cloudflare-config.ts'; import { writeFileAtomic } from './shared/atomic-write.ts'; import { loadLocalEnv } from './shared/env.ts'; @@ -54,6 +54,7 @@ export function withLocalPreviewBindings( worker.env[name] = { type: 'secret' }; } } + restoreLocalD1Bindings(worker); addLocalQueueProducers(worker, localQueueProducers); writeFileAtomic(path, JSON.stringify(worker)); // The upstream preview resolves declared secrets from this env directory. diff --git a/packages/void/src/plugin-cloudflare.ts b/packages/void/src/plugin-cloudflare.ts index 6b672d4102..96248346e3 100644 --- a/packages/void/src/plugin-cloudflare.ts +++ b/packages/void/src/plugin-cloudflare.ts @@ -16,6 +16,7 @@ import { mergeCloudflareConfig, nativeAuxiliaryWorkers, restoreLocalContainerBindings, + restoreLocalD1Bindings, toNativeCloudflareConfig, } from './plugin-cloudflare-config.ts'; import type { @@ -112,6 +113,7 @@ export function cloudflare(options: { } } else if (environment.command === 'serve') { restoreLocalContainerBindings(pluginNative.worker); + restoreLocalD1Bindings(pluginNative.worker); localQueueBindings = addLocalQueueProducers( pluginNative.worker, options.localQueueProducers, diff --git a/packages/void/test/unit/local-d1.test.ts b/packages/void/test/unit/local-d1.test.ts index 0a80997860..8e6ca01515 100644 --- a/packages/void/test/unit/local-d1.test.ts +++ b/packages/void/test/unit/local-d1.test.ts @@ -3,11 +3,15 @@ import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { Miniflare, convertV4MiniflareOptions } from 'miniflare'; import { toSlash } from 'pathslash'; +import { resolveConfig } from 'vite'; import { afterEach, describe, expect, it, vi } from 'vite-plus/test'; import { getLocalD1Target } from '../../src/cli/db.ts'; +import { materializeCloudflareConfig } from '../../src/cloudflare-config.ts'; import { voidPlugin } from '../../src/index.ts'; import { resolveLocalD1Target, writeLocalD1PersistState } from '../../src/local-d1.ts'; import { getApplicationTables } from '../../src/migrations/runner.ts'; +import { restoreLocalD1Bindings } from '../../src/plugin-cloudflare-config.ts'; +import { cloudflare } from '../../src/plugin-cloudflare.ts'; const roots: Array = []; @@ -234,4 +238,73 @@ describe('local D1 identity', () => { writeLocalD1PersistState(root, customPersistRoot); expect(getLocalD1Target(root).persistRoot).toBe(customPersistRoot); }); + + it('fills omitted D1 binding IDs with the binding name locally without touching explicit or non-D1 bindings', () => { + const worker = { + env: { + DB: { type: 'd1' as const, name: 'repro' }, + EXPLICIT: { type: 'd1' as const, name: 'explicit', id: 'custom-id' }, + KV: { type: 'kv' as const, id: 'kv-id' }, + }, + }; + + restoreLocalD1Bindings(worker); + + expect(worker.env.DB).toEqual({ type: 'd1', name: 'repro', id: 'DB' }); + expect(worker.env.EXPLICIT).toEqual({ type: 'd1', name: 'explicit', id: 'custom-id' }); + expect(worker.env.KV).toEqual({ type: 'kv', id: 'kv-id' }); + }); + + it('uses the same binding-name D1 ID in dev server config as resolveLocalD1Target when database_id is omitted', async () => { + const root = createRoot(); + writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module', name: 'd1-repro' })); + writeFileSync( + join(root, 'void.config.ts'), + "export default { cloudflare: { d1_databases: [{ binding: 'DB', database_name: 'repro' }] } };\n", + ); + mkdirSync(join(root, 'src'), { recursive: true }); + writeFileSync( + join(root, 'src/entry.ts'), + 'export default { fetch() { return new Response("ok"); } };\n', + ); + vi.spyOn(process, 'cwd').mockReturnValue(root); + + const cliTarget = getLocalD1Target(root); + expect(cliTarget.databaseId).toBe('DB'); + + const configPath = materializeCloudflareConfig(root); + const resolveEntryD1 = async (command: 'serve' | 'build') => { + let seenD1: Array<{ binding: string; database_id?: string }> | undefined; + const plugins = cloudflare({ + configPath, + config: () => ({ + name: 'd1-repro', + main: './src/entry.ts', + compatibility_date: '2026-02-24', + }), + auxiliaryWorkers: [ + { + config(_config, { entryWorkerConfig }) { + seenD1 = entryWorkerConfig.d1_databases; + return { + name: 'helper', + main: './src/entry.ts', + compatibility_date: '2026-02-24', + }; + }, + }, + ], + viteEnvironment: { name: 'ssr' }, + }); + await resolveConfig({ root, configFile: false, plugins }, command); + return seenD1; + }; + + expect(await resolveEntryD1('serve')).toEqual([ + expect.objectContaining({ binding: 'DB', database_name: 'repro', database_id: 'DB' }), + ]); + const buildD1 = await resolveEntryD1('build'); + expect(buildD1).toEqual([expect.objectContaining({ binding: 'DB', database_name: 'repro' })]); + expect(buildD1?.[0]?.database_id).toBeUndefined(); + }); }); From 76f277e32621038465bbcf31576bf1a90c3a5f66 Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Sat, 5 Dec 2026 12:01:07 +0900 Subject: [PATCH 2/2] Preserve local D1 isolation for secondary bindings. --- packages/void/src/index.ts | 2 ++ packages/void/src/plugin-cloudflare-config.ts | 17 ++++++++--------- packages/void/src/plugin-cloudflare-preview.ts | 5 +++-- packages/void/src/plugin-cloudflare.ts | 7 +++++-- packages/void/test/unit/local-d1.test.ts | 13 ++++++++----- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/packages/void/src/index.ts b/packages/void/src/index.ts index 69a151a7b0..4f6730713c 100644 --- a/packages/void/src/index.ts +++ b/packages/void/src/index.ts @@ -953,6 +953,7 @@ export function voidPlugin(options?: { configPath: cloudflareConfigPath, ...inspectorPortOption, persistState: { path: localD1.persistRoot }, + d1BindingName: localD1.bindingName, auxiliaryWorkers: options?.auxiliaryWorkers, viteEnvironment, config: mergeBindings, @@ -1230,6 +1231,7 @@ export function voidPlugin(options?: { configPath: cloudflareConfigPath, ...inspectorPortOption, persistState: { path: localD1.persistRoot }, + d1BindingName: localD1.bindingName, auxiliaryWorkers: options?.auxiliaryWorkers, viteEnvironment: { name: DEFAULT_WORKER_ENVIRONMENT_NAME }, config: mergeBindings, diff --git a/packages/void/src/plugin-cloudflare-config.ts b/packages/void/src/plugin-cloudflare-config.ts index 37e6707396..f80e2c2acc 100644 --- a/packages/void/src/plugin-cloudflare-config.ts +++ b/packages/void/src/plugin-cloudflare-config.ts @@ -193,16 +193,15 @@ export function restoreLocalContainerBindings(worker: CloudflareConfig['worker'] } } -/** Give D1 bindings without an ID their binding name locally, matching Wrangler and `local-d1.ts`. */ -export function restoreLocalD1Bindings(worker: Pick): void { +/** Match the local migration target while leaving other D1 bindings scoped to their Worker. */ +export function restoreLocalD1Binding( + worker: Pick, + bindingName: string, +): void { const env = worker.env; - if (!env) { - return; - } - for (const [name, binding] of Object.entries(env)) { - if (binding.type === 'd1' && !binding.id) { - env[name] = { ...binding, id: name }; - } + const binding = env?.[bindingName]; + if (env && binding?.type === 'd1' && !binding.id) { + env[bindingName] = { ...binding, id: bindingName }; } } diff --git a/packages/void/src/plugin-cloudflare-preview.ts b/packages/void/src/plugin-cloudflare-preview.ts index 8d236fc330..e65093381c 100644 --- a/packages/void/src/plugin-cloudflare-preview.ts +++ b/packages/void/src/plugin-cloudflare-preview.ts @@ -3,7 +3,7 @@ import { getRootConfigPath, getWorkerConfigPath } from '@cloudflare/build-output import type { CloudflareConfig } from '@cloudflare/vite-plugin'; import { dirname, join } from 'pathslash'; import type { Plugin } from 'vite'; -import { addLocalQueueProducers, restoreLocalD1Bindings } from './plugin-cloudflare-config.ts'; +import { addLocalQueueProducers, restoreLocalD1Binding } from './plugin-cloudflare-config.ts'; import type { LocalQueueProducers } from './plugin-cloudflare-config.ts'; import { writeFileAtomic } from './shared/atomic-write.ts'; import { loadLocalEnv } from './shared/env.ts'; @@ -12,6 +12,7 @@ import { loadLocalEnv } from './shared/env.ts'; export function withLocalPreviewBindings( plugins: Array, root: string, + d1BindingName: string, localQueueProducers?: LocalQueueProducers, ): Array { let directory: string | undefined; @@ -54,7 +55,7 @@ export function withLocalPreviewBindings( worker.env[name] = { type: 'secret' }; } } - restoreLocalD1Bindings(worker); + restoreLocalD1Binding(worker, d1BindingName); addLocalQueueProducers(worker, localQueueProducers); writeFileAtomic(path, JSON.stringify(worker)); // The upstream preview resolves declared secrets from this env directory. diff --git a/packages/void/src/plugin-cloudflare.ts b/packages/void/src/plugin-cloudflare.ts index 156fc8b37a..66ccc72893 100644 --- a/packages/void/src/plugin-cloudflare.ts +++ b/packages/void/src/plugin-cloudflare.ts @@ -16,7 +16,7 @@ import { mergeCloudflareConfig, nativeAuxiliaryWorkers, restoreLocalContainerBindings, - restoreLocalD1Bindings, + restoreLocalD1Binding, toNativeCloudflareConfig, } from './plugin-cloudflare-config.ts'; import type { @@ -60,12 +60,14 @@ export function cloudflare(options: { config: (resolved: WorkerConfig) => Partial; auxiliaryWorkers?: Array; inspectorPort?: PluginConfig['inspectorPort']; + d1BindingName?: string; persistState?: PluginConfig['persistState']; viteEnvironment: NonNullable; localQueueProducers?: LocalQueueProducers; finalizeAssets?: (clientDir: string, environment: BuildEnvironment | undefined) => void; }): Array { const root = process.cwd(); + const d1BindingName = options.d1BindingName ?? 'DB'; let workerConfig: WorkerConfig; let native: ReturnType; let pluginNative: ReturnType; @@ -113,7 +115,7 @@ export function cloudflare(options: { } } else if (environment.command === 'serve') { restoreLocalContainerBindings(pluginNative.worker); - restoreLocalD1Bindings(pluginNative.worker); + restoreLocalD1Binding(pluginNative.worker, d1BindingName); localQueueBindings = addLocalQueueProducers( pluginNative.worker, options.localQueueProducers, @@ -305,6 +307,7 @@ export function cloudflare(options: { }, }), root, + d1BindingName, options.localQueueProducers, ), output, diff --git a/packages/void/test/unit/local-d1.test.ts b/packages/void/test/unit/local-d1.test.ts index 8e6ca01515..f8de40eabd 100644 --- a/packages/void/test/unit/local-d1.test.ts +++ b/packages/void/test/unit/local-d1.test.ts @@ -10,7 +10,7 @@ import { materializeCloudflareConfig } from '../../src/cloudflare-config.ts'; import { voidPlugin } from '../../src/index.ts'; import { resolveLocalD1Target, writeLocalD1PersistState } from '../../src/local-d1.ts'; import { getApplicationTables } from '../../src/migrations/runner.ts'; -import { restoreLocalD1Bindings } from '../../src/plugin-cloudflare-config.ts'; +import { restoreLocalD1Binding } from '../../src/plugin-cloudflare-config.ts'; import { cloudflare } from '../../src/plugin-cloudflare.ts'; const roots: Array = []; @@ -239,18 +239,21 @@ describe('local D1 identity', () => { expect(getLocalD1Target(root).persistRoot).toBe(customPersistRoot); }); - it('fills omitted D1 binding IDs with the binding name locally without touching explicit or non-D1 bindings', () => { + it('fills only the migration target’s missing D1 ID and preserves secondary binding isolation', () => { const worker = { env: { - DB: { type: 'd1' as const, name: 'repro' }, + APP_DATABASE: { type: 'd1' as const, name: 'repro' }, + DB: { type: 'd1' as const, name: 'secondary' }, EXPLICIT: { type: 'd1' as const, name: 'explicit', id: 'custom-id' }, KV: { type: 'kv' as const, id: 'kv-id' }, }, }; - restoreLocalD1Bindings(worker); + restoreLocalD1Binding(worker, 'APP_DATABASE'); + restoreLocalD1Binding(worker, 'EXPLICIT'); - expect(worker.env.DB).toEqual({ type: 'd1', name: 'repro', id: 'DB' }); + expect(worker.env.APP_DATABASE).toEqual({ type: 'd1', name: 'repro', id: 'APP_DATABASE' }); + expect(worker.env.DB).toEqual({ type: 'd1', name: 'secondary' }); expect(worker.env.EXPLICIT).toEqual({ type: 'd1', name: 'explicit', id: 'custom-id' }); expect(worker.env.KV).toEqual({ type: 'kv', id: 'kv-id' }); });