Skip to content
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

[wrangler] Clean up compatibility fallback warnings #4209

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
mrbbot marked this conversation as resolved.
Show resolved Hide resolved

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 { 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 @@

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) => {

Check warning on line 130 in packages/wrangler/src/dev/miniflare.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/dev/miniflare.ts#L130

Added line #L130 was not covered by tests
if (maybeNewVersion === undefined) return;
message += [

Check warning on line 132 in packages/wrangler/src/dev/miniflare.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/dev/miniflare.ts#L132

Added line #L132 was not covered by tests
"",
"Features enabled by your requested compatibility date may not be available.",
`Upgrade to \`wrangler@${maybeNewVersion}\` to remove this warning.`,
].join("\n");
super.warn(message);

Check warning on line 137 in packages/wrangler/src/dev/miniflare.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/dev/miniflare.ts#L137

Added line #L137 was not covered by tests
});
}
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 pkg from "../package.json";
import type { Result } from "update-check";

export async function updateCheck(): Promise<string | undefined> {
async function doUpdateCheck(): Promise<string | undefined> {

Check warning on line 5 in packages/wrangler/src/update-check.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/update-check.ts#L5

Added line #L5 was not covered by tests
let update: Result | null = null;
try {
// default cache for update check is 1 day
Expand All @@ -14,3 +14,11 @@
}
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> {

Check warning on line 22 in packages/wrangler/src/update-check.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/update-check.ts#L22

Added line #L22 was not covered by tests
return (updateCheckPromise ??= doUpdateCheck());
}