Skip to content

Commit 813a714

Browse files
Brooooooklynclaude
andcommitted
fix(config): pin the default compatibility date without creating void.json
When a project had no config file and no date, the first dev server, build or deploy created a new void.json, a format the docs call legacy. Adding void.config.ts later then failed with "Found both". Now the date goes where the project already keeps its config: - a new project gets a generated void.config.ts with only the date, as `void init` writes it, and `.void-wrangler.jsonc` is added to .gitignore; - framework apps (Astro, SvelteKit, Nuxt, Analog) and projects with a root wrangler.json(c) get it in the root Wrangler file their adapter reads; - a project with wrangler.toml keeps getting it in void.json, because void.config.ts would hide the toml's bindings. A date already in void.json is still read, so existing projects keep their date. The .gitignore helper moves from `void migrate` to cloudflare-config.ts so both paths share it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 170d14f commit 813a714

7 files changed

Lines changed: 154 additions & 53 deletions

File tree

‎docs/reference/config.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ Void-managed WebSocket route files (`*.ws.ts`) are Cloudflare-only because they
297297

298298
### `worker`
299299

300-
Curated Cloudflare Workers configuration. `void init` pins `compatibility_date` here. Binding arrays such as `d1_databases`, `kv_namespaces`, and `r2_buckets` go in [`cloudflare`](#cloudflare), where Void preserves custom settings and resolved IDs during deployment.
300+
Curated Cloudflare Workers configuration. `void init` pins `compatibility_date` here. If a project has no config file and no date, the first dev server or build creates `void.config.ts` with only this date and adds `.void-wrangler.jsonc` to `.gitignore`. Astro, SvelteKit, Nuxt, and Analog apps get the date in a root `wrangler.jsonc` instead, which their adapter reads by default. A project with a `wrangler.toml` gets it in `void.json`. Binding arrays such as `d1_databases`, `kv_namespaces`, and `r2_buckets` go in [`cloudflare`](#cloudflare), where Void preserves custom settings and resolved IDs during deployment.
301301

302302
| Field | Type | Description |
303303
| --------------------- | ---------- | -------------------------------------- |

‎packages/void/src/cli/migrate.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { log } from '@voidzero-dev/vite-plus-prompts';
1313
import { parseTree, type Node } from 'jsonc-parser';
1414
import { join } from 'pathslash';
1515
import {
16+
ignoreMaterializedCloudflareConfig,
1617
seedCloudflareConfig,
1718
stashCloudflareDeployPointer,
1819
VOID_LOCK_FILE,
@@ -96,18 +97,6 @@ function isOrphanedCompatibilityConfig(
9697
}
9798
}
9899

99-
function ignoreGeneratedCloudflareConfig(root: string): void {
100-
const path = join(root, '.gitignore');
101-
const source = existsSync(path) ? readFileSync(path, 'utf8') : '';
102-
if (source.split(/\r?\n/).some((line) => /^\/?\.void-wrangler\.jsonc$/.test(line.trim()))) {
103-
return;
104-
}
105-
writeFileSync(
106-
path,
107-
`${source}${source && !source.endsWith('\n') ? '\n' : ''}.void-wrangler.jsonc\n`,
108-
);
109-
}
110-
111100
/** Move Void and Cloudflare JSON/JSONC settings into a typed, checked-in project config. */
112101
export function migrateLegacyVoidConfig(root: string): boolean {
113102
const legacyPath = join(root, LEGACY_FILE);
@@ -226,7 +215,7 @@ export function migrateLegacyVoidConfig(root: string): boolean {
226215
if (wranglerName) {
227216
unlinkSync(join(root, wranglerName));
228217
}
229-
ignoreGeneratedCloudflareConfig(root);
218+
ignoreMaterializedCloudflareConfig(root);
230219
stashCloudflareDeployPointer(root);
231220
return true;
232221
} finally {

‎packages/void/src/cli/wrangler.ts‎

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ import {
2020
} from 'jsonc-parser';
2121
import { basename, dirname, join, normalize, resolve } from 'pathslash';
2222
import {
23+
ignoreMaterializedCloudflareConfig,
2324
materializeCloudflareConfig,
2425
MATERIALIZED_CLOUDFLARE_FILE,
2526
persistCloudflareConfig,
2627
} from '../cloudflare-config.ts';
2728
import { TYPED_CONFIG_FILE } from '../config-loader.ts';
29+
import { writeVoidProjectConfig } from '../config-write.ts';
2830
import { readConfig, type VoidConfig } from '../config.ts';
2931
import type { DurableObjectModule } from '../durable/scan.ts';
3032
import type { InferredBindings } from '../inference/walk.ts';
@@ -209,7 +211,6 @@ export type WranglerCompat = {
209211
};
210212

211213
type WranglerCompatFields = {
212-
voidConfig: VoidConfig | undefined;
213214
voidDate: string | undefined;
214215
voidFlags: Array<string> | undefined;
215216
wranglerDate: string | undefined;
@@ -372,17 +373,23 @@ export function readWranglerCompat(
372373
return resolveWranglerCompat(readWranglerCompatFields(root, config, buildOutputDir));
373374
}
374375

375-
/** Like `readWranglerCompat()`, but pins the default date when no source sets one. */
376+
/**
377+
* Like `readWranglerCompat()`, but pins the default date when no source sets one.
378+
* With `rootWrangler`, a project with no config gets the date in a root wrangler.jsonc
379+
* instead of a new void.config.ts, so a framework adapter that reads the root file by
380+
* default still finds it.
381+
*/
376382
export function ensureWranglerCompat(
377383
root: string,
378384
config?: VoidConfig,
379385
buildOutputDir?: string,
386+
options?: { rootWrangler?: boolean },
380387
): WranglerCompat {
381388
const fields = readWranglerCompatFields(root, config, buildOutputDir);
382389
const hasExplicitDate = fields.voidDate ?? fields.wranglerDate ?? fields.buildDate;
383390
const defaultDate = hasExplicitDate
384391
? undefined
385-
: ensureVoidCompatibilityDate(root, fields.voidConfig);
392+
: ensureVoidCompatibilityDate(root, options?.rootWrangler === true);
386393
return resolveWranglerCompat(fields, defaultDate ? { defaultDate } : undefined);
387394
}
388395

@@ -442,7 +449,6 @@ function readWranglerCompatFields(
442449
}
443450

444451
return {
445-
voidConfig,
446452
voidDate: voidConfig?.worker?.compatibility_date,
447453
voidFlags: voidConfig?.worker?.compatibility_flags,
448454
wranglerDate,
@@ -489,29 +495,41 @@ function readVoidConfigIfExists(root: string): VoidConfig | undefined {
489495
: undefined;
490496
}
491497

492-
function ensureVoidCompatibilityDate(root: string, config?: VoidConfig): string {
498+
function ensureVoidCompatibilityDate(root: string, rootWrangler: boolean): string {
493499
if (existsSync(join(root, TYPED_CONFIG_FILE))) {
494500
writeProvisionedWranglerConfig(root, { compatibilityDate: LATEST_KNOWN_COMPAT_DATE });
495501
return LATEST_KNOWN_COMPAT_DATE;
496502
}
497503
const filePath = join(root, VOID_CONFIG_FILE);
498-
let nextConfig: VoidConfig = {};
504+
if (!existsSync(filePath)) {
505+
if (existsSync(join(root, 'wrangler.toml'))) {
506+
// void.config.ts would point the Cloudflare plugin away from wrangler.toml and drop
507+
// its bindings, so keep the date in void.json.
508+
const pinned: VoidConfig = { worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE } };
509+
writeFileSync(filePath, `${JSON.stringify(pinned, null, 2)}\n`);
510+
} else if (rootWrangler || projectWranglerPaths(root).some((path) => existsSync(path))) {
511+
// A root Wrangler config cannot sit next to void.config.ts, so pin the date in it.
512+
writeProvisionedWranglerConfig(root, { compatibilityDate: LATEST_KNOWN_COMPAT_DATE });
513+
} else {
514+
// New projects get the date in void.config.ts, as `void init` writes it.
515+
writeVoidProjectConfig(root, { worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE } });
516+
ignoreMaterializedCloudflareConfig(root);
517+
}
518+
return LATEST_KNOWN_COMPAT_DATE;
519+
}
499520

500-
if (existsSync(filePath)) {
501-
try {
502-
const parsed = JSON.parse(readFileSync(filePath, 'utf-8'));
503-
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
504-
throw new Error(`config: ${VOID_CONFIG_FILE} must be a JSON object.`);
505-
}
506-
nextConfig = parsed as VoidConfig;
507-
} catch (error) {
508-
if (error instanceof SyntaxError) {
509-
throw new Error(`config: Invalid JSON in ${VOID_CONFIG_FILE}.`);
510-
}
511-
throw error;
521+
let nextConfig: VoidConfig;
522+
try {
523+
const parsed = JSON.parse(readFileSync(filePath, 'utf-8'));
524+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
525+
throw new Error(`config: ${VOID_CONFIG_FILE} must be a JSON object.`);
526+
}
527+
nextConfig = parsed as VoidConfig;
528+
} catch (error) {
529+
if (error instanceof SyntaxError) {
530+
throw new Error(`config: Invalid JSON in ${VOID_CONFIG_FILE}.`);
512531
}
513-
} else if (config) {
514-
nextConfig = { ...config };
532+
throw error;
515533
}
516534

517535
nextConfig.worker = {

‎packages/void/src/cloudflare-config.ts‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
1+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
22
import { isDeepStrictEqual } from 'node:util';
33
import { isRecord } from '@void/deploy-core';
44
import { join } from 'pathslash';
@@ -11,6 +11,21 @@ export const MATERIALIZED_CLOUDFLARE_FILE = '.void-wrangler.jsonc';
1111
export const VOID_LOCK_FILE = 'void.lock.json';
1212
const PREVIEW_POINTER_FILE = 'preview-deploy-config.json';
1313

14+
/** Add the generated Cloudflare config to `.gitignore` unless a line already ignores it. */
15+
export function ignoreMaterializedCloudflareConfig(root: string): void {
16+
const path = join(root, '.gitignore');
17+
const source = existsSync(path) ? readFileSync(path, 'utf8') : '';
18+
const ignored = source.split('\n').some((line) => {
19+
const entry = line.trim();
20+
return entry === MATERIALIZED_CLOUDFLARE_FILE || entry === `/${MATERIALIZED_CLOUDFLARE_FILE}`;
21+
});
22+
if (ignored) {
23+
return;
24+
}
25+
const separator = source && !source.endsWith('\n') ? '\n' : '';
26+
writeFileSync(path, `${source}${separator}${MATERIALIZED_CLOUDFLARE_FILE}\n`);
27+
}
28+
1429
type JsonObject = Record<string, unknown>;
1530
type CloudflareState = {
1631
version: 1;

‎packages/void/src/plugin-framework.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,18 @@ export function createFrameworkPlugins(input: {
8686
}
8787
};
8888

89+
// Adapters read the root Wrangler file by default, so a project with no config gets
90+
// the date there instead of in a new void.config.ts.
91+
const ensureFrameworkCompat = () =>
92+
ensureWranglerCompat(root, config, undefined, { rootWrangler: true });
93+
8994
// Ensure required compat flags are written before framework adapters
9095
// initialize Cloudflare dev runtime / Miniflare.
9196
const wranglerCompatPlugin: Plugin = {
9297
name: 'void:wrangler-compat',
9398
enforce: 'pre',
9499
config(_, env) {
95-
syncFrameworkCompat(ensureWranglerCompat(root, config));
100+
syncFrameworkCompat(ensureFrameworkCompat());
96101
if (env.command !== 'serve') {
97102
return;
98103
}
@@ -102,10 +107,10 @@ export function createFrameworkPlugins(input: {
102107
}
103108
},
104109
buildStart() {
105-
syncFrameworkCompat(ensureWranglerCompat(root, config));
110+
syncFrameworkCompat(ensureFrameworkCompat());
106111
},
107112
configureServer() {
108-
syncFrameworkCompat(ensureWranglerCompat(root, config));
113+
syncFrameworkCompat(ensureFrameworkCompat());
109114
},
110115
};
111116

‎packages/void/test/unit/plugin-inference.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ describe('detectFramework', () => {
578578
try {
579579
voidPlugin();
580580
expect(existsSync(join(root, 'void.json'))).toBe(false);
581+
expect(existsSync(join(root, 'void.config.ts'))).toBe(false);
581582
} finally {
582583
cwd.mockRestore();
583584
}
@@ -601,16 +602,24 @@ describe('detectFramework', () => {
601602
) => void;
602603

603604
expect(compatPlugin).toBeTruthy();
604-
expect(existsSync(join(root, 'void.json'))).toBe(false);
605+
expect(existsSync(join(root, 'void.config.ts'))).toBe(false);
605606

606607
config.call({}, {}, { command: 'serve', mode: 'development' } as import('vite').ConfigEnv);
607608

608-
expect(JSON.parse(readFileSync(join(root, 'void.json'), 'utf-8'))).toEqual({
609-
worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE },
610-
});
609+
// The adapter reads the root Wrangler file by default, so the date and the dev
610+
// flags land there, not in a new void.config.ts.
611+
expect(existsSync(join(root, 'void.json'))).toBe(false);
612+
expect(existsSync(join(root, 'void.config.ts'))).toBe(false);
613+
expect(existsSync(join(root, '.void-wrangler.jsonc'))).toBe(false);
611614
expect(JSON.parse(readFileSync(join(root, 'wrangler.jsonc'), 'utf-8'))).toMatchObject({
612615
compatibility_date: LATEST_KNOWN_COMPAT_DATE,
616+
compatibility_flags: ['nodejs_als'],
613617
});
618+
619+
// A second run reads the pinned date and leaves the root file alone.
620+
const written = readFileSync(join(root, 'wrangler.jsonc'), 'utf-8');
621+
config.call({}, {}, { command: 'serve', mode: 'development' } as import('vite').ConfigEnv);
622+
expect(readFileSync(join(root, 'wrangler.jsonc'), 'utf-8')).toBe(written);
614623
} finally {
615624
cwd.mockRestore();
616625
}
@@ -650,7 +659,7 @@ describe('detectFramework', () => {
650659
}
651660
});
652661

653-
it('persists default void.json compatibility during build startup', () => {
662+
it('persists default compatibility in void.config.ts during build startup', () => {
654663
const root = createTempRoot();
655664
writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));
656665
const cwd = vi.spyOn(process, 'cwd').mockReturnValue(root);
@@ -664,7 +673,9 @@ describe('detectFramework', () => {
664673

665674
expect(persistPlugin).toBeTruthy();
666675
buildStart.call({});
667-
expect(JSON.parse(readFileSync(join(root, 'void.json'), 'utf-8'))).toEqual({
676+
expect(existsSync(join(root, 'void.json'))).toBe(false);
677+
expect(existsSync(join(root, 'wrangler.jsonc'))).toBe(false);
678+
expect(readConfig(root)).toEqual({
668679
worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE },
669680
});
670681
} finally {

‎packages/void/test/unit/wrangler.test.ts‎

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,25 +267,87 @@ describe('readWranglerCompat', () => {
267267
});
268268

269269
describe('ensureWranglerCompat', () => {
270-
it('pins the latest known-good compatibility date in void.json when none exists', () => {
270+
it('pins the latest known-good compatibility date in a new void.config.ts', () => {
271271
rmSync(tmpDir, { recursive: true, force: true });
272272
mkdirSync(tmpDir, { recursive: true });
273-
expect(ensureWranglerCompat(tmpDir)).toEqual({
273+
expect(ensureWranglerCompat(tmpDir, readConfig(tmpDir))).toEqual({
274274
compatibilityDate: LATEST_KNOWN_COMPAT_DATE,
275275
});
276-
expect(JSON.parse(readFileSync(join(tmpDir, 'void.json'), 'utf-8'))).toEqual({
276+
expect(existsSync(join(tmpDir, 'void.json'))).toBe(false);
277+
expect(existsSync(join(tmpDir, 'wrangler.jsonc'))).toBe(false);
278+
expect(existsSync(join(tmpDir, 'void.lock.json'))).toBe(false);
279+
expect(readRaw('void.config.ts')).toBe(
280+
"// Generated by void init.\nimport { defineConfig } from 'void/config';\n\n" +
281+
`export default defineConfig({\n "worker": {\n "compatibility_date": "${LATEST_KNOWN_COMPAT_DATE}"\n }\n});\n`,
282+
);
283+
expect(readConfig(tmpDir)).toEqual({
277284
worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE },
278285
});
286+
// The generated Cloudflare config that void.config.ts implies stays out of Git.
287+
expect(readRaw('.gitignore')).toBe('.void-wrangler.jsonc\n');
288+
// The date is now read from void.config.ts, so nothing is written again.
289+
const written = readRaw('void.config.ts');
290+
expect(ensureWranglerCompat(tmpDir)).toEqual({ compatibilityDate: LATEST_KNOWN_COMPAT_DATE });
291+
expect(readRaw('void.config.ts')).toBe(written);
279292
});
280293

281-
it('tells the user how to resolve the pinned void.json when void.config.ts is added later', () => {
294+
it('adds the generated Cloudflare config to an existing .gitignore once', () => {
295+
setup('.gitignore', 'node_modules');
296+
ensureWranglerCompat(tmpDir);
297+
expect(readRaw('.gitignore')).toBe('node_modules\n.void-wrangler.jsonc\n');
298+
299+
rmSync(join(tmpDir, 'void.config.ts'));
300+
writeFileSync(join(tmpDir, '.gitignore'), '/.void-wrangler.jsonc\r\n');
301+
ensureWranglerCompat(tmpDir);
302+
expect(readRaw('.gitignore')).toBe('/.void-wrangler.jsonc\r\n');
303+
});
304+
305+
it('keeps a wrangler.toml project on void.json so the toml stays in use', () => {
306+
setup('wrangler.toml', 'name = "app"\n');
307+
expect(ensureWranglerCompat(tmpDir)).toEqual({ compatibilityDate: LATEST_KNOWN_COMPAT_DATE });
308+
expect(existsSync(join(tmpDir, 'void.config.ts'))).toBe(false);
309+
expect(existsSync(join(tmpDir, 'wrangler.jsonc'))).toBe(false);
310+
expect(existsSync(join(tmpDir, '.void-wrangler.jsonc'))).toBe(false);
311+
expect(JSON.parse(readRaw('void.json'))).toEqual({
312+
worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE },
313+
});
314+
// The next run reads the date from void.json and still adds no void.config.ts.
315+
expect(ensureWranglerCompat(tmpDir)).toEqual({ compatibilityDate: LATEST_KNOWN_COMPAT_DATE });
316+
expect(existsSync(join(tmpDir, 'void.config.ts'))).toBe(false);
317+
});
318+
319+
it('pins the date in a new root wrangler.jsonc when asked to keep the root Wrangler file', () => {
282320
rmSync(tmpDir, { recursive: true, force: true });
283321
mkdirSync(tmpDir, { recursive: true });
284-
ensureWranglerCompat(tmpDir);
285-
writeFileSync(join(tmpDir, 'void.config.ts'), 'export default {};\n');
286-
expect(() => readConfig(tmpDir)).toThrow(
287-
'config: Found both void.config.ts and void.json. Move the settings from void.json into void.config.ts, then delete void.json.',
288-
);
322+
expect(ensureWranglerCompat(tmpDir, undefined, undefined, { rootWrangler: true })).toEqual({
323+
compatibilityDate: LATEST_KNOWN_COMPAT_DATE,
324+
});
325+
expect(existsSync(join(tmpDir, 'void.config.ts'))).toBe(false);
326+
expect(existsSync(join(tmpDir, 'void.json'))).toBe(false);
327+
expect(existsSync(join(tmpDir, '.gitignore'))).toBe(false);
328+
expect(parseRaw('wrangler.jsonc')).toEqual({ compatibility_date: LATEST_KNOWN_COMPAT_DATE });
329+
});
330+
331+
it('writes only the date to void.config.ts, not the passed config', () => {
332+
rmSync(tmpDir, { recursive: true, force: true });
333+
mkdirSync(tmpDir, { recursive: true });
334+
ensureWranglerCompat(tmpDir, { database: 'pg', routing: { isr: true } } as VoidConfig);
335+
expect(readConfig(tmpDir)).toEqual({
336+
worker: { compatibility_date: LATEST_KNOWN_COMPAT_DATE },
337+
});
338+
});
339+
340+
it('pins the date in an existing root Wrangler config instead of adding void.config.ts', () => {
341+
setup('wrangler.jsonc', '{\n // app name\n "name": "app"\n}\n');
342+
expect(ensureWranglerCompat(tmpDir)).toEqual({ compatibilityDate: LATEST_KNOWN_COMPAT_DATE });
343+
expect(existsSync(join(tmpDir, 'void.config.ts'))).toBe(false);
344+
expect(existsSync(join(tmpDir, 'void.json'))).toBe(false);
345+
expect(readRaw('wrangler.jsonc')).toContain('// app name');
346+
expect(parseRaw('wrangler.jsonc')).toEqual({
347+
name: 'app',
348+
compatibility_date: LATEST_KNOWN_COMPAT_DATE,
349+
});
350+
expect(() => readConfig(tmpDir)).not.toThrow();
289351
});
290352

291353
it('preserves existing void.json fields when pinning compatibility_date', () => {
@@ -309,6 +371,7 @@ describe('ensureWranglerCompat', () => {
309371
compatibility_date: LATEST_KNOWN_COMPAT_DATE,
310372
},
311373
});
374+
expect(existsSync(join(tmpDir, 'void.config.ts'))).toBe(false);
312375
});
313376
});
314377

0 commit comments

Comments
 (0)