-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
XmlSerializer.Serialize doesn't work when using TrimMode=link #41389
Comments
/cc @eerhardt @rolfbjarne |
In order to make XmlSerializer serialization work with the ILLinker, we will probably need to address dotnet/linker#1087. |
Here is another test case: linkertestcase-d179b60.zip $ ./test.sh
+ dotnet --version
5.0.100-rc.1.20426.3
+ dotnet publish -r osx-x64 --self-contained true -p:MonoAOT=true -p:VM=mono -p:PublishTrimmed=true Executable/Executable.csproj /bl:x.binlog
Microsoft (R) Build Engine version 16.8.0-preview-20425-03+384d02a5f for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
/usr/local/share/dotnet/sdk/5.0.100-rc.1.20426.3/MSBuild.dll -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,/usr/local/share/dotnet/sdk/5.0.100-rc.1.20426.3/dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,/usr/local/share/dotnet/sdk/5.0.100-rc.1.20426.3/dotnet.dll -maxcpucount -property:RuntimeIdentifier=osx-x64 -property:SelfContained=true -p:MonoAOT=true -p:VM=mono -p:PublishTrimmed=true -restore -target:Publish -verbosity:m /bl:x.binlog Executable/Executable.csproj
Determining projects to restore...
Restored /Users/rolf/test/linkertestcase/Executable/Executable.csproj (in 159 ms).
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
Executable -> /Users/rolf/test/linkertestcase/Executable/bin/Debug/net5.0/osx-x64/Executable.dll
Optimizing assemblies for size, which may change the behavior of the app. Be sure to test after publishing. See: https://aka.ms/dotnet-illink
Executable -> /Users/rolf/test/linkertestcase/Executable/bin/Debug/net5.0/osx-x64/publish/
+ ./Executable/bin/Debug/net5.0/osx-x64/publish/Executable
Unhandled exception. System.InvalidOperationException: There was an error reflecting type 'XmlResult`1[System.String]'.
---> System.InvalidOperationException: There was an error reflecting property 'Result'.
---> System.InvalidOperationException: You need to add XmlChoiceIdentifierAttribute to the 'Result' member.
at System.Xml.Serialization.XmlReflectionImporter.CheckAmbiguousChoice(XmlAttributes a, Type accessorType, String accessorName)
at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at XmlSerializationTest.GenericProperty_Bug5543() in /Users/rolf/test/linkertestcase/Executable/Program.cs:line 41
at C.Main() in /Users/rolf/test/linkertestcase/Executable/Program.cs:line 59
./test.sh: line 6: 97907 Abort trap: 6 ./Executable/bin/Debug/net5.0/osx-x64/publish/Executable |
…oesn't support Xml serialization yet. Ref: dotnet/runtime#41389 (comment) Fixes this test failure: LinkAll.Serialization.Xml.XmlSerializationTest [FAIL] GenericProperty_Bug5543 : System.InvalidOperationException : There was an error reflecting type 'LinkAll.Serialization.Xml.XmlResult`1[System.String]'. ----> System.InvalidOperationException : There was an error reflecting property 'Result'. ----> System.InvalidOperationException : You need to add XmlChoiceIdentifierAttribute to the 'Result' member. at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type) at LinkAll.Serialization.Xml.XmlSerializationTest.GenericProperty_Bug5543() in /Users/rolf/work/maccore/whatever/xamarin-macios/tests/linker/ios/link all/XmlSerializationTest.cs:line 52 at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) --InvalidOperationException at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) --InvalidOperationException at System.Xml.Serialization.XmlReflectionImporter.CheckAmbiguousChoice(XmlAttributes a, Type accessorType, String accessorName) at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
If this is for Xamarin only, then we should run the test in what will be a Xamarin-like environment. So most notably, Xamarin only trims framework assemblies (just like Blazor) and not the app. I'm guessing, but the types the tests are failing on are probably from "the app". Otherwise if the goal is to make XmlSerializer work for any trimmed app - or for Xamarin when trimming is enabled on the app itself, then we need either a source generator (or fix sgen and use that instead, which is basically a source generator as well) or new linker functionality. |
My current work here is to make XmlSerializer work by default when As for my previous comment and the |
I just tried your repro in #41389 (comment). That test fails even if the app isn't trimmed.
This same exception which you mention above occurs for a regular (untrimmed) net5.0 app. |
Make XmlSerializer work correctly with ILLinker trimming. 1. Use constant BindingFlags to work around dotnet/linker#1617 2. Annotate CodeGenerator.CreateTypeBuilder correctly to preserve base class members Fix dotnet#41389
Looks like I diagnosed that test case wrong, it seems to be different than the original issue reported here, so I'll have another look at it. |
* XmlSerializer.Serialize doesn't work when using TrimMode=link Make XmlSerializer work correctly with ILLinker trimming. 1. Use constant BindingFlags to work around dotnet/linker#1617 2. Annotate CodeGenerator.CreateTypeBuilder correctly to preserve base class members Fix #41389 * Update ILLinker suppressions file. * PR feedback. Fix ILLinker suppresions.
Repro: consoleapp-bfe9f43.zip
Run
make linked
to run withTrimMode=link
, andmake notlinked
to run the same command, except without settingTrimMode
.*** Moved from dotnet/linker#1454 ***
The text was updated successfully, but these errors were encountered: