A couple of different options
Use built in attributes
The RuntimeFeature.IsDynamicCodeSupported Property looks like it might do what we want in many cases (I think we're using AotHelper.IsNativeAot where we should probably be checking for Trimming compatibility instead in some cases). Need to do a search for uses of AotHelper.IsNativeAot and a bit of a review of these.
Generate these at compile time
For this AotHelper, instead of:
|
var stackTrace = new StackTrace(false); |
|
IsNativeAot = stackTrace.GetFrame(0)?.GetMethod() is null; |
Could we instead write an attribute at compile time? I imagine it's a lot cheaper (worth measuring) than trying to get a stack trace on a static constructor:
Something like this:
|
<Target Name="WriteSentryAttributes" |
|
Condition="$(Language) == 'VB' or $(Language) == 'C#' or $(Language) == 'F#'" |
|
BeforeTargets="BeforeCompile;CoreCompile" |
|
Inputs="$(MSBuildAllProjects)" |
|
Outputs="$(IntermediateOutputPath)$(SentryAttributesFile)"> |
|
<PropertyGroup> |
|
<SentryAttributesFilePath>$(IntermediateOutputPath)$(SentryAttributesFile)</SentryAttributesFilePath> |
|
</PropertyGroup> |
|
<ItemGroup> |
|
<SentryAttributes Include="System.Reflection.AssemblyMetadata"> |
|
<_Parameter1>Sentry.ProjectDirectory</_Parameter1> |
|
<_Parameter2>$(ProjectDir)</_Parameter2> |
|
</SentryAttributes> |
|
<!-- Ensure not part of Compile, as a workaround for https://github.com/dotnet/sdk/issues/114 --> |
|
<Compile Remove="$(SentryAttributesFilePath)" /> |
|
</ItemGroup> |
|
<WriteCodeFragment AssemblyAttributes="@(SentryAttributes)" Language="$(Language)" OutputFile="$(SentryAttributesFilePath)"> |
|
<Output Condition="$(Language) != 'F#'" TaskParameter="OutputFile" ItemName="Compile" /> |
|
<Output Condition="$(Language) == 'F#'" TaskParameter="OutputFile" ItemName="CompileBefore" /> |
|
<Output TaskParameter="OutputFile" ItemName="FileWrites" /> |
|
</WriteCodeFragment> |
|
</Target> |
Here's a PR that added writing the attribute at build time as a reference: #1739
Originally posted by @bruno-garcia in #2920 (comment)
References
A couple of different options
Use built in attributes
The RuntimeFeature.IsDynamicCodeSupported Property looks like it might do what we want in many cases (I think we're using
AotHelper.IsNativeAotwhere we should probably be checking for Trimming compatibility instead in some cases). Need to do a search for uses ofAotHelper.IsNativeAotand a bit of a review of these.Generate these at compile time
For this AotHelper, instead of:
sentry-dotnet/src/Sentry/Internal/AotHelper.cs
Lines 18 to 19 in 217c11c
Could we instead write an attribute at compile time? I imagine it's a lot cheaper (worth measuring) than trying to get a stack trace on a static constructor:
Something like this:
sentry-dotnet/src/Sentry/buildTransitive/Sentry.targets
Lines 8 to 29 in 217c11c
Here's a PR that added writing the attribute at build time as a reference: #1739
Originally posted by @bruno-garcia in #2920 (comment)
References