Skip to content

Conversation

@jpnurmi
Copy link
Collaborator

@jpnurmi jpnurmi commented May 26, 2025

This PR fixes the problem that <SentryNative>false</SentryNative> results in Sentry.Native.IsEnabled=true at runtime, as it came up in:

@jpnurmi jpnurmi changed the title fix: respect -p:SentryNative=false at runtime fix: respect SentryNative=false at runtime May 26, 2025
@jpnurmi
Copy link
Collaborator Author

jpnurmi commented May 26, 2025

The Sentry.Native.IsEnabled runtime host configuration option was only defined when SentryNative was not false nor disabled at build time.

<!-- When user sets <SentryNative>false</SentryNative> or <SentryNative>disable</SentryNative> in their project -->
<!-- SentryNative.IsEnabled should result in compile-time constant for trimmed applications -->
<!-- Effectively disabling native library -->
<RuntimeHostConfigurationOption Include="Sentry.Native.IsEnabled"
Condition="'$(SentryNative)' != 'false' and '$(SentryNative)' != 'disable'"
Value="true"
Trim="true" />

Subsequently, when the runtime host configuration option was undefined at runtime, it would default to true.

// Should be in-sync with Sentry.Native.targets const.
private const string SentryNativeIsEnabledSwitchName = "Sentry.Native.IsEnabled";
private static readonly bool IsAvailableCore;
#if NET9_0_OR_GREATER
// FeatureSwitchDefinition should help with trimming disabled code.
// This way, `SentryNative.IsEnabled` should be treated as a compile-time constant for trimmed apps.
[FeatureSwitchDefinition(SentryNativeIsEnabledSwitchName)]
#endif
private static bool IsEnabled => !AppContext.TryGetSwitch(SentryNativeIsEnabledSwitchName, out var isEnabled) || isEnabled;

Manual steps for reproducing the problem:

$ dotnet new console -o HelloWorld --aot
[...]
$ cd HelloWorld/
$ dotnet add package Sentry -v 5.8.0
[...]
$ cat > Program.cs <<'EOF'
SentrySdk.Init(options =>
{
    options.Dsn = "https://[email protected]/42";
    options.Debug = true;
});
Console.WriteLine("Hello, Sentry!");
EOF
$ dotnet publish -p:SentryNative=false
Restore complete (0.3s)
  HelloWorld succeeded (10.0s) → bin/Release/net9.0/linux-x64/publish/

Build succeeded in 10.5s

Workload updates are available. Run `dotnet workload list` for more information.
$ ./bin/Release/net9.0/linux-x64/publish/HelloWorld
  Debug: Logging enabled with ConsoleDiagnosticLogger and min level: Debug
Warning: Sentry option 'Debug' is set to true while Environment is production. Be aware this can cause performance degradation and is not advised. See https://docs.sentry.io/platforms/dotnet/configuration/diagnostic-logger for more information
  Debug: This looks like a Native AOT application build.
Unhandled exception. System.DllNotFoundException: Unable to load shared library 'sentry-native' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable:
sentry-native.so: cannot open shared object file: No such file or directory
libsentry-native.so: cannot open shared object file: No such file or directory
sentry-native: cannot open shared object file: No such file or directory
libsentry-native: cannot open shared object file: No such file or directory

   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x46
   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x139
   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x35
   at Sentry.Native.C.sentry_options_new() + 0x1f
   at Sentry.Native.C.Init(SentryOptions) + 0x1f
   at Sentry.SentrySdk.InitNativeSdk(SentryOptions) + 0x2d
   at Sentry.SentrySdk.InitHub(SentryOptions) + 0x190
   at Sentry.SentrySdk.Init(Action`1) + 0x3a
   at Program.<Main>$(String[] args) + 0x51
Aborted (core dumped)
runtimeconfig.json (SentryNative=true => "Sentry.Native.IsEnabled": true)
$ dotnet publish -p:SentryNative=true
Restore complete (0.2s)
  HelloWorld succeeded (8.6s) → bin/Release/net9.0/linux-x64/publish/

Build succeeded in 8.9s

Workload updates are available. Run `dotnet workload list` for more information.
$ cat ./bin/Release/net9.0/linux-x64/HelloWorld.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net9.0",
    "includedFrameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "9.0.4"
      }
    ],
    "configProperties": {
      "Sentry.Native.IsEnabled": true,
      "Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability": true,
      "System.ComponentModel.DefaultValueAttribute.IsSupported": false,
      "System.ComponentModel.Design.IDesignerHost.IsSupported": false,
      "System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization": false,
      "System.ComponentModel.TypeDescriptor.IsComObjectDescriptorSupported": false,
      "System.Diagnostics.Tracing.EventSource.IsSupported": false,
      "System.Globalization.Invariant": true,
      "System.Globalization.PredefinedCulturesOnly": true,
      "System.Net.Security.UseManagedNtlm": false,
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Resources.ResourceManager.AllowCustomResourceTypes": false,
      "System.Resources.UseSystemResourceKeys": false,
      "System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported": false,
      "System.Runtime.InteropServices.BuiltInComInterop.IsSupported": false,
      "System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting": false,
      "System.Runtime.InteropServices.EnableCppCLIHostActivation": false,
      "System.Runtime.InteropServices.Marshalling.EnableGeneratedComInterfaceComImportInterop": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
      "System.StartupHookProvider.IsSupported": false,
      "System.Text.Encoding.EnableUnsafeUTF7Encoding": false,
      "System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": false,
      "System.Threading.Thread.EnableAutoreleasePool": false,
      "System.Linq.Expressions.CanEmitObjectArrayDelegate": false
    }
  }
}
runtimeconfig.json (SentryNative=false => N/A)
$ dotnet publish -p:SentryNative=false
Restore complete (0.2s)
  HelloWorld succeeded (8.3s) → bin/Release/net9.0/linux-x64/publish/

Build succeeded in 8.6s

Workload updates are available. Run `dotnet workload list` for more information.
$ cat ./bin/Release/net9.0/linux-x64/HelloWorld.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net9.0",
    "includedFrameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "9.0.4"
      }
    ],
    "configProperties": {
      "Microsoft.Extensions.DependencyInjection.VerifyOpenGenericServiceTrimmability": true,
      "System.ComponentModel.DefaultValueAttribute.IsSupported": false,
      "System.ComponentModel.Design.IDesignerHost.IsSupported": false,
      "System.ComponentModel.TypeConverter.EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization": false,
      "System.ComponentModel.TypeDescriptor.IsComObjectDescriptorSupported": false,
      "System.Diagnostics.Tracing.EventSource.IsSupported": false,
      "System.Globalization.Invariant": true,
      "System.Globalization.PredefinedCulturesOnly": true,
      "System.Net.Security.UseManagedNtlm": false,
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Resources.ResourceManager.AllowCustomResourceTypes": false,
      "System.Resources.UseSystemResourceKeys": false,
      "System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported": false,
      "System.Runtime.InteropServices.BuiltInComInterop.IsSupported": false,
      "System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting": false,
      "System.Runtime.InteropServices.EnableCppCLIHostActivation": false,
      "System.Runtime.InteropServices.Marshalling.EnableGeneratedComInterfaceComImportInterop": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false,
      "System.StartupHookProvider.IsSupported": false,
      "System.Text.Encoding.EnableUnsafeUTF7Encoding": false,
      "System.Text.Json.JsonSerializer.IsReflectionEnabledByDefault": false,
      "System.Threading.Thread.EnableAutoreleasePool": false,
      "System.Linq.Expressions.CanEmitObjectArrayDelegate": false
    }
  }
}

@jpnurmi jpnurmi force-pushed the fix/disable-sentry-native branch from ccb7e24 to d33c54a Compare May 26, 2025 13:49
@jpnurmi jpnurmi force-pushed the fix/disable-sentry-native branch from d33c54a to a8fbd22 Compare May 26, 2025 15:07
@bruno-garcia bruno-garcia merged commit 8b4ac33 into main May 26, 2025
40 of 41 checks passed
@bruno-garcia bruno-garcia deleted the fix/disable-sentry-native branch May 26, 2025 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants