Skip to content

Commit 0257c06

Browse files
instead of patching the tracer.js file to throw on @opentelemetry/api imports, delete the @opentelemetry/api dependency itself
1 parent e6078b5 commit 0257c06

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

Diff for: .changeset/forty-jobs-press.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
instead of patching the `tracer.js` file to throw on `@opentelemetry/api` imports, delete the `@opentelemetry/api` dependency itself
6+
7+
the problem that this addresses is that the `@opentelemetry/api` package is not only imported by the `tracer.js` file
8+
we patch, so just deleting the library itself makes sure that all files requiring it get the same throwing behavior
9+
(besides decreasing the overall worker size)

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ 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 { deleteOpenTelemetryDep } from "../patches/index.js";
2021
import { normalizePath } from "../utils/index.js";
2122

2223
export async function createServerBundle(options: buildHelper.BuildOptions) {
@@ -86,12 +87,14 @@ export async function createServerBundle(options: buildHelper.BuildOptions) {
8687
}
8788

8889
// Generate default function
89-
await generateBundle("default", options, {
90+
const outputPath = await generateBundle("default", options, {
9091
...defaultFn,
9192
// @ts-expect-error - Those string are RouteTemplate
9293
routes: Array.from(remainingRoutes),
9394
patterns: ["*"],
9495
});
96+
97+
deleteOpenTelemetryDep(outputPath);
9598
}
9699

97100
async function generateBundle(
@@ -250,6 +253,8 @@ CMD ["node", "index.mjs"]
250253
`
251254
);
252255
}
256+
257+
return outputPath;
253258
}
254259

255260
function shouldGenerateDockerfile(options: FunctionOptions) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { rmSync } from "node:fs";
2+
import { join } from "node:path";
3+
4+
/**
5+
* Given a directory path, it deletes a `node_modules/@opentelemetry` subdirectory if present.
6+
*
7+
* Explanation:
8+
* The standard `@opentelemetry/api` library doesn't work in workerd since there are paths that it can't resolve (without a
9+
* compilation step), fortunately Next.js has a try-catch statement that replaces, when failing, `require('@opentelemetry/api')`
10+
* calls with a precompiled version of the library ('next/dist/compiled/@opentelemetry/api') which does properly in our runtime
11+
* (source code: https://github.com/vercel/next.js/blob/9e8266a7/packages/next/src/server/lib/trace/tracer.ts#L27-L31)
12+
*
13+
* So this function is used to delete the `@opentelemetry` dependency entirely so to guarantee that
14+
* `require('@opentelemetry/api')` fail ensuring that the precompiled version is used
15+
*/
16+
export async function deleteOpenTelemetryDep(path: string): Promise<void> {
17+
const nodeModulesDirPath = join(path, "node_modules");
18+
19+
rmSync(join(nodeModulesDirPath, "@opentelemetry"), {
20+
recursive: true,
21+
force: true,
22+
});
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./copy-package-cli-files.js";
2+
export * from "./delete-open-telemetry-dep.js";
23
export * from "./patch-cache.js";
34
export * from "./patch-require.js";
45
export * from "./update-webpack-chunks-file/index.js";

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

+3-19
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import * as ts from "ts-morph";
55

66
import { Config } from "../../../config.js";
77
import { tsParseFile } from "../../utils/index.js";
8+
import { deleteOpenTelemetryDep } from "../investigated/delete-open-telemetry-dep.js";
89

910
export function patchWranglerDeps(config: Config) {
1011
console.log("# patchWranglerDeps");
1112

13+
deleteOpenTelemetryDep(config.paths.output.standaloneRoot);
14+
1215
const distPath = getDistPath(config);
1316
// Patch .next/standalone/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js
1417
//
@@ -27,25 +30,6 @@ export function patchWranglerDeps(config: Config) {
2730
writeFileSync(pagesRuntimeFile, patchedPagesRuntime);
2831

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

5135
/**

0 commit comments

Comments
 (0)