Skip to content

Configuration Binder source generator can generate code that won't compile #111517

Open
@keegan-caruso

Description

@keegan-caruso

Description

The configuration binder source generator can generate code that won't compile. It can generate code of the form new (value1, value2)()

Which gives an error of: 'new' cannot be used with tuple type. Use a literal expression instead.

Reproduction Steps

Enable source generated configuration binder:

<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>

Try to bind a class that has a getter that includes a property of the class in a value tuple.

    public class ExampleOptions
    {
        public List<string> ExampleCollection { get; set; }

        internal List<(string, ICollection<string>?)> UsesCollection =>
                [
                    ("Label-1", ExampleCollection )
                ];
    }

    builder.Services.Configure<ExampleOptions>("Repro", builder.Configuration.GetSection("Repro"));

The generated code that fails to compile:

        public static void BindCore(IConfiguration configuration, ref global::System.Collections.Generic.List<(string, global::System.Collections.Generic.ICollection<string>)> instance, bool defaultValueIfNotFound, BinderOptions? binderOptions)
        {
            foreach (IConfigurationSection section in configuration.GetChildren())
            {
                if (!string.IsNullOrEmpty(section.Value) && !section.GetChildren().Any())
                {
                    continue;
                }
                var value = new (string, global::System.Collections.Generic.ICollection<string>)();
                instance.Add(value);
            }
        }

Additionally, is not clear to me why it is generating code to bind against a property that only has a getter.

Expected behavior

The generated code should compile.

Actual behavior

The generated code does not compile.

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
Version: 9.0.102
Commit: cb83cd4923
Workload version: 9.0.100-manifests.221265b2
MSBuild version: 17.12.18+ed8c6aec5

This reproduction was created off of the net9.0 ASP.NET Core Web API template.

The version of Microsoft.Extensions.Configuration.Binder.SourceGeneration used was 9.0.11.11010

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions