Skip to content

Commit cbf87ac

Browse files
antonisclaude
andauthored
fix(core): Always re-initialize native SDK from JS in dev builds (#5872)
When `sentry.options.json` exists (generated by the Expo config plugin with `useNativeInit: true`), the SDK was setting `autoInitializeNativeSdk = false`, skipping the JS-side `initNativeSdk` call entirely. This prevented the native log bridge and runtime values (devServerUrl, defaultSidecarUrl) from being set up, causing zero envelopes to be sent to sentry.io in development builds. The fix adds a `!__DEV__` guard so that in dev builds, JS always re-initializes native (the native SDK handles double-init gracefully with "Previous configuration will be overwritten"). In release builds, the existing behavior is preserved for app start crash capture. Closes #5734 Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent beeb5b9 commit cbf87ac

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

packages/core/src/js/sdk.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ export function init(passedOptions: ReactNativeOptions): void {
140140
initialScope: safeFactory(userOptions.initialScope, { loggerMessage: 'The initialScope threw an error' }),
141141
};
142142

143-
if (!('autoInitializeNativeSdk' in userOptions) && RN_GLOBAL_OBJ.__SENTRY_OPTIONS__) {
144-
// Options file is present, native SDK is expected to be initialized
143+
if (!('autoInitializeNativeSdk' in userOptions) && RN_GLOBAL_OBJ.__SENTRY_OPTIONS__ && !__DEV__) {
144+
// Options file is present in a release build, native SDK is expected to be initialized
145145
// before JS from the native app entry point (e.g. AppDelegate, MainApplication).
146+
// In dev builds, we always re-initialize from JS to set up the native log bridge
147+
// and provide runtime values (devServerUrl, defaultSidecarUrl, etc.).
146148
// eslint-disable-next-line no-console
147149
console.info('[Sentry] Using options file. Native SDK is expected to be initialized before JS, skipping automatic native initialization from JS.');
148150
options.autoInitializeNativeSdk = false;

packages/core/test/sdk.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,34 @@ describe('Tests the SDK functionality', () => {
135135
expect(usedOptions()?.dsn).toBe('https://key@example.io/code');
136136
});
137137

138-
it('initializing with __SENTRY_OPTIONS__ disabled native auto initialization', () => {
139-
RN_GLOBAL_OBJ.__SENTRY_OPTIONS__ = {};
140-
init({});
141-
expect(usedOptions()?.autoInitializeNativeSdk).toBe(false);
138+
it('does not disable native auto initialization in dev builds even with __SENTRY_OPTIONS__', () => {
139+
// @ts-expect-error __DEV__ is a global
140+
const originalDev = globalThis.__DEV__;
141+
// @ts-expect-error __DEV__ is a global
142+
globalThis.__DEV__ = true;
143+
try {
144+
RN_GLOBAL_OBJ.__SENTRY_OPTIONS__ = {};
145+
init({});
146+
expect(usedOptions()?.autoInitializeNativeSdk).toBe(true);
147+
} finally {
148+
// @ts-expect-error __DEV__ is a global
149+
globalThis.__DEV__ = originalDev;
150+
}
151+
});
152+
153+
it('disables native auto initialization in release builds with __SENTRY_OPTIONS__', () => {
154+
// @ts-expect-error __DEV__ is a global
155+
const originalDev = globalThis.__DEV__;
156+
// @ts-expect-error __DEV__ is a global
157+
globalThis.__DEV__ = false;
158+
try {
159+
RN_GLOBAL_OBJ.__SENTRY_OPTIONS__ = {};
160+
init({});
161+
expect(usedOptions()?.autoInitializeNativeSdk).toBe(false);
162+
} finally {
163+
// @ts-expect-error __DEV__ is a global
164+
globalThis.__DEV__ = originalDev;
165+
}
142166
});
143167

144168
it('initializing without __SENTRY_OPTIONS__ enables native auto initialization', () => {
@@ -155,7 +179,7 @@ describe('Tests the SDK functionality', () => {
155179
expect(usedOptions()?.autoInitializeNativeSdk).toBe(true);
156180
});
157181

158-
it('initializing with __SENTRY_OPTIONS__ keeps native auto initialization false if set', () => {
182+
it('respects explicit autoInitializeNativeSdk false even with __SENTRY_OPTIONS__', () => {
159183
RN_GLOBAL_OBJ.__SENTRY_OPTIONS__ = {};
160184
init({
161185
autoInitializeNativeSdk: false,

0 commit comments

Comments
 (0)