Skip to content

Commit 6ecc74e

Browse files
authored
feat(wrangler): make resources identifier optional if x-provision flag is enabled (#7377)
1 parent 4f6f2ec commit 6ecc74e

File tree

12 files changed

+133
-31
lines changed

12 files changed

+133
-31
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
The `x-provision` experimental flag now skips validation of KV, R2, and D1 IDs in the configuration file.

packages/wrangler/src/__tests__/configuration.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import { readConfig } from "../config";
33
import { normalizeAndValidateConfig } from "../config/validation";
4+
import { run } from "../experimental-flags";
45
import { normalizeString } from "./helpers/normalize";
56
import { runInTempDir } from "./helpers/run-in-tmp";
67
import { writeWranglerConfig } from "./helpers/write-wrangler-config";
@@ -2324,6 +2325,26 @@ describe("normalizeAndValidateConfig()", () => {
23242325
- \\"kv_namespaces[4]\\" bindings should have a string \\"id\\" field but got {\\"binding\\":\\"VALID\\",\\"id\\":\\"\\"}."
23252326
`);
23262327
});
2328+
2329+
it("should allow the id field to be omitted when the RESOURCES_PROVISION experimental flag is enabled", () => {
2330+
const { diagnostics } = run(
2331+
{
2332+
RESOURCES_PROVISION: true,
2333+
FILE_BASED_REGISTRY: false,
2334+
},
2335+
() =>
2336+
normalizeAndValidateConfig(
2337+
{
2338+
kv_namespaces: [{ binding: "VALID" }],
2339+
} as unknown as RawConfig,
2340+
undefined,
2341+
{ env: undefined }
2342+
)
2343+
);
2344+
2345+
expect(diagnostics.hasWarnings()).toBe(false);
2346+
expect(diagnostics.hasErrors()).toBe(false);
2347+
});
23272348
});
23282349

23292350
it("should error if send_email.bindings are not valid", () => {
@@ -2452,6 +2473,26 @@ describe("normalizeAndValidateConfig()", () => {
24522473
- \\"d1_databases[4]\\" bindings must have a \\"database_id\\" field but got {\\"binding\\":\\"VALID\\",\\"id\\":\\"\\"}."
24532474
`);
24542475
});
2476+
2477+
it("should allow the database_id field to be omitted when the RESOURCES_PROVISION experimental flag is enabled", () => {
2478+
const { diagnostics } = run(
2479+
{
2480+
RESOURCES_PROVISION: true,
2481+
FILE_BASED_REGISTRY: false,
2482+
},
2483+
() =>
2484+
normalizeAndValidateConfig(
2485+
{
2486+
d1_databases: [{ binding: "VALID" }],
2487+
} as unknown as RawConfig,
2488+
undefined,
2489+
{ env: undefined }
2490+
)
2491+
);
2492+
2493+
expect(diagnostics.hasWarnings()).toBe(false);
2494+
expect(diagnostics.hasErrors()).toBe(false);
2495+
});
24552496
});
24562497

24572498
describe("[hyperdrive]", () => {
@@ -2746,6 +2787,26 @@ describe("normalizeAndValidateConfig()", () => {
27462787
- \\"r2_buckets[4]\\" bindings should have a string \\"bucket_name\\" field but got {\\"binding\\":\\"R2_BINDING_1\\",\\"bucket_name\\":\\"\\"}."
27472788
`);
27482789
});
2790+
2791+
it("should allow the bucket_name field to be omitted when the RESOURCES_PROVISION experimental flag is enabled", () => {
2792+
const { diagnostics } = run(
2793+
{
2794+
RESOURCES_PROVISION: true,
2795+
FILE_BASED_REGISTRY: false,
2796+
},
2797+
() =>
2798+
normalizeAndValidateConfig(
2799+
{
2800+
d1_databases: [{ binding: "VALID" }],
2801+
} as unknown as RawConfig,
2802+
undefined,
2803+
{ env: undefined }
2804+
)
2805+
);
2806+
2807+
expect(diagnostics.hasWarnings()).toBe(false);
2808+
expect(diagnostics.hasErrors()).toBe(false);
2809+
});
27492810
});
27502811

27512812
describe("[services]", () => {

packages/wrangler/src/api/dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface UnstableDevOptions {
3434
vars?: Record<string, string | Json>;
3535
kv?: {
3636
binding: string;
37-
id: string;
37+
id?: string;
3838
preview_id?: string;
3939
}[];
4040
durableObjects?: {
@@ -51,7 +51,7 @@ export interface UnstableDevOptions {
5151
}[];
5252
r2?: {
5353
binding: string;
54-
bucket_name: string;
54+
bucket_name?: string;
5555
preview_bucket_name?: string;
5656
}[];
5757
ai?: {

packages/wrangler/src/config/environment.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ export interface EnvironmentNonInheritable {
476476
/** The binding name used to refer to the KV Namespace */
477477
binding: string;
478478
/** The ID of the KV namespace */
479-
id: string;
479+
id?: string;
480480
/** The ID of the KV namespace used during `wrangler dev` */
481481
preview_id?: string;
482482
}[];
@@ -565,7 +565,7 @@ export interface EnvironmentNonInheritable {
565565
/** The binding name used to refer to the R2 bucket in the Worker. */
566566
binding: string;
567567
/** The name of this R2 bucket at the edge. */
568-
bucket_name: string;
568+
bucket_name?: string;
569569
/** The preview name of this R2 bucket at the edge. */
570570
preview_bucket_name?: string;
571571
/** The jurisdiction that the bucket exists in. Default if not present. */
@@ -585,9 +585,9 @@ export interface EnvironmentNonInheritable {
585585
/** The binding name used to refer to the D1 database in the Worker. */
586586
binding: string;
587587
/** The name of this D1 database. */
588-
database_name: string;
588+
database_name?: string;
589589
/** The UUID of this D1 database (not required). */
590-
database_id: string;
590+
database_id?: string;
591591
/** The UUID of this D1 database for Wrangler Dev (if specified). */
592592
preview_database_id?: string;
593593
/** The name of the migrations table for this D1 database (defaults to 'd1_migrations'). */

packages/wrangler/src/config/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ export function findWranglerConfig(
208208
);
209209
}
210210

211-
function addLocalSuffix(id: string, local: boolean = false) {
211+
function addLocalSuffix(id: string | undefined, local: boolean = false) {
212+
if (!id) {
213+
return local ? "(local)" : "(remote)";
214+
}
215+
212216
return `${id}${local ? " (local)" : ""}`;
213217
}
214218

@@ -403,13 +407,14 @@ export function printBindings(
403407
name: friendlyBindingNames.d1_databases,
404408
entries: d1_databases.map(
405409
({ binding, database_name, database_id, preview_database_id }) => {
406-
let databaseValue = `${database_id}`;
407-
if (database_name) {
408-
databaseValue = `${database_name} (${database_id})`;
409-
}
410+
let databaseValue =
411+
database_id && database_name
412+
? `${database_name} (${database_id})`
413+
: database_id ?? database_name;
414+
410415
//database_id is local when running `wrangler dev --local`
411416
if (preview_database_id && database_id !== "local") {
412-
databaseValue += `, Preview: (${preview_database_id})`;
417+
databaseValue = `${databaseValue ? `${databaseValue}, ` : ""}Preview: (${preview_database_id})`;
413418
}
414419
return {
415420
key: binding,

packages/wrangler/src/config/validation.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "node:assert";
22
import path from "node:path";
33
import TOML from "@iarna/toml";
44
import { dedent } from "ts-dedent";
5+
import { getFlag } from "../experimental-flags";
56
import { Diagnostics } from "./diagnostics";
67
import {
78
all,
@@ -2433,8 +2434,10 @@ const validateKVBinding: ValidatorFn = (diagnostics, field, value) => {
24332434
isValid = false;
24342435
}
24352436
if (
2436-
!isRequiredProperty(value, "id", "string") ||
2437-
(value as { id: string }).id.length === 0
2437+
getFlag("RESOURCES_PROVISION")
2438+
? !isOptionalProperty(value, "id", "string") ||
2439+
(value.id !== undefined && value.id.length === 0)
2440+
: !isRequiredProperty(value, "id", "string") || value.id.length === 0
24382441
) {
24392442
diagnostics.errors.push(
24402443
`"${field}" bindings should have a string "id" field but got ${JSON.stringify(
@@ -2595,8 +2598,11 @@ const validateR2Binding: ValidatorFn = (diagnostics, field, value) => {
25952598
isValid = false;
25962599
}
25972600
if (
2598-
!isRequiredProperty(value, "bucket_name", "string") ||
2599-
(value as { bucket_name: string }).bucket_name.length === 0
2601+
getFlag("RESOURCES_PROVISION")
2602+
? !isOptionalProperty(value, "bucket_name", "string") ||
2603+
(value.bucket_name !== undefined && value.bucket_name.length === 0)
2604+
: !isRequiredProperty(value, "bucket_name", "string") ||
2605+
value.bucket_name.length === 0
26002606
) {
26012607
diagnostics.errors.push(
26022608
`"${field}" bindings should have a string "bucket_name" field but got ${JSON.stringify(
@@ -2653,9 +2659,11 @@ const validateD1Binding: ValidatorFn = (diagnostics, field, value) => {
26532659
isValid = false;
26542660
}
26552661
if (
2656-
// TODO: allow name only, where we look up the ID dynamically
2657-
// !isOptionalProperty(value, "database_name", "string") &&
2658-
!isRequiredProperty(value, "database_id", "string")
2662+
getFlag("RESOURCES_PROVISION")
2663+
? !isOptionalProperty(value, "database_id", "string")
2664+
: // TODO: allow name only, where we look up the ID dynamically
2665+
// !isOptionalProperty(value, "database_name", "string") &&
2666+
!isRequiredProperty(value, "database_id", "string")
26592667
) {
26602668
diagnostics.errors.push(
26612669
`"${field}" bindings must have a "database_id" field but got ${JSON.stringify(

packages/wrangler/src/d1/utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export function getDatabaseInfoFromConfig(
1414
d1Database.database_id &&
1515
(name === d1Database.database_name || name === d1Database.binding)
1616
) {
17+
if (!d1Database.database_name) {
18+
throw new UserError(
19+
`${name} bindings must have a "database_name" field`
20+
);
21+
}
22+
1723
return {
1824
uuid: d1Database.database_id,
1925
previewDatabaseUuid: d1Database.preview_database_id,

packages/wrangler/src/deployment-bundle/create-worker-upload-form.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "node:assert";
22
import { readFileSync } from "node:fs";
33
import path from "node:path";
44
import { File, FormData } from "undici";
5+
import { UserError } from "../errors";
56
import { handleUnsafeCapnp } from "./capnp";
67
import type { Observability } from "../config/environment";
78
import type {
@@ -225,6 +226,9 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
225226
});
226227

227228
bindings.kv_namespaces?.forEach(({ id, binding }) => {
229+
if (id === undefined) {
230+
throw new UserError(`${binding} bindings must have an "id" field`);
231+
}
228232
metadataBindings.push({
229233
name: binding,
230234
type: "kv_namespace",
@@ -275,6 +279,11 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
275279
});
276280

277281
bindings.r2_buckets?.forEach(({ binding, bucket_name, jurisdiction }) => {
282+
if (bucket_name === undefined) {
283+
throw new UserError(
284+
`${binding} bindings must have a "bucket_name" field`
285+
);
286+
}
278287
metadataBindings.push({
279288
name: binding,
280289
type: "r2_bucket",
@@ -285,6 +294,11 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
285294

286295
bindings.d1_databases?.forEach(
287296
({ binding, database_id, database_internal_env }) => {
297+
if (database_id === undefined) {
298+
throw new UserError(
299+
`${binding} bindings must have a "database_id" field`
300+
);
301+
}
288302
metadataBindings.push({
289303
name: binding,
290304
type: "d1",

packages/wrangler/src/deployment-bundle/worker.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface CfVars {
8181
*/
8282
export interface CfKvNamespace {
8383
binding: string;
84-
id: string;
84+
id?: string;
8585
}
8686

8787
/**
@@ -167,15 +167,15 @@ export interface CfQueue {
167167

168168
export interface CfR2Bucket {
169169
binding: string;
170-
bucket_name: string;
170+
bucket_name?: string;
171171
jurisdiction?: string;
172172
}
173173

174174
// TODO: figure out if this is duplicated in packages/wrangler/src/config/environment.ts
175175
export interface CfD1Database {
176176
binding: string;
177-
database_id: string;
178-
database_name: string;
177+
database_id?: string;
178+
database_name?: string;
179179
preview_database_id?: string;
180180
database_internal_env?: string;
181181
migrations_table?: string;

packages/wrangler/src/dev.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ import type {
4646
Route,
4747
Rule,
4848
} from "./config/environment";
49-
import type { CfModule, CfWorkerInit } from "./deployment-bundle/worker";
49+
import type {
50+
CfKvNamespace,
51+
CfModule,
52+
CfWorkerInit,
53+
} from "./deployment-bundle/worker";
5054
import type { WorkerRegistry } from "./dev-registry";
5155
import type { CfAccount } from "./dev/create-worker-preview";
5256
import type { LoggerLevel } from "./logger";
@@ -394,7 +398,7 @@ export type AdditionalDevProps = {
394398
vars?: Record<string, string | Json>;
395399
kv?: {
396400
binding: string;
397-
id: string;
401+
id?: string;
398402
preview_id?: string;
399403
}[];
400404
durableObjects?: {
@@ -411,7 +415,7 @@ export type AdditionalDevProps = {
411415
}[];
412416
r2?: {
413417
binding: string;
414-
bucket_name: string;
418+
bucket_name?: string;
415419
preview_bucket_name?: string;
416420
jurisdiction?: string;
417421
}[];
@@ -956,7 +960,7 @@ export function getBindings(
956960
* config in `wrangler.toml`.
957961
*/
958962
// merge KV bindings
959-
const kvConfig = (configParam.kv_namespaces || []).map(
963+
const kvConfig = (configParam.kv_namespaces || []).map<CfKvNamespace>(
960964
({ binding, preview_id, id }) => {
961965
// In remote `dev`, we make folks use a separate kv namespace called
962966
// `preview_id` instead of `id` so that they don't

0 commit comments

Comments
 (0)