Skip to content

Commit

Permalink
fix(C3): Use today's date for compat date when the latest workerd rel…
Browse files Browse the repository at this point in the history
…ease date is in the future (#7891)

Fixes an issue where deployments would fail when there is a workerd
release on the same day due to workerd releases having a date in the
future because Workers does not allow using a compat date in the future.
  • Loading branch information
jahands authored Jan 24, 2025
1 parent 248bdb2 commit a7965c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/heavy-otters-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"create-cloudflare": patch
---

fix: Use today's date for compat date when the latest workerd release date is in the future

Fixes an issue where deployments would fail when there is a workerd release on the same day due to workerd releases having a date in the future because Workers does not allow using a compat date in the future.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getWorkerdCompatibilityDate,
} from "helpers/compatDate";
import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from "undici";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, test, vi } from "vitest";
import { createTestContext } from "../../__tests__/helpers";
import { mockSpinner, mockWorkersTypesDirectory } from "./mocks";

Expand All @@ -29,6 +29,7 @@ describe("Compatibility Date Helpers", () => {
afterEach(() => {
agent.assertNoPendingInterceptors();
setGlobalDispatcher(originalDispatcher);
vi.useRealTimers();
});

const mockRegistryFetch = (latest: string) => {
Expand Down Expand Up @@ -62,7 +63,7 @@ describe("Compatibility Date Helpers", () => {

const date = await getWorkerdCompatibilityDate();

const fallbackDate = "2023-05-18";
const fallbackDate = "2024-11-11";
expect(date).toBe(fallbackDate);
expect(spinner.start).toHaveBeenCalled();
expect(spinner.stop).toHaveBeenCalledWith(
Expand All @@ -78,13 +79,29 @@ describe("Compatibility Date Helpers", () => {

const date = await getWorkerdCompatibilityDate();

const fallbackDate = "2023-05-18";
const fallbackDate = "2024-11-11";
expect(date).toBe(fallbackDate);
expect(spinner.start).toHaveBeenCalled();
expect(spinner.stop).toHaveBeenCalledWith(
expect.stringContaining(fallbackDate),
);
});

it("should use today's date if workerd's release is in the future", async () => {
vi.setSystemTime("2025-01-09T23:59:59.999Z");
console.log(new Date().toISOString());
mockRegistryFetch("2.20250110.5");

const date = await getWorkerdCompatibilityDate();

// should get back today because deploy will fail with a future date
const expectedDate = "2025-01-09";
expect(date).toBe(expectedDate);
expect(spinner.start).toHaveBeenCalled();
expect(spinner.stop).toHaveBeenCalledWith(
expect.stringContaining(expectedDate),
);
});
});

test("compatDateFlag", async () => {
Expand Down
22 changes: 17 additions & 5 deletions packages/create-cloudflare/src/helpers/compatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@ export async function getWorkerdCompatibilityDate() {
// The format of the workerd version is `major.yyyymmdd.patch`.
const match = latestWorkerdVersion.match(/\d+\.(\d{4})(\d{2})(\d{2})\.\d+/);

// workerd releases often have a date for the following day.
// Unfortunately, Workers deployments will fail if they specify
// a compatibility date in the future. This means that most
// who create a new project on the same day as a workerd
// release will have their deployments fail until they
// manually adjust the compatibility date.
//
// To work around this, we must manually ensure that the compat date
// is not on a future UTC day when there was a recent workerd release.
if (match) {
const [, year, month, date] = match;
const compatDate = `${year}-${month}-${date}`;

s.stop(`${brandColor("compatibility date")} ${dim(compatDate)}`);
return compatDate;
let compatDate = new Date(`${year}-${month}-${date}`);
if (compatDate.getTime() > Date.now()) {
compatDate = new Date(Date.now());
}
const compatDateString = compatDate.toISOString().slice(0, 10);
s.stop(`${brandColor("compatibility date")} ${dim(compatDateString)}`);
return compatDateString;
}
} catch {}

const fallbackDate = "2023-05-18";
const fallbackDate = "2024-11-11";

s.stop(
`${brandColor("compatibility date")} ${dim(
Expand Down

0 comments on commit a7965c7

Please sign in to comment.