Skip to content

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

Closed
@rstropek

Description

@rstropek

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions