-
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
[release/8.0] Fix Options Source Gen Trimming Issues #93193
Conversation
Tagging subscribers to this area: @dotnet/area-meta Issue DetailsBackport of #93088 to release/8.0 /cc @tarekgh Customer ImpactTestingRiskIMPORTANT: If this backport is for a servicing release, please verify that:
|
Tagging subscribers to this area: @dotnet/area-extensions-options Issue DetailsBackport of #93088 to release/8.0 /cc @tarekgh Customer ImpactApplications constructed with AOT compilation and employing the Options validation source generator may encounter warnings stemming from the generated code. This occurs due to the generation of code reliant on types adorned with the 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 The solution consists of two parts:
These changes address the issues and improve the overall safety and performance of the code in AOT buildings. TestingI conducted a manual test of the relevant scenario and successfully passed all regression tests. Additionally, I included more tests to encompass the specific areas we are addressing. RiskThese modifications are limited in scope to the five validation attributes that are not trim-friendly. We have not made any alterations to the source generator's other logic. IMPORTANT: If this backport is for a servicing release, please verify that:
|
Test failure here looks related:
|
Yes, I am looking at that. |
#93260) * Make Emitted Attribute Order Deterministic in Options Source Generator * Use ordinal comparison when ordering the list
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.
LGTM, thank you for fixing the failing test
Backport of #93088 to release/8.0
#92327
/cc @tarekgh
Customer Impact
Applications constructed with AOT compilation and employing the Options validation source generator may encounter warnings stemming from the generated code. This occurs due to the generation of code reliant on types adorned with the
RequiredUnreferencedCode
attribute. These types utilize reflection and are not conducive to AOT builds due to their lack of trim-friendliness.Details
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.
Testing
I conducted a manual test of the relevant scenario and successfully passed all regression tests. Additionally, I included more tests to encompass the specific areas we are addressing.
Risk
These modifications are limited in scope to the five validation attributes that are not trim-friendly. We have not made any alterations to the source generator's other logic.
IMPORTANT: If this backport is for a servicing release, please verify that:
The PR target branch is
release/X.0-staging
, notrelease/X.0
.If the change touches code that ships in a NuGet package, you have added the necessary package authoring and gotten it explicitly reviewed.