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

[release/8.0] Fix Options Source Gen Trimming Issues #93193

Merged
merged 2 commits into from
Oct 12, 2023

Conversation

github-actions[bot]
Copy link
Contributor

@github-actions github-actions bot commented Oct 8, 2023

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 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.

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, not release/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.

@ghost
Copy link

ghost commented Oct 8, 2023

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

Issue Details

Backport of #93088 to release/8.0

/cc @tarekgh

Customer Impact

Testing

Risk

IMPORTANT: If this backport is for a servicing release, please verify that:

  • The PR target branch is release/X.0-staging, not release/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.

Author: github-actions[bot]
Assignees: -
Labels:

area-Meta

Milestone: -

@tarekgh tarekgh added Servicing-consider Issue for next servicing release review area-Extensions-Options and removed area-Meta labels Oct 8, 2023
@ghost
Copy link

ghost commented Oct 8, 2023

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

Issue Details

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. Here are some more 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 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.

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, not release/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.

Author: github-actions[bot]
Assignees: -
Labels:

Servicing-consider, area-Extensions-Options

Milestone: -

@tarekgh tarekgh self-assigned this Oct 8, 2023
@tarekgh tarekgh added this to the 8.0.0 milestone Oct 8, 2023
@tarekgh
Copy link
Member

tarekgh commented Oct 8, 2023

@ericstj
Copy link
Member

ericstj commented Oct 9, 2023

Test failure here looks related:

  Starting:    Microsoft.Extensions.Options.SourceGeneration.Tests (parallel test collections = on, max threads = 2)
    Microsoft.Gen.OptionsValidation.Test.EmitterTests.TestEmitter [FAIL]
      Assert.Equal() Failure
                                       ↓ (pos 128490)
      Expected: ··· class __SourceGen__MinLengthAttribute : global::System.Compo···
      Actual:   ··· class __SourceGen__RangeAttribute : global::System.Component···
                                       ↑ (pos 128490)
      Stack Trace:
        /_/src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/EmitterTests.cs(56,0): at Microsoft.Gen.OptionsValidation.Test.EmitterTests.TestEmitter()
        --- End of stack trace from previous location ---

@tarekgh
Copy link
Member

tarekgh commented Oct 9, 2023

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
Copy link
Member

@ericstj ericstj left a 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

@leecow leecow added Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review labels Oct 10, 2023
@carlossanlop
Copy link
Member

CI failures are unrelated: #91757 #93250

@carlossanlop carlossanlop merged commit ba51641 into release/8.0 Oct 12, 2023
107 of 111 checks passed
@carlossanlop carlossanlop deleted the backport/pr-93088-to-release/8.0 branch October 12, 2023 00:02
@ghost ghost locked as resolved and limited conversation to collaborators Nov 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Options Servicing-approved Approved for servicing release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants