Skip to content

Configuration binder source generator producing invalid code #92021

@jamiewinder

Description

@jamiewinder

Description

In the new NET 8 RC1, even the most basic configuration binder source generator scenarios seem to produce invalid code.

Reproduction Steps

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

var services = new ServiceCollection();

services.Configure<A>(configuration.GetSection("MyOptions"));

Console.WriteLine("Hello, World!");

public class MyOptions 
{
    public string Text { get; set; } = string.Empty;
}

With the following csproj:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <LangVersion>preview</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot>
    <InvariantGlobalization>true</InvariantGlobalization>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
    <Features>InterceptorsPreview</Features>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0-rc.1.23419.4" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0-rc.1.23419.4" />
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0-rc.1.23419.4" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0-rc.1.23419.4" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0-rc.1.23419.4" />
  </ItemGroup>

</Project>

Expected behavior

Should build.

Actual behavior

The source generator seems to generate invalid code, which produce the following errors:

  • The type or namespace name 'IOptionsChangeTokenSource<>' could not be found (are you missing a using directive or an assembly reference?) (line 55)
  • The type or namespace name 'ConfigurationChangeTokenSource<>' could not be found (are you missing a using directive or an assembly reference?) (line 55)
  • Syntax error, ',' expected (line 56)

Regression?

Worked in .NET 8 Preview 7

Known Workarounds

No response

Configuration

.NET 8.0.100-rc.1.23455.8

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions