From 5be0c1eef72ff602f457d4d64d2a28cbdc096a1d Mon Sep 17 00:00:00 2001 From: Jacob M-G Evans Date: Mon, 27 Jun 2022 14:44:42 -0500 Subject: [PATCH] bugfix: Allow route setting to be Previously Wrangler1 behavior had allowed for . To keep parity it will be possible to set in the config file and represent not setting a route, while providing a warning. resolves #1329 --- .changeset/thirty-eagles-add.md | 8 ++++++ .../wrangler/src/__tests__/publish.test.ts | 27 +++++++++++++++++++ packages/wrangler/src/config/validation.ts | 25 ++++++++++++++--- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 .changeset/thirty-eagles-add.md diff --git a/.changeset/thirty-eagles-add.md b/.changeset/thirty-eagles-add.md new file mode 100644 index 000000000000..ec652452d4eb --- /dev/null +++ b/.changeset/thirty-eagles-add.md @@ -0,0 +1,8 @@ +--- +"wrangler": patch +--- + +bugfix: Allow route setting to be `""` +Previously Wrangler1 behavior had allowed for `route = ""`. To keep parity it will be possible to set `route = ""` in the config file and represent not setting a route, while providing a warning. + +resolves #1329 diff --git a/packages/wrangler/src/__tests__/publish.test.ts b/packages/wrangler/src/__tests__/publish.test.ts index 96564191dd22..a8129a50b286 100644 --- a/packages/wrangler/src/__tests__/publish.test.ts +++ b/packages/wrangler/src/__tests__/publish.test.ts @@ -495,6 +495,33 @@ describe("publish", () => { await runWrangler("publish ./index"); }); + it("should publish with an empty string route", async () => { + writeWranglerToml({ + route: "", + }); + writeWorkerSource(); + mockUpdateWorkerRequest({ enabled: false }); + mockUploadWorkerRequest({ expectedType: "esm" }); + mockSubDomainRequest(); + mockPublishRoutesRequest({ routes: [] }); + await runWrangler("publish ./index"); + expect(std).toMatchInlineSnapshot(` + Object { + "debug": "", + "err": "", + "out": "Total Upload: 0xx KiB / gzip: 0xx KiB + Uploaded test-name (TIMINGS) + Published test-name (TIMINGS) + test-name.test-sub-domain.workers.dev", + "warn": "▲ [WARNING] Processing wrangler.toml configuration: + + - Route assignment \\"\\" will be ignored. + + ", + } + `); + }); + it("should publish to a route with a pattern/{zone_id|zone_name} combo", async () => { writeWranglerToml({ routes: [ diff --git a/packages/wrangler/src/config/validation.ts b/packages/wrangler/src/config/validation.ts index b872362f2c5c..e2ce4ec349c9 100644 --- a/packages/wrangler/src/config/validation.ts +++ b/packages/wrangler/src/config/validation.ts @@ -670,6 +670,25 @@ function isValidRouteValue(item: unknown): boolean { return false; } +/** + * Normalize empty string to `undefined` by mutating rawEnv.route value. + * As part of backward compatibility with Wrangler1 converting empty string to `undefined` + */ +function mutateEmptyStringRouteValue( + rawEnv: RawEnvironment, + diagnostics: Diagnostics +): RawEnvironment { + if (rawEnv["route"] === "") { + diagnostics.warnings.push( + `Route assignment ${JSON.stringify(rawEnv["route"])} will be ignored.` + ); + rawEnv["route"] = undefined; + return rawEnv; + } + + return rawEnv; +} + /** * Validate that the field is a route. */ @@ -716,7 +735,7 @@ const isRouteArray: ValidatorFn = (diagnostics, field, value) => { return invalidRoutes.length === 0; }; -function validateRoute( +function normalizeAndValidateRoute( diagnostics: Diagnostics, topLevelEnv: Environment | undefined, rawEnv: RawEnvironment @@ -724,7 +743,7 @@ function validateRoute( return inheritable( diagnostics, topLevelEnv, - rawEnv, + mutateEmptyStringRouteValue(rawEnv, diagnostics), "route", isRoute, undefined @@ -795,7 +814,7 @@ function normalizeAndValidateEnvironment( experimental(diagnostics, rawEnv, "unsafe"); experimental(diagnostics, rawEnv, "services"); - const route = validateRoute(diagnostics, topLevelEnv, rawEnv); + const route = normalizeAndValidateRoute(diagnostics, topLevelEnv, rawEnv); const routes = validateRoutes(diagnostics, topLevelEnv, rawEnv);