Skip to content

Commit 34b0296

Browse files
fix: patch both trace/tracer.js files
1 parent 4b6a50b commit 34b0296

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

packages/cloudflare/src/cli/build/open-next/createServerBundle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { openNextReplacementPlugin } from "@opennextjs/aws/plugins/replacement.j
1717
import { openNextResolvePlugin } from "@opennextjs/aws/plugins/resolve.js";
1818
import type { FunctionOptions, SplittedFunctionOptions } from "@opennextjs/aws/types/open-next.js";
1919

20+
import { patchTracerFile } from "../patches/to-investigate/wrangler-deps.js";
21+
2022
export async function createServerBundle(options: buildHelper.BuildOptions) {
2123
const { config } = options;
2224
const foundRoutes = new Set<string>();
@@ -152,6 +154,10 @@ async function generateBundle(
152154
isBundled
153155
);
154156

157+
patchTracerFile(
158+
path.join(outputPath, "node_modules", "next", "dist", "server", "lib", "trace", "tracer.js")
159+
);
160+
155161
// Build Lambda code
156162
// note: bundle in OpenNext package b/c the adapter relies on the
157163
// "serverless-http" package which is not a dependency in user's

packages/cloudflare/src/cli/build/patches/to-investigate/wrangler-deps.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,7 @@ export function patchWranglerDeps(config: Config) {
2323

2424
writeFileSync(pagesRuntimeFile, patchedPagesRuntime);
2525

26-
// Patch .next/standalone/node_modules/next/dist/server/lib/trace/tracer.js
27-
//
28-
// Remove the need for an alias in wrangler.toml:
29-
//
30-
// [alias]
31-
// # @opentelemetry/api is `require`d when running wrangler dev, so we need to stub it out
32-
// # IMPORTANT: we shim @opentelemetry/api to the throwing shim so that it will throw right away, this is so that we throw inside the
33-
// # try block here: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31
34-
// # causing the code to require the 'next/dist/compiled/@opentelemetry/api' module instead (which properly works)
35-
// #"@opentelemetry/api" = "./.next/standalone/node_modules/cf/templates/shims/throw.ts"
36-
const tracerFile = join(distPath, "server", "lib", "trace", "tracer.js");
37-
38-
const patchedTracer = readFileSync(tracerFile, "utf-8").replaceAll(
39-
/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g,
40-
`throw new Error("@opentelemetry/api")`
41-
);
42-
43-
writeFileSync(tracerFile, patchedTracer);
26+
patchTracerFile(join(distPath, "server", "lib", "trace", "tracer.js"));
4427
}
4528

4629
/**
@@ -67,3 +50,27 @@ function getDistPath(config: Config): string {
6750

6851
throw new Error("Unexpected error: unable to detect the node_modules/next/dist directory");
6952
}
53+
54+
/**
55+
* Patch trace/tracer.js files that require from `@opentelemetry/api` by replacing such `require`
56+
* calls with error throwing expressions.
57+
*
58+
* The replacement works because code that requires from `@opentelementry/api` is `try-catch`ed
59+
* and a supported alternative is imported in the catch blocks
60+
* (see: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31)
61+
*
62+
* @param tracerFilePath path to the tracer file to patch
63+
*/
64+
export function patchTracerFile(tracerFilePath: string) {
65+
const tracerFileContent = readFileSync(tracerFilePath, "utf-8");
66+
const patchedTracerFileContent = tracerFileContent.replaceAll(
67+
/\w+\s*=\s*require\([^/]*opentelemetry.*\)/g,
68+
`throw new Error("@opentelemetry/api")`
69+
);
70+
71+
if (patchedTracerFileContent === tracerFileContent) {
72+
throw new Error(`Failed to patch tracer file at ${tracerFilePath}`);
73+
}
74+
75+
writeFileSync(tracerFilePath, patchedTracerFileContent);
76+
}

0 commit comments

Comments
 (0)