From dcfc85952b61b51667bb2b4a27b9f0663b8cea20 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Wed, 3 Dec 2025 18:29:12 +0000 Subject: [PATCH 1/7] When running wrangler commands add a `RUN_BY_OPEN_NEXT` environment variable that wrangler can use to detect if it is run by open-next --- .changeset/mighty-heads-shine.md | 5 +++++ packages/cloudflare/src/cli/utils/run-wrangler.ts | 2 ++ 2 files changed, 7 insertions(+) create mode 100644 .changeset/mighty-heads-shine.md diff --git a/.changeset/mighty-heads-shine.md b/.changeset/mighty-heads-shine.md new file mode 100644 index 00000000..94c0160d --- /dev/null +++ b/.changeset/mighty-heads-shine.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +When running wrangler commands add a `RUN_BY_OPEN_NEXT` environment variable that wrangler can use to detect if it is run by open-next diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index 226ea50b..03fbbbd9 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -83,6 +83,8 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: // Wrangler would load `.env.` while we should load `.env.` // See https://opennext.js.org/cloudflare/howtos/env-vars CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", + // We pass this variable to wrangler so that it can be aware that it is being run by us + RUN_BY_OPEN_NEXT: "true", }, } ); From eee41f7443666a9a089f0b567a6bf114454c316f Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Thu, 4 Dec 2025 13:02:36 +0000 Subject: [PATCH 2/7] set environment variable only for deploy command --- .changeset/mighty-heads-shine.md | 2 +- packages/cloudflare/src/cli/utils/run-wrangler.ts | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.changeset/mighty-heads-shine.md b/.changeset/mighty-heads-shine.md index 94c0160d..2e032e3d 100644 --- a/.changeset/mighty-heads-shine.md +++ b/.changeset/mighty-heads-shine.md @@ -2,4 +2,4 @@ "@opennextjs/cloudflare": patch --- -When running wrangler commands add a `RUN_BY_OPEN_NEXT` environment variable that wrangler can use to detect if it is run by open-next +When running `wrangler deploy` add a `RUN_BY_OPEN_NEXT` environment variable to let wrangler know that it is being run by open-next diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index 03fbbbd9..fa799dda 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -58,6 +58,8 @@ function injectPassthroughFlagForArgs(options: BuildOptions, args: string[]) { export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: WranglerOptions = {}) { const shouldPipeLogs = wranglerOpts.logging === "error"; + const isDeployCommand = args[0] === "deploy"; + const result = spawnSync( options.packager, [ @@ -83,8 +85,11 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: // Wrangler would load `.env.` while we should load `.env.` // See https://opennext.js.org/cloudflare/howtos/env-vars CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", - // We pass this variable to wrangler so that it can be aware that it is being run by us - RUN_BY_OPEN_NEXT: "true", + // If we are running the deploy command we set this RUN_BY_OPEN_NEXT environment variable + // to let `wrangler deploy` know that it is being run from open-next. We do this because + // otherwise `wrangler deploy` run in an open-next project would call + // `opennextjs-cloudflare deploy` (thus causing an unwanted recursion). + ...(isDeployCommand ? { RUN_BY_OPEN_NEXT: "true" } : {}), }, } ); From ec32b68e1baba52bf1c769066b8db584d12b5dc8 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Thu, 4 Dec 2025 13:15:29 +0000 Subject: [PATCH 3/7] rename the variable to OPEN_NEXT_DEPLOY --- .changeset/mighty-heads-shine.md | 2 +- packages/cloudflare/src/cli/utils/run-wrangler.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.changeset/mighty-heads-shine.md b/.changeset/mighty-heads-shine.md index 2e032e3d..dbf88865 100644 --- a/.changeset/mighty-heads-shine.md +++ b/.changeset/mighty-heads-shine.md @@ -2,4 +2,4 @@ "@opennextjs/cloudflare": patch --- -When running `wrangler deploy` add a `RUN_BY_OPEN_NEXT` environment variable to let wrangler know that it is being run by open-next +When running `wrangler deploy` add a `OPEN_NEXT_DEPLOY` environment variable to let wrangler know that it is being run by open-next diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index fa799dda..576318f3 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -85,11 +85,11 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: // Wrangler would load `.env.` while we should load `.env.` // See https://opennext.js.org/cloudflare/howtos/env-vars CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", - // If we are running the deploy command we set this RUN_BY_OPEN_NEXT environment variable + // If we are running the deploy command we set this OPEN_NEXT_DEPLOY environment variable // to let `wrangler deploy` know that it is being run from open-next. We do this because // otherwise `wrangler deploy` run in an open-next project would call // `opennextjs-cloudflare deploy` (thus causing an unwanted recursion). - ...(isDeployCommand ? { RUN_BY_OPEN_NEXT: "true" } : {}), + ...(isDeployCommand ? { OPEN_NEXT_DEPLOY: "true" } : {}), }, } ); From 2f460ed2f81cc4a85ce9a333b13d02565dab4c57 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 8 Dec 2025 17:58:52 +0000 Subject: [PATCH 4/7] add `deploy` field to `WranglerOptions` --- packages/cloudflare/src/cli/commands/deploy.ts | 1 + packages/cloudflare/src/cli/utils/run-wrangler.ts | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/cloudflare/src/cli/commands/deploy.ts b/packages/cloudflare/src/cli/commands/deploy.ts index beb70bef..484777e1 100644 --- a/packages/cloudflare/src/cli/commands/deploy.ts +++ b/packages/cloudflare/src/cli/commands/deploy.ts @@ -56,6 +56,7 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu ], { logging: "all", + deploy: true, } ); } diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index 576318f3..032ad1af 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -13,6 +13,7 @@ type WranglerOptions = { environment?: string; configPath?: string; logging?: "all" | "error"; + deploy?: boolean; }; /** @@ -58,8 +59,6 @@ function injectPassthroughFlagForArgs(options: BuildOptions, args: string[]) { export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: WranglerOptions = {}) { const shouldPipeLogs = wranglerOpts.logging === "error"; - const isDeployCommand = args[0] === "deploy"; - const result = spawnSync( options.packager, [ @@ -89,7 +88,7 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: // to let `wrangler deploy` know that it is being run from open-next. We do this because // otherwise `wrangler deploy` run in an open-next project would call // `opennextjs-cloudflare deploy` (thus causing an unwanted recursion). - ...(isDeployCommand ? { OPEN_NEXT_DEPLOY: "true" } : {}), + ...(wranglerOpts.deploy ? { OPEN_NEXT_DEPLOY: "true" } : {}), }, } ); From 092f5d1b3f5b9fc1bc2f18b6dac792dd09f08cb3 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Mon, 8 Dec 2025 18:03:08 +0000 Subject: [PATCH 5/7] update solution to use `env` filed instead --- packages/cloudflare/src/cli/commands/deploy.ts | 8 +++++++- packages/cloudflare/src/cli/utils/run-wrangler.ts | 8 ++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/cloudflare/src/cli/commands/deploy.ts b/packages/cloudflare/src/cli/commands/deploy.ts index 484777e1..b81ad8b4 100644 --- a/packages/cloudflare/src/cli/commands/deploy.ts +++ b/packages/cloudflare/src/cli/commands/deploy.ts @@ -56,7 +56,13 @@ export async function deployCommand(args: WithWranglerArgs<{ cacheChunkSize?: nu ], { logging: "all", - deploy: true, + env: { + // If we are running the deploy command we set this OPEN_NEXT_DEPLOY environment variable + // to let `wrangler deploy` know that it is being run from open-next. We do this because + // otherwise `wrangler deploy` run in an open-next project would call + // `opennextjs-cloudflare deploy` (thus causing an unwanted recursion). + OPEN_NEXT_DEPLOY: "true", + }, } ); } diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index 032ad1af..c21807ea 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -13,7 +13,7 @@ type WranglerOptions = { environment?: string; configPath?: string; logging?: "all" | "error"; - deploy?: boolean; + env?: Record; }; /** @@ -84,11 +84,7 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: // Wrangler would load `.env.` while we should load `.env.` // See https://opennext.js.org/cloudflare/howtos/env-vars CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", - // If we are running the deploy command we set this OPEN_NEXT_DEPLOY environment variable - // to let `wrangler deploy` know that it is being run from open-next. We do this because - // otherwise `wrangler deploy` run in an open-next project would call - // `opennextjs-cloudflare deploy` (thus causing an unwanted recursion). - ...(wranglerOpts.deploy ? { OPEN_NEXT_DEPLOY: "true" } : {}), + ...wranglerOpts.env, }, } ); From c0a6631736ef4ce2ab9587ef68929d06c3aa255f Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 9 Dec 2025 10:36:34 +0000 Subject: [PATCH 6/7] Make env a record of Co-authored-by: Victor Berchet --- packages/cloudflare/src/cli/utils/run-wrangler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index c21807ea..9f2f7894 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -13,7 +13,7 @@ type WranglerOptions = { environment?: string; configPath?: string; logging?: "all" | "error"; - env?: Record; + env?: Record; }; /** From bac42c89939e114f98da32fbf73ceb05829c8d19 Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Tue, 9 Dec 2025 10:38:16 +0000 Subject: [PATCH 7/7] move wranglerOpts.env spreading up --- packages/cloudflare/src/cli/utils/run-wrangler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cloudflare/src/cli/utils/run-wrangler.ts b/packages/cloudflare/src/cli/utils/run-wrangler.ts index 9f2f7894..f6c07cdb 100644 --- a/packages/cloudflare/src/cli/utils/run-wrangler.ts +++ b/packages/cloudflare/src/cli/utils/run-wrangler.ts @@ -80,11 +80,11 @@ export function runWrangler(options: BuildOptions, args: string[], wranglerOpts: stdio: shouldPipeLogs ? ["ignore", "pipe", "pipe"] : "inherit", env: { ...process.env, + ...wranglerOpts.env, // `.env` files are handled by the adapter. // Wrangler would load `.env.` while we should load `.env.` // See https://opennext.js.org/cloudflare/howtos/env-vars CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV: "false", - ...wranglerOpts.env, }, } );