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

Configuration binder generator generates source with compilation errors #92453

Closed
tonesandtones opened this issue Sep 22, 2023 · 4 comments
Closed

Comments

@tonesandtones
Copy link

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>
      
  • 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

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

ghost commented Sep 22, 2023

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

Issue Details

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>
      
  • 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

Author: tonesandtones
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@vcsjones
Copy link
Member

This looks like a duplicate of #92273.

Can you try the RC2 package as described in the linked issue to see if that resolves it for you?

@tonesandtones
Copy link
Author

tonesandtones commented Sep 22, 2023

This looks like a duplicate of #92273.

Can you try the RC2 package as described in the linked issue to see if that resolves it for you?

Fantastic, yes it's fixed in RC2 package ref 8.0.0-rc.2.23466.4 👍

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 22, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants