fix: externalize @opentelemetry/api to prevent chunk colocation#16302
fix: externalize @opentelemetry/api to prevent chunk colocation#16302elliott-with-the-longest-name-on-github wants to merge 1 commit into
@opentelemetry/api to prevent chunk colocation#16302Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/2b5ae26db27ca58ff75eb17e4132987031743460Open in |
🦋 Changeset detectedLatest commit: 2b5ae26 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
90d3458 to
a436ecb
Compare
When `instrumentation.server.js` and application code both import `@opentelemetry/api`, the bundler may colocate it into a shared chunk that also contains application modules. The compiled instrumentation entry then imports that chunk — evaluating application modules before `Server.init()` has called `set_env()`. Externalizing `@opentelemetry/api` ensures: - No shared chunks between instrumentation and app code (via this dep) - A single module instance so OTEL hooks registered in instrumentation are visible to the SvelteKit runtime's tracer Added to `ssr.external` in the kit Vite plugin and to adapter-node's external list (since `@opentelemetry/api` is an optional peer dep, not in `pkg.dependencies`, and wouldn't be matched by the existing regex). Closes #16288
a436ecb to
2b5ae26
Compare
|
Is this just treating a symptom? Couldn't the same thing happen with any module that was imported by both |
|
You're correct. The only foolproof solution to this problem is externalizing all dependencies of Not sure what a better solution would look like... |
closes #16288
When
instrumentation.server.jsand application code both import@opentelemetry/api, the bundler may colocate it into a shared chunk that also contains application modules. The compiled instrumentation entry then imports that chunk — evaluating application modules beforeServer.init()has calledset_env(). Any module that reads$env/dynamic/privateat module scope silently capturesundefined.This is a nastier variant of #14286: there the env modules are imported by instrumentation.server.ts itself; here instrumentation never touches
$env— it merely imports@opentelemetry/api, and the chunking pulls in unrelated app modules.What changed
@opentelemetry/apitossr.externalin the kit Vite plugin, so it stays as a bareimport('@opentelemetry/api')in the build output rather than being bundled into a shared chunk.@opentelemetry/apito adapter-node's external list (it's an optional peer dep, not inpkg.dependencies, so the existing regex wouldn't match it).Why externalize?
@opentelemetry/apifrom instrumentation can never cause app modules to evaluate.@opentelemetry/apimodule instance. The global tracer/propagation is set on that instance. Two bundled copies would mean instrumentation hooks are invisible to the SvelteKit runtime's tracer.Other adapters don't need changes:
nodeFileTrace(copies deps, doesn't bundle) — the bare import resolves fromnode_modules.builder.writeServer.