Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Allow route setting to be "" #1352

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/thirty-eagles-add.md
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this will only fail if we actually make a request to publish routes with a non-empty array of routes. If the API request is never made, this would pass whatever you put for routes here.

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: [
Expand Down
25 changes: 22 additions & 3 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -716,15 +735,15 @@ const isRouteArray: ValidatorFn = (diagnostics, field, value) => {
return invalidRoutes.length === 0;
};

function validateRoute(
function normalizeAndValidateRoute(
diagnostics: Diagnostics,
topLevelEnv: Environment | undefined,
rawEnv: RawEnvironment
): Config["route"] {
return inheritable(
diagnostics,
topLevelEnv,
rawEnv,
mutateEmptyStringRouteValue(rawEnv, diagnostics),
"route",
isRoute,
undefined
Expand Down Expand Up @@ -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);

Expand Down