Skip to content

Configuration binder source generator allocates and binds a throwaway collection for getter-only properties whose getter returns null #130879

Closed

Description

@svick

Description

For a public getter-only collection property, the configuration binding source generator emits code that unconditionally allocates a new collection instance (??= new List<>()), populates it via BindCore, and then discards it — because there is no setter to write it back to. This is wasted allocation and binding work at runtime with no observable effect.

The runtime reflection binder does not do this: it explicitly short-circuits when a read-only binding point has a null value and allocates nothing.

This is a performance/parity gap, not a correctness bug — the binding result is identical (nothing is observably bound in either case).

Reproduction Steps

Enable the source generator and bind a type with a public getter-only collection property that returns null:

<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
public class ExampleOptions
{
    public List<string> UsesCollection { get; } // getter-only, backing value null
}

services.Configure<ExampleOptions>(config.GetSection("Repro"));

If the "Repro" section has children, the generated BindCore runs the allocate-populate-discard sequence below.

Generated code (simplified)

// in BindCore for ExampleOptions
List<string>? temp = instance.UsesCollection;   // null
temp ??= new List<string>();                     // allocates a list...
BindCore(section, ref temp, defaultValueIfNotFound: false, binderOptions); // ...and populates it
// temp is never written back (no setter) -> discarded

Expected behavior

When a property has a getter but no usable setter (canSet == false) and the getter returns null, the generator should not allocate or bind a throwaway instance — matching the reflection binder, which returns early:

// ConfigurationBinder.cs (reflection binder)
if (bindingPoint.Value is null)
{
    // if the binding point doesn't let us set a new instance, there's nothing more we can do
    if (bindingPoint.IsReadOnly)
        return;
    ...
}

Actual behavior

The generator allocates a new collection, fully binds it from configuration, and then throws it away.

Root cause

In CoreBindingHelpers.EmitBindingLogicForComplexMember, the initKind for a reference-type member is selected from member.CanGet alone, ignoring canSet:

else if (member.CanGet)
{
    targetObjAccessExpr = memberAccessExpr;
    initKind = InitializationKind.AssignmentWithNullCheck; // emits `temp ??= new ...()`
}

The write-back is suppressed separately (writeOnSuccess = !canSet ? null : ...), so for a getter-only property the allocation and BindCore still execute but the result is discarded. A guard that skips the ??=/BindCore when !canSet and the current value is null (e.g. wrapping in if (temp is not null)) would bring it in line with the reflection binder.

  • Source generator: src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Emitter/CoreBindingHelpers.cs (EmitBindingLogicForComplexMember, getter branch + writeOnSuccess)
  • Reflection binder short-circuit: src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs (BindInstance, if (bindingPoint.IsReadOnly) return;)

Related

Note

This issue was drafted with the assistance of GitHub Copilot.

Activity

  1. added
    source-generator
    on Jul 16, 2026
  2. added
    untriaged
    on Jul 16, 2026
  3. removed
    untriaged
    on Jul 16, 2026
  4. added this to the Future milestone on Jul 16, 2026
  5. dotnet-policy-service commented on Jul 16, 2026

    @dotnet-policy-service
    Contributor

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

  6. added
    help wanted
    on Jul 16, 2026
  7. Suriya987 commented on Jul 19, 2026

    @Suriya987

    I'd like to work on this — planning to add the canSet-aware guard in EmitBindingLogicForComplexMember as described. Will look for the existing source-gen test project to add a regression test

  8. Sahithbasani commented on Jul 19, 2026

    @Sahithbasani
    Contributor

    Hi @svick, I implemented the proposed fix in #131045.

    The source generator now checks the current value of a getter-only property and only emits the binding logic when that value is non-null. I also added a regression baseline covering both a null getter-only collection and an initialized getter-only collection.

    The targeted tests and the full SourceGenerationTests passed for both net11.0 and net472.

    Could you please review the approach when you have a chance? Thank you.

  9. svick commented on Jul 20, 2026

    @svick
    MemberAuthor

    @Suriya987 Sorry, it looks like @Sahithbasani was faster.

  10. Suriya987 commented on Jul 20, 2026

    @Suriya987

    @svick may be yah but , I will do for upcoming issues as fast as possible

  11. svick commented on Jul 21, 2026

    @svick
    MemberAuthor

    @Suriya987 There are plenty of existing open issues, those have a much smaller chance of a race. 😄

  12. added a commit that references this issue on Jul 30, 2026
    19ad191
  13. locked and limited conversation to collaborators on Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationhelp wantedsource-generator

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions