From eee742f2934c287f2726fde532d79de0c9e939d9 Mon Sep 17 00:00:00 2001 From: Carmen Popoviciu Date: Wed, 15 Jan 2025 12:30:08 +0100 Subject: [PATCH] fix(create-cloudflare): Fix regression in C3's next template [#7676](https://github.com/cloudflare/workers-sdk/pull/7676) switched C3 templates to default to `wrangler.json` instead of `wrangler.toml`. Unfortunately, this unintendedly broke the `next` template, which was still attempting to read `wrangler.toml` specifically. This commit fixes the regression. Fixes #7770 --- .changeset/chilly-kings-retire.md | 9 ++++++ .../e2e-tests/frameworks.test.ts | 1 - .../src/wrangler/__tests__/config.test.ts | 30 +++++++++++++++++++ .../create-cloudflare/src/wrangler/config.ts | 12 ++++++++ .../templates/next/README.md | 9 ++++-- .../create-cloudflare/templates/next/c3.ts | 24 ++------------- 6 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 .changeset/chilly-kings-retire.md diff --git a/.changeset/chilly-kings-retire.md b/.changeset/chilly-kings-retire.md new file mode 100644 index 000000000000..4bc17b51f8b5 --- /dev/null +++ b/.changeset/chilly-kings-retire.md @@ -0,0 +1,9 @@ +--- +"create-cloudflare": patch +--- + +Fix regression in C3's `next` template + +[#7676](https://github.com/cloudflare/workers-sdk/pull/7676) switched C3 templates to default to `wrangler.json` instead of `wrangler.toml`. Unfortunately, this unintendedly broke the `next` template, which was still attempting to read `wrangler.toml` specifically. This commit fixes the regression. + +Fixes #7770 diff --git a/packages/create-cloudflare/e2e-tests/frameworks.test.ts b/packages/create-cloudflare/e2e-tests/frameworks.test.ts index 2aa23efa6e68..0a39a7e398c5 100644 --- a/packages/create-cloudflare/e2e-tests/frameworks.test.ts +++ b/packages/create-cloudflare/e2e-tests/frameworks.test.ts @@ -475,7 +475,6 @@ function getFrameworkTests(opts: { }, ], testCommitMessage: true, - quarantine: true, verifyBuildCfTypes: { outputFile: "env.d.ts", envInterfaceName: "CloudflareEnv", diff --git a/packages/create-cloudflare/src/wrangler/__tests__/config.test.ts b/packages/create-cloudflare/src/wrangler/__tests__/config.test.ts index cf6bc29a9d29..50b579121b83 100644 --- a/packages/create-cloudflare/src/wrangler/__tests__/config.test.ts +++ b/packages/create-cloudflare/src/wrangler/__tests__/config.test.ts @@ -83,6 +83,12 @@ describe("update wrangler config", () => { # [[services]] # binding = "MY_SERVICE" # service = "my-service" + + # KV Namespaces + # https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + # [[kv_namespaces]] + # binding = "MY_KV_NAMESPACE" + # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" " `); }); @@ -148,6 +154,12 @@ describe("update wrangler config", () => { * https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings */ // "services": [{ "binding": "MY_SERVICE", "service": "my-service" }] + + /** + * KV Namespaces + * https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + */ + // "kv_namespaces": [{ "binding": "MY_KV_NAMESPACE", "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }], } " `); @@ -204,6 +216,12 @@ describe("update wrangler config", () => { # [[services]] # binding = "MY_SERVICE" # service = "my-service" + + # KV Namespaces + # https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + # [[kv_namespaces]] + # binding = "MY_KV_NAMESPACE" + # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" " `); }); @@ -257,6 +275,12 @@ describe("update wrangler config", () => { # [[services]] # binding = "MY_SERVICE" # service = "my-service" + + # KV Namespaces + # https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + # [[kv_namespaces]] + # binding = "MY_KV_NAMESPACE" + # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" " `); }); @@ -312,6 +336,12 @@ describe("update wrangler config", () => { # [[services]] # binding = "MY_SERVICE" # service = "my-service" + + # KV Namespaces + # https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + # [[kv_namespaces]] + # binding = "MY_KV_NAMESPACE" + # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" " `); }); diff --git a/packages/create-cloudflare/src/wrangler/config.ts b/packages/create-cloudflare/src/wrangler/config.ts index 70d731b1c0be..05a801c1a99c 100644 --- a/packages/create-cloudflare/src/wrangler/config.ts +++ b/packages/create-cloudflare/src/wrangler/config.ts @@ -90,6 +90,12 @@ export const updateWranglerConfig = async (ctx: C3Context) => { * https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings */ // "services": [{ "binding": "MY_SERVICE", "service": "my-service" }] + + /** + * KV Namespaces + * https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + */ + // "kv_namespaces": [{ "binding": "MY_KV_NAMESPACE", "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }], } `, ); @@ -142,6 +148,12 @@ export const updateWranglerConfig = async (ctx: C3Context) => { # [[services]] # binding = "MY_SERVICE" # service = "my-service" + +# KV Namespaces +# https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces +# [[kv_namespaces]] +# binding = "MY_KV_NAMESPACE" +# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" `, ); } diff --git a/packages/create-cloudflare/templates/next/README.md b/packages/create-cloudflare/templates/next/README.md index e5ee722e50c3..36d8ff4cc5f7 100644 --- a/packages/create-cloudflare/templates/next/README.md +++ b/packages/create-cloudflare/templates/next/README.md @@ -47,10 +47,13 @@ In order to enable the example: // KV Example: ``` and uncomment the commented lines below it (also uncomment the relevant imports). -- Do the same in the `wrangler.toml` file, where - the comment is: +- In the `wrangler.json` file search for the block below, and uncomment uncomment the line that contains the `"kv_namespaces"` declaration: ``` - # KV Example: + /** + * KV Namespaces + * https://developers.cloudflare.com/pages/functions/bindings/#kv-namespaces + */ + // "kv_namespaces": [{ "binding": "MY_KV_NAMESPACE", "id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }], ``` - If you're using TypeScript run the `cf-typegen` script to update the `env.d.ts` file: ```bash diff --git a/packages/create-cloudflare/templates/next/c3.ts b/packages/create-cloudflare/templates/next/c3.ts index 71cb7b480c68..4202bf280a70 100644 --- a/packages/create-cloudflare/templates/next/c3.ts +++ b/packages/create-cloudflare/templates/next/c3.ts @@ -26,27 +26,9 @@ const generate = async (ctx: C3Context) => { await runFrameworkGenerator(ctx, [projectName]); - const wranglerToml = readFile(join(getTemplatePath(ctx), "wrangler.toml")); - - // Note: here we add `# KV Example:` to the toml file for the KV example, we don't actually - // include the comment in the template wrangler.toml file just so to keep it identical - // and consistent with that of all the other frameworks - // (instead of making it a special case which needs extra care) - const newTomlContent = wranglerToml.replace( - /#\s+\[\[kv_namespaces\]\]\n#\s+binding\s+=\s+"MY_KV_NAMESPACE"\n#\s+id\s+=\s+"[a-zA-Z0-9]+?"/, - ($1) => `# KV Example:\n${$1}`, - ); - - if (!/# KV Example/.test(newTomlContent)) { - // This should never happen to users, it is a check mostly so that - // if the toml file is changed in a way that breaks the "KV Example" addition - // the C3 Next.js e2e runs will fail with this - throw new Error("Failed to properly generate the wrangler.toml file"); - } - - writeFile(join(ctx.project.path, "wrangler.toml"), newTomlContent); - - updateStatus("Created wrangler.toml file"); + const wranglerConfig = readFile(join(getTemplatePath(ctx), "wrangler.json")); + writeFile(join(ctx.project.path, "wrangler.json"), wranglerConfig); + updateStatus("Created wrangler.json file"); }; const updateNextConfig = (usesTs: boolean) => {