Skip to content

fix: externalize @opentelemetry/api to prevent chunk colocation - #16302

Merged
elliott-with-the-longest-name-on-github merged 1 commit into
version-3from
elliott/externalize-opentelemetry-api
Aug 10, 2026
Merged

fix: externalize @opentelemetry/api to prevent chunk colocation#16302
elliott-with-the-longest-name-on-github merged 1 commit into
version-3from
elliott/externalize-opentelemetry-api

Conversation

@elliott-with-the-longest-name-on-github

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

closes #16288

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(). Any module that reads $env/dynamic/private at module scope silently captures undefined.

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

  • Added @opentelemetry/api to ssr.external in the kit Vite plugin, so it stays as a bare import('@opentelemetry/api') in the build output rather than being bundled into a shared chunk.
  • Added @opentelemetry/api to adapter-node's external list (it's an optional peer dep, not in pkg.dependencies, so the existing regex wouldn't match it).

Why externalize?

  1. Prevents chunk colocation — no shared chunks between instrumentation and app code via this dep, so importing @opentelemetry/api from instrumentation can never cause app modules to evaluate.
  2. OTEL correctness — OpenTelemetry requires a single @opentelemetry/api module 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:

  • adapter-vercel serverless uses nodeFileTrace (copies deps, doesn't bundle) — the bare import resolves from node_modules.
  • adapter-vercel edge and adapter-netlify edge already bundle instrumentation as a separate single-file graph, and the edge bundler resolves the bare import into the bundle.
  • adapter-netlify serverless copies the kit server output directly via builder.writeServer.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Jul 9, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from eb39e12:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/eb39e12d567c48aed1e937b353500dda83918a4e

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16302

@changeset-bot

changeset-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: eb39e12

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@sveltejs/kit Patch
@sveltejs/adapter-node Patch

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

@svelte-docs-bot

Copy link
Copy Markdown

@Rich-Harris

Copy link
Copy Markdown
Member

Is this just treating a symptom? Couldn't the same thing happen with any module that was imported by both instrumentation.server.ts and app code?

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor Author

You're correct. The only foolproof solution to this problem is externalizing all dependencies of instrumentation.server.ts to ensure they're not bundled with app code. In practice, though, tracing setup code other than @opentelemetry/api is very rarely something you're going to end up importing anywhere but your setup file.

Not sure what a better solution would look like...

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
@teemingc
teemingc force-pushed the elliott/externalize-opentelemetry-api branch from fc13f64 to eb39e12 Compare August 10, 2026 18:34
@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github merged commit 813726d into version-3 Aug 10, 2026
27 checks passed
@elliott-with-the-longest-name-on-github
elliott-with-the-longest-name-on-github deleted the elliott/externalize-opentelemetry-api branch August 10, 2026 19:24
teemingc added a commit that referenced this pull request Aug 11, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to version-3, this PR
will be updated.

⚠️⚠️⚠️⚠️⚠️⚠️

`version-3` is currently in **pre mode** so this branch has prereleases
rather than normal releases. If you want to exit prereleases, run
`changeset pre exit` on `version-3`.

⚠️⚠️⚠️⚠️⚠️⚠️

# Releases
## @sveltejs/kit@3.0.0-next.19

### Major Changes

- breaking: move `defineParams` and associated types to
`@sveltejs/kit/params`
([#16716](#16716))

- breaking: run all errors through the `handleError` hook
([#16664](#16664))

- breaking: move `Page`, `ReadonlyURL` and `ReadonlyURLSearchParams`
from `@sveltejs/kit` to `$app/state`
([#16694](#16694))

- breaking: move `BeforeNavigate`, `OnNavigate`, `AfterNavigate`,
`Navigation`, `NavigationTarget`, `NavigationType`, `GotoOptions` and
the `Navigation*` variant types from `@sveltejs/kit` to
`$app/navigation` ([#16694](#16694))

- breaking: move `ActionResult` and `SubmitFunction` from
`@sveltejs/kit` to `$app/forms`
([#16694](#16694))

- breaking: remove `handleValidationError` and pass remote function
validation errors to `handleError` with `kind: 'validation'`
([#16672](#16672))

### Minor Changes

- feat: ignore files with + prefix if they contain test/spec/stories
([#16715](#16715))

### Patch Changes

- fix: rebuild the dev manifest when route files disappear during an
incremental update
([#16643](#16643))

- fix: externalize `@opentelemetry/api` to prevent bundler chunk
colocation between `instrumentation.server.js` and application code
([#16302](#16302))

- fix: surface prerender errors during development
([#16507](#16507))
## @sveltejs/adapter-node@6.0.0-next.9

### Patch Changes

- fix: externalize `@opentelemetry/api` to prevent bundler chunk
colocation between `instrumentation.server.js` and application code
([#16302](#16302))
- Updated dependencies
[[`04f9ab4`](04f9ab4),
[`1e198bd`](1e198bd),
[`813726d`](813726d),
[`a115a7b`](a115a7b),
[`031ac69`](031ac69),
[`dc7442c`](dc7442c),
[`19c4478`](19c4478),
[`dc7442c`](dc7442c),
[`dc7442c`](dc7442c),
[`0b3e2b3`](0b3e2b3)]:
  - @sveltejs/kit@3.0.0-next.19

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tee Ming <chewteeming01@gmail.com>
teemingc pushed a commit that referenced this pull request Aug 11, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to version-3, this PR
will be updated.

⚠️⚠️⚠️⚠️⚠️⚠️

`version-3` is currently in **pre mode** so this branch has prereleases
rather than normal releases. If you want to exit prereleases, run
`changeset pre exit` on `version-3`.

⚠️⚠️⚠️⚠️⚠️⚠️

# Releases
## @sveltejs/kit@3.0.0-next.19

### Major Changes

- breaking: move `defineParams` and associated types to
`@sveltejs/kit/params`
([#16716](#16716))

- breaking: run all errors through the `handleError` hook
([#16664](#16664))

- breaking: move `Page`, `ReadonlyURL` and `ReadonlyURLSearchParams`
from `@sveltejs/kit` to `$app/state`
([#16694](#16694))

- breaking: move `BeforeNavigate`, `OnNavigate`, `AfterNavigate`,
`Navigation`, `NavigationTarget`, `NavigationType`, `GotoOptions` and
the `Navigation*` variant types from `@sveltejs/kit` to
`$app/navigation` ([#16694](#16694))

- breaking: move `ActionResult` and `SubmitFunction` from
`@sveltejs/kit` to `$app/forms`
([#16694](#16694))

- breaking: remove `handleValidationError` and pass remote function
validation errors to `handleError` with `kind: 'validation'`
([#16672](#16672))

### Minor Changes

- feat: ignore files with + prefix if they contain test/spec/stories
([#16715](#16715))

### Patch Changes

- fix: rebuild the dev manifest when route files disappear during an
incremental update
([#16643](#16643))

- fix: externalize `@opentelemetry/api` to prevent bundler chunk
colocation between `instrumentation.server.js` and application code
([#16302](#16302))

- fix: surface prerender errors during development
([#16507](#16507))

- fix: adjust error overload for optional `App.Error` parameters
([#16725](#16725))
## @sveltejs/adapter-node@6.0.0-next.9

### Patch Changes

- fix: externalize `@opentelemetry/api` to prevent bundler chunk
colocation between `instrumentation.server.js` and application code
([#16302](#16302))
- Updated dependencies
[[`04f9ab4`](04f9ab4),
[`1e198bd`](1e198bd),
[`813726d`](813726d),
[`a115a7b`](a115a7b),
[`031ac69`](031ac69),
[`dc7442c`](dc7442c),
[`19c4478`](19c4478),
[`dc7442c`](dc7442c),
[`dc7442c`](dc7442c),
[`0b3e2b3`](0b3e2b3),
[`c5d0ce2`](c5d0ce2)]:
  - @sveltejs/kit@3.0.0-next.19

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants