-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Remove getLocalWorkerdCompatibilityDate from workers-utils and re-export supportedCompatibilityDate from miniflare instead
#12387
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
Changes from all commits
3adbe20
6ebf636
a702c3f
5e49112
1e60ce9
c91c2cb
ec884f0
22b2b2d
befed3d
8c0addc
de99ecb
c01cf7c
f3031b5
8b4ba49
9eb1dd3
7214732
3aa2610
490a0d1
af2445e
cb41c70
3402f7a
81b5623
ae72d62
a86d1e0
edf4d29
2997ba3
c9ccae5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| Re-export `supportedCompatibilityDate` from miniflare | ||
|
|
||
| The re-exports contains a recent and safe compatibility date from the local version of `workerd`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@cloudflare/vite-plugin": patch | ||
| --- | ||
|
|
||
| Use `supportedCompatibilityDate` value from `miniflare` instead of getting the date from `@cloudflare/workers-utils` | ||
|
|
||
| `miniflare` exports a recent safe compatibility date as `supportedCompatibilityDate`, and that is the value the package now uses as the latest supported workerd compatibility date. This doesn't have any specific user-facing effect (besides potentially making the function more reliable). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| "create-cloudflare": patch | ||
| --- | ||
|
|
||
| Avoid resorting to the fallback compatibility date when running via `pnpm` | ||
|
|
||
| Previously, when running `create-cloudflare` via `pnpm` the resolution of the local `miniflare` package failed and the fallback date was always used instead of the actual `workerd` compatibility date. | ||
|
|
||
| The fix switches to using `supportedCompatibilityDate` from `wrangler` directly, which does not depend on the `miniflare` resolution and works reliably across all package managers. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| "@cloudflare/workers-utils": minor | ||
| --- | ||
|
|
||
| Remove the `getLocalWorkerdCompatibilityDate` utility from the package | ||
|
|
||
| This utility has been removed because its implementation relied on retrieving the compat date from `workerd` by resolving to the `miniflare` dependency, which was unreliable in certain environments (e.g. when using `pnpm`). The functionality is now provided more reliably as a static export from `wrangler`. | ||
|
|
||
| Consumers should migrate to one of the following alternatives: | ||
|
|
||
| - `supportedCompatibilityDate` from `miniflare` — for direct miniflare users | ||
| - `supportedCompatibilityDate` re-exported from `wrangler` — for consumers of the wrangler programmatic API | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,56 @@ | ||
| import { readdirSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import { createRequire } from "node:module"; | ||
| import { join, resolve } from "node:path"; | ||
| import { brandColor, dim } from "@cloudflare/cli/colors"; | ||
| import { spinner } from "@cloudflare/cli/interactive"; | ||
| import { getLocalWorkerdCompatibilityDate } from "@cloudflare/workers-utils"; | ||
| import type { C3Context } from "types"; | ||
| import type { CompatDate } from "wrangler"; | ||
|
|
||
| /** | ||
| * Compatibility date to fallback to if getting the compatibility date from wrangler fails for whatever reason. | ||
| * | ||
| * Note: this fallback date doesn't have any special meaning, it's simply the latest compatibility date at the time of writing | ||
| * (source: https://github.com/cloudflare/workerd/blob/main/src/workerd/io/release-version.txt#L1) | ||
| */ | ||
| export const FALLBACK_COMPAT_DATE = "2026-03-24"; | ||
|
|
||
| /** | ||
| * Retrieves the latest workerd compatibility date | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you expand this JSDoc to describe that this function
|
||
| * | ||
| * @returns The latest compatibility date for workerd in the form "YYYY-MM-DD" | ||
| */ | ||
| export function getWorkerdCompatibilityDate(projectPath: string) { | ||
| export function getWorkerdCompatibilityDate(projectPath: string): CompatDate { | ||
| const s = spinner(); | ||
| s.start("Retrieving current workerd compatibility date"); | ||
|
|
||
| const { date, source } = getLocalWorkerdCompatibilityDate({ projectPath }); | ||
| try { | ||
| const projectRequire = createRequire(join(projectPath, "package.json")); | ||
| // eslint-disable-next-line @typescript-eslint/consistent-type-imports | ||
| const wrangler: Awaited<typeof import("wrangler")> = | ||
| projectRequire("wrangler"); | ||
| const { supportedCompatibilityDate } = wrangler; | ||
|
|
||
| if ( | ||
| typeof supportedCompatibilityDate !== "string" || | ||
| !/^\d{4}-\d{2}-\d{2}$/.test(supportedCompatibilityDate) | ||
| ) { | ||
| throw new Error( | ||
| "wrangler does not export a valid supportedCompatibilityDate" | ||
| ); | ||
| } | ||
|
|
||
| if (source === "fallback") { | ||
| s.stop( | ||
| `${brandColor("compatibility date")} ${dim(supportedCompatibilityDate)}` | ||
| ); | ||
| return supportedCompatibilityDate; | ||
|
dario-piotrowicz marked this conversation as resolved.
|
||
| } catch { | ||
| s.stop( | ||
| `${brandColor("compatibility date")} ${dim( | ||
| ` Could not find workerd date, falling back to ${date}` | ||
| `Could not find workerd date, falling back to "${FALLBACK_COMPAT_DATE}"` | ||
| )}` | ||
| ); | ||
| } else { | ||
| s.stop(`${brandColor("compatibility date")} ${dim(date)}`); | ||
| return FALLBACK_COMPAT_DATE; | ||
| } | ||
| return date; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,58 @@ | ||||||||||||
| import assert from "node:assert"; | ||||||||||||
| import { compatibilityDate as workerdCompatibilityDate } from "workerd"; | ||||||||||||
|
|
||||||||||||
| export type YYYY = `${number}${number}${number}${number}`; | ||||||||||||
| export type MM = `${number}${number}`; | ||||||||||||
| export type DD = `${number}${number}`; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * String representing a date following the Cloudflare compatibility date format, such as `2025-09-27` | ||||||||||||
| */ | ||||||||||||
| export type CompatDate = `${YYYY}-${MM}-${DD}`; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Discern whether a string represents a compatibility date (`YYYY-MM-DD`) | ||||||||||||
| * | ||||||||||||
| * @param str The target string | ||||||||||||
| * @returns true if the string represents a compatibility date, false otherwise | ||||||||||||
| */ | ||||||||||||
| export function isCompatDate(str: string): str is CompatDate { | ||||||||||||
| return /^\d{4}-\d{2}-\d{2}$/.test(str); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Returns the date formatted as a compatibility date | ||||||||||||
| * | ||||||||||||
| * @param date The target date to convert | ||||||||||||
| * @returns The date as a CompatDate string (`YYYY-MM-DD`) | ||||||||||||
| */ | ||||||||||||
| export function formatCompatibilityDate(date: Date): CompatDate { | ||||||||||||
| const compatDate = date.toISOString().slice(0, 10); | ||||||||||||
| assert(isCompatDate(compatDate)); | ||||||||||||
| return compatDate; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Gets a safe compatibility date from workerd. If the workerd compatibility | ||||||||||||
| * date is in the future, returns today's date instead. This handles the case | ||||||||||||
| * where workerd releases set their compatibility date up to 7 days in the future. | ||||||||||||
|
Comment on lines
+36
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we do need to write up the resolution logic carefully here. It is still confusing even to the ANT team members! |
||||||||||||
| * | ||||||||||||
| * @return The compatibility Date (`YYYY-MM-DD`) | ||||||||||||
| */ | ||||||||||||
|
dario-piotrowicz marked this conversation as resolved.
|
||||||||||||
| function getSafeCompatibilityDate(): CompatDate { | ||||||||||||
| // The compatibility data from workerd follows the CompatDate format | ||||||||||||
| assert(isCompatDate(workerdCompatibilityDate)); | ||||||||||||
|
dario-piotrowicz marked this conversation as resolved.
|
||||||||||||
|
|
||||||||||||
| const today = formatCompatibilityDate(new Date()); | ||||||||||||
|
|
||||||||||||
| if (workerdCompatibilityDate > today) { | ||||||||||||
| return today; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return workerdCompatibilityDate; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** `YYYY-MM-DD` compatibility date */ | ||||||||||||
| const supportedCompatibilityDate = getSafeCompatibilityDate(); | ||||||||||||
|
|
||||||||||||
| export { supportedCompatibilityDate }; | ||||||||||||
|
dario-piotrowicz marked this conversation as resolved.
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.