Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

jpnurmi
Copy link
Collaborator

@jpnurmi jpnurmi commented Jun 19, 2025

This PR fixes a regression introduced by #4275: the _SentryIsNet8OrGreater helper property was not correctly evaluated when publishing AOT, and Sentry was used as a NuGet package.

The newly introduced integration test creates a minimal AOT app out of the tree in the system temp directory, and tests both PublishAot and also PublishContainer on Linux with Docker available.

Fixes: #4296

@jpnurmi jpnurmi changed the title WIP: Native AOT linking WIP: fix: Native AOT linking Jun 20, 2025
@jpnurmi jpnurmi changed the title WIP: fix: Native AOT linking fix: Native AOT linking Jun 20, 2025
@jpnurmi jpnurmi marked this pull request as ready for review June 20, 2025 14:44
@bruno-garcia bruno-garcia requested a review from vaind June 20, 2025 15:01
@jpnurmi
Copy link
Collaborator Author

jpnurmi commented Jun 20, 2025

Background:

As you might notice from the commit history, I started with trying to make the existing integration test fail but eventually gave up. Since I had a set of commands (#4296 (comment)) I was able to use to reproduce the error locally, I decided to try running those in the CI. First, directly in the workflow for Linux, then as a manual test script for all platforms, and finally, I turned it into a Pester test script.

The second last commit shows what kind of failures the new integration test produces when the properties are not correctly evaluated:

ubuntu-22.04 (linux-x64)
Describing Publish
  Determining projects to restore...
  Restored /tmp/0xsgwocv.qfm/hello-sentry.csproj (in 1.34 sec).
  hello-sentry -> /tmp/0xsgwocv.qfm/bin/Release/net9.0/linux-x64/hello-sentry.dll
  Generating native code
  hello-sentry -> /tmp/0xsgwocv.qfm/bin/Release/net9.0/linux-x64/publish/

Workload updates are available. Run `dotnet workload list` for more information.
  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: DSN read from options: https://[email protected]/42
  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) + 0x22
   at Sentry.SentrySdk.InitNativeSdk(SentryOptions) + 0x2d
   at Sentry.SentrySdk.InitHub(SentryOptions) + 0x1[90](https://github.com/getsentry/sentry-dotnet/actions/runs/15779751203/job/44482314804#step:24:94)
   at Sentry.SentrySdk.Init(Action`1) + 0x3a
   at Program.<Main>$(String[] args) + 0x51
Error: [-] Aot 28.16s (28.13s|29ms)
Message
  Determining projects to restore...
  All projects are up-to-date for restore.
  hello-sentry -> /tmp/0xsgwocv.qfm/bin/Release/net9.0/linux-x64/hello-sentry.dll
  hello-sentry -> /tmp/0xsgwocv.qfm/bin/Release/net9.0/linux-x64/publish/
  Building image 'hello-sentry' with tags 'latest' on top of base image 'mcr.microsoft.com/dotnet/nightly/runtime-deps:9.0-noble-chiseled-aot'.
  Pushed image 'hello-sentry:latest' to local registry via 'docker'.

Workload updates are available. Run `dotnet workload list` for more information.
  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: DSN read from options: https://[email protected]/42
  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) + 0x22
   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
Error: [-] Container 10.33s (10.33s|1ms)

https://github.com/getsentry/sentry-dotnet/actions/runs/15779751203/job/44482314804

windows-latest (win-x64)
Describing Publish
  Determining projects to restore...
  Restored C:\Users\runneradmin\AppData\Local\Temp\s4hlsiu4.2qj\hello-sentry.csproj (in 6.41 sec).
  hello-sentry -> C:\Users\runneradmin\AppData\Local\Temp\s4hlsiu4.2qj\bin\Release\net9.0\win-x64\hello-sentry.dll
  Generating native code
  hello-sentry -> C:\Users\runneradmin\AppData\Local\Temp\s4hlsiu4.2qj\bin\Release\net9.0\win-x64\publish\

Workload updates are available. Run `dotnet workload list` for more information.
  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: DSN read from options: https://[email protected]/42
  Debug: This looks like a Native AOT application build.
Unhandled exception. System.DllNotFoundException: Unable to load DLL 'sentry-native' or one of its dependencies: The specified module could not be found.
   at System.Runtime.InteropServices.NativeLibrary.LoadLibErrorTracker.Throw(String) + 0x50
   at Internal.Runtime.CompilerHelpers.InteropHelpers.FixupModuleCell(InteropHelpers.ModuleFixupCell*) + 0x10e
   at Internal.Runtime.CompilerHelpers.InteropHelpers.ResolvePInvokeSlow(InteropHelpers.MethodFixupCell*) + 0x2f
   at Sentry.Native.C.sentry_options_new() + 0x24
   at Sentry.Native.C.Init(SentryOptions) + 0x25
   at Sentry.SentrySdk.InitNativeSdk(SentryOptions) + 0x2b
   at Sentry.SentrySdk.InitHub(SentryOptions) + 0x199
   at Sentry.SentrySdk.Init(Action`1) + 0x37
   at Program.<Main>$(String[] args) + 0x4e
Error: [-] Aot 30.33s (30.29s|33ms)
Message
  [!] Container 2ms (0ms|2ms)

https://github.com/getsentry/sentry-dotnet/actions/runs/15779751203/job/44482314808

Copy link
Collaborator

@vaind vaind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please make sure tests also pass on mac before merging:

Describing Publish
  Determining projects to restore...
  Restored /private/var/folders/x7/ch5v91h56_zbvbd1y2f600dm0000gn/T/qc3adnir.lmp/hello-sentry.csproj (in 1.45 sec).
Error: /private/var/folders/x7/ch5v91h56_zbvbd1y2f600dm0000gn/T/qc3adnir.lmp/Program.cs(1,1): error CS0103: The name 'SentrySdk' does not exist in the current context [/private/var/folders/x7/ch5v91h56_zbvbd1y2f600dm0000gn/T/qc3adnir.lmp/hello-sentry.csproj]

Workload updates are available. Run `dotnet workload list` for more information.
Error: [-] Aot 2.73s (2.[71](https://github.com/getsentry/sentry-dotnet/actions/runs/15780852336/job/44485984274?pr=4298#step:24:75)s|19ms)
Message
  Expected 0, but got 1.
  at $LASTEXITCODE | Should -Be 0, /Users/runner/work/sentry-dotnet/sentry-dotnet/integration-test/aot.Tests.ps1:52
  at <ScriptBlock>, /Users/runner/work/sentry-dotnet/sentry-dotnet/integration-test/aot.Tests.ps1:52

@bruno-garcia bruno-garcia merged commit f666304 into main Jun 21, 2025
29 checks passed
@bruno-garcia bruno-garcia deleted the fix/native-aot branch June 21, 2025 03:18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We really need to get to the bottom of why these props are not being read from:

<_SentryTargetFrameworkVersion>$([MSBuild]::GetTargetFrameworkVersion($(TargetFramework)))</_SentryTargetFrameworkVersion>
<_SentryIsNet8OrGreater>$([MSBuild]::VersionGreaterThanOrEquals($(_SentryTargetFrameworkVersion), 8.0))</_SentryIsNet8OrGreater>
<_SentryIsNet9OrGreater>$([MSBuild]::VersionGreaterThanOrEquals($(_SentryTargetFrameworkVersion), 9.0))</_SentryIsNet9OrGreater>

... and the closely related:

Note: The following platform-specific properties need to be set in both Directory.Build.props and Directory.Build.targets.
TODO: Figure out how to consolidate to a single location.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentry.props is read but before TargetFramework is set 🙁

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so no. See:

I think MS Build runs a couple of passes though... once to do stuff that is framework specific (when the TargetFramework is set) and once to do stuff that is framework agnostic. That's why this condition was added to the props file:

<PropertyGroup Condition="'$(TargetFramework)' != ''">

In the experimenting that I did, the agnostic run seems to happen after the specific stuff.

Any code in the props/targets/csproj files that depends on the TargetFramework being set really needs to be guarded by that condition then, since otherwise it will get executed with an effective target framework version of 0 (and likely do something unexpected).

All of this is just from experimenting and trying to reverse engineer it though... I'd feel way more comfortable if there were some docs somewhere explaining what the heck is going on. Whatever it is, it's not very intuitive.

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.

Sentry 5.11 causes DllNotFoundException in Native AoT application
4 participants