Skip to content

fix(react): Remove unnecessary withScope wrapper in captureReact#20123

Closed
harshit078 wants to merge 2 commits intogetsentry:developfrom
harshit078:fix-react-componentStack
Closed

fix(react): Remove unnecessary withScope wrapper in captureReact#20123
harshit078 wants to merge 2 commits intogetsentry:developfrom
harshit078:fix-react-componentStack

Conversation

@harshit078
Copy link
Copy Markdown
Contributor

@harshit078 harshit078 commented Apr 8, 2026

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Closes #20094

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Core

  • Support registerTool/registerResource/registerPrompt in MCP integration by betegon in #20071
  • Support embeddings in langchain by nicohrubec in #20017

Deps

  • Bump lodash.template from 4.5.0 to 4.18.1 by dependabot in #20085
  • Bump @xmldom/xmldom from 0.8.3 to 0.8.12 by dependabot in #20066

Other

  • (cloudflare) Support basic WorkerEntrypoint by JPeer264 in #19884
  • (core, node) Portable Express integration by isaacs in #19928
  • (deno) Add denoRuntimeMetricsIntegration by chargome in #20023
  • (node, bun) Enforce minimum collection interval in runtime metrics integrations by chargome in #20068

Bug Fixes 🐛

Core

  • Replace regex with string check in stack parser to prevent main thread blocking by chargome in #20089
  • Set span.status to error when MCP tool returns JSON-RPC error response by betegon in #20082

Other

  • (aws-serverless) Add timeout to _endSpan forceFlush to prevent Lambda hanging by logaretm in #20064
  • (cloudflare) Ensure every request instruments functions by JPeer264 in #20044
  • (gatsby) Fix errorHandler signature to match bundler-plugin-core API by JPeer264 in #20048
  • (react) Remove unnecessary withScope wrapper in captureReact by harshit078 in #20123

Internal Changes 🔧

Core

  • Do not emit spans for chats.create in google-genai by nicohrubec in #19990
  • Unify .do* span ops to gen_ai.generate_content by nicohrubec in #20074
  • Simplify addResponseAttributes in openai integration by nicohrubec in #20013
  • Extract shared endStreamSpan for AI integrations by nicohrubec in #20021
  • Remove provider-specific AI span attributes in favor of gen_ai attributes in sentry conventions by nicohrubec in #20011

Deps

  • Bump mshick/add-pr-comment from dd126dd8c253650d181ad9538d8b4fa218fc31e8 to e7516d74559b5514092f5b096ed29a629a1237c6 by dependabot in #20078
  • Bump getsentry/craft/.github/workflows/changelog-preview.yml from 2.24.1 to 2.25.2 by dependabot in #20081

Other

  • (node) Add node integration tests for Vercel ToolLoopAgent by nicohrubec in #20087
  • (nuxt) Make Nuxt 5 (nightly) E2E optional by s1gr1d in #20113
  • (oxlint) Add typeawareness into oxlintrc by JPeer264 in #20075
  • Update validate-pr workflow by stephanie-anderson in #20072
  • Remove unused tsconfig-template folder by mydea in #20067

🤖 This preview updates automatically when you update the PR.

Comment on lines +67 to 70
return captureException(error, hint);
}

/**
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: Removing the withScope wrapper in captureReactException loses the componentStack for React < 17 and removes the 'react' context from Sentry events for all versions.
Severity: HIGH

Suggested Fix

Restore the withScope wrapper in the captureReactException function to ensure the componentStack is consistently added to the Sentry event scope via scope.setContext('react', { componentStack }). This will fix the data loss for React < 17 users and restore the 'react' context for all versions, ensuring consistent error reporting behavior and passing existing tests.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/react/src/error.ts#L67-L70

Potential issue: The removal of the `withScope` wrapper from `captureReactException`
eliminates the mechanism that added the React `componentStack` to the Sentry event scope
via `scope.setContext('react', { componentStack })`. For React versions below 17, this
results in the complete loss of component stack information in Sentry error reports, as
there is no alternative mechanism to capture it. For React 17 and above, while the stack
is preserved via `error.cause`, the dedicated `'react'` context is no longer present,
which is a regression. This change will likely cause existing tests that assert the
presence of this context to fail.

Did we get this right? 👍 / 👎 to inform future reviews.

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

scope.setContext('react', { componentStack });
return captureException(error, hint);
});
return captureException(error, hint);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Removal of componentStack context data from Sentry events

High Severity

Removing the withScope wrapper also removed the scope.setContext('react', { componentStack }) call, which means the React component stack is no longer attached as structured context data on Sentry events. For React < 17, non-Error throws, or when componentStack is falsy (where the setCause linked-error approach doesn't apply), the component stack is now completely lost. Multiple existing tests in errorboundary.test.tsx assert that scopeSetContextSpy is called with the react context — those will now fail.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ef55354. Configure here.

scope.setContext('react', { componentStack });
return captureException(error, hint);
});
return captureException(error, hint);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fix PR missing regression test for the change

Low Severity

Per project review rules, a fix PR needs at least one unit, integration, or E2E test that tests the regression being fixed (i.e., a test that would have failed prior to the fix and passes with it). This PR doesn't appear to include any new tests validating the behavior change. In fact, existing tests in errorboundary.test.tsx that assert setContext('react', { componentStack }) was called appear incompatible with this change.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit ef55354. Configure here.

@mydea
Copy link
Copy Markdown
Member

mydea commented Apr 8, 2026

This PR simply removes some functionality, the call was not unnecessary but actually does something!

@mydea mydea closed this Apr 8, 2026
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.

React: Don't set react.componentStack context

2 participants