-
Notifications
You must be signed in to change notification settings - Fork 475
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
Add trimming support for RuntimeSupport, SystemTextJson serializer and Lambda event packages #1596
Conversation
…d Lambda event packages
deba8a9
to
e088468
Compare
{ | ||
if (value is System.Enum) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code was not trimming compatible and it turn out our only use of the method was to the value
being a string. So I choose to just simplify this method. Along time again this class was generated from a swagger file which I assume the generator just differentiated for all possible use cases.
Label = method.Name; | ||
} | ||
else | ||
var method = stackFrame.GetMethod(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This GetMethod
call is marked as RequiresUnreferencedCode
. The code is required so I just made the code more defensive in case something gets trimmed. Since we only really need the that is unlikely to be metadata trimmed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And for the failed CI, can you add a .NET 8 block here?
aws-lambda-dotnet/.github/workflows/source-generator-ci.yml
Lines 17 to 24 in 68396f5
- name: Setup .NET 3.1 | |
uses: actions/setup-dotnet@v1 | |
with: | |
dotnet-version: 3.1.x | |
- name: Setup .NET 6.0 | |
uses: actions/setup-dotnet@v1 | |
with: | |
dotnet-version: 6.0.x |
Libraries/src/Amazon.Lambda.SNSEvents/Amazon.Lambda.SNSEvents.csproj
Outdated
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.ConnectEvents/Amazon.Lambda.ConnectEvents.csproj
Outdated
Show resolved
Hide resolved
Libraries/test/TestFunctionFSharp/TestFunctionCSharp/TestFunctionCSharp.csproj
Outdated
Show resolved
Hide resolved
@@ -47,8 +47,12 @@ public class DictionaryLongToStringJsonConverter : JsonConverter<Dictionary<stri | |||
|
|||
public override void Write(Utf8JsonWriter writer, Dictionary<string, string> value, JsonSerializerOptions options) | |||
{ | |||
#if NET8_0_OR_GREATER | |||
JsonSerializer.Serialize(writer, value, typeof(Dictionary<string, string>), new DictionaryStringStringJsonSerializerContext(options)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful to add a comment here explaining why we're doing it this way for .NET8 and above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@@ -47,8 +47,12 @@ public class DictionaryLongToStringJsonConverter : JsonConverter<Dictionary<stri | |||
|
|||
public override void Write(Utf8JsonWriter writer, Dictionary<string, string> value, JsonSerializerOptions options) | |||
{ | |||
#if NET8_0_OR_GREATER | |||
JsonSerializer.Serialize(writer, value, typeof(Dictionary<string, string>), new DictionaryStringStringJsonSerializerContext(options)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful to add a comment here explaining why we're doing it this way for .NET8 and above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'"> | ||
<WarningsAsErrors>IL2026,IL2067,IL2075</WarningsAsErrors> | ||
<IsTrimmable>true</IsTrimmable> | ||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does RuntimeSupport need EnableTrimAnalyzer
but the rest don't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added EnableTrimAnalyzer
to the other projects. I added this property so we can see future breaks to trimming at compile time for these libraries.
@@ -112,6 +112,11 @@ private LambdaBootstrap(HttpClient httpClient, LambdaBootstrapHandler handler, L | |||
/// </summary> | |||
/// <param name="cancellationToken"></param> | |||
/// <returns>A Task that represents the operation.</returns> | |||
#if NET8_0_OR_GREATER | |||
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", | |||
Justification = "Unreferenced code paths are excluding when RuntimeFeature.IsDynamicCodeSupported is false.")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"excluding" should be "excluded"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -41,10 +41,14 @@ public override bool CanConvert(Type typeToConvert) | |||
/// <param name="typeToConvert"></param> | |||
/// <param name="options"></param> | |||
/// <returns></returns> | |||
#if NET8_0_OR_GREATER | |||
[System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067", | |||
Justification = "Constant classes are only used in the DynamoDB event referencing the SDK. S3 originally referenced the SDK but has been rewritten to no longer reference the SDK or ConstantClass. Suppressing this trim warning because we have marked the DynamoDB event as ")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sentence incomplete? what did we mark the DynamoDB event as?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved, assuming you fix the failing PR checks
Fixed the CI build. Since .NET 8 isn't GA I had to set the workflow to the current RC. Will need to update that to 8.0.x once .NET 8 has been released. |
@@ -140,6 +140,9 @@ public static void LoadStringCultureInfo() | |||
} | |||
} | |||
|
|||
#if NET8_0_OR_GREATER | |||
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("PreJitAssembly is not used for Native AOT")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically someone could want to trim their assembly and not use native AOT, although obviously native AOT is the big use case. In general some of the explanations assume trimming = native AOT, and it might help to be a little more clear why that is going forward.
Description of changes:
Updated the Lambda RuntimeSupport, SystemTextJson serializer and Lambda events to support trimming.
NETCOREAPP_3_1
preprocessor references toNETCOREAPP3_1_OR_GREATER
net8.0
target framework to projects and mark the projects as trimmable.RequiresUnreferencedCode
. These code paths are not used when deployed as an executable which is the AOT use case.SourceGeneratorLambdaJsonSerializer
that takes in the JsonSerializerContext so the serializer doesn't have to use reflection to get an instance of the generic parameter.I have verified with these changes I can package a Lambda function using just these libraries and received no trim warnings.
Work not included in this PR
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.