-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Options Source Gen Trimming Issues #93088
Conversation
Tagging subscribers to this area: @dotnet/area-extensions-options Issue DetailsThis change addresses issues that arise when using the Options source generator in AOT builds. The trimming linker generates warnings due to the use of Validation attributes that rely on reflection. Additionally, the source generator emits code that uses The solution consists of two parts:
These changes address the issues and improve the overall safety and performance of the code in AOT buildings.
|
....Options/tests/SourceGeneration.Unit.Tests/Baselines/DataAnnotationAttributesWithParams.g.cs
Outdated
Show resolved
Hide resolved
....Options/tests/SourceGeneration.Unit.Tests/Baselines/DataAnnotationAttributesWithParams.g.cs
Show resolved
Hide resolved
It's kind of a bummer that we have to special-case the built-in validators to pull this off, but it makes sense for .NET 8. This will address the vast majority of instances where AOT would have been blocked for customers. If we hear feedback about ecosystem or customer attributes that need this same type of substitution treatment, we can explore approaches for a general-purpose substitution. |
src/libraries/Microsoft.Extensions.Options/gen/CommonUtilities.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing this will be backported to 8?
Yes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, save @eiriktsarpalis's question about the target framework handling.
ff86e3f
to
6eddd0d
Compare
/backport to release/8.0 |
Started backporting to release/8.0: https://github.com/dotnet/runtime/actions/runs/6450606563 |
Fixes #92327
This change addresses issues that arise when using the Options source generator in AOT builds. The trimming linker generates warnings due to the use of Validation attributes that rely on reflection. Additionally, the source generator emits code that uses
ValidationContext
, which has constructors annotated with theRequiredUnreferencedCode
attribute, leading to linker warnings. It's important to note that theValidationContext
object created in this context is exclusively used within theValidator.TryValidateValue
API call.The solution consists of two parts:
ValidationContext
object and initialize the display and member names within it. This ensures the object's safe use because the reflection code path is not executed during this time. Consequently, we suppress the trimming linker warnings associated with this part.MaxLengthAttribute
,MinLengthAttribute
,LengthAttribute
,CompareAttribute
, andRangeAttribute
, which rely on reflection, we generate replacement attributes. These replacement attributes are strongly typed and entirely avoid the use of reflection.These changes address the issues and improve the overall safety and performance of the code in AOT buildings.