-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
ConfigurationBinder source generator generates code with the wrong nullable annotations #94105
Comments
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsDescriptionWhen 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 behaviorShould be without warnings Actual behaviorGet a nullable warning from generated code:
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:
Note the Regression?No response Known WorkaroundsNo response ConfigurationNo response Other information
|
That's surprising, doesn't the generated code suppress nullability warnings in source? |
No, it explicitly enables it: Lines 39 to 43 in 44a5abd
|
That seems surprising given that this was picked up from the JSON generator: runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs Lines 136 to 145 in c1f4341
I think we should consistently disable nullability warnings in all generated code (except perhaps in debug builds of the generator?) |
@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? |
Unfortunately, no I don't. I haven't had time to pare it down. I would try making a type of where HedgingActionGeneratorArguments and Outcome are generic types. |
Is that warning a side-effect of the generator erroneously generating binding code for delegate types? |
Yes, but I believe the issue will also occur if you replace the |
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. |
Moving to 10.0.0 |
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
Expected behavior
Should be without warnings
Actual behavior
Get a nullable warning from generated code:
The generated code looks like:
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
The text was updated successfully, but these errors were encountered: