Repository navigation
feat(bundler-plugins): Add the call-site rewriter core - #26101
Conversation
size-limit report 📦Compared: 42a4a6f (PR test merge using develop at a12f1aa) Against: a12f1aa (develop) · base run #68171 ✅ Baseline matches: this diff contains only this PR's changes.
|
Optional E2E testsSome optional checks were not successful. They do not block the merge. 7 not successful, 49 successful. See the run.
Successful checks
|
03fd861 to
d2efbeb
Compare
fe24b4d to
8b2355d
Compare
| if (include.basename && !include.basename.test(basename(id))) return false; | ||
| if (include.contains && !include.contains.every(substring => code.includes(substring))) return false; | ||
| return true; | ||
| } |
There was a problem hiding this comment.
Rule matching ignores Vite query suffixes
Medium Severity
matches tests include.id and include.basename against the raw module id, while langFromId already strips query and hash via stripUrlQueryAndFragment. Vite IDs commonly look like +page.ts?t=123 or file.ts?v=hash, so patterns such as /\.ts$/ and /^\+page\.ts$/ fail to match and the module is left uninstrumented.
Reviewed by Cursor Bugbot for commit 8b2355d. Configure here.
There was a problem hiding this comment.
The SDK plugins test the raw id on develop too, so this keeps current behaviour; query ids can come in a follow-up.
| if (classNode.id?.name) classes.set(classNode.id.name, classNode); | ||
| } | ||
| return classes; | ||
| } |
There was a problem hiding this comment.
Default-exported classes are not collected
Medium Severity
collectTopLevelClasses and findClassesExtending unwrap only export class via declarationOf, so export default class Foo extends Base {} never enters the class map. shapeFromAst in the same package does record that form. Callers that wrap named re-exports of Foo then miss the local class, skip wrappedLocals, and can wrap the default export a second time.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 8b2355d. Configure here.
There was a problem hiding this comment.
This code moved unchanged from the Cloudflare plugin on develop, so it is not new in this PR.
| "types": "./build/types/rewriter/index.d.ts", | ||
| "import": "./build/esm/rewriter/index.js", | ||
| "require": "./build/cjs/rewriter/index.js" | ||
| }, |
There was a problem hiding this comment.
Rewriter export missing runtime-entries coverage
Low Severity
This was flagged because it is called out in the review rules file: a new ./rewriter export that can load on Node, while @sentry/bundler-plugins has no entry in CASES or BUILD_TIME_CASES in dev-packages/bundler-tests/tests/runtime-entries.test.ts. That test is what keeps server runtime entries from pulling in build-time tooling.
Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 8b2355d. Configure here.
There was a problem hiding this comment.
./rewriter is build-time code that only the SDK Vite plugins import, and BUILD_TIME_PATTERNS already fails any runtime entry that pulls in @sentry/bundler-plugins.
| return wrappedName ? `${wrappedName} as ${name}` : name; | ||
| }); | ||
| // A second call for another name of this declaration overwrites the same range again. | ||
| state.ms.overwrite(exportNode.start, declaration.start, `export { ${specifiers.join(', ')} };\n`); |
There was a problem hiding this comment.
In Cloudflare we have wrapInlineClassExport, keep in mind that the snippet right now does not support classes as it would cut the class keyword from the export.
There was a problem hiding this comment.
This only replaces the export text before the class, so the class keyword stays (export class A {} becomes export { __SENTRY_WRAPPED_A__ as A }; then class A {}). Testing it did show that @dec export class A {} gave invalid output. b449f6a now puts the export clause before the decorators and adds tests.
b449f6a to
65ef1b6
Compare
| /** The declaration a top-level statement makes, looking through `export` (`export const x = ...`). */ | ||
| export function declarationOf(node: BaseNode): BaseNode | null | undefined { | ||
| return node.type === 'ExportNamedDeclaration' ? (node as ExportNamedNode).declaration : node; | ||
| } |
There was a problem hiding this comment.
Default-exported classes are not found
Medium Severity
declarationOf only unwraps ExportNamedDeclaration, so findClassesExtending, collectTopLevelClasses, and collectDeclaringStatements never see export default class Foo {} or export default function foo() {}. shapeFromAst already records those named default classes, so the collectors disagree on the same module. A default-exported WorkerEntrypoint subclass is a common Cloudflare form and would not be found.
Additional Locations (2)
Reviewed by Cursor Bugbot for commit 65ef1b6. Configure here.
There was a problem hiding this comment.
The Cloudflare class scans on develop also skip export default class, so this is not new in this PR.
| const warning = formatReport(state.report, id); | ||
| if (warning) options.logger?.warn(warning); | ||
| if (!state.ms.hasChanged()) return null; | ||
| return { code: state.ms.toString(), map: state.ms.generateMap({ hires: true }) }; |
There was a problem hiding this comment.
Source maps omit original file path
Low Severity
generateMap is called with only hires: true, even though transform already has the module id. MagicString then emits sources: [null] and no sourcesContent. The component-annotation transform in this same package passes source and includeContent so composed maps keep a real path. Debuggers and overlay tools can fail to map rewritten frames back to the user file.
Reviewed by Cursor Bugbot for commit 65ef1b6. Configure here.
There was a problem hiding this comment.
This matches the generateMap({ hires: true }) call the Cloudflare and SvelteKit plugins use on develop, and Vite fills in the source path when it combines the maps.
65ef1b6 to
5ec67be
Compare
|
|
||
| const wrappedName = `__SENTRY_WRAPPED_${localName}__`; | ||
| state.wrappedLocals.set(localName, wrappedName); | ||
| state.ms.appendLeft(declaringNode.end, `\nconst ${wrappedName} = ${wrap(localName)};\n`); |
There was a problem hiding this comment.
Wrapped function exports introduce TDZ
Medium Severity
wrapLocalBinding always binds the wrapper with const, and wrapInlineExport re-points the export at that binding. A hoisted export function is then a live binding to a const still in the temporal dead zone, so a circular import that previously read the function during evaluation now throws and can stop the module from loading. The default-export path already uses var for this reason.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 5ec67be. Configure here.
There was a problem hiding this comment.
A circular import that calls an entry export or load while its module is still loading is rare enough that we keep const, since var would only trade the error for undefined.
| const decorators = state.ms.original.slice(start, exportNode.start); | ||
| // A second call for another name of this declaration overwrites the same range again. | ||
| state.ms.overwrite(start, declaration.start, `export { ${specifiers.join(', ')} };\n${decorators}`); | ||
| return true; |
There was a problem hiding this comment.
Overload signatures emit duplicate exports
Medium Severity
wrapInlineExport treats every export function with a matching name as a runtime declaration, including TypeScript overload signatures that have no body. Combined with findNamedExports, wrapping load emits a second export { __SENTRY_WRAPPED_load__ as load } and inserts const __SENTRY_WRAPPED_load__ = wrap(load) between the overload and its implementation, which is invalid TypeScript and a duplicate export.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 5ec67be. Configure here.
There was a problem hiding this comment.
Overload signatures parse as TSDeclareFunction, which declaredNames skips, so only the implementation is wrapped once and the signature is stripped with the other types.
3208230 to
f96f39e
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
There are 8 total unresolved issues (including 7 from previous reviews).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f96f39e. Configure here.
| const wrappedName = `__SENTRY_WRAPPED_${exportedAs}__`; | ||
| prelude.push(`const ${wrappedName} = ${wrap(target)};`); | ||
| state.needsImport = true; | ||
| return `${wrappedName} as ${exportedAs}`; |
There was a problem hiding this comment.
Generated specifiers drop required quotes
Medium Severity
exportedName() now unwraps string literal export and import names, but wrapForeignExport and importAs interpolate those values as bare identifiers. String names that are not valid identifiers, including reserved words, produce invalid import/export clauses and broken __SENTRY_* bindings. The previous Identifier-only path left those specifiers unchanged.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit f96f39e. Configure here.
There was a problem hiding this comment.
The Cloudflare and SvelteKit callers only pass identifier names to these helpers and skip string-literal locals, so no generated clause gets a name that needs quotes.
Three SDKs rewrite user modules at build time with three parsers and their own export handling. Fixes for one export form landed in one SDK only. This adds one core under `@sentry/bundler-plugins/rewriter` for them to share. It parses with oxc, so raw TypeScript parses too, and it keeps the edits in one MagicString, so every rewrite carries a source map. When the plugin context is Rolldown's, the host parses with its `this.parse`, which reads TypeScript. Rollup's cannot, so other bundlers parse with oxc. oxc loads with a dynamic import, so a platform without its native binary gets a warning and unchanged modules instead of a failed build.
CodeQL flagged `/[?#].*$/` as slow on ids with many `#` characters. `stripUrlQueryAndFragment` from `@sentry/core` splits at the first `?` or `#` instead.
…inline class export
768623f to
f54f2e3
Compare


Cloudflare, SvelteKit and TanStack Start each rewrite user modules at build time, each with its own parser and its own export handling, so a fix for one export form reaches only one SDK. This adds one core under
@sentry/bundler-plugins/rewriterfor them to share; the next PRs in this stack move the three SDKs onto it, and no SDK uses it in this PR. It parses with oxc, so raw TypeScript parses too, and it keeps all edits in one MagicString, so every rewrite returns a source map. Under Rolldown it parses withthis.parse. Rollup's parser cannot read TypeScript, so other bundlers parse with oxc.oxc loads with a dynamic
import(). This goes against the lazy-loading rule in AGENTS.md, but if a platform has no oxc native binary, the build logs one warning and leaves modules unchanged instead of failing. The component-annotation code loads oxc the same way.How to review: Start with
rewriter.tsfor the host and the rule model, thenexports.ts, which holds the export forms that come from Cloudflare. There is one test file per module.Unsure about: The dynamic import. Also, every SDK plugin still parses each file on its own. A follow-up PR will make
sentryVitePluginthe host, so all rules share one parse per file.Ref #25776