Skip to content

feat: add SentryDebugIdPlugin - #1451

Open
elirangoshen wants to merge 5 commits into
callstack:mainfrom
elirangoshen:feat/sentry-debug-id-plugin
Open

elirangoshen wants to merge 5 commits into
callstack:mainfrom
elirangoshen:feat/sentry-debug-id-plugin

Conversation

@elirangoshen

@elirangoshen elirangoshen commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

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-native provides this through createSentryMetroSerializer; Re.Pack currently has no equivalent, so apps that migrate lose JS symbolication.

Why @sentry/webpack-plugin doesn't cover this

@sentry/webpack-plugin 5.4.1 (@sentry/bundler-plugins 11.x) injects the snippet at processAssets ADDITIONS, filtered by WEBPACK_JAVASCRIPT_ASSET_REGEX, and in sourcemaps.disable: 'disable-upload' mode it also stamps emitted maps, but only for files passing isJsFile() (.js/.mjs/.cjs). Re.Pack emits index.bundle, so neither the bundle nor its map gets a Debug ID, silently and with no warning.

@sentry/react-native uploads 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-plugin still handles release creation and upload.

Implementation

  • Injection at processAssets PROCESS_ASSETS_STAGE_ADDITIONS, before minification and source map generation, so the snippet is minified with the bundle and maps account for it. test defaults to /\.([cm]?jsx?|bundle)$/, the same pattern SourceMapPlugin uses.
  • Deterministic IDs. The ID is derived from the chunk's pre-injection content hash (chunk.contentHash.javascript ?? chunk.hash) using the same stringToUUID as @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.
  • Source maps are stamped at PROCESS_ASSETS_STAGE_DEV_TOOLING + 1, right after they are emitted, so later stages (content hashing) see final assets. debugId and debug_id are 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, so sentry-cli can pair bundle and map for JSC builds and uploads outside the Gradle/Xcode scripts.
  • Production only. The plugin skips non-production builds, like createSentryMetroSerializer skips dev/hot.
  • Hermes. HermesBytecodePlugin's composeSourceMaps runs RN's compose-source-maps.js, which writes a fresh map without debugId, 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.js only helps index.bundle).
  • The runtime snippet body is copied from @sentry/bundler-plugin-core; noted in the code and changeset.
  • Not wired into RepackPlugin. Sentry is opt-in, so users add new Repack.plugins.SentryDebugIdPlugin() explicitly.

Verification

Unit tests do real compiles and cover: .bundle and .js outputs, 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 the Repack.plugins.SentryDebugIdPlugin import path. composeSourceMaps and the source map splicing have their own tests.

Also verified end to end in the app on iOS and Android production bundles:

Check Before After
map debugId absent present (debugId + debug_id)
bundle sentry-dbid-<uuid> stamp absent present, matches the map
has-sourcemap-debugid.js fails prints UUID, exit 0
copy-debugid.js → Hermes map no-op copies the ID

jest src/plugins → 125 tests pass (13 suites), tsc --noEmit clean, biome check clean. Changeset and a docs page under website/src/latest/api/plugins/ included.

Env: repo tests on @rspack/core 1.6.0 and webpack 5.105.4; app verified with react-native 0.86.0, @sentry/react-native 8.7.0, @sentry/webpack-plugin 5.2.0.

Open questions for maintainers

  • Placement: kept in core for now, since the Hermes part has to change HermesBytecodePlugin in core regardless. Happy to move the plugin itself to a packages/plugin-sentry package if you prefer.
  • I kept it out of RepackPlugin. If you'd prefer an opt-in flag there (e.g. sentry: true), that's an easy follow-up.

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-bot

changeset-bot Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3cd7419

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@callstack/repack Minor
@callstack/repack-plugin-expo-modules Minor
@callstack/repack-plugin-nativewind Minor
@callstack/repack-plugin-reanimated Minor
@callstack/repack-dev-server Minor
@callstack/repack-init Minor

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

@vercel

vercel Bot commented Sep 4, 2026

Copy link
Copy Markdown

@elirangoshen is attempting to deploy a commit to the Callstack Team on Vercel.

A member of the Team first needs to authorize it.

@elirangoshen
elirangoshen marked this pull request as ready for review September 4, 2026 08:22

@dannyhw dannyhw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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-plugin 5.4.1 (@sentry/bundler-plugins 11.x) no longer uses BannerPlugin. It injects at processAssets ADDITIONS, filtered by WEBPACK_JAVASCRIPT_ASSET_REGEX. It now also stamps emitted maps in sourcemaps.disable: 'disable-upload' mode, but only for files passing isJsFile() (.js/.mjs/.cjs). .bundle is 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. composeSourceMaps runs RN's compose-source-maps.js and replaces the packager map. As far as I can tell, the composed map has no debugId, so remote chunks compiled to bytecode by Re.Pack would lose it. (Not verified here; the main bundle is covered by RN's native copy-debugid.js step.) Please check this and either copy the ID across or document the limitation.
  • Placement. I'd suggest a separate packages/plugin-sentry rather than core. It's vendor-specific, the snippet has to follow Sentry's format, and it matches the plugin-reanimated / plugin-expo-modules pattern. Keeping it out of RepackPlugin is right.

@CristiCeban

Copy link
Copy Markdown
Contributor

The Hermes interaction is real — I was able to verify it locally, and the scope is slightly different from what the review above assumes.

copy-debugid.js does cover index.bundle, but only because RN's own pipeline leaves two separate files on disk: the packager map and the composed Hermes map. It can't help remote chunks, because HermesBytecodePlugin ends up with exactly one map — composeSourceMaps.ts:42-45 unlinks the stamped packager map and renames the composed one over it, so there is no second file for copy-debugid to read from.

Reproduced on a real release bundle (RN 0.88, 3.1MB map), driving transformBundleToHermesBytecode's exact flags and then composeSourceMaps:

BEFORE: debugId 74f197a7-…   keys: version,file,sources,sourcesContent,names,mappings,debugId,debug_id
AFTER : debugId DROPPED      keys: version,sources,names,mappings,sourcesContent,x_facebook_sources,x_hermes_function_offsets

has-sourcemap-debugid.js: exit 1 -> does not contain 'debugId' nor 'debug_id'.
copy-debugid.js:          exit 0 -> Packager source map does not have `debugId`.

The cause is compose-source-maps.js building a fresh SourceMapGenerator, so its output is only {version,file,sources,names,mappings,sourcesContent,x_facebook_*}. That copy-debugid.js line is also character-for-character what getsentry/sentry-react-native#3956 reports.

The fix looks small: read debugId off the input map in composeSourceMaps.ts and re-apply it to the composed output before the rename. I'd lean toward doing that rather than documenting the limitation, since remote chunks are exactly the Module Federation setup this plugin will attract — and it also means the feature can't be complete from a separate package.

@CristiCeban

Copy link
Copy Markdown
Contributor

Verified the build claims locally on a real RN 0.88 release bundle (~810KB bundle, 3.1MB map):

plugin off plugin on
sentry-dbid-<uuid> in bundle absent present
map debugId / debug_id absent present
has-sourcemap-debugid.js exit 1 exit 0
bundle ↔ map pair — match

The mapping-integrity claim holds too, which is the part I was most sceptical about: the same generated token resolves to the same original line:column with and without the plugin (bundle 1180→1557 bytes, original position identical). No column drift.

Repo gates are green on the branch: pnpm test 13/13 tasks exit 0, pnpm lint exit 0 with biome not touching any file in the diff, tsc --noEmit 0 errors — identical to a clean origin/main baseline.
Small description nit: it lists @rspack/core 2.1.3, but the branch resolves 1.6.0. Worth correcting so anyone reproducing the verification ends up on the same code path.

@elirangoshen

Copy link
Copy Markdown
Contributor Author

Hi, would you like me to apply the requested changes ?

@dannyhw

dannyhw commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

@elirangoshen yes please, feel free to push back on anything that doesn't seem right though of course!

@elirangoshen

Copy link
Copy Markdown
Contributor Author

@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
@elirangoshen

Copy link
Copy Markdown
Contributor Author

On placement: the Hermes part has to live in core either way. HermesBytecodePlugin runs RN's compose-source-maps.js, which writes a fresh map without debugId, then deletes the packager map and renames the composed one over it. After that the ID is gone, so the only place to carry it over is inside composeSourceMaps.ts, between those two steps. A separate package can't hook in there.

The plugin itself could still move to packages/plugin-sentry if you prefer. Happy to do that.

@MrMuzyk

MrMuzyk commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

/review

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of c805459

2 finding(s): 2 minor.

🟡 Minor (2)

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.

@apex-sentinel-staging apex-sentinel-staging 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.

Apex Sentinel: 2 new findings on c805459. The verdict and the full list are in the summary comment.

Comment on lines +18 to +29
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}"}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

⚠️ Check this patch before you commit it: the review doubted it would be right as written.

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

Comment on lines +119 to +123
compilation.updateAsset(
file,
(source) =>
new sources.ConcatSource(getDebugIdSnippet(debugId), source)
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 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.

⚠️ Check this patch before you commit it: the review doubted it would be right as written.

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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.

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.

4 participants