From 8e20b9d26bd2bd3304e15c085196b2c99c80d97a Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 25 May 2023 17:59:07 +0100 Subject: [PATCH] [C3] fix: use a valid compatibility date for worker templates Previously, we changed wrangler.toml to use the current date for the compatibility_date setting in wrangler.toml when generating workers. But this is almost always going to be new recent and results in a warning. Now we look up the most recent compatibility date via npm on the workerd package and use that instead. Fixes https://github.com/cloudflare/workers-sdk/issues/2385 --- .changeset/small-lies-thank.md | 14 +++++++++ .vscode/settings.json | 1 + .../src/helpers/__tests__/command.test.ts | 6 ++++ .../create-cloudflare/src/helpers/command.ts | 13 +++++++-- packages/create-cloudflare/src/workers.ts | 29 +++++++++++++++++-- .../templates/chatgptPlugin/ts/wrangler.toml | 4 +-- .../templates/common/js/wrangler.toml | 2 +- .../templates/common/ts/wrangler.toml | 2 +- .../templates/simple/js/wrangler.toml | 2 +- .../templates/simple/ts/wrangler.toml | 2 +- 10 files changed, 64 insertions(+), 11 deletions(-) create mode 100644 .changeset/small-lies-thank.md diff --git a/.changeset/small-lies-thank.md b/.changeset/small-lies-thank.md new file mode 100644 index 000000000000..0b5fa42593db --- /dev/null +++ b/.changeset/small-lies-thank.md @@ -0,0 +1,14 @@ +--- +"create-cloudflare": patch +--- + +fix: use a valid compatibility date for worker templates + +Previously, we changed wrangler.toml to use the current date for the +compatibility_date setting in wrangler.toml when generating workers. +But this is almost always going to be new recent and results in a warning. + +Now we look up the most recent compatibility date via npm on the workerd +package and use that instead. + +Fixes https://github.com/cloudflare/workers-sdk/issues/2385 diff --git a/.vscode/settings.json b/.vscode/settings.json index c000345229de..d57f4ab7376f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -37,6 +37,7 @@ "weakset", "webassemblymemory", "websockets", + "workerd", "xxhash" ], "cSpell.ignoreWords": [ diff --git a/packages/create-cloudflare/src/helpers/__tests__/command.test.ts b/packages/create-cloudflare/src/helpers/__tests__/command.test.ts index 2b49bc00347c..ff1c03e01918 100644 --- a/packages/create-cloudflare/src/helpers/__tests__/command.test.ts +++ b/packages/create-cloudflare/src/helpers/__tests__/command.test.ts @@ -1,6 +1,7 @@ import { spawn } from "cross-spawn"; import { beforeEach, afterEach, describe, expect, test, vi } from "vitest"; import whichPMRuns from "which-pm-runs"; +import { getWorkerdCompatibilityDate } from "../../workers"; import { detectPackageManager, installPackages, @@ -105,4 +106,9 @@ describe("Command Helpers", () => { expect(pm.npm).toBe("yarn"); expect(pm.npx).toBe("npx"); }); + + test("getWorkerdCompatibilityDate()", async () => { + await getWorkerdCompatibilityDate(); + expectSpawnWith("npm info workerd dist-tags.latest"); + }); }); diff --git a/packages/create-cloudflare/src/helpers/command.ts b/packages/create-cloudflare/src/helpers/command.ts index dd3fcb43e4d9..9fd4aef260ef 100644 --- a/packages/create-cloudflare/src/helpers/command.ts +++ b/packages/create-cloudflare/src/helpers/command.ts @@ -9,11 +9,12 @@ import type { PagesGeneratorContext } from "types"; type RunOptions = { startText?: string; - doneText?: string; + doneText?: string | ((result: string) => string); silent?: boolean; captureOutput?: boolean; env?: NodeJS.ProcessEnv; cwd?: string; + transform?: (output: string) => string; }; export const runCommand = async ( @@ -55,10 +56,16 @@ export const runCommand = async ( return await new Promise((resolve, reject) => { cmd.on("close", (code) => { if (code === 0) { + const transformedOutput = + opts?.transform?.(stripAnsi(output)) ?? stripAnsi(output); if (opts?.doneText && !process.env.VITEST) { - s.stop(opts?.doneText); + const doneText = + typeof opts.doneText === "function" + ? opts.doneText(transformedOutput) + : opts.doneText; + s.stop(doneText); } - resolve(stripAnsi(output)); + resolve(transformedOutput); } else { logRaw(output); reject(code); diff --git a/packages/create-cloudflare/src/workers.ts b/packages/create-cloudflare/src/workers.ts index 32573e1ada6a..847d7242f31b 100644 --- a/packages/create-cloudflare/src/workers.ts +++ b/packages/create-cloudflare/src/workers.ts @@ -147,8 +147,8 @@ async function updateFiles(ctx: Context) { contents.wranglertoml = contents.wranglertoml .replace(/^name = .+$/m, `name = "${ctx.project.name}"`) .replace( - /^compatibility_date = .+$/m, - `compatibility_date = "${new Date().toISOString().substring(0, 10)}"` + /^compatibility_date\s=\s""$/m, + `compatibility_date = "${await getWorkerdCompatibilityDate()}"` ); // write files @@ -158,3 +158,28 @@ async function updateFiles(ctx: Context) { ); await writeFile(paths.wranglertoml, contents.wranglertoml); } + +/** + * Look up the latest release of workerd and use its date as the compatibility_date + * configuration value for wrangler.toml. + * + * If the look up fails then we fall back to a well known date. + * + * @returns The latest compatibility date for workerd in the form "YYYY-MM-DD" + */ +export async function getWorkerdCompatibilityDate() { + return runCommand("npm info workerd dist-tags.latest", { + silent: true, + captureOutput: true, + startText: "Retrieving current workerd compatibility date", + transform: (result) => { + try { + const date = result.split(".")[1]; + return `${date.slice(0, 4)}-${date.slice(4, 6)}-${date.slice(6, 8)}`; + } catch { + return "2023-05-18"; + } + }, + doneText: (output) => `${brandColor("compatibility date")} ${dim(output)}`, + }).then(); +} diff --git a/packages/create-cloudflare/templates/chatgptPlugin/ts/wrangler.toml b/packages/create-cloudflare/templates/chatgptPlugin/ts/wrangler.toml index d65c181a43b7..96a248cbe314 100644 --- a/packages/create-cloudflare/templates/chatgptPlugin/ts/wrangler.toml +++ b/packages/create-cloudflare/templates/chatgptPlugin/ts/wrangler.toml @@ -1,3 +1,3 @@ -name = "cloudflare-workers-chatgpt-plugin-example" +name = "" main = "src/index.ts" -compatibility_date = "2023-04-07" +compatibility_date = "" diff --git a/packages/create-cloudflare/templates/common/js/wrangler.toml b/packages/create-cloudflare/templates/common/js/wrangler.toml index 32ffe31de588..b75b697f3fb7 100644 --- a/packages/create-cloudflare/templates/common/js/wrangler.toml +++ b/packages/create-cloudflare/templates/common/js/wrangler.toml @@ -1,6 +1,6 @@ name = "" main = "src/worker.js" -compatibility_date = "2023-04-21" +compatibility_date = "" # # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv # [[kv_namespaces]] diff --git a/packages/create-cloudflare/templates/common/ts/wrangler.toml b/packages/create-cloudflare/templates/common/ts/wrangler.toml index de257af0c02c..a0eadb28e124 100644 --- a/packages/create-cloudflare/templates/common/ts/wrangler.toml +++ b/packages/create-cloudflare/templates/common/ts/wrangler.toml @@ -1,6 +1,6 @@ name = "" main = "src/worker.ts" -compatibility_date = "2023-04-21" +compatibility_date = "" # # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv # [[kv_namespaces]] diff --git a/packages/create-cloudflare/templates/simple/js/wrangler.toml b/packages/create-cloudflare/templates/simple/js/wrangler.toml index 32ffe31de588..b75b697f3fb7 100644 --- a/packages/create-cloudflare/templates/simple/js/wrangler.toml +++ b/packages/create-cloudflare/templates/simple/js/wrangler.toml @@ -1,6 +1,6 @@ name = "" main = "src/worker.js" -compatibility_date = "2023-04-21" +compatibility_date = "" # # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv # [[kv_namespaces]] diff --git a/packages/create-cloudflare/templates/simple/ts/wrangler.toml b/packages/create-cloudflare/templates/simple/ts/wrangler.toml index de257af0c02c..a0eadb28e124 100644 --- a/packages/create-cloudflare/templates/simple/ts/wrangler.toml +++ b/packages/create-cloudflare/templates/simple/ts/wrangler.toml @@ -1,6 +1,6 @@ name = "" main = "src/worker.ts" -compatibility_date = "2023-04-21" +compatibility_date = "" # # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv # [[kv_namespaces]]