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

[Xamarin.Android.Build.Tasks] fix System.Text.Json with default trimmer settings #8266

Merged
merged 2 commits into from
Aug 14, 2023

Conversation

jonathanpeppers
Copy link
Member

Fixes: dotnet/maui#16038

Changes to trimmer defaults in .NET 8 require a new $(JsonSerializerIsReflectionEnabledByDefault) MSBuild property to be set by default for project types that use $(TrimMode)=partial like when running on mobile (iOS/Android).

I added a very basic System.Text.Json test, which fails with the exception:

System.InvalidOperationException : JsonSerializerIsReflectionDisabled
at System.Text.Json.JsonSerializerOptions.ConfigureForJsonSerializer()
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions , Type )
at System.Text.Json.JsonSerializer.Deserialize(String , Type , JsonSerializerOptions )
at System.Text.JsonTests.JsonSerializerTest.Deserialize()
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object , BindingFlags )

Simply running the test again with the MSBuild property set allows the test to pass. This will be a good smoke test for System.Text.Json going forward.

The iOS side of this is tracked here:

xamarin/xamarin-macios#18057

…er settings

Fixes: dotnet/maui#16038

Changes to trimmer defaults in .NET 8 require a new
`$(JsonSerializerIsReflectionEnabledByDefault)` MSBuild property to be
set by default for project types that use `$(TrimMode)=partial` like
when running on mobile (iOS/Android).

I added a very basic System.Text.Json test, which fails with the
exception:

    System.InvalidOperationException : JsonSerializerIsReflectionDisabled
    at System.Text.Json.JsonSerializerOptions.ConfigureForJsonSerializer()
    at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions , Type )
    at System.Text.Json.JsonSerializer.Deserialize(String , Type , JsonSerializerOptions )
    at System.Text.JsonTests.JsonSerializerTest.Deserialize()
    at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
    at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object , BindingFlags )

Simply running the test again with the MSBuild property set allows the
test to pass. This will be a good smoke test for System.Text.Json going
forward.

The iOS side of this is tracked here:

xamarin/xamarin-macios#18057
@@ -112,6 +112,7 @@
<_AggressiveAttributeTrimming Condition="'$(_AggressiveAttributeTrimming)' == ''">true</_AggressiveAttributeTrimming>
<NullabilityInfoContextSupport Condition="'$(NullabilityInfoContextSupport)' == ''">false</NullabilityInfoContextSupport>
<BuiltInComInteropSupport Condition="'$(BuiltInComInteropSupport)' == ''">false</BuiltInComInteropSupport>
<JsonSerializerIsReflectionEnabledByDefault Condition="'$(JsonSerializerIsReflectionEnabledByDefault)' == ''">true</JsonSerializerIsReflectionEnabledByDefault>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldthis not check '$(TrimMode)' as well? given the commit message this should be set only when '$(TrimMode)' is 'partial'.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think that makes sense. It looks like TrimMode is set earlier in this file, so this is the right place we can check it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense too. If someone sets TrimMode=full, JsonSerializer is basically guaranteed to break. So you might as well disable reflection and guide them to use the source generator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC we didn't add a similar condition in Blazor. Do we need to revisit there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blazor doesn't support TrimMode=full at all. So I don't think we need to worry about it for .NET 8.

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@jonathanpeppers
Copy link
Member Author

We can ignore the test failures, they look unrelated:

System.Net.Http.HttpRequestException : Response status code does not indicate success: 504 (Gateway Time-out).

Going to merge.

@jonathanpeppers jonathanpeppers merged commit 05f79c0 into dotnet:main Aug 14, 2023
44 of 47 checks passed
@jonathanpeppers jonathanpeppers deleted the System.Text.Json branch August 14, 2023 19:05
@github-actions github-actions bot locked and limited conversation to collaborators Jan 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MAUI sdk should default the JsonSerializerIsReflectionEnabledByDefault property to true
4 participants