fix: externalize @opentelemetry/api to prevent chunk colocation - #16302
Conversation
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/eb39e12d567c48aed1e937b353500dda83918a4eOpen in |
🦋 Changeset detectedLatest commit: eb39e12 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 |
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... |
2b5ae26 to
fc13f64
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
fc13f64 to
eb39e12
Compare
813726d
into
version-3
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>
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>
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.