Skip to content
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

Merged
merged 1 commit into from
Oct 8, 2023

Conversation

tarekgh
Copy link
Member

@tarekgh tarekgh commented Oct 5, 2023

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 the RequiredUnreferencedCode attribute, leading to linker warnings. It's important to note that the ValidationContext object created in this context is exclusively used within the Validator.TryValidateValue API call.

The solution consists of two parts:

  • We generate code to create the 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.
  • For attributes such as MaxLengthAttribute, MinLengthAttribute, LengthAttribute, CompareAttribute, and RangeAttribute, 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.

@ghost
Copy link

ghost commented Oct 5, 2023

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

Issue Details

#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 the RequiredUnreferencedCode attribute, leading to linker warnings. It's important to note that the ValidationContext object created in this context is exclusively used within the Validator.TryValidateValue API call.

The solution consists of two parts:

  • We generate code to create the 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.
  • For attributes such as MaxLengthAttribute, MinLengthAttribute, LengthAttribute, CompareAttribute, and RangeAttribute, 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.

Author: tarekgh
Assignees: tarekgh
Labels:

area-Extensions-Options

Milestone: -

@tarekgh tarekgh added this to the 8.0.0 milestone Oct 5, 2023
@jeffhandley
Copy link
Member

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.

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a 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?

@tarekgh
Copy link
Member Author

tarekgh commented Oct 6, 2023

I'm guessing this will be backported to 8?

Yes.

Copy link
Member

@jeffhandley jeffhandley left a 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.

@tarekgh tarekgh force-pushed the FixOptionsSourceGenTrimmingIssues branch from ff86e3f to 6eddd0d Compare October 8, 2023 21:45
@tarekgh tarekgh merged commit 26a809a into dotnet:main Oct 8, 2023
110 checks passed
@tarekgh
Copy link
Member Author

tarekgh commented Oct 8, 2023

/backport to release/8.0

@github-actions
Copy link
Contributor

github-actions bot commented Oct 8, 2023

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Source generator for options validation leads to trimming warnings/errors in AOT
3 participants