Skip to content

Commit

Permalink
[C3] fix: use a valid compatibility date for worker templates
Browse files Browse the repository at this point in the history
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 #2385
  • Loading branch information
petebacondarwin committed May 26, 2023
1 parent 2dc55da commit 8e20b9d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 11 deletions.
14 changes: 14 additions & 0 deletions .changeset/small-lies-thank.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"weakset",
"webassemblymemory",
"websockets",
"workerd",
"xxhash"
],
"cSpell.ignoreWords": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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");
});
});
13 changes: 10 additions & 3 deletions packages/create-cloudflare/src/helpers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 27 additions & 2 deletions packages/create-cloudflare/src/workers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"<TBD>"$/m,
`compatibility_date = "${await getWorkerdCompatibilityDate()}"`
);

// write files
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
name = "cloudflare-workers-chatgpt-plugin-example"
name = "<TBD>"
main = "src/index.ts"
compatibility_date = "2023-04-07"
compatibility_date = "<TBD>"
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "<TBD>"
main = "src/worker.js"
compatibility_date = "2023-04-21"
compatibility_date = "<TBD>"

# # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "<TBD>"
main = "src/worker.ts"
compatibility_date = "2023-04-21"
compatibility_date = "<TBD>"

# # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "<TBD>"
main = "src/worker.js"
compatibility_date = "2023-04-21"
compatibility_date = "<TBD>"

# # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "<TBD>"
main = "src/worker.ts"
compatibility_date = "2023-04-21"
compatibility_date = "<TBD>"

# # KV Namespace binding - For more information: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
Expand Down

0 comments on commit 8e20b9d

Please sign in to comment.