sentry-dotnet icon indicating copy to clipboard operation
sentry-dotnet copied to clipboard

Multi-target Roslyn Compiler Extensions

Open Flash0ver opened this issue 6 months ago • 3 comments

Description

We don't know under which environment the user is hosting Roslyn when building a project that depends on Sentry.

  • .NET SDK version
  • "Full"-Framework MSBuild
  • custom Host

See https://github.com/dotnet/sdk/issues/20355

So that older hosts won't load the Roslyn Component, and newer hosts can choose a compatible version.

E.g. fixes issues like

An instance of analyzer Sentry.SourceGenerators.BuildPropertySourceGenerator cannot be created from {..}\.nuget\packages\sentry\5.11.0\analyzers\dotnet\cs\Sentry.SourceGenerators.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

Examples

Flash0ver avatar Jun 25 '25 11:06 Flash0ver

Only .NET 7 and up compile AOT which was the main reason for the source generators. Just an FYI unless you're looking to expand on what it does.

aritchie avatar Jul 11 '25 13:07 aritchie

Thanks for the hint, @aritchie. And most welcome it is indeed!❗️

We're looking into expanding the source generator to enable #3212 via #4330. But I just realize that we would conflate concerns here. The Build-Properties are intended to detect NativeAOT, for NET7_0_OR_GREATER. But Auto-Debug-Mode could/should work regardless of the TFM.

So this should really be two different source generators. And perhaps the existing BuildPropertySourceGenerator should be renamed and repurposed to NativeAotGenerator, generating code that sets a Native-AOT flag to true when #if NET7_0_OR_GREATER. And another AutoDebugGenerator should then generate code that sets a Auto-Debug flag to true when #if DEBUG. So that we don't depend on Build-Properties being exposed publicly, where the Source-Generator could already evaluate the Build-Properties at Compile-Time, and we don't pay the price of allocating and performing a lookup at run-time.

Flash0ver avatar Jul 11 '25 18:07 Flash0ver

It looks like there might not be any clean way to implement auto-debug (see comment)... in which case, the only thing we'd be using the source generators for is to detect AOT compilation settings.

jamescrosswell avatar Jul 13 '25 21:07 jamescrosswell