Skip to content

fix(cloudflare): keep default exports intact when auto-instrumenting - #25372

Merged
isaacs merged 2 commits into
isaacschlueter/js-3934-sentryastro-use-the-cloudflare-vite-plugin-for-workersfrom
isaacs/fix-default-export-transform
Oct 15, 2026
Merged

isaacs merged 2 commits into
isaacschlueter/js-3934-sentryastro-use-the-cloudflare-vite-plugin-for-workersfrom
isaacs/fix-default-export-transform

Conversation

@isaacs

@isaacs isaacs commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

The Vite transform rewrote export default <expr> to const __SENTRY_DEFAULT_EXPORT__ = <expr> and set the wrapped default export at the end of the module. That broke the module in four ways:

  • A parenthesized default export, such as export default (handler);, became a syntax error. The rewrite replaced everything up to the expression, which dropped the ( and kept the ).
  • An anonymous class, function or arrow took the name __SENTRY_DEFAULT_EXPORT__ in place of default. Libraries can read .name, as fix(cloudflare): Keep the user's class name when auto-wrapping an export #24700 found for named classes.
  • export default class Handler {} lost the Handler binding, so other code in the module that used Handler threw.
  • A circular import that read the default export after the original export default statement, but before the end of the module, threw.

The transform now replaces only the export default keywords, keeps a named class or function declaration as it is, and gives an anonymous one the name default by way of an object property.

The default export is set in two steps, as for export { handler as default }. Where the original statement set it, it is bound to the value as-is, so a circular import gets the handler. At the end of the module, it is rebound to the wrapper, so methods added to the handler after export default are instrumented too.

@isaacs
isaacs added this pull request to stack #25346 October 14, 2026 17:05
@isaacs
isaacs requested a review from a team as a code owner October 14, 2026 17:05
@isaacs
isaacs requested review from JPeer264 and mydea and removed request for a team October 14, 2026 17:05

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

Comment thread packages/cloudflare/src/vite/transform.ts Outdated
@isaacs
isaacs force-pushed the isaacs/fix-default-export-transform branch from 34b2d7c to f61848b Compare October 14, 2026 18:18

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f61848b. Configure here.

Comment thread packages/cloudflare/src/vite/transform.ts
@isaacs
isaacs force-pushed the isaacs/fix-default-export-transform branch 3 times, most recently from 7843742 to 48ede38 Compare October 14, 2026 23:36
@JPeer264
JPeer264 force-pushed the isaacs/fix-default-export-transform branch from 48ede38 to 17aa842 Compare October 15, 2026 08:45
@isaacs
isaacs force-pushed the isaacs/fix-default-export-transform branch 2 times, most recently from adc460d to 11dfd25 Compare October 15, 2026 14:27
isaacs and others added 2 commits October 15, 2026 09:43
The Vite transform rewrote `export default <expr>` to
`const __SENTRY_DEFAULT_EXPORT__ = <expr>` and set the wrapped default
export at the end of the module. That broke the module in four ways:

- A parenthesized default export, such as `export default (handler);`,
  became a syntax error. The rewrite replaced everything up to the
  expression, which dropped the `(` and kept the `)`.
- An anonymous class, function or arrow took the name
  `__SENTRY_DEFAULT_EXPORT__` in place of `default`. Libraries can read
  `.name`, as #24700 found for named classes.
- `export default class Handler {}` lost the `Handler` binding, so other
  code in the module that used `Handler` threw.
- A circular import that read the default export after the original
  `export default` statement, but before the end of the module, threw.

The transform now replaces only the `export default` keywords, keeps a
named class or function declaration as it is, and gives an anonymous one
the name `default` by way of an object property.

The default export is set in two steps, as for
`export { handler as default }`. Where the original statement set it,
it is bound to the value as-is, so a circular import gets the handler.
At the end of the module, it is rebound to the wrapper, so methods added
to the handler after `export default` are instrumented too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@isaacs
isaacs force-pushed the isaacs/fix-default-export-transform branch from 11dfd25 to a37b84a Compare October 15, 2026 16:43
@isaacs
isaacs merged commit 6b728c6 into develop Oct 15, 2026
504 of 510 checks passed
@isaacs
isaacs deleted the isaacs/fix-default-export-transform branch October 15, 2026 19:03
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