Skip to content

Events silently dropped when Sentry.init() is first statement (production builds only) #5508

@mwx27

Description

@mwx27

What React Native libraries do you use?

RN New Architecture, Hermes, Expo Application Services (EAS), Expo Router, Expo (mobile only)

Are you using sentry.io or on-premise?

sentry.io (SaS)

@sentry/react-native SDK Version

7.8.0

How does your development environment look like?

⬇  Place the `npx react-native@latest info` output here. ⬇
System:
  OS: macOS 15.5
  CPU: (8) arm64 Apple M1 Pro
  Memory: 131.55 MB / 16.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 22.16.0
    path: /Users/maciejwojda/Library/Caches/fnm_multishells/45964_1767025842589/bin/node
  Yarn:
    version: 1.22.1
    path: /Users/maciejwojda/Library/Caches/fnm_multishells/45964_1767025842589/bin/yarn
  npm:
    version: 10.9.2
    path: /Users/maciejwojda/Library/Caches/fnm_multishells/45964_1767025842589/bin/npm
  Watchman: Not Found
Managers:
  CocoaPods:
    version: 1.16.2
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.5
      - iOS 18.5
      - macOS 15.5
      - tvOS 18.5
      - visionOS 2.5
      - watchOS 11.5
  Android SDK:
    API Levels:
      - "34"
      - "35"
      - "36"
    Build Tools:
      - 34.0.0
      - 35.0.0
      - 35.0.1
      - 36.0.0
      - 36.0.0
    System Images:
      - android-35 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2025.1 AI-251.26094.121.2513.14007798
  Xcode:
    version: 16.4/16F6
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.15
    path: /opt/homebrew/opt/openjdk@17/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 20.0.2
    wanted: latest
  react:
    installed: 19.1.0
    wanted: 19.1.0
  react-native:
    installed: 0.81.5
    wanted: 0.81.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

info React Native v0.83.0 is now available (your project is running on v0.81.5).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.83.0
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.81.5&to=0.83.0
info For more info, check out "https://reactnative.dev/docs/upgrading?os=macos".

Sentry.init()

  Sentry.init({
    dsn: SENTRY_DSN,

    // Disable in development mode to avoid noise
    enabled: !__DEV__,

    // Environment: preview or production (from APP_VARIANT in eas.json)
    environment: process.env.APP_VARIANT,

    // Release version for source maps and filtering
    release: Constants.expoConfig?.version ?? '1.0.0',

    // Adds more context data to events (IP address, cookies, user, etc.)
    // For more information, visit: https://docs.sentry.io/platforms/react-native/data-management/data-collected/
    sendDefaultPii: true,

    // Enable Logs
    enableLogs: true,

    // Configure Session Replay
    replaysSessionSampleRate: 0.1,
    replaysOnErrorSampleRate: 1,
    integrations: [Sentry.mobileReplayIntegration()],

    // Performance monitoring - sample 20% of transactions
    tracesSampleRate: 0.2,

    // Filter out non-actionable errors
    beforeSend(event, hint) {
      const error = hint.originalException;
      if (error instanceof Error) {
        // Ignore user connectivity issues
        if (error.message.includes('Network request failed')) {
          return null;
        }
      }
      return event;
    },

    // uncomment the line below to enable Spotlight (https://spotlightjs.com)
    // spotlight: __DEV__,
  });

Steps to Reproduce

  1. Create new Expo app using npx create-expo-app@latest my-app

  2. Install Sentry SDK using npx expo install @sentry/react-native

  3. Configure EAS Update channel in eas.json:

    {
      "build": {
        "preview": {
          "channel": "preview",
          "env": {
            "EXPO_PUBLIC_SENTRY_DSN": "https://your-dsn@sentry.io/project"
          }
        }
      }
    }
    
  4. Create Sentry initialization file. File structure may vary - in our case it's lib/sentry/index.ts:
    import * as Sentry from '@sentry/react-native';

export function initializeSentry(): void {
if (!process.env.EXPO_PUBLIC_SENTRY_DSN) {
return;
}
// No statement here - Sentry.init() is first after early return
Sentry.init({
dsn: process.env.EXPO_PUBLIC_SENTRY_DSN,
enabled: true
});
}
5. Call initialization at app entry point. In our case it's app/_layout.tsx:
import { initializeSentry } from '@/lib/sentry';
initializeSentry();
6. Add test button that executes Sentry.captureException(new Error('Test error'))
7. Build Android preview app using eas build --profile preview --platform android
8. Install APK on device and launch the app
9. Click button executing Sentry.captureException(new Error('Test error'))
10. Check Sentry dashboard - no events appear (even though captureException does not throw)
11. Add any statement before Sentry.init():
export function initializeSentry(): void {
if (!process.env.EXPO_PUBLIC_SENTRY_DSN) {
return;
}
console.log('test'); // <-- adding this line fixes the issue
Sentry.init({ ... });
}
12. Push OTA update using eas update --branch preview
13. Restart app to receive update
14. Click same button executing Sentry.captureException(new Error('Test error'))
15. Check Sentry dashboard - events now appear

Expected Result

described in steps to reproduce

Actual Result

described in steps to reproduce

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions