Skip to content

fix(cloudflare): Use correct env types for withSentry#19836

Open
JPeer264 wants to merge 1 commit intodevelopfrom
jp/cloudflare-env-types
Open

fix(cloudflare): Use correct env types for withSentry#19836
JPeer264 wants to merge 1 commit intodevelopfrom
jp/cloudflare-env-types

Conversation

@JPeer264
Copy link
Member

closes #18294
closes JS-1202

By not using unknown but going directly to the env export of cloudflare:workers, this should resolve the typing issue, without changing the current generic API (as proposed in #18302).

The test proofs that when changing the Cloudflare globals, that this works OOTB now.

@JPeer264 JPeer264 requested review from chargome and s1gr1d March 17, 2026 13:33
@JPeer264 JPeer264 self-assigned this Mar 17, 2026
@linear-code
Copy link

linear-code bot commented Mar 17, 2026

@JPeer264 JPeer264 force-pushed the jp/cloudflare-env-types branch from d781f4c to 5fbcf2d Compare March 17, 2026 13:36
@github-actions
Copy link
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 25.64 kB - -
@sentry/browser - with treeshaking flags 24.14 kB - -
@sentry/browser (incl. Tracing) 42.62 kB - -
@sentry/browser (incl. Tracing, Profiling) 47.28 kB - -
@sentry/browser (incl. Tracing, Replay) 81.42 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 71 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 86.12 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 98.37 kB - -
@sentry/browser (incl. Feedback) 42.45 kB - -
@sentry/browser (incl. sendFeedback) 30.31 kB - -
@sentry/browser (incl. FeedbackAsync) 35.36 kB - -
@sentry/browser (incl. Metrics) 26.92 kB - -
@sentry/browser (incl. Logs) 27.07 kB - -
@sentry/browser (incl. Metrics & Logs) 27.74 kB - -
@sentry/react 27.39 kB - -
@sentry/react (incl. Tracing) 44.95 kB - -
@sentry/vue 30.08 kB - -
@sentry/vue (incl. Tracing) 44.48 kB - -
@sentry/svelte 25.66 kB - -
CDN Bundle 28.28 kB +0.04% +11 B 🔺
CDN Bundle (incl. Tracing) 43.51 kB +0.03% +10 B 🔺
CDN Bundle (incl. Logs, Metrics) 29.14 kB +0.05% +12 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) 44.36 kB +0.03% +11 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) 68.21 kB +0.02% +12 B 🔺
CDN Bundle (incl. Tracing, Replay) 80.33 kB +0.02% +13 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 81.23 kB +0.02% +13 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) 85.87 kB +0.02% +12 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 86.77 kB +0.02% +12 B 🔺
CDN Bundle - uncompressed 82.62 kB +0.08% +59 B 🔺
CDN Bundle (incl. Tracing) - uncompressed 128.56 kB +0.05% +59 B 🔺
CDN Bundle (incl. Logs, Metrics) - uncompressed 85.49 kB +0.07% +59 B 🔺
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 131.43 kB +0.05% +59 B 🔺
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 209.12 kB +0.03% +59 B 🔺
CDN Bundle (incl. Tracing, Replay) - uncompressed 245.41 kB +0.03% +59 B 🔺
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 248.26 kB +0.03% +59 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 258.32 kB +0.03% +59 B 🔺
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 261.17 kB +0.03% +59 B 🔺
@sentry/nextjs (client) 47.37 kB - -
@sentry/sveltekit (client) 43.07 kB - -
@sentry/node-core 56.35 kB +0.06% +29 B 🔺
@sentry/node 173.17 kB -0.01% -17 B 🔽
@sentry/node - without tracing 96.35 kB +0.01% +5 B 🔺
@sentry/aws-serverless 113.35 kB +0.01% +11 B 🔺

View base workflow run

*/
export function withSentry<
Env = unknown,
Env = typeof env,
Copy link
Member

Choose a reason for hiding this comment

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

q: Isn't this theoretically breaking?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really see this as a breaking change, as Env is only there so it gets inferred by the third generic entry, which I think just grew over time. So there are couple of usages and non of them look breaking to me:

// sets "Env" directly, so it isn't affected at all
withSentry<{ myEnv: string }>()

// "Env" is manually set, which means it is the same type now
withSentry((env: Env) => ({}), exportedHandler)

// Not setting anything, this wasn't working before, but works with this PR
withSentry(env => ({}), exportedHandler)

It could also be that people used not existing env variables, which wouldn't work as the exported handler is already typed, as documented, with the env usually (which is the same behavior with and without this PR):

const exportedHandler = { fetch() {} } satisfies ExportedHandler<Env>

withSentry((env: { notExistingEnv: string }) => ({}), exportedHandler)
                                                      ^^^^^^^^^^^^^^^

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.

Cloudflare types for env no longer work

2 participants