Skip to content

ConfigurationBinder source generator generates code with the wrong nullable annotations #94105

Open

Description

@eerhardt

Description

When attempting to use the ConfigurationBinder source generator in dotnet/extensions (see dotnet/extensions#4625), I'm getting a nullable warning that is causing the generated code to not compile (I would need to disable nullable validation for the whole project if I wanted to ignore the nullable warning, which isn't acceptable).

I'm not sure what the general pattern is here that causes this issue, so I logged the issue for the scenario specifically. I think it might need to include nested generics where one of the nested generic types is nullable.

Reproduction Steps

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
    <NoWarn>$(NoWarn);SYSLIB1100;SYSLIB1101</NoWarn>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rtm.23511.16" />
    <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0-rc.2.23510.2" />
  </ItemGroup>

</Project>
// needed to work around https://github.com/dotnet/runtime/issues/94065
global using Polly;
global using Polly.Hedging;
global using Polly.RateLimiting;
global using Polly.Timeout;

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Http.Resilience;

var c = new ConfigurationBuilder().Build();
c.Get<HttpStandardHedgingResilienceOptions>();

Expected behavior

Should be without warnings

Actual behavior

Get a nullable warning from generated code:

CS8619	Nullability of reference types in value of type 'Func<HedgingActionGeneratorArguments<HttpResponseMessage>, Func<ValueTask<Outcome<HttpResponseMessage>>>?>' doesn't match target type 'Func<HedgingActionGeneratorArguments<HttpResponseMessage>, Func<ValueTask<Outcome<HttpResponseMessage>>>>'.	ConsoleApp101	C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs	1418		

The generated code looks like:

            if (AsConfigWithChildren(configuration.GetSection("ActionGenerator")) is IConfigurationSection section256)
            {
                Func<HedgingActionGeneratorArguments<HttpResponseMessage>, Func<ValueTask<Outcome<HttpResponseMessage>>>>? temp258 = instance.ActionGenerator;
                if (temp258 is not null)
                {

That middle line is the issue, it is of type:

Func<HedgingActionGeneratorArguments<HttpResponseMessage>, Func<ValueTask<Outcome<HttpResponseMessage>>>>
but the type of instance.ActionGenerator is:
Func<HedgingActionGeneratorArguments<TResult>, Func<ValueTask<Outcome<TResult>>>?>

Note the ? inside the last > brace.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

cc @tarekgh @ericstj

Activity

  1. ghost added
    untriaged
    on Oct 27, 2023
  2. eiriktsarpalis commented on Oct 29, 2023

    @eiriktsarpalis
    Member

    That's surprising, doesn't the generated code suppress nullability warnings in source?

  3. eerhardt commented on Oct 30, 2023

    @eerhardt
    MemberAuthor

    That's surprising, doesn't the generated code suppress nullability warnings in source?

    No, it explicitly enables it:

    _writer.WriteLine("""
    // <auto-generated/>
    #nullable enable
    #pragma warning disable CS0612, CS0618 // Suppress warnings about [Obsolete] member usage in generated code.
    """);

  4. eiriktsarpalis commented on Oct 30, 2023

    @eiriktsarpalis
    Member

    That seems surprising given that this was picked up from the JSON generator:

    writer.WriteLine("""
    // <auto-generated/>
    #nullable enable annotations
    #nullable disable warnings
    // Suppress warnings about [Obsolete] member usage in generated code.
    #pragma warning disable CS0612, CS0618
    """);

    I think we should consistently disable nullability warnings in all generated code (except perhaps in debug builds of the generator?)

  5. added this to the 8.0.x milestone on Nov 1, 2023
  6. ghost removed
    untriaged
    on Nov 1, 2023
  7. added
    untriaged
    on Nov 1, 2023
  8. ghost removed
    untriaged
    on Nov 1, 2023
  9. eiriktsarpalis commented on Nov 1, 2023

    @eiriktsarpalis
    Member

    @eerhardt I'm preparing a PR that disables nullability warnings in generated code. I'm not super familiar with this library though, do you know if there's a minimal repro I can use that doesn't require the dependency on Polly?

  10. eerhardt commented on Nov 1, 2023

    @eerhardt
    MemberAuthor

    Unfortunately, no I don't. I haven't had time to pare it down.

    I would try making a type of Func<HedgingActionGeneratorArguments<TResult>, Func<ValueTask<Outcome<TResult>>>?>

    where HedgingActionGeneratorArguments and Outcome are generic types.

  11. eiriktsarpalis commented on Nov 1, 2023

    @eiriktsarpalis
    Member

    Is that warning a side-effect of the generator erroneously generating binding code for delegate types?

  12. eerhardt commented on Nov 1, 2023

    @eerhardt
    MemberAuthor

    Yes, but I believe the issue will also occur if you replace the Func with some other generic types.

  13. eiriktsarpalis commented on Nov 8, 2023

    @eiriktsarpalis
    Member

    This was addressed in .NET 8 via #94267 by disabling nullability warnings altogether. Moving to Future for a fix that addresses the specific annotation issue.

  14. modified the milestones: 8.0.x, Future on Nov 8, 2023
  15. modified the milestones: Future, 9.0.0 on Nov 20, 2023
  16. added
    source-generator
    on Jan 12, 2024
  17. modified the milestones: 9.0.0, 10.0.0 on Aug 7, 2024
  18. removed their assignment
    on Aug 7, 2024
  19. eiriktsarpalis commented on Aug 7, 2024

    @eiriktsarpalis
    Member

    Moving to 10.0.0

  20. modified the milestones: 10.0.0, Future on Jul 26, 2025
  21. rosebyte commented on Mar 5, 2026

    @rosebyte
    Member

    Triage: we will investigate the issue together with other fixes for the generator and potentially fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationsource-generator

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions