dotnet / dotnet/sdk

`dotnet format` raises native AOT warnings regardless of `<EnableConfigurationBindingGenerator/>`

Open
#45,054 3 comments 9 reactions 0 assignees View on GitHub
Area-Format untriaged
Dominant language
C#
Stars
3.2k
Forks
1.3k
PR merge metrics
PR metrics pending

Description

### Describe the bug

In a native AOT project that uses configuration binding with ``, there should be no warnings on the use of configuration binding methods like
- [`OptionsBuilderConfigurationExtensions.BindConfiguration`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.optionsbuilderconfigurationextensions.bindconfiguration?view=net-9.0-pp)
- [`ConfigurationBinder.GetValue`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationbinder.getvalue?view=net-9.0-pp).

However, `dotnet format` continues to raise [IL2026](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-warnings/il2026) and [IL3050](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/warnings/il3050) on such projects.

### To Reproduce

```xml


net9.0
enable
enable
true
true
true




```

```csharp
// Program.cs
using Microsoft.Extensions.Options;

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddOptions().BindConfiguration("Foo");
builder.Services.AddHostedService();
builder.Build().Run();

class Foo
{
public Bar? Bar { get; init; }
}
class Bar
{
public string? Baz { get; init; }
}
class Worker(ILogger logger, IOptions options) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
logger.LogInformation("Baz: {baz}", options.Value.Bar?.Baz);
await Task.Delay(1000, stoppingToken);
}
}
}
```

```jsonc
// appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Foo": {
"Bar": {
"Baz": "Hello, World!"
}
}
}
```

`dotnet build` & `dotnet publish` & IDEs don't raise any warnings as expected.

However, `dotnet format --verify-no-changes` returns with exit code 2 and the following output:
```
C:\worker\Program.cs(4,1): warning IL2026: Using member 'Microsoft.Extensions.DependencyInjection.OptionsBuilderConfigurationExtensions.BindConfiguration(OptionsBuilder, String, Action)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. TOptions's dependent types may have their members trimmed. Ensure all required members are preserved. [C:\worker\worker.csproj]
C:\worker\Program.cs(4,1): warning IL3050: Using member 'Microsoft.Extensions.DependencyInjection.OptionsBuilderConfigurationExtensions.BindConfiguration(OptionsBuilder, String, Action)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Binding strongly typed objects to configuration values may require generating dynamic code at runtime. [C:\worker\worker.csproj]
```

### Further technical details

`dotnet --info` output:
```
.NET SDK:
Version: 9.0.100
Commit: 59db016f11
Workload version: 9.0.100-manifests.3068a692
MSBuild version: 17.12.7+5b8665660

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.100\

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.0
Architecture: x64
Commit: 9d5a6a9aa4

.NET SDKs installed:
8.0.404 [C:\Program Files\dotnet\sdk]
9.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download
```

IDE: VS Code 1.95.3 + C# extension v2.57.28

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.