aws / aws-dotnet-extensions-configuration

This repository hosts various libraries that help developers configure .NET applications using AWS services.
https://aws.amazon.com/developer/language/net/
Apache License 2.0
174 stars 56 forks source link

Mark project as not Native AoT ready #173

Open boarnoah opened 3 weeks ago

boarnoah commented 3 weeks ago

Describe the bug

With #168 there was bump for AWSSDK.Extensions.NETCore.Setup from 3.7.1 (2021) to 3.7.300 (2023).

In the interim AWSSDK.Extensions.NETCore.Setup has been marked not trimming safe for Native AoT (https://github.com/aws/aws-sdk-net/pull/3044) with .NET 8.

I think that means, Amazon.Extensions.Configuration.SystemsManager should also be marked not trimming friendly for Native AoT now? Since with .NET 7.0 on-wards IIUC libraries need to opt out of trimming support.

Since there are now trimming warnings when including this package in a NativeAoT application + runtime errors.

Expected Behavior

Amazon.Extensions.Configuration.SystemsManager should not be trimmed since it doesn't support trimming

Current Behavior

Build succeeds with trim warnings, With package 6.1.0+:

    <PackageReference Include="Amazon.Extensions.Configuration.SystemsManager" Version="6.2.0" />

we now get trim warnings on a successful build:

warning IL2104: Assembly 'Amazon.Extensions.Configuration.SystemsManager' produced trim warnings.

here is the same warning with <TrimmerSingleWarn>false</TrimmerSingleWarn>:

ILC : Trim analysis warning IL2026: Amazon.Extensions.Configuration.SystemsManager.Internal.AwsOptionsProvider.GetAwsOptions(IConfigurationBuilder): Using member 'Microsoft.Extensions.Configuration.ConfigurationExtensions.GetAWSOptions(IConfiguration)' which has 'RequiresUnrefer
encedCodeAttribute' can break functionality when trimming application code. The AWSSDK.Extensions.NETCore.Setup package has not been updated to support Native AOT compilations.

There is runtime errors in the application as a result ex:

System.NullReferenceException: Object reference not set to an instance of an object.\n at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateConfig(Type, AWSOptions) + 0x8a\n at Amazon.Extensions.NETCore.Setup.ClientFactory.CreateServiceClient(ILogger, Type, AWSOptions) + 0x8f\n at Amazon.Extensions.NETCore.Setup.AWSOptions.CreateServiceClient[T]() + 0x2c\n at Amazon.Extensions.Configuration.SystemsManager.Internal.SystemsManagerProcessor.<GetParametersByPathAsync>d__6.MoveNex

Reproduction Steps

Sorry, setting a Native AoT project is a little involved, I can cook up a PoC if its really needed.

  1. Include Amazon.Extensions.Configuration.SystemsManager > 6.1.0 in project
  2. Setup project for Native AoT build
  3. Notice null reference exceptions w/ the package

Possible Solution

I believe we can mark the project with:

<IsAotCompatible>false</IsAotCompatible>

https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/?tabs=net8plus#aot-compatibility-analyzers

Additional Information/Context

No response

AWS .NET SDK and/or Package version used

Amazon.Extensions.Configuration.SystemsManager 6.1.0+

Targeted .NET Platform

.NET 8

Operating System and version

AmazonLinux, scratch

ashishdhingra commented 3 weeks ago

Needs review with the team.

boarnoah commented 2 weeks ago

Did some more testing excluding various assemblies (that are transitive deps from this) from trimming,

Was able to eliminate runtime errors with:

  <ItemGroup>
    <TrimmerRootAssembly Include="AWSSDK.SimpleSystemsManagement" />
  </ItemGroup>

I believe that is the actual culprit here (at least with the runtime error I see).

There appears to already be an issue in the core SDK re: trimming problems that reports this: https://github.com/aws/aws-sdk-net/issues/3173

EDIT: Sorry my understanding of trimming was a bit wrong, it was issues with AWSSDK.Extensions.NETCore.Setup that lead to AWSSDK.SimpleSystemsManagement being trimmed out incorrectly, which is why hoisting it up as a root assembly resolves the issue.

normj commented 2 weeks ago

@boarnoah I just pushed out a PR AWSSDK.Extensions.NETCore.Setup to make it AOT compatible. That is part of in development V4 of the SDK. https://github.com/aws/aws-sdk-net/pull/3353