Skip to content

feat(babel): Auto-inject sentry-label from static text children#925

Merged
antonis merged 4 commits into
mainfrom
antonis/auto-inject-sentry-label
May 11, 2026
Merged

feat(babel): Auto-inject sentry-label from static text children#925
antonis merged 4 commits into
mainfrom
antonis/auto-inject-sentry-label

Conversation

@antonis
Copy link
Copy Markdown
Contributor

@antonis antonis commented May 7, 2026

Summary

Adds an opt-in autoInjectSentryLabel option to @sentry/babel-plugin-component-annotate that automatically extracts static text from JSX children and injects a sentry-label attribute on the root element.

This enables React Native apps to get meaningful touch breadcrumb labels (e.g., "Save workout", "Add to cart") without manual annotation. The feature walks the JSX tree up to 3 levels deep, collecting text from recognized text components (default: Text, text), and bails out entirely if any dynamic content is found anywhere in the subtree.

Resolves getsentry/sentry-react-native#6098

Key design decisions

  • Opt-in onlyautoInjectSentryLabel defaults to false, no behavior change for existing users. When disabled, the only overhead is two property reads during context creation and one boolean check per processJSX call — none of the new extraction/injection code is reached.
  • Conservative bail-out — any dynamic expression ({variable}, {t('key')}, template literals) anywhere in the subtree causes the entire label to be skipped — including dynamic content nested inside non-text wrapper elements within text components (e.g., <Text>Hello <Bold>{name}</Bold></Text>). This is stricter than the runtime extraction in sentry-react-native#6106, which skips dynamic nodes and continues. Build-time can't evaluate dynamic content, so skipping entirely avoids partial/misleading labels.
  • Hyphenated sentry-label — React strips unknown hyphenated props from DOM elements, so this is safe for web even though the feature targets React Native
  • Cross-SDK safebundler-plugin-core's createComponentNameAnnotateHooks does not pass this option through, so only React Native (which configures the Babel plugin directly via Metro) can activate it. Web SDKs are unaffected.
  • Nested <Text> support — handles the standard RN inline styling pattern (<Text>Hello <Text style={bold}>world</Text></Text>), recursively extracting text from nested text components
  • Fragment handling — when the root is a fragment, the first JSXElement child is used as the injection target. Fragments are treated as transparent wrappers and do not consume depth budget during text search.
  • Truncation — labels exceeding 64 characters are truncated with ...
  • textComponentNames option — configurable list of component names whose children are treated as text content

New plugin options

Option Type Default Description
autoInjectSentryLabel boolean false Enable auto-injection of sentry-label
textComponentNames string[] ["Text", "text"] Component names whose JSXText children are considered text content

Test plan

  • 55 new tests in sentry-label.test.ts covering: opt-in behavior, basic extraction, multiple text children, skip conditions, truncation, depth limit, text component names, nested text (RN inline styling), fragment handling (including depth budget), dynamic content bail-out in non-text wrappers, web compatibility, edge cases, multiple components per file, ternary returns
  • All 175 tests pass (55 new + 120 existing unchanged)
  • Verified no regressions in existing test-plugin.test.ts (65 tests) and experimental.test.ts (55 tests)

🤖 Generated with Claude Code

Comment thread packages/babel-plugin-component-annotate/src/index.ts
Comment thread packages/babel-plugin-component-annotate/src/index.ts Outdated
Comment thread packages/babel-plugin-component-annotate/src/index.ts
@linear-code
Copy link
Copy Markdown

linear-code Bot commented May 7, 2026

Comment thread packages/babel-plugin-component-annotate/src/index.ts Outdated
Comment thread packages/babel-plugin-component-annotate/src/index.ts Outdated
@antonis antonis force-pushed the antonis/auto-inject-sentry-label branch from 0bd679b to 9eaa08f Compare May 7, 2026 13:57
@antonis antonis requested a review from alwx May 7, 2026 14:21
@antonis antonis marked this pull request as ready for review May 7, 2026 14:47
@timfish
Copy link
Copy Markdown
Collaborator

timfish commented May 7, 2026

The regular component injection plugin breaks props in some circumstances which has stopped us from enabling this plugin by default:

We've recently added an experimental mode which injects the annotations into the HTML elements inside the component:
https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/main/packages/babel-plugin-component-annotate/src/experimental.ts

@antonis
Copy link
Copy Markdown
Contributor Author

antonis commented May 8, 2026

Thank you for having a look and the context @timfish 🙇

The sentry-label attribute is indeed injected on the root JSX element, the same injection point as data-sentry-component described in #492. I think this should still work in this case since this option is only reachable from React Native (via direct Babel plugin config in Metro). The bundler-plugin-core doesn't pass it through, so web SDKs should not be affected. It is also opt-in and autoInjectSentryLabel defaults to false.

We've recently added an experimental mode which injects the annotations into the HTML elements inside the component

I think this is not applicable for RN since there are no HTML elements.

@antonis antonis requested a review from timfish May 8, 2026 09:33
Copy link
Copy Markdown
Collaborator

@timfish timfish left a comment

Choose a reason for hiding this comment

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

Rather than separate autoInjectSentryLabel and textComponentNames options, we should probably combine these so it's more obvious they are always used together and are unrelated to the other options.

Something like this:

autoInjectSentryLabel?: boolean | { textComponentNames: string[] }

autoInjectSentryLabel defaults to false. autoInjectSentryLabel: true gets transformed to autoInjectSentryLabel: { textComponentNames: ["Text", "text"] }?

since this option is only reachable from React Native

If this option should not be visible to other users of the plugin, we might want to use jsdoc @hidden on the options so that users can't discover it.

Comment thread packages/babel-plugin-component-annotate/src/index.ts
@antonis
Copy link
Copy Markdown
Contributor Author

antonis commented May 11, 2026

Thank you for you feedback and suggestions @timfish 🙇

Rather than separate autoInjectSentryLabel and textComponentNames options, we should probably combine these so it's more obvious they are always used together and are unrelated to the other options.

Good idea 👍 Combined with 985b967

If this option should not be visible to other users of the plugin, we might want to use jsdoc @hidden on the options so that users can't discover it.

I've added it on the internal AnnotationOpts interface as a signal in 985b967

Copy link
Copy Markdown

@cursor cursor Bot left a comment

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 2 potential issues.

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 a46f878. Configure here.

Comment thread packages/babel-plugin-component-annotate/src/index.ts
Comment thread packages/babel-plugin-component-annotate/src/index.ts
@antonis antonis requested a review from timfish May 11, 2026 08:38
@timfish
Copy link
Copy Markdown
Collaborator

timfish commented May 11, 2026

I'm struggling to work out how this has broken the component annotation tests on Windows 🤔

antonis and others added 4 commits May 11, 2026 13:08
Add opt-in `autoInjectSentryLabel` option to the Babel component annotate
plugin. When enabled, the plugin extracts static text from JSX children
(up to 3 levels deep) and injects a `sentry-label` attribute on the root
element. This gives React Native apps meaningful touch breadcrumb labels
without manual annotation.

Closes getsentry/sentry-react-native#6098

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
… into single option

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…omponents

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…on-text wrapper behavior

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@antonis antonis force-pushed the antonis/auto-inject-sentry-label branch from d0cacb6 to b815ca9 Compare May 11, 2026 11:08
@antonis
Copy link
Copy Markdown
Contributor Author

antonis commented May 11, 2026

I'm struggling to work out how this has broken the component annotation tests on Windows 🤔

I've rebased on main in case this was some caching issue but still fails :( 👀

@antonis
Copy link
Copy Markdown
Contributor Author

antonis commented May 11, 2026

I'm struggling to work out how this has broken the component annotation tests on Windows 🤔

I've rebased on main in case this was some caching issue but still fails :( 👀

Deleting the actual ci caches worked 🎉
It seems that the Windows node_modules cache was populated before the Rolldown v1 merge, and subsequent runs kept restoring stale dependencies.

@antonis antonis merged commit 7ac1cbc into main May 11, 2026
35 of 39 checks passed
@antonis antonis deleted the antonis/auto-inject-sentry-label branch May 11, 2026 11:44
renovate Bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request May 13, 2026
| datasource | package             | from  | to    |
| ---------- | ------------------- | ----- | ----- |
| npm        | @sentry/vite-plugin | 5.2.0 | 5.3.0 |


## [v5.3.0](https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/HEAD/CHANGELOG.md#530)

##### New Features ✨

- (babel) Auto-inject sentry-label from static text children by [@antonis](https://github.com/antonis) in [#925](getsentry/sentry-javascript-bundler-plugins#925)

##### Bug Fixes 🐛

- (vite) Avoid version-specific plugin return type by [@logaretm](https://github.com/logaretm) in [#928](getsentry/sentry-javascript-bundler-plugins#928)

##### Internal Changes 🔧

- Fix craft config by [@timfish](https://github.com/timfish) in [#930](getsentry/sentry-javascript-bundler-plugins#930)
- Update craft by [@timfish](https://github.com/timfish) in [#929](getsentry/sentry-javascript-bundler-plugins#929)
- Use Rolldown v1 stable by [@timfish](https://github.com/timfish) in [#924](getsentry/sentry-javascript-bundler-plugins#924)
- Remove old integration tests by [@timfish](https://github.com/timfish) in [#922](getsentry/sentry-javascript-bundler-plugins#922)
- Ensure correct bundlers are resolved by [@timfish](https://github.com/timfish) in [#921](getsentry/sentry-javascript-bundler-plugins#921)
- Fix telemetry tests to capture all envelopes by [@timfish](https://github.com/timfish) in [#920](getsentry/sentry-javascript-bundler-plugins#920)


## [v5.2.1](https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/HEAD/CHANGELOG.md#521)

##### Bug Fixes 🐛

- (webpack) Await source map deletion before signaling build completion by [@andreiborza](https://github.com/andreiborza) in [#918](getsentry/sentry-javascript-bundler-plugins#918)

##### Internal Changes 🔧

- (ci) Disable changelog preview by [@chargome](https://github.com/chargome) in [#917](getsentry/sentry-javascript-bundler-plugins#917)
- Add additional integration tests by [@timfish](https://github.com/timfish) in [#914](getsentry/sentry-javascript-bundler-plugins#914)
- Remove unused e2e tests by [@timfish](https://github.com/timfish) in [#915](getsentry/sentry-javascript-bundler-plugins#915)
renovate Bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request May 13, 2026
| datasource | package             | from  | to    |
| ---------- | ------------------- | ----- | ----- |
| npm        | @sentry/vite-plugin | 5.2.0 | 5.3.0 |


## [v5.3.0](https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/HEAD/CHANGELOG.md#530)

##### New Features ✨

- (babel) Auto-inject sentry-label from static text children by [@antonis](https://github.com/antonis) in [#925](getsentry/sentry-javascript-bundler-plugins#925)

##### Bug Fixes 🐛

- (vite) Avoid version-specific plugin return type by [@logaretm](https://github.com/logaretm) in [#928](getsentry/sentry-javascript-bundler-plugins#928)

##### Internal Changes 🔧

- Fix craft config by [@timfish](https://github.com/timfish) in [#930](getsentry/sentry-javascript-bundler-plugins#930)
- Update craft by [@timfish](https://github.com/timfish) in [#929](getsentry/sentry-javascript-bundler-plugins#929)
- Use Rolldown v1 stable by [@timfish](https://github.com/timfish) in [#924](getsentry/sentry-javascript-bundler-plugins#924)
- Remove old integration tests by [@timfish](https://github.com/timfish) in [#922](getsentry/sentry-javascript-bundler-plugins#922)
- Ensure correct bundlers are resolved by [@timfish](https://github.com/timfish) in [#921](getsentry/sentry-javascript-bundler-plugins#921)
- Fix telemetry tests to capture all envelopes by [@timfish](https://github.com/timfish) in [#920](getsentry/sentry-javascript-bundler-plugins#920)


## [v5.2.1](https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/HEAD/CHANGELOG.md#521)

##### Bug Fixes 🐛

- (webpack) Await source map deletion before signaling build completion by [@andreiborza](https://github.com/andreiborza) in [#918](getsentry/sentry-javascript-bundler-plugins#918)

##### Internal Changes 🔧

- (ci) Disable changelog preview by [@chargome](https://github.com/chargome) in [#917](getsentry/sentry-javascript-bundler-plugins#917)
- Add additional integration tests by [@timfish](https://github.com/timfish) in [#914](getsentry/sentry-javascript-bundler-plugins#914)
- Remove unused e2e tests by [@timfish](https://github.com/timfish) in [#915](getsentry/sentry-javascript-bundler-plugins#915)
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.

Auto-inject sentry-label from static JSX text children in Babel plugin

2 participants