Skip to content

Commit

Permalink
[wrangler] Clean up compatibility fallback warnings (#4209)
Browse files Browse the repository at this point in the history
* Only show compatibility date fallback warning if new version available

When a compatibility date greater than the installed `workerd` version
is selected, Miniflare logs a warning. The intention here is to warn
users if they've set a compatibility date, but haven't updated
`workerd`, so features they've requested won't be available.
The problem is that this warning is only actionable if there's an
update available. This change suppresses the warning if there's no
new version available.

* 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.
  • Loading branch information
mrbbot authored Oct 24, 2023
1 parent 5321826 commit 24d1c5c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
17 changes: 17 additions & 0 deletions .changeset/five-suits-rest.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 10 additions & 2 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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, "<current-date>"))
.toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mNo compatibility_date was specified. Using today's date: <current-date>.[0m
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mNo compatibility_date was specified. Using the installed Workers runtime's latest supported date: <current-date>.[0m
Add one to your wrangler.toml file:
\`\`\`
Expand Down
12 changes: 11 additions & 1 deletion packages/wrangler/src/dev/miniflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ModuleTypeToRuleType } from "../deployment-bundle/module-collection";
import { withSourceURLs } from "../deployment-bundle/source-url";
import { getHttpsOptions } from "../https-options";
import { logger } from "../logger";
import { updateCheck } from "../update-check";
import type { Config } from "../config";
import type {
CfD1Database,
Expand Down Expand Up @@ -122,10 +123,19 @@ class WranglerLog extends Log {

warn(message: string) {
// Only log warning about requesting a compatibility date after the workerd
// binary's version once
// binary's version once, and only if there's an update available.
if (message.startsWith("The latest compatibility date supported by")) {
if (this.#warnedCompatibilityDateFallback) return;
this.#warnedCompatibilityDateFallback = true;
return void updateCheck().then((maybeNewVersion) => {
if (maybeNewVersion === undefined) return;
message += [
"",
"Features enabled by your requested compatibility date may not be available.",
`Upgrade to \`wrangler@${maybeNewVersion}\` to remove this warning.`,
].join("\n");
super.warn(message);
});
}
super.warn(message);
}
Expand Down
14 changes: 11 additions & 3 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import module from "node:module";
import os from "node:os";
import TOML from "@iarna/toml";
import chalk from "chalk";
Expand Down Expand Up @@ -806,11 +807,18 @@ export async function main(argv: string[]): Promise<void> {
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` +
Expand Down
10 changes: 9 additions & 1 deletion packages/wrangler/src/update-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import checkForUpdate from "update-check";
import pkg from "../package.json";
import type { Result } from "update-check";

export async function updateCheck(): Promise<string | undefined> {
async function doUpdateCheck(): Promise<string | undefined> {
let update: Result | null = null;
try {
// default cache for update check is 1 day
Expand All @@ -14,3 +14,11 @@ export async function updateCheck(): Promise<string | undefined> {
}
return update?.latest;
}

// Memoise update check promise, so we can call this multiple times as required
// without having to prop drill the result. It's unlikely to change through the
// process lifetime.
let updateCheckPromise: Promise<string | undefined>;
export function updateCheck(): Promise<string | undefined> {
return (updateCheckPromise ??= doUpdateCheck());
}

0 comments on commit 24d1c5c

Please sign in to comment.