Skip to content

Generate AotHelper properties at compile time #2927

@jamescrosswell

Description

@jamescrosswell

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

Metadata

Metadata

Assignees

Projects

Status

Blocked

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions