Skip to content

Commit

Permalink
fix: add a warning if dev is defaulting to the latest compatibility…
Browse files Browse the repository at this point in the history
…-date

Fixes cloudflare#741
  • Loading branch information
petebacondarwin committed Apr 3, 2022
1 parent b933641 commit a809fbd
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changeset/bright-numbers-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: add a warning if `dev` is defaulting to the latest compatibility-date

Fixes https://github.com/cloudflare/wrangler2/issues/741
47 changes: 47 additions & 0 deletions packages/wrangler/src/__tests__/dev.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,53 @@ describe("wrangler dev", () => {
unsetAllMocks();
});

describe("compatibility-date", () => {
it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev index.js");
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
writeWranglerToml({
main: "index.js",
compatibility_date: undefined,
});
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev");
const currentDate = new Date().toISOString().substring(0, 10);
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn.replaceAll(currentDate, "<current-date>"))
.toMatchInlineSnapshot(`
"No compatibility_date was specified. Using today's date: <current-date>.
Add one to your wrangler.toml file:
\`\`\`
compatibility_date = \\"<current-date>\\"
\`\`\`
or pass it in your terminal:
\`\`\`
--compatibility_date=<current-date>
\`\`\`
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
writeWranglerToml({
main: "index.js",
compatibility_date: undefined,
});
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev --compatibility-date=2020-01-01");
expect(std.out).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
expect(std.err).toMatchInlineSnapshot(`""`);
});
});

describe("entry-points", () => {
it("should error if there is no entry-point specified", async () => {
writeWranglerToml();
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type DevProps = {
crons: Config["triggers"]["crons"];
public: undefined | string;
assetPaths: undefined | AssetPaths;
compatibilityDate: undefined | string;
compatibilityDate: string;
compatibilityFlags: undefined | string[];
usageModel: undefined | "bundled" | "unbound";
build: {
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/local.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface LocalProps {
name: undefined | string;
bundle: EsbuildBundle | undefined;
format: CfScriptFormat | undefined;
compatibilityDate: string | undefined;
compatibilityDate: string;
compatibilityFlags: undefined | string[];
bindings: CfWorkerInit["bindings"];
assetPaths: undefined | AssetPaths;
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/dev/remote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function Remote(props: {
accountId: undefined | string;
apiToken: undefined | string;
bindings: CfWorkerInit["bindings"];
compatibilityDate: string | undefined;
compatibilityDate: string;
compatibilityFlags: undefined | string[];
usageModel: undefined | "bundled" | "unbound";
env: string | undefined;
Expand Down
36 changes: 27 additions & 9 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,10 @@ export async function main(argv: string[]): Promise<void> {
args["inspector-port"] ?? (await getPort({ port: 9229 }))
}
public={args["experimental-public"]}
compatibilityDate={
args["compatibility-date"] ||
config.compatibility_date ||
new Date().toISOString().substring(0, 10)
}
compatibilityDate={getDevCompatibilityDate(
config,
args["compatibility-date"]
)}
compatibilityFlags={
(args["compatibility-flags"] as string[]) ||
config.compatibility_flags
Expand Down Expand Up @@ -1268,10 +1267,7 @@ export async function main(argv: string[]): Promise<void> {
port={config.dev?.port}
ip={config.dev.ip}
public={undefined}
compatibilityDate={
config.compatibility_date ||
new Date().toISOString().substring(0, 10)
}
compatibilityDate={getDevCompatibilityDate(config)}
compatibilityFlags={
(args["compatibility-flags"] as string[]) ||
config.compatibility_flags
Expand Down Expand Up @@ -2390,3 +2386,25 @@ export async function main(argv: string[]): Promise<void> {
throw e;
}
}

function getDevCompatibilityDate(
config: Config,
compatibilityDate = config.compatibility_date
) {
const currentDate = new Date().toISOString().substring(0, 10);
if (config.configPath !== undefined && compatibilityDate === undefined) {
console.warn(
`No compatibility_date was specified. Using today's date: ${currentDate}.\n` +
"Add one to your wrangler.toml file:\n" +
"```\n" +
`compatibility_date = "${currentDate}"\n` +
"```\n" +
"or pass it in your terminal:\n" +
"```\n" +
`--compatibility_date=${currentDate}\n` +
"```\n" +
"See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
);
}
return compatibilityDate ?? currentDate;
}

0 comments on commit a809fbd

Please sign in to comment.