From 7c3e500de7398247b6f4c1349c3eb1ac57cb9867 Mon Sep 17 00:00:00 2001 From: bcoll Date: Wed, 18 Oct 2023 00:55:18 +0100 Subject: [PATCH] Use installed `workerd` version as default compat date instead of now `wrangler dev` would previously use the current date as the default compatibility date if one wasn't set in `wrangler.toml`. Before the previous commit, this would show a warning if it was greater than the installed `workerd` version. Whilst using the current date as the default would no longer log a warning with this change, if there was an update it would. This change sets the default to the `workerd` version instead. --- .changeset/five-suits-rest.md | 17 +++++++++++++++++ packages/wrangler/src/__tests__/dev.test.tsx | 12 ++++++++++-- packages/wrangler/src/index.ts | 14 +++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .changeset/five-suits-rest.md diff --git a/.changeset/five-suits-rest.md b/.changeset/five-suits-rest.md new file mode 100644 index 000000000000..cae677642a42 --- /dev/null +++ b/.changeset/five-suits-rest.md @@ -0,0 +1,17 @@ +--- +"wrangler": minor +--- + +fix: suppress compatibility date fallback warnings if no `wrangler` update is available + +If a compatibility date greater than the installed version of `workerd` was +configured, a warning would be logged. This warning was only actionable if a new +version of `wrangler` was available. The intent here was to warn if a user set +a new compatibility date, but forgot to update `wrangler` meaning changes +enabled by the new date wouldn't take effect. This change hides the warning if +no update is available. + +It also changes the default compatibility date for `wrangler dev` sessions +without a configured compatibility date to the installed version of `workerd`. +This previously defaulted to the current date, which may have been unsupported +by the installed runtime. diff --git a/packages/wrangler/src/__tests__/dev.test.tsx b/packages/wrangler/src/__tests__/dev.test.tsx index 587c1c892e95..ffab0fd92546 100644 --- a/packages/wrangler/src/__tests__/dev.test.tsx +++ b/packages/wrangler/src/__tests__/dev.test.tsx @@ -1,4 +1,5 @@ import * as fs from "node:fs"; +import module from "node:module"; import getPort from "get-port"; import { rest } from "msw"; import patchConsole from "patch-console"; @@ -68,11 +69,18 @@ describe("wrangler dev", () => { }); fs.writeFileSync("index.js", `export default {};`); await runWrangler("dev"); - const currentDate = new Date().toISOString().substring(0, 10); + + const miniflareEntry = require.resolve("miniflare"); + const miniflareRequire = module.createRequire(miniflareEntry); + const miniflareWorkerd = miniflareRequire("workerd") as { + compatibilityDate: string; + }; + const currentDate = miniflareWorkerd.compatibilityDate; + expect(std.out).toMatchInlineSnapshot(`""`); expect(std.warn.replaceAll(currentDate, "")) .toMatchInlineSnapshot(` - "▲ [WARNING] No compatibility_date was specified. Using today's date: . + "▲ [WARNING] No compatibility_date was specified. Using the installed Workers runtime's latest supported date: . Add one to your wrangler.toml file: \`\`\` diff --git a/packages/wrangler/src/index.ts b/packages/wrangler/src/index.ts index 5ef5f9e22760..c1cbc9237db1 100644 --- a/packages/wrangler/src/index.ts +++ b/packages/wrangler/src/index.ts @@ -1,3 +1,4 @@ +import module from "node:module"; import os from "node:os"; import TOML from "@iarna/toml"; import chalk from "chalk"; @@ -806,11 +807,18 @@ export async function main(argv: string[]): Promise { export function getDevCompatibilityDate( config: Config, compatibilityDate = config.compatibility_date -) { - const currentDate = new Date().toISOString().substring(0, 10); +): string { + // Get the maximum compatibility date supported by the installed Miniflare + const miniflareEntry = require.resolve("miniflare"); + const miniflareRequire = module.createRequire(miniflareEntry); + const miniflareWorkerd = miniflareRequire("workerd") as { + compatibilityDate: string; + }; + const currentDate = miniflareWorkerd.compatibilityDate; + if (config.configPath !== undefined && compatibilityDate === undefined) { logger.warn( - `No compatibility_date was specified. Using today's date: ${currentDate}.\n` + + `No compatibility_date was specified. Using the installed Workers runtime's latest supported date: ${currentDate}.\n` + "Add one to your wrangler.toml file:\n" + "```\n" + `compatibility_date = "${currentDate}"\n` +