feat(mobile): start Sentry natively so launch crashes carry native context

Merged
SA
2mo ago
+9-0

Problem

Sentry's native iOS SDK only starts when JS calls Sentry.init (app/_layout.tsx), after the whole bundle has loaded. A crash during launch arrives with nothing native before it. MOBILE-7 (the keychain read at module load, 4 devices, still recurring) came in with one breadcrumb, JS starting 100 ms before the crash, so there was no way to tell what launched the app.

Change

Follows Sentry's Capture App Start Errors Expo setup:

  • useNativeInit: true plus options on the @sentry/react-native/expo plugin. At prebuild the plugin writes sentry.options.json and inserts RNSentrySDK.start() as the first line of didFinishLaunching, before React Native starts. In release builds the JS Sentry.init then leaves the already-started native SDK alone; beforeSend still runs in JS.
  • enableMetricKit: true (docs) forwards Apple's MetricKit hang, CPU-exception and disk-write diagnostics. It is in both the plugin options and Sentry.init: OTA bundles are exported without prebuild, so they carry no options file and re-initialize the native SDK from the JS options alone. MetricKit hangs can duplicate the SDK's own app-hang events, as Sentry's docs note.
  • sentry.options.json is gitignored. The plugin generates it and merges into any existing copy, so a committed or stale one would keep keys app.config.ts no longer sets.

Without a DSN (dev builds) the file carries only enableMetricKit and the native SDK stays off, as today.

Native change: needs a new binary; the fingerprint runtime keeps it off existing builds' OTA channel.

Verification

  • expo prebuild --platform ios with a DSN: sentry.options.json = {dsn, environment, enableMetricKit}, RNSentrySDK.start() at AppDelegate.swift:17, first line of didFinishLaunching. Without a DSN: {enableMetricKit} only. Generated files removed afterwards.
  • bun run lint clean, bun run typecheck 42/42, apps/mobile bun test 129 pass.
  • Not yet run: a real native build and a forced crash on device. The mobile-build label would build one.

Summary by cubic

Starts Sentry's native iOS SDK at launch so crashes during launch arrive with native context, not just the JS breadcrumb that follows bundle load. The native SDK previously only started when JS called Sentry.init, making launch crashes like MOBILE-7 nearly impossible to diagnose.

  • useNativeInit has the @sentry/react-native/expo plugin generate sentry.options.json at prebuild and call RNSentrySDK.start() as the first line of didFinishLaunching.
  • MetricKit diagnostics are enabled in both the plugin options and Sentry.init, since OTA bundles are exported without prebuild and re-initialize the native SDK from JS options alone.
  • sentry.options.json is gitignored because the plugin merges into any existing copy, and a stale one would keep options app.config.ts no longer sets.
  • Requires a new binary; the fingerprint runtime keeps it off existing builds' OTA channel.

Summary by CodeRabbit

  • New Features
    • Improved mobile app error monitoring with native initialization.
    • Added environment-specific Sentry configuration using the mobile app’s DSN and environment settings.
    • Enabled MetricKit support for enhanced iOS performance and diagnostic data.