Skip to content

fix(cloudflare): Run build-time instrumentation in vite dev - #25777

Open
JPeer264 wants to merge 2 commits into
jp/orchestrion-transform-sourcemap-sourcesfrom
jp/cloudflare-vite-dev-instrumentation
Open

JPeer264 wants to merge 2 commits into
jp/orchestrion-transform-sourcemap-sourcesfrom
jp/cloudflare-vite-dev-instrumentation

Conversation

@JPeer264

@JPeer264 JPeer264 commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

part of #25765
part of JS-4010

Description

This runs the orchestrion transform in the Vite dependency optimizer, so integrations that are added at build time (for example hono and graphql) also send spans in vite dev on Cloudflare. @cloudflare/vite-plugin pre-bundles the Worker dependencies into node_modules/.vite/deps_*, and before, the transform did not run on these files. The plugin adds a Rolldown plugin for Vite 8 and an esbuild plugin for Vite 6 and 7.

The new transformOptimizedDeps plugin option turns this on, and sentryCloudflareVitePlugin sets it. Node servers do not need it, because the runtime hook instruments the dependencies that Node loads.

The Cloudflare variant of the hono-4 e2e app now also runs its tests in vite dev.

Known issues

Sentry traces

Before (11.9.0), /graphql in vite dev, no graphql spans: https://sentry-sdks.sentry.io/explore/traces/trace/9221590d886f4648b401eded8a996046/
After, /graphql in vite dev, with graphql spans: https://sentry-sdks.sentry.io/explore/traces/trace/2aea20834d3d4fa38e39d37166273e14/
Before (11.9.0), /hono/users/1 in vite dev, span name GET /hono/users/1: https://sentry-sdks.sentry.io/explore/traces/trace/b8e5b8dba83b4b5aba45bdfdf40b307c/
After, /hono/users/1 in vite dev, span name GET /hono/users/:id from the Hono route: https://sentry-sdks.sentry.io/explore/traces/trace/20b2a5dcfe6a4dbfb7e68eb3037bf3b2/

`@cloudflare/vite-plugin` pre-bundles the worker dependencies in `vite dev`,
and the Vite dependency optimizer runs no Vite plugins. So the orchestrion
transform never ran on instrumented packages, and their spans were missing.

The orchestrion Vite plugin now adds the transform to `optimizeDeps` of
server environments in `serve`: a Rolldown plugin on Vite 8 and an esbuild
plugin on Vite 6 and 7. Both resolve the snippet import to the ESM entry of
`@sentry/server-utils`, like the build does.

The cloudflare variant of the `hono-4` e2e app now also runs its tests
against `vite dev`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JPeer264
JPeer264 added this pull request to stack #25778 October 27, 2026 17:47
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Compared: b11dd91 (PR test merge using develop at 609a0cc)

Against: a6c28d0 (develop) · base run #63865

⚠️ Baseline mismatch: 2 develop commit(s) in the PR build are missing from the baseline; 16 develop commit(s) in the baseline are missing from the PR build. Their size changes also affect this comparison. Compare commits. Rebase your PR onto the latest develop to compare against an up-to-date baseline.

Path Size % Change Change
@sentry/browser 30.02 kB -1.04% -315 B 🔽
@sentry/browser - with treeshaking flags 28.16 kB -1.03% -291 B 🔽
@sentry/browser - with treeshaking flags tracing without tracing 28.06 kB -1% -283 B 🔽
@sentry/browser (incl. Tracing) 52.21 kB -0.63% -329 B 🔽
@sentry/browser (incl. Tracing + Span Streaming) 52.22 kB -0.61% -319 B 🔽
@sentry/browser (incl. Tracing, Profiling) 55.14 kB -0.59% -326 B 🔽
@sentry/browser (incl. Tracing, Replay) 91.98 kB -0.35% -314 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 80.89 kB -0.36% -289 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas) 96.72 kB -0.33% -315 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 109.7 kB -0.3% -329 B 🔽
@sentry/browser (incl. Feedback) 47.58 kB -0.65% -311 B 🔽
@sentry/browser (incl. sendFeedback) 35.09 kB -0.9% -317 B 🔽
@sentry/browser (incl. FeedbackAsync) 40.19 kB -0.8% -324 B 🔽
@sentry/browser (incl. Metrics) 31.04 kB -0.95% -296 B 🔽
@sentry/browser (incl. Logs) 31.32 kB -0.96% -302 B 🔽
@sentry/browser (incl. Metrics & Logs) 31.99 kB -0.96% -310 B 🔽
@sentry/react 31.84 kB -0.94% -300 B 🔽
@sentry/react (incl. Tracing) 54.52 kB -0.54% -292 B 🔽
@sentry/vue 38.17 kB -0.76% -291 B 🔽
@sentry/vue (incl. Tracing) 55.12 kB -0.6% -328 B 🔽
@sentry/svelte 30.04 kB -1.04% -315 B 🔽
@sentry/remix (Remix 3 client bundle) 57.24 kB -0.62% -355 B 🔽
CDN Bundle 31.79 kB -0.88% -279 B 🔽
CDN Bundle (incl. Tracing) 52.73 kB -0.56% -294 B 🔽
CDN Bundle (incl. Logs, Metrics) 33.99 kB -0.8% -272 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) 54.7 kB -0.54% -292 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) 74.96 kB -0.38% -285 B 🔽
CDN Bundle (incl. Tracing, Replay) 90.45 kB -0.32% -288 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 92.36 kB -0.33% -305 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 96.56 kB -0.3% -288 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 98.53 kB -0.3% -291 B 🔽
CDN Bundle - uncompressed 93.62 kB -0.72% -670 B 🔽
CDN Bundle (incl. Tracing) - uncompressed 156.47 kB -0.43% -670 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed 100.17 kB -0.67% -670 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 162.44 kB -0.42% -670 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 230.39 kB -0.29% -670 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed 276.81 kB -0.25% -670 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 282.77 kB -0.24% -670 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 290.51 kB -0.24% -670 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 296.46 kB -0.23% -670 B 🔽
@sentry/nextjs (client) 57.42 kB -0.47% -270 B 🔽
@sentry/sveltekit (client) 52.56 kB -0.84% -441 B 🔽
@sentry/core/server 43.66 kB -0.09% -36 B 🔽
@sentry/core/browser 13.81 kB - -
@sentry/node 154.13 kB -0.07% -94 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 81.61 kB -0.01% -3 B 🔽
@sentry/node - without tracing 95.56 kB +0.04% +36 B 🔺
@sentry/node - without channel injection 134.78 kB -0.09% -121 B 🔽
@sentry/aws-serverless 103.73 kB +0.03% +28 B 🔺
@sentry/cloudflare (withSentry) - minified 197.4 kB +0.07% +123 B 🔺
@sentry/cloudflare (withSentry) 490.4 kB +0.07% +323 B 🔺
@sentry/nextjs/cloudflare (withSentry) - minified 217.98 kB +0.06% +123 B 🔺

View base workflow run

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Optional E2E tests

Some optional checks were not successful. They do not block the merge. 4 not successful, 61 successful. See the run.

Check Status
❌ CI: Optional E2E Tests / E2E node-anthropic-send-to-sentry (latest) Test (optional) Failing after 11s Details
❌ CI: Optional E2E Tests / E2E node-openai-decisions-send-to-sentry Test (optional) Failing after 4m Details
❌ CI: Optional E2E Tests / E2E node-eve (latest) Test (optional) Failing after 2m Details
❌ CI: Optional E2E Tests / E2E node-openai-decisions-send-to-sentry (latest) Test (optional) Failing after 4m Details
Successful checks
Check Status
✅ CI: Optional E2E Tests / E2E cloudflare-think (ai v6) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-think Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext, bun) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E cloudflare-agent Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (canary) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-flue-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E astro-7 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E angular (canary) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E debug-id-sourcemaps Test (optional) Successful in 43s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries Test (optional) Successful in 48s Details
✅ CI: Optional E2E Tests / E2E nuxt-5 Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (latest) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E cloudflare-workers-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E angular (canary) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable Test (optional) Successful in 35s Details
✅ CI: Optional E2E Tests / E2E nextjs-15 (turbo) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nextjs-14 (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (canary, turbopack) Test (optional) Successful in 4m Details
✅ CI: Optional E2E Tests / E2E astro-7-static Test (optional) Successful in 51s Details
✅ CI: Optional E2E Tests / E2E gen-ai-libraries (cloudflare) Test (optional) Successful in 27s Details
✅ CI: Optional E2E Tests / E2E deno (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-standalone (canary, turbopack) Test (optional) Successful in 58s Details
✅ CI: Optional E2E Tests / E2E cloudflare-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16-tunnel (cloudflare) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (canary, webpack) Test (optional) Successful in 5m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-pi-durable (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E cloudflare-mastra Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (deno) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 (tracer provider) Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E nextjs-orpc Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-fastify-6 Test (optional) Successful in 22s Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (vinext) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v1 Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E node-mastra (latest) Test (optional) Successful in 49s Details
✅ CI: Optional E2E Tests / E2E node-eve Test (optional) Successful in 38s Details
✅ CI: Optional E2E Tests / E2E nuxt-4-static (canary) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E vue-3 (no Options API) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-mcp-v2 Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-anthropic-send-to-sentry Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-eve (orchestrion) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-send-to-sentry Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nuxt-3 (canary) Test (optional) Successful in 59s Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (static / nuxt generate) Test (optional) Successful in 56s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-sourcemaps Test (optional) Successful in 21s Details
✅ CI: Optional E2E Tests / E2E tanstackstart-react-cloudflare Test (optional) Successful in 38s Details
✅ CI: Optional E2E Tests / E2E supabase-nextjs Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-mastra Test (optional) Successful in 55s Details
✅ CI: Optional E2E Tests / E2E node-express-send-to-sentry-static Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-openai-send-to-sentry (latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E vue-3 (canary) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E node-flue (latest) Test (optional) Successful in 1m Details
✅ CI: Optional E2E Tests / E2E nuxt-4 (canary) Test (optional) Successful in 3m Details
✅ CI: Optional E2E Tests / E2E nuxt-5-sourcemaps (client source maps kept) Test (optional) Successful in 29s Details
✅ CI: Optional E2E Tests / E2E solidstart-2 Test (optional) Successful in 52s Details
✅ CI: Optional E2E Tests / E2E react-router-7-framework-instrumentation Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E nextjs-16 (cloudflare, latest) Test (optional) Successful in 2m Details
✅ CI: Optional E2E Tests / E2E react-send-to-sentry Test (optional) Successful in 2m Details

@isaacs isaacs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design is sound for its target. The main problem is scope. It applies to every server environment of every SDK that uses the shared Vite plugin, not only cloudflare.

Comment on lines +92 to +94
if (isDuplicate()) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is function is gonna cause a crash if it's used in a node bundle with vite.

On Vite 6 and 7, the dependency optimizer uses platform: 'node' for any server environment that is not webworker (the default ssr environment of every Node SSR framework). For that platform, Vite adds this banner to every optimized file:

import { createRequire } from 'module';const require = createRequire(import.meta.url);

The new esbuild plugin makes the optimizer bundle @sentry/server-utils (and @sentry/core) into the pre-bundled dependency.

Two lines in depOptimizerEsbuildPlugin cause this. Line 112 (upstream.setup(build)) adds the transform, which puts an import of @sentry/server-utils into each instrumented module. Line 107 then resolves that import to a file path (toEsmEntry(resolved.path)), so esbuild bundles the file instead of leaving the import external. The @sentry/server-utils ESM build has its own top-level import { createRequire } from "module", and esbuild cannot rename around a text banner. The result is a file that doesn't parse:

vite 6.4.3 esm  load error: Identifier 'createRequire' has already been declared
vite 6.4.3 cjs  load error: Identifier 'createRequire' has already been declared
vite 7.3.6 esm  load error: Identifier 'createRequire' has already been declared
vite 7.3.6 cjs  load error: Identifier 'createRequire' has already been declared
vite 8.3.1 esm  runs: x channel fired: true
vite 8.3.1 cjs  runs: x channel fired: true

Cloudflare is not affected, because @cloudflare/vite-plugin sets platform: 'neutral', which has no banner. With platform: 'neutral' the Vite 6/7 ESM case loads and fires the channel.

Suggestion: scope the dev optimizer plugins to the case this PR targets. Two options:

  1. Minimal: don't register the resolver or the transform when the optimizer bundles for Node. Vite passes the platform it picked to esbuild as build.initialOptions.platform, so setup (line 91) can return before it reaches lines 96-112. The existing duplicate-instance check here already returns early in the same place, so the new check can go next to it:
Suggested change
if (isDuplicate()) {
return;
}
// Vite 6/7 add a `createRequire` banner for `platform: 'node'`, which
// collides with the `createRequire` import in the bundled SDK.
if (build.initialOptions.platform === 'node') {
return;
}
if (isDuplicate()) {
return;
}
  1. Cleaner: add an opt-in option to PluginOptions that sentryCloudflareVitePlugin sets, so the other SDKs keep today's serve behavior. The PR title says fix(cloudflare), and the comment at lines 205-207 names @cloudflare/vite-plugin as the reason. An opt-in would make the change match that scope. It would also avoid the side effects on Node SSR envs. This option touches three places: the new field in PluginOptions (packages/server-utils/src/orchestrion/bundler/options.ts), the serve branch at line 208 of vite.ts (return null when the option is off), and the sentryOrchestrionPlugin({ ... }) call at packages/cloudflare/src/vite/index.ts line 88.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm gonna check. I like the opt-in method.

const plugin = vitePlugin();
// Vite 6 passes no context, Vite 8 passes `rolldownVersion`.
context?: { meta: { rolldownVersion?: string } },
plugin = vitePlugin(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this now takes 5 positional parameters. Might be time for an options object?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's actually better! I'll change it

@JPeer264
JPeer264 marked this pull request as ready for review October 27, 2026 22:10
@JPeer264
JPeer264 requested a review from a team as a code owner October 27, 2026 22:11
@JPeer264
JPeer264 requested review from andreiborza, isaacs and mydea and removed request for a team and mydea October 27, 2026 22:11
@JPeer264

Copy link
Copy Markdown
Member Author

The main problem is scope. It applies to every server environment of every SDK that uses the shared Vite plugin, not only cloudflare.

Good that you brought that up. I actually assumed there might be cases where it is applicable in Node as well. Since on plain Vite+Node it's already working we can do the opt-in as you suggested.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants