-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Merged
+114
−0
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7bb9d60
feat(core): Warn when multiple versions of Sentry JS SDK are detected
antonis 3c2d04f
docs: Add changelog entry for SDK version mismatch warning
antonis 19611d8
Merge branch 'main' into antonis/warn-multiple-sdk-versions
lucas-zimerman 682b73f
refactor(core): Move __DEV__ guard to call site in sdk.tsx
antonis a863084
docs: Move changelog entry to Unreleased section
antonis 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
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; | ||
| } | ||
|
|
||
| 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.
Uh oh!
There was an error while loading. Please reload this page.