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

Source generator for options validation leads to trimming warnings/errors in AOT #92327

Closed
rstropek opened this issue Sep 20, 2023 · 7 comments · Fixed by #93088
Closed

Source generator for options validation leads to trimming warnings/errors in AOT #92327

rstropek opened this issue Sep 20, 2023 · 7 comments · Fixed by #93088
Assignees
Milestone

Comments

@rstropek
Copy link

Description

If you follow the guide in the docs, you get trimming warnings related to data annotations. In my own program, these warnings lead to errors.

Reproduction Steps

  • Create a new app: dotnet new webapiaot

  • Add NuGet.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
      </packageSources>
    </configuration>
  • Add nightly package references in .csproj:

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rc.2.23466.4" />
      <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0-rc.2.23466.4" />
    </ItemGroup>
  • Copy sample code from the docs into your Program.cs

  • Build app: dotnet run build

Expected behavior

Program compiles without error and warnings.

Actual behavior

You get a bunch of trimming warnings related to data annotations:

/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(50): Trim analysis warning IL2026: SecondValidatorNoNamespace.Validate(String,SecondModelNoNamespace): Using member 'System.ComponentModel.DataAnnotations.ValidationContext.ValidationContext(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The Type of instance cannot be statically discovered and the Type's properties can be trimmed. [/tmp/optionsvalidator/optionsvalidator.csproj]
/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(17): Trim analysis warning IL2026: FirstValidatorNoNamespace.Validate(String,FirstModelNoNamespace): Using member 'System.ComponentModel.DataAnnotations.ValidationContext.ValidationContext(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The Type of instance cannot be statically discovered and the Type's properties can be trimmed. [/tmp/optionsvalidator/optionsvalidator.csproj]
/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(73): Trim analysis warning IL2026: __OptionValidationStaticInstances.<Validators_g>F1872690B80E3D4EBF0BB4EEFEFFBD255387F31D123553B47D3D3ED7D782424E7____Attributes..cctor(): Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved. [/tmp/optionsvalidator/optionsvalidator.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

  • Runtime: Microsoft.AspNetCore.App 8.0.0-rc.1.23421.29
  • SDK: 8.0.100-rc.1.23455.8
  • OS: Linux

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 20, 2023
@ghost
Copy link

ghost commented Sep 20, 2023

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

Issue Details

Description

If you follow the guide in the docs, you get trimming warnings related to data annotations. In my own program, these warnings lead to errors.

Reproduction Steps

  • Create a new app: dotnet new webapiaot

  • Add NuGet.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
      </packageSources>
    </configuration>
  • Add nightly package references in .csproj:

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rc.2.23466.4" />
      <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0-rc.2.23466.4" />
    </ItemGroup>
  • Copy sample code from the docs into your Program.cs

  • Build app: dotnet run build

Expected behavior

Program compiles without error and warnings.

Actual behavior

You get a bunch of trimming warnings related to data annotations:

/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(50): Trim analysis warning IL2026: SecondValidatorNoNamespace.Validate(String,SecondModelNoNamespace): Using member 'System.ComponentModel.DataAnnotations.ValidationContext.ValidationContext(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The Type of instance cannot be statically discovered and the Type's properties can be trimmed. [/tmp/optionsvalidator/optionsvalidator.csproj]
/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(17): Trim analysis warning IL2026: FirstValidatorNoNamespace.Validate(String,FirstModelNoNamespace): Using member 'System.ComponentModel.DataAnnotations.ValidationContext.ValidationContext(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The Type of instance cannot be statically discovered and the Type's properties can be trimmed. [/tmp/optionsvalidator/optionsvalidator.csproj]
/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(73): Trim analysis warning IL2026: __OptionValidationStaticInstances.<Validators_g>F1872690B80E3D4EBF0BB4EEFEFFBD255387F31D123553B47D3D3ED7D782424E7____Attributes..cctor(): Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved. [/tmp/optionsvalidator/optionsvalidator.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

  • Runtime: Microsoft.AspNetCore.App 8.0.0-rc.1.23421.29
  • SDK: 8.0.100-rc.1.23455.8
  • OS: Linux

Other information

No response

Author: rstropek
Assignees: -
Labels:

area-System.ComponentModel.DataAnnotations

Milestone: -

@rstropek
Copy link
Author

Did further testing. The functionality seems to work. So the problem is the warnings.

@tarekgh tarekgh added this to the 8.0.0 milestone Sep 20, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 20, 2023
@tarekgh tarekgh self-assigned this Sep 20, 2023
@tarekgh tarekgh added untriaged New issue has not been triaged by the area owner area-Extensions-Options and removed area-System.ComponentModel.DataAnnotations untriaged New issue has not been triaged by the area owner labels Sep 20, 2023
@ghost
Copy link

ghost commented Sep 20, 2023

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

Issue Details

Description

If you follow the guide in the docs, you get trimming warnings related to data annotations. In my own program, these warnings lead to errors.

Reproduction Steps

  • Create a new app: dotnet new webapiaot

  • Add NuGet.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
      </packageSources>
    </configuration>
  • Add nightly package references in .csproj:

    <ItemGroup>
      <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rc.2.23466.4" />
      <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0-rc.2.23466.4" />
    </ItemGroup>
  • Copy sample code from the docs into your Program.cs

  • Build app: dotnet run build

Expected behavior

Program compiles without error and warnings.

Actual behavior

You get a bunch of trimming warnings related to data annotations:

/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(50): Trim analysis warning IL2026: SecondValidatorNoNamespace.Validate(String,SecondModelNoNamespace): Using member 'System.ComponentModel.DataAnnotations.ValidationContext.ValidationContext(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The Type of instance cannot be statically discovered and the Type's properties can be trimmed. [/tmp/optionsvalidator/optionsvalidator.csproj]
/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(17): Trim analysis warning IL2026: FirstValidatorNoNamespace.Validate(String,FirstModelNoNamespace): Using member 'System.ComponentModel.DataAnnotations.ValidationContext.ValidationContext(Object)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. The Type of instance cannot be statically discovered and the Type's properties can be trimmed. [/tmp/optionsvalidator/optionsvalidator.csproj]
/tmp/optionsvalidator/Microsoft.Extensions.Options.SourceGeneration/Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator/Validators.g.cs(73): Trim analysis warning IL2026: __OptionValidationStaticInstances.<Validators_g>F1872690B80E3D4EBF0BB4EEFEFFBD255387F31D123553B47D3D3ED7D782424E7____Attributes..cctor(): Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved. [/tmp/optionsvalidator/optionsvalidator.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

  • Runtime: Microsoft.AspNetCore.App 8.0.0-rc.1.23421.29
  • SDK: 8.0.100-rc.1.23455.8
  • OS: Linux

Other information

No response

Author: rstropek
Assignees: tarekgh
Labels:

untriaged, area-Extensions-Options

Milestone: 8.0.0

@tarekgh
Copy link
Member

tarekgh commented Sep 20, 2023

Thanks @rstropek. I will look at that.

@tarekgh
Copy link
Member

tarekgh commented Sep 20, 2023

@rstropek I am trying the exact steps, but I am getting the warnings:

C:\Temp\OptionsTrimming\Program.cs(55,6): warning IL2026: Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses 
 reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved. [C:\Temp\OptionsTrimming\OptionsTrimming.csproj]
C:\Temp\OptionsTrimming\Program.cs(43,6): warning IL2026: Using member 'System.ComponentModel.DataAnnotations.MinLengthAttribute.MinLengthAttribute(Int32)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Uses 
 reflection to get the 'Count' property on types that don't implement ICollection. This 'Count' property may be trimmed. Ensure it is preserved. [C:\Temp\OptionsTrimming\OptionsTrimming.csproj]

these are expected and for this specific example which has the MinLengthAttribute on a string property, it is safe to disable this warning because no reflection will be executed at those cases.

Now, I am trying to get the same failures you are getting on the generated code Validators.g.cs. Somehow it is not happening on my repo. Is there anything else you missed in the repro? I'll keep trying.

@tarekgh
Copy link
Member

tarekgh commented Sep 20, 2023

@rstropek never mind, I can see the warnings now when publishing.

@rstropek
Copy link
Author

@tarekgh Oh, sorry. I mentioned build in the repro steps, but forgot that the warnings I included were from publish. Thank you for taking a look at this and confirming that I can ignore these warnings for now.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Oct 5, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Oct 8, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Nov 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 a pull request may close this issue.

2 participants