Skip to content

ConfigurationBinder Source Generator is suppressing IL2026 and IL3050 diagnostics even when it doesn't actually intercept the call #96643

@eerhardt

Description

@eerhardt

There are certain cases where the ConfigurationBinder Source Generator won't intercept a call to Bind, for example if the Type is an unsupported type. When this is the case, the Source Generator is still suppressing the IL2026 and IL3050 diagnostics even though it didn't intercept the call. This can lead to the developer not getting notified that their code isn't trim/AOT compatible.

Repro steps

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
  </ItemGroup>
  
</Project>
using Microsoft.Extensions.Configuration;
using System.Collections;

namespace ClassLibrary7;

public class Class1
{
    public static void Bind(IConfiguration config)
    {
        var supported = new SupportedType();
        config.Bind(supported);

        var unsupported = new UnsupportedType();
        config.Bind(unsupported);
    }
}

public class SupportedType
{
    public string? Name { get; set; }
}

public class UnsupportedType : IEnumerable<KeyValuePair<string, string>>
{
    public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
    {
        yield break;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

Expected results

Should still get AOT warnings for the call to Bind(unsupported), since this call wasn't intercepted by the Source Generator. At runtime it will follow the reflection based implementation and not use a source generated version.

Actual results

No warnings are emitted for IL2026 and IL3050.

You do get SYSLIB1100 diagnostic to tell you that UnsupportedType is unsupported. But this warning is often disabled/NoWarn'd because a lot of times there is just a single property not supported on a Type, and you just want the property to be ignored. Getting this diagnostic isn't enough to turn off the AOT compatibility warning that should be emitted here.

Metadata

Metadata

Assignees

Labels

area-Extensions-Configurationbugin-prThere is an active PR which will close this issue when it is mergedsource-generatorIndicates an issue with a source generator feature

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions