Skip to content

Commit 2b5ae26

Browse files
fix: externalize @opentelemetry/api to prevent chunk colocation
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
1 parent 96898e9 commit 2b5ae26

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@sveltejs/kit': patch
3+
'@sveltejs/adapter-node': patch
4+
---
5+
6+
fix: externalize `@opentelemetry/api` to prevent bundler chunk colocation between `instrumentation.server.js` and application code

packages/adapter-node/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,12 @@ export default function (opts = {}) {
7777
input,
7878
external: [
7979
// dependencies could have deep exports, so we need a regex
80-
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`))
80+
...Object.keys(pkg.dependencies || {}).map((d) => new RegExp(`^${d}(\\/.*)?$`)),
81+
// `@opentelemetry/api` is an optional peer dependency of `@sveltejs/kit`,
82+
// so it's not in `pkg.dependencies` and wouldn't be matched by the regex above.
83+
// It must stay external so that `instrumentation.server.js` and the SvelteKit
84+
// runtime share a single instance — see https://github.com/sveltejs/kit/issues/16288
85+
/^@opentelemetry\/api(\/.*)?$/
8186
],
8287
platform: 'node',
8388
resolve: {

packages/kit/src/exports/vite/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,18 @@ function kit({ svelte_config }) {
494494

495495
// These Kit dependencies are packaged as CommonJS, which means they must always be externalized.
496496
// Without this, the tests will still pass but `pnpm dev` will fail in projects that link `@sveltejs/kit`.
497-
/** @type {NonNullable<UserConfig['ssr']>} */ (new_config.ssr).external = ['cookie'];
497+
//
498+
// `@opentelemetry/api` must be externalized so that `instrumentation.server.js` and the
499+
// SvelteKit runtime share a single instance of the module (the global tracer/propagation
500+
// is set on that instance — two bundled copies would mean instrumentation hooks are
501+
// invisible to the runtime). Externalizing also prevents the bundler from colocating
502+
// `@opentelemetry/api` into a shared chunk that also contains application modules, which
503+
// would cause those modules to be evaluated before `Server.init()` sets env vars — see
504+
// https://github.com/sveltejs/kit/issues/16288
505+
/** @type {NonNullable<UserConfig['ssr']>} */ (new_config.ssr).external = [
506+
'cookie',
507+
'@opentelemetry/api'
508+
];
498509
}
499510

500511
// Vite's `define` is a compile-time text replacement, but Vitest strips

0 commit comments

Comments
 (0)