From a5cb01d2e881f8321988af9653cbec0a7a1b5846 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 23 Sep 2025 17:15:57 +0200 Subject: [PATCH] fix: enable using workerd process v2 --- .changeset/stale-adults-divide.md | 7 +++++ examples/playground15/wrangler.jsonc | 2 +- .../cloudflare/src/cli/build/bundle-server.ts | 3 +- .../cli/build/patches/plugins/open-next.ts | 29 +++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 .changeset/stale-adults-divide.md diff --git a/.changeset/stale-adults-divide.md b/.changeset/stale-adults-divide.md new file mode 100644 index 00000000..6b0aec04 --- /dev/null +++ b/.changeset/stale-adults-divide.md @@ -0,0 +1,7 @@ +--- +"@opennextjs/cloudflare": patch +--- + +fix: enable using workerd process v2 + +process v2 is an updated version of `node:process` active by default after 2025-09-15 diff --git a/examples/playground15/wrangler.jsonc b/examples/playground15/wrangler.jsonc index d5aa33d7..56965f57 100644 --- a/examples/playground15/wrangler.jsonc +++ b/examples/playground15/wrangler.jsonc @@ -2,7 +2,7 @@ "$schema": "node_modules/wrangler/config-schema.json", "main": ".open-next/worker.js", "name": "playground15", - "compatibility_date": "2024-12-30", + "compatibility_date": "2025-09-15", "compatibility_flags": ["nodejs_compat", "global_fetch_strictly_public", "enable_request_signal"], "assets": { "directory": ".open-next/assets", diff --git a/packages/cloudflare/src/cli/build/bundle-server.ts b/packages/cloudflare/src/cli/build/bundle-server.ts index 050cbb7d..95402451 100644 --- a/packages/cloudflare/src/cli/build/bundle-server.ts +++ b/packages/cloudflare/src/cli/build/bundle-server.ts @@ -16,7 +16,7 @@ import { inlineFindDir } from "./patches/plugins/find-dir.js"; import { patchInstrumentation } from "./patches/plugins/instrumentation.js"; import { inlineLoadManifest } from "./patches/plugins/load-manifest.js"; import { patchNextServer } from "./patches/plugins/next-server.js"; -import { patchResolveCache } from "./patches/plugins/open-next.js"; +import { patchResolveCache, patchSetWorkingDirectory } from "./patches/plugins/open-next.js"; import { handleOptionalDependencies } from "./patches/plugins/optional-deps.js"; import { patchPagesRouterContext } from "./patches/plugins/pages-router-context.js"; import { patchDepdDeprecations } from "./patches/plugins/patch-depd-deprecations.js"; @@ -107,6 +107,7 @@ export async function bundleServer(buildOpts: BuildOptions, projectOpts: Project patchRouteModules(updater, buildOpts), patchDepdDeprecations(updater), patchResolveCache(updater, buildOpts), + patchSetWorkingDirectory(updater, buildOpts), // Apply updater updates, must be the last plugin updater.plugin, ] as Plugin[], diff --git a/packages/cloudflare/src/cli/build/patches/plugins/open-next.ts b/packages/cloudflare/src/cli/build/patches/plugins/open-next.ts index 23bacbe4..7b643208 100644 --- a/packages/cloudflare/src/cli/build/patches/plugins/open-next.ts +++ b/packages/cloudflare/src/cli/build/patches/plugins/open-next.ts @@ -45,3 +45,32 @@ rule: fix: |- var composableCacheHandlerPath = ""; `; + +export function patchSetWorkingDirectory(updater: ContentUpdater, buildOpts: BuildOptions): Plugin { + const { outputDir } = buildOpts; + const packagePath = getPackagePath(buildOpts); + const outputPath = path.join(outputDir, "server-functions/default"); + + const indexPath = path.relative( + buildOpts.appBuildOutputPath, + path.join(outputPath, packagePath, `index.mjs`) + ); + + return updater.updateContent("do-not-set-working-directory", [ + { + filter: getCrossPlatformPathRegex(indexPath), + contentFilter: /function setNextjsServerWorkingDirectory\(/, + callback: async ({ contents }) => patchCode(contents, workingDirectoryRule), + }, + ]); +} + +// `setNextjsServerWorkingDirectory` calls `process.chdir("")` which errors because the directory does not exists +// See https://github.com/opennextjs/opennextjs-cloudflare/issues/899 +export const workingDirectoryRule = ` +rule: + pattern: function setNextjsServerWorkingDirectory() { $$$BODY } +fix: |- + function setNextjsServerWorkingDirectory() { + } +`;