-
-
Notifications
You must be signed in to change notification settings - Fork 358
feat(core): Warn when multiple versions of Sentry JS SDK are detected #6269
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
antonis
wants to merge
3
commits into
main
Choose a base branch
from
antonis/warn-multiple-sdk-versions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+126
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| - Note: Expo Router span/breadcrumb attributes that may contain user identifiers (`route.href`, `route.params`, and concrete pathnames derived from string hrefs such as `/users/42`) are now gated behind `sendDefaultPii`. When `sendDefaultPii` is off (the default), prefetch spans for string hrefs use `route.name: 'unknown'` and omit `route.href`. Templated object hrefs (e.g. `{ pathname: '/users/[id]' }`) are unaffected. | ||
| - Warn when Gradle resolves `sentry-android` to a version incompatible with the SDK ([#6238](https://github.com/getsentry/sentry-react-native/pull/6238)) | ||
| - Attach the active TurboModule method to native crash reports as `contexts.turbo_module` + `turbo_module.name` / `turbo_module.method` tags ([#6227](https://github.com/getsentry/sentry-react-native/pull/6227)) | ||
| - Warn during dev builds when multiple versions of Sentry JS SDK are detected ([#6269](https://github.com/getsentry/sentry-react-native/pull/6269)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets move this field to a unreleased version |
||
|
|
||
| ### Fixes | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { debug, getMainCarrier, SDK_VERSION as CORE_SDK_VERSION } from '@sentry/core'; | ||
|
|
||
| export function checkSentryJsSdkVersionMismatch(): void { | ||
| if (!__DEV__) { | ||
| return; | ||
| } | ||
|
Comment on lines
+4
to
+6
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you move this into the sdk.tsx? That way, all the return flows will match the function description. |
||
|
|
||
| try { | ||
| const carrier = getMainCarrier(); | ||
| const sentryCarrier = carrier.__SENTRY__; | ||
| if (!sentryCarrier) { | ||
| return; | ||
| } | ||
|
|
||
| const versions = Object.keys(sentryCarrier).filter(key => key !== CORE_SDK_VERSION && key !== 'version'); | ||
| if (versions.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| debug.warn( | ||
| `Multiple versions of Sentry JavaScript SDKs were detected in your application. ` + | ||
| `Found versions: ${[CORE_SDK_VERSION, ...versions].join(', ')}. ` + | ||
| `This may cause unexpected behavior. ` + | ||
| `Ensure all Sentry packages use the same version. ` + | ||
| `See https://docs.sentry.io/platforms/react-native/troubleshooting/ for more details.`, | ||
| ); | ||
| } catch (_e) { | ||
| // Ignore errors from version check | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| const mockDebugWarn = jest.fn(); | ||
| const mockCarrier: Record<string, unknown> = {}; | ||
|
|
||
| jest.mock('@sentry/core', () => ({ | ||
| debug: { | ||
| get warn() { | ||
| return mockDebugWarn; | ||
| }, | ||
| }, | ||
| getMainCarrier: () => mockCarrier, | ||
| SDK_VERSION: '10.0.0', | ||
| })); | ||
|
|
||
| import { checkSentryJsSdkVersionMismatch } from '../../src/js/utils/sdkVersionCheck'; | ||
|
|
||
| describe('checkSentryJsSdkVersionMismatch', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| delete mockCarrier.__SENTRY__; | ||
| }); | ||
|
|
||
| it('does not warn when only one SDK version is present', () => { | ||
| mockCarrier.__SENTRY__ = { '10.0.0': {} }; | ||
|
|
||
| checkSentryJsSdkVersionMismatch(); | ||
|
|
||
| expect(mockDebugWarn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('warns when multiple SDK versions are present', () => { | ||
| mockCarrier.__SENTRY__ = { '10.0.0': {}, '9.0.0': {} }; | ||
|
|
||
| checkSentryJsSdkVersionMismatch(); | ||
|
|
||
| expect(mockDebugWarn).toHaveBeenCalledTimes(1); | ||
| expect(mockDebugWarn).toHaveBeenCalledWith(expect.stringContaining('Multiple versions of Sentry JavaScript SDKs')); | ||
| expect(mockDebugWarn).toHaveBeenCalledWith(expect.stringContaining('9.0.0')); | ||
| expect(mockDebugWarn).toHaveBeenCalledWith(expect.stringContaining('10.0.0')); | ||
| }); | ||
|
|
||
| it('warns when more than two SDK versions are present', () => { | ||
| mockCarrier.__SENTRY__ = { '10.0.0': {}, '9.0.0': {}, '8.0.0': {} }; | ||
|
|
||
| checkSentryJsSdkVersionMismatch(); | ||
|
|
||
| expect(mockDebugWarn).toHaveBeenCalledTimes(1); | ||
| expect(mockDebugWarn).toHaveBeenCalledWith(expect.stringContaining('9.0.0')); | ||
| expect(mockDebugWarn).toHaveBeenCalledWith(expect.stringContaining('8.0.0')); | ||
| }); | ||
|
|
||
| it('does not warn when carrier has the version key set by @sentry/core', () => { | ||
| mockCarrier.__SENTRY__ = { '10.0.0': {}, version: '10.0.0' }; | ||
|
|
||
| checkSentryJsSdkVersionMismatch(); | ||
|
|
||
| expect(mockDebugWarn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not warn when __SENTRY__ is not set', () => { | ||
| checkSentryJsSdkVersionMismatch(); | ||
|
|
||
| expect(mockDebugWarn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not warn in production builds', () => { | ||
| // @ts-expect-error -- __DEV__ is a RN global | ||
| globalThis.__DEV__ = false; | ||
| try { | ||
| mockCarrier.__SENTRY__ = { '10.0.0': {}, '9.0.0': {} }; | ||
|
|
||
| checkSentryJsSdkVersionMismatch(); | ||
|
|
||
| expect(mockDebugWarn).not.toHaveBeenCalled(); | ||
| } finally { | ||
| // @ts-expect-error -- __DEV__ is a RN global | ||
| globalThis.__DEV__ = true; | ||
| } | ||
| }); | ||
|
|
||
| it('does not throw on unexpected errors', () => { | ||
| Object.defineProperty(mockCarrier, '__SENTRY__', { | ||
| get() { | ||
| throw new Error('test error'); | ||
| }, | ||
| configurable: true, | ||
| }); | ||
|
|
||
| expect(() => checkSentryJsSdkVersionMismatch()).not.toThrow(); | ||
| expect(mockDebugWarn).not.toHaveBeenCalled(); | ||
|
|
||
| delete mockCarrier.__SENTRY__; | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## 8.14.0.Consider moving the entry to the
## Unreleasedsection, please.