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

[net9.0-preview3]: NativeAOT System.Text.Json throws "System.NotSupportedException: 'System.Text.Json.Serialization.JsonDerivedTypeAttribute[]' is missing native code or metadata." #101205

Closed
lambdageek opened this issue Apr 17, 2024 · 4 comments

Comments

@lambdageek
Copy link
Member

When compiling an app that uses System.Text.Json using NativeAOT, using .NET 9 Preview 3, the app throws an exception:

Unhandled exception. System.NotSupportedException: 'System.Text.Json.Serialization.JsonDerivedTypeAttribute[]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.General.TypeUnifier.WithVerifiedTypeHandle(RuntimeArrayTypeInfo, RuntimeTypeInfo) + 0x78
   at System.Array.InternalCreate(RuntimeType, Int32, Int32*, Int32*) + 0xdc
   at System.Array.CreateInstance(Type, Int32) + 0x4c
   at System.Attribute.Instantiate(IEnumerable`1, Type) + 0xd8
   at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo, Boolean) + 0x48
   at System.Text.Json.Serialization.Metadata.JsonPolymorphismOptions.CreateFromAttributeDeclarations(Type) + 0x9c
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.PopulatePolymorphismMetadata() + 0x18
   at System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateCore[T](JsonSerializerOptions, JsonObjectInfoValues`1) + 0xdc
   at ContractDescriptorParser.ContractDescriptorContext.Create_ContractDescriptor(JsonSerializerOptions) + 0xec
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type) + 0x4c
   at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type, JsonSerializerOptions.CachingContext) + 0x20
--- End of stack trace from previous location ---
   at System.Text.Json.JsonSerializerOptions.CachingContext.CacheEntry.GetResult() + 0x24
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Nullable`1, Boolean, Boolean) + 0x54
   at System.Text.Json.JsonSerializerOptions.GetTypeInfo(Type) + 0x4c
   at ContractDescriptorParser.ContractDescriptorContext.get_ContractDescriptor() + 0x30
   at ContractDescriptorParser.ParseCompact(ReadOnlySpan`1) + 0x3c
   at Program.<Main>$(String[] args) + 0x104
zsh: abort      ./bin/Release/net9.0/osx-arm64/native/roundtrip-json ./foo.jsonc

This is a regression from .NET 8.

Repro: https://github.com/lambdageek/roundtrip-json

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 17, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

lambdageek added a commit to lambdageek/runtime that referenced this issue Apr 17, 2024
@lambdageek
Copy link
Member Author

As a workaround I can add:

public class Root
{
        public static System.Text.Json.Serialization.JsonDerivedTypeAttribute[] r1 = new System.Text.Json.Serialization.JsonDerivedTypeAttribute[] {null};
}

roots.xml:

<linker>
  <assembly fullname="roundtrip-json">
    <type fullname="Root" preserve="all" />
  </assembly>
</linker>

and then add that as a root descriptor in my .csproj:

  <ItemGroup>
    <TrimmerRootDescriptor Include="roots.xml" />
  </ItemGroup>

@jkotas
Copy link
Member

jkotas commented Apr 17, 2024

cc @MichalStrehovsky

@MichalStrehovsky
Copy link
Member

This is what I fixed in #100626 (and #101048). Custom attribute activation has a suppression that assumes we can always create arrays of reference types but this is only possible if there's a __Canon[] template.

We have not been running a ton of tests since the test merging happened. This would have been caught there. I'm in the process of fixing the regressions in #100331.

@MichalStrehovsky MichalStrehovsky closed this as not planned Won't fix, can't repro, duplicate, stale Apr 17, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Apr 17, 2024
lambdageek added a commit to lambdageek/runtime that referenced this issue Apr 19, 2024
Until we get dotnet#101048 from
codeflow, work around dotnet#101205 by adding
a trimmer root for JsonDerivedTypeAttribute[].
elinor-fung pushed a commit to elinor-fung/runtime that referenced this issue Apr 19, 2024
Until we get dotnet#101048 from
codeflow, work around dotnet#101205 by adding
a trimmer root for JsonDerivedTypeAttribute[].
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants