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

ConfigurationBinder source generator build fails with CS9035 if option class have Property with requried #95006

Open
xmarshal opened this issue Nov 20, 2023 · 5 comments
Labels
area-Extensions-Configuration source-generator Indicates an issue with a source generator feature
Milestone

Comments

@xmarshal
Copy link

xmarshal commented Nov 20, 2023

Description

ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property

Reproduction Steps

build this code

namespace BindGeneratorRepro;

using System.Reflection;

internal class Program
{
    private static void Main(string[] args)
    {
        var builder = WebApplication.CreateSlimBuilder(args);

        builder.Services.AddOptions();

        builder.Services.AddOptions<MyOptionA>()
            .BindConfiguration("myPath", options => options.BindNonPublicProperties = true);

        var app = builder.Build();

        app.MapGet("/version", () =>
        {
            var fileVersionAttribute = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>();

            return Results.Text(fileVersionAttribute!.Version);
        });

        app.Run();
    }
}

internal sealed record MyOptionA
{
    public required string MyParamA { get; init; }
    public required MyOptionB MyParamAB { get; init; }
}

internal sealed record MyOptionB
{
    public required string MyParamB { get; init; }
}

Expected behavior

Code should compiles

Actual behavior

build files with error CS9035
BinGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(106,31,106,40): error CS9035: Required member 'MyOptionB.MyParamB' must be set in the object initializer or attribute constructor.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
    <EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
    <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  </PropertyGroup>

</Project>
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 20, 2023
@ghost
Copy link

ghost commented Nov 20, 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

ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property

Reproduction Steps

build this code
`namespace BindGeneratorRepro;

using System.Reflection;

internal class Program
{
private static void Main(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);

    builder.Services.AddOptions();

    builder.Services.AddOptions<MyOptionA>()
        .BindConfiguration("myPath", options => options.BindNonPublicProperties = true);

    var app = builder.Build();

    app.MapGet("/version", () =>
    {
        var fileVersionAttribute = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>();

        return Results.Text(fileVersionAttribute!.Version);
    });

    app.Run();
}

}

internal sealed record MyOptionA
{
public required string MyParamA { get; init; }
public required MyOptionB MyParamAB { get; init; }
}

internal sealed record MyOptionB
{
public required string MyParamB { get; init; }
}`

Expected behavior

Code should compiles

Actual behavior

build files with error CS9035
BinGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(106,31,106,40): error CS9035: Required member 'MyOptionB.MyParamB' must be set in the object initializer or attribute constructor.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: xmarshal
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@xmarshal
Copy link
Author

image

@xmarshal xmarshal changed the title ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property ConfigurationBinder source generator generates code that doesn't compile when using recodrs or classes with requried properties as option Property Nov 20, 2023
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Nov 20, 2023
@tarekgh tarekgh added this to the 9.0.0 milestone Nov 20, 2023
@xmarshal xmarshal changed the title ConfigurationBinder source generator generates code that doesn't compile when using recodrs or classes with requried properties as option Property ConfigurationBinder source generator build fails with CS9035 if option class have Property with requried Nov 21, 2023
@ThaDaVos
Copy link

Any update on this?

@tarekgh tarekgh added the source-generator Indicates an issue with a source generator feature label May 24, 2024
@ericstj ericstj modified the milestones: 9.0.0, 10.0.0 Aug 8, 2024
@ericstj
Copy link
Member

ericstj commented Aug 8, 2024

Right now the source generator doesn't use object initializers at all, nor will it bind to init properties. This would require some significant refactoring of the generated code to support this scenario. We can consider this in a future release.

@KnapSac
Copy link

KnapSac commented Dec 23, 2024

Are there plans to do some work in this area for .NET 10? Anything I can do to help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-Configuration source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

5 participants