getsentry / sentry-cocoa

The official Sentry SDK for iOS, tvOS, macOS, watchOS.
https://sentry.io/for/cocoa/
MIT License
773 stars 306 forks source link

Mac app crashes on launch when using Sentry static framework #3949

Closed bjhomer closed 2 weeks ago

bjhomer commented 3 weeks ago

Platform

macOS

Environment

Production, Develop, TestFlight

Installed

Swift Package Manager

Version

8.25.0

Did it work on previous versions?

8.21.0

Steps to Reproduce

  1. Create a new macOS app
  2. Add Sentry as an SPM dependency
  3. Change the Principal Class to SentryCrashExceptionApplication, as described here.
  4. Run the app

See the attached sample project, which should reproduce the issue.

Expected Result

The app should launch when run.

Actual Result

The app crashes on launch. Nothing appears in the Xcode console, but in the macOS console, we see this:

Unable to find class: SentryCrashExceptionApplication, exiting

I suspect that because there is no reference to SentryCrashExceptionApplication in the code, the class was never linked in from the static library, and thus is not present at runtime. If I add any reference to SentryCrashExceptionApplication in the code, it launches correctly. For example, I can add this:

let x = SentryCrashExceptionApplication.self
print(x)

Note, though, that if this reference were optimized out by the compiler, then the app might crash again. (Hence the need to actually print it.)

It would be nice if Sentry itself could reference SentryCrashExceptionApplication on macOS, so that it is seen as a transitive dependency and thus would be linked in when other Sentry APIs are referenced. Alternatively, if referencing SentryCrashExceptionApplication in code is necessary, this should be documented.

Are you willing to submit a PR?

I'd be willing to, but it's not clear what route the team wants to take

bjhomer commented 3 weeks ago

Sample project that reproduces the issue: SentryMacCrash.zip

brustolin commented 3 weeks ago

Hello @bjhomer, this happens because the compiler is stripping the lib from unused code. You can solve this by adding a reference to SentryCrashExceptionApplication somewhere in your code just like this:

_ = SentryCrashExceptionApplication.shared
bjhomer commented 3 weeks ago

Right, and I noted that in my ticket. This should perhaps be documented, though, as right now, following the instructions leads to an app that won't launch.

brustolin commented 3 weeks ago

Thanks for the feedback, we will implement your suggestion of having the reference inside Sentry SDK.