Description
Description
Enabling ASP.Net configuration binding source generator produces uncompilable source.
- OS: Windows 10 22H2
- Dotnet SDK version: 8.0.100-rc.1.23463.5
Compiler errors produced:
- Missing usings for
Microsoft.Extensions.Options
- Easily worked around by adding
Microsoft.Extensions.Options
to implicit global usings in the csproj. -
<ItemGroup> <Using Include="Microsoft.Extensions.Options" /> </ItemGroup>
- Easily worked around by adding
- Invalid source produced in
BindingExtensions.g.cs
, missing comma near the end of line.-
// Missing comma near the end of the line 👉 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------> 👇 return services.AddSingleton<Microsoft.Extensions.Options.IConfigureOptions<TOptions>>(new Microsoft.Extensions.Options.ConfigureNamedOptions<TOptions>(name, obj => BindCoreMain(configuration, obj, typeof(TOptions)configureOptions)));
-
Reproduction Steps
Simple webapi project repro repository at https://github.com/tonesandtones/ConfigBinderGeneratorRepro.
- Project created with
dotnet new web --name ConfigBinderGeneratorRepro --use-program-main
- Enable the source-generated config binder with
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
- Add a simple POCO to bind configuration to. For example, the repro uses
-
public class AppConfiguration { public string? ConfigKey1 { get; set; } public int? ConfigKey2 { get; set; } }
-
- Bind the POCO to a configuration section
-
var configurationSection = builder.Configuration.GetSection("AppConfiguration"); builder.Services.Configure<AppConfiguration>(configurationSection);
-
- Also included in the repro;
- Additional csproj declarations to persist generated source to disk.
- Commented-out
ItemGroup
to demonstrate the workaround that adds the missing using to the implicit global usings.
Expected behavior
Project should compile.
Actual behavior
Project does not compile. Produces the following compilation errors.
> dotnet build
MSBuild version 17.8.0-preview-23418-03+0125fc9fb for .NET
Determining projects to restore...
All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\8.0.100-rc.1.23463.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(311,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-suppor
t-policy [C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro.csproj]
C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(
57,227): error CS1003: Syntax error, ',' expected [C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro.csproj]
C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(
56,35): error CS0246: The type or namespace name 'IOptionsChangeTokenSource<>' could not be found (are you missing a using directive or an assembly reference?) [C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\ConfigBind
erGeneratorRepro.csproj]
C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(
57,227): error CS1003: Syntax error, ',' expected [C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro.csproj]
C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(
56,35): error CS0246: The type or namespace name 'IOptionsChangeTokenSource<>' could not be found (are you missing a using directive or an assembly reference?) [C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\ConfigBind
erGeneratorRepro.csproj]
C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(
56,76): error CS0246: The type or namespace name 'ConfigurationChangeTokenSource<>' could not be found (are you missing a using directive or an assembly reference?) [C:\pd\ConfigBinderGeneratorRepro\ConfigBinderGeneratorRepro\Confi
gBinderGeneratorRepro.csproj]
0 Warning(s)
3 Error(s)
Time Elapsed 00:00:01.21
Regression?
Unknown, I haven't tried this project with previous releases.
Known Workarounds
Partial workaround for missing usings is to add the missing using to global implicit usings.
<ItemGroup>
<Using Include="Microsoft.Extensions.Options" />
</ItemGroup>
It's also possible to generate the source once, persist to disk, manually modify the source to add the missing comma, and disable the generator. This is obviously a bad idea though as future changes will not regenerate the source which will break the source-generated interceptor as line numbers may not match any more.
Configuration
- OS: Windows 10 22H2 19045.3448
- Dotnet SDK version: 8.0.100-rc.1.23463.5
Other information
No response