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

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

Open
cfbao opened this issue Nov 22, 2024 · 0 comments
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@cfbao
Copy link

cfbao commented Nov 22, 2024

Describe the bug

In a native AOT project that uses configuration binding with <EnableConfigurationBindingGenerator/>, there should be no warnings on the use of configuration binding methods like

However, dotnet format continues to raise IL2026 and IL3050 on such projects.

To Reproduce

<!-- worker.csproj -->
<Project Sdk="Microsoft.NET.Sdk.Worker">
	<PropertyGroup>
		<TargetFramework>net9.0</TargetFramework>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
		<InvariantGlobalization>true</InvariantGlobalization>
		<PublishAot>true</PublishAot>
		<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
		<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.0" />
	</ItemGroup>
</Project>
// Program.cs
using Microsoft.Extensions.Options;

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

class Foo
{
	public Bar? Bar { get; init; }
}
class Bar
{
	public string? Baz { get; init; }
}
class Worker(ILogger<Worker> logger, IOptions<Foo> 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);
		}
	}
}
// 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<TOptions>(OptionsBuilder<TOptions>, String, Action<BinderOptions>)' 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<TOptions>(OptionsBuilder<TOptions>, String, Action<BinderOptions>)' 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

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

1 participant