Repository navigation
feat: add SentryDebugIdPlugin - #1451
elirangoshen wants to merge 5 commits into
Conversation
Stamps a Sentry Debug ID into the bundle and its source map, so Sentry can pair a JS stack trace with the source map that resolves it. This is the Re.Pack counterpart of what @sentry/react-native provides for Metro through createSentryMetroSerializer.
🦋 Changeset detectedLatest commit: 3cd7419 The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 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 |
|
@elirangoshen is attempting to deploy a commit to the Callstack Team on Vercel. A member of the Team first needs to authorize it. |
dannyhw
left a comment
There was a problem hiding this comment.
automated review:
The gap is real (see getsentry/sentry-react-native#3956), and per-chunk IDs are an improvement over the community gronxb/repack-plugin-sentry. Inline comments cover the code. Some broader points:
- PR description is out of date.
@sentry/webpack-plugin5.4.1 (@sentry/bundler-plugins11.x) no longer usesBannerPlugin. It injects atprocessAssetsADDITIONS, filtered byWEBPACK_JAVASCRIPT_ASSET_REGEX. It now also stamps emitted maps insourcemaps.disable: 'disable-upload'mode, but only for files passingisJsFile()(.js/.mjs/.cjs)..bundleis still excluded, so the gap stands. Please update the description. It may also be worth raising upstream, since relaxing those two filters would cover most of this. - HermesBytecodePlugin likely drops the ID.
composeSourceMapsruns RN'scompose-source-maps.jsand replaces the packager map. As far as I can tell, the composed map has nodebugId, so remote chunks compiled to bytecode by Re.Pack would lose it. (Not verified here; the main bundle is covered by RN's nativecopy-debugid.jsstep.) Please check this and either copy the ID across or document the limitation. - Placement. I'd suggest a separate
packages/plugin-sentryrather than core. It's vendor-specific, the snippet has to follow Sentry's format, and it matches theplugin-reanimated/plugin-expo-modulespattern. Keeping it out ofRepackPluginis right.
|
The Hermes interaction is real — I was able to verify it locally, and the scope is slightly different from what the review above assumes.
Reproduced on a real release bundle (RN 0.88, 3.1MB map), driving The cause is The fix looks small: read |
|
Verified the build claims locally on a real RN 0.88 release bundle (~810KB bundle, 3.1MB map):
The mapping-integrity claim holds too, which is the part I was most sceptical about: the same generated token resolves to the same original Repo gates are green on the branch: |
|
Hi, would you like me to apply the requested changes ? |
|
@elirangoshen yes please, feel free to push back on anything that doesn't seem right though of course! |
Sure , I ask as Im not sure whether there is changes you think its better to do before merging :) |
- derive Debug IDs from the chunk's pre-injection content hash, scoped per compilation - inject at processAssets ADDITIONS and stamp maps at DEV_TOOLING + 1 - add the //# debugId= comment before //# sourceMappingURL - skip non-production builds - carry the Debug ID over in HermesBytecodePlugin's composed source map - splice the Debug ID into map JSON instead of a parse/stringify round-trip - fix Repack.plugins.SentryDebugIdPlugin usage in JSDoc and docs - cover multiple chunks, determinism, dev skip, webpack and the package entry in tests
|
On placement: the Hermes part has to live in core either way. The plugin itself could still move to |
|
/review |
Apex Sentinel · ✅ Approve · review of
|
| Where | Finding |
|---|---|
packages/repack/src/plugins/utils/sourceMapDebugId.ts:18 |
Stamping an already-stamped map emits duplicate debugId JSON keys |
packages/repack/src/plugins/SentryDebugIdPlugin.ts:119 |
Prepending the Debug ID snippet breaks strict-mode semantics of the bundle |
4 findings below minor hidden (user_settings.min_severity).
Reviewed with this repository's own Sentinel configuration (b9f19df36563).
💬 Talking to Sentinel
In a finding's thread, reply with:
| Command | What it does |
|---|---|
@apex-sentinel-staging note <context> |
Adds context; Sentinel re-checks the cited code |
@apex-sentinel-staging pivot <why> |
Says the finding is wrong; Sentinel re-checks the cited code |
@apex-sentinel-staging like |
Marks the finding as useful |
@apex-sentinel-staging dislike <why> |
Marks it as not useful, so it is not raised again |
On this conversation, comment @apex-sentinel-staging review to run a full review once again.
There was a problem hiding this comment.
Apex Sentinel: 2 new findings on c805459. The verdict and the full list are in the summary comment.
| export function addDebugIdToSourceMap( | ||
| sourceMap: string, | ||
| debugId: string | ||
| ): string { | ||
| const end = sourceMap.lastIndexOf('}'); | ||
| if (end === -1) { | ||
| throw new Error('Source map is not a JSON object'); | ||
| } | ||
|
|
||
| const head = sourceMap.slice(0, end).trimEnd(); | ||
| const separator = head.endsWith('{') ? '' : ','; | ||
| return `${head}${separator}"debugId":"${debugId}","debug_id":"${debugId}"}`; |
There was a problem hiding this comment.
🟡 Minor · Stamping an already-stamped map emits duplicate debugId JSON keys
addDebugIdToSourceMap splices "debugId":... in without removing an existing one, so a map that already carries one comes out with duplicate keys: addDebugIdToSourceMap('{"version":3,"debugId":"old"}', 'new') yields {"version":3,"debugId":"old","debugId":"new","debug_id":"new"}. This is reachable when @sentry/webpack-plugin is used together with this plugin in sourcemaps.disable: 'disable-upload' mode—both stamp the same map with different IDs, and which one wins depends on the JSON parser.
| export function addDebugIdToSourceMap( | |
| sourceMap: string, | |
| debugId: string | |
| ): string { | |
| const end = sourceMap.lastIndexOf('}'); | |
| if (end === -1) { | |
| throw new Error('Source map is not a JSON object'); | |
| } | |
| const head = sourceMap.slice(0, end).trimEnd(); | |
| const separator = head.endsWith('{') ? '' : ','; | |
| return `${head}${separator}"debugId":"${debugId}","debug_id":"${debugId}"}`; | |
| export function addDebugIdToSourceMap( | |
| sourceMap: string, | |
| debugId: string | |
| ): string { | |
| const end = sourceMap.lastIndexOf('}'); | |
| if (end === -1) { | |
| throw new Error('Source map is not a JSON object'); | |
| } | |
| const head = sourceMap | |
| .slice(0, end) | |
| .replace(/,?\s*"debug_?[iI]d"\s*:\s*"[^"]*"\s*(?=,|\s*$)/g, '') | |
| .trimEnd(); | |
| const separator = head.endsWith('{') ? '' : ','; | |
| return `${head}${separator}"debugId":"${debugId}","debug_id":"${debugId}"}`; | |
| } |
Suggested change
Strip any existing debugId/debug_id field before splicing the new one in (or detect one and skip with a debug log), and document the precedence when @sentry/webpack-plugin has already stamped the same map.
Details
persona: senior-developer, javascript
Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.
Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.
There was a problem hiding this comment.
@apex-sentinel-staging note Fixed in 3cd7419. addDebugIdToSourceMap now overwrites existing debugId/debug_id fields in place (same escaped-quote lookbehind, so sourcesContent is untouched) and only appends a spelling that's missing, so no key appears twice. Skipped the suggested patch: its regex has no lookbehind and can leave {, when the field is first. Tests cover re-stamping a stamped map and a debugId inside sourcesContent.
| compilation.updateAsset( | ||
| file, | ||
| (source) => | ||
| new sources.ConcatSource(getDebugIdSnippet(debugId), source) | ||
| ); |
There was a problem hiding this comment.
🟡 Minor · Prepending the Debug ID snippet breaks strict-mode semantics of the bundle
Prepending ;{try{...}} before a bundle whose first statement is "use strict" (or "use asm" or a shebang) means the directive is no longer the first statement, so the whole bundle silently loses strict-mode. The snippet is inserted as the very first bytes of the asset via ConcatSource(getDebugIdSnippet(debugId), source) with no check of what the asset starts with. Rspack/webpack emit "use strict" at the top of generated modules, and the default test /\.([cm]?jsx?|bundle)$/ matches those assets.
| compilation.updateAsset( | |
| file, | |
| (source) => | |
| new sources.ConcatSource(getDebugIdSnippet(debugId), source) | |
| ); | |
| compilation.updateAsset( | |
| file, | |
| (source) => | |
| new sources.ConcatSource( | |
| source, | |
| getDebugIdSnippet(debugId) | |
| ) | |
| ); |
Suggested change
Append the snippet after the existing source instead of prepending it (Metro does the same), or detect a leading "use strict"/"use asm"/shebang directive and insert the snippet after it. Note that appending shifts the debug-id comment position, so keep the //# debugId= insertion logic consistent with wherever the snippet lands.
Details
persona: senior-developer
Useful? Reply @apex-sentinel-staging like, or @apex-sentinel-staging dislike <why> if it is not.
Reply @apex-sentinel-staging note ... to add context, or @apex-sentinel-staging pivot ... if it is wrong — I will re-check the cited code.
There was a problem hiding this comment.
@apex-sentinel-staging pivot False positive for Re.Pack output. Rspack/webpack wrap the bundle in an IIFE (output.iife defaults to true), so "use strict" is a function-level directive inside the wrapper, and a statement before it doesn't change it. Real iOS/Android production bundles don't start with a "use strict" directive: Re.Pack's own repackGlobal banner (var self = self || this || ...) is already the first statement, and it relies on sloppy-mode this. Shebangs and "use asm" don't appear in RN bundles. Prepending also matches upstream: @sentry/webpack-plugin prepends the same snippet, and createSentryMetroSerializer puts the debug ID module first, not last. Appending would also break the //# debugId= placement before //# sourceMappingURL=.
sentry-cli rejects a source map with duplicate debugId keys, which happens when another tool already stamped the map.
Summary
Adds
SentryDebugIdPlugin, which stamps a Sentry Debug ID into the bundle and its source map.A Debug ID is a UUID present in both artifacts. The Sentry SDK reports it with every event and Sentry uses it to pair a JS stack trace with the source map that resolves it. For Metro,
@sentry/react-nativeprovides this throughcreateSentryMetroSerializer; Re.Pack currently has no equivalent, so apps that migrate lose JS symbolication.Why
@sentry/webpack-plugindoesn't cover this@sentry/webpack-plugin5.4.1 (@sentry/bundler-plugins11.x) injects the snippet atprocessAssetsADDITIONS, filtered byWEBPACK_JAVASCRIPT_ASSET_REGEX, and insourcemaps.disable: 'disable-upload'mode it also stamps emitted maps, but only for files passingisJsFile()(.js/.mjs/.cjs). Re.Pack emitsindex.bundle, so neither the bundle nor its map gets a Debug ID, silently and with no warning.@sentry/react-nativeuploads through its own Gradle/Xcode integration, which reads the on-disk map (scripts/has-sourcemap-debugid.js,scripts/copy-debugid.js). Both no-op when the field is absent, so the build succeeds and symbolication quietly fails (see getsentry/sentry-react-native#3956).Relaxing those two filters upstream would cover most of this; worth raising with Sentry separately. Until then, the two plugins compose fine: this one handles the Debug ID,
@sentry/webpack-pluginstill handles release creation and upload.Implementation
processAssetsPROCESS_ASSETS_STAGE_ADDITIONS, before minification and source map generation, so the snippet is minified with the bundle and maps account for it.testdefaults to/\.([cm]?jsx?|bundle)$/, the same patternSourceMapPluginuses.chunk.contentHash.javascript ?? chunk.hash) using the samestringToUUIDas@sentry/bundler-plugin-core, so the same input always gets the same ID and each chunk gets its own. IDs are scoped per compilation, so watch rebuilds never reuse a stale one.PROCESS_ASSETS_STAGE_DEV_TOOLING + 1, right after they are emitted, so later stages (content hashing) see final assets.debugIdanddebug_idare both written, since Sentry's tooling reads either depending on version. The fields are spliced into the JSON text instead of a parse/stringify round-trip, which costs ~200MB transient memory on a 29MB map.//# debugId=<id>comment is added before//# sourceMappingURL, sosentry-clican pair bundle and map for JSC builds and uploads outside the Gradle/Xcode scripts.createSentryMetroSerializerskips dev/hot.HermesBytecodePlugin'scomposeSourceMapsruns RN'scompose-source-maps.js, which writes a fresh map withoutdebugId, then replaces the packager map with it. It now carries the Debug ID from the packager map over to the composed one, so remote chunks compiled to bytecode keep their ID (copy-debugid.jsonly helpsindex.bundle).@sentry/bundler-plugin-core; noted in the code and changeset.RepackPlugin. Sentry is opt-in, so users addnew Repack.plugins.SentryDebugIdPlugin()explicitly.Verification
Unit tests do real compiles and cover:
.bundleand.jsoutputs, multiple chunks with distinct IDs matching their own maps, the same ID for the same input and a new one for changed input, the dev-mode skip, the//# debugId=comment, a webpack 5 compile, and theRepack.plugins.SentryDebugIdPluginimport path.composeSourceMapsand the source map splicing have their own tests.Also verified end to end in the app on iOS and Android production bundles:
debugIddebugId+debug_id)sentry-dbid-<uuid>stamphas-sourcemap-debugid.jscopy-debugid.js→ Hermes mapjest src/plugins→ 125 tests pass (13 suites),tsc --noEmitclean,biome checkclean. Changeset and a docs page underwebsite/src/latest/api/plugins/included.Env: repo tests on
@rspack/core1.6.0 andwebpack5.105.4; app verified withreact-native0.86.0,@sentry/react-native8.7.0,@sentry/webpack-plugin5.2.0.Open questions for maintainers
HermesBytecodePluginin core regardless. Happy to move the plugin itself to apackages/plugin-sentrypackage if you prefer.RepackPlugin. If you'd prefer an opt-in flag there (e.g.sentry: true), that's an easy follow-up.