dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.58k stars 4.55k forks source link

System.Runtime.Serialization.Formatters throws TypeInitializationException for WASM/Mobile #104142

Open adamsitnik opened 1 week ago

adamsitnik commented 1 week ago

In #104104 I've tried to re-enable all BF tests and got following error for WASM/Mobile builds:

[00:49:29] info: [FAIL] System.Formats.Nrbf.Tests.ArraySinglePrimitiveRecordTests.CanReadArrayOfAnySize_Bool(size: 127, canSeek: False)
[00:49:29] info: System.TypeInitializationException : The type initializer for 'System.Runtime.Serialization.Formatters.Binary.Converter' threw an exception.
[00:49:29] info: ---- System.IO.FileNotFoundException :
[00:49:29] info:    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetAssemblyId(WriteObjectInfo )
[00:49:29] info:    at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object , BinaryFormatterWriter )
[00:49:29] info:    at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream , Object )
[00:49:29] info:    at System.Formats.Nrbf.Tests.ReadTests.Serialize[Boolean[]](Boolean[] instance)
[00:49:29] info:    at System.Formats.Nrbf.Tests.ArraySinglePrimitiveRecordTests.Test[Boolean](Int32 size, Boolean canSeek)
[00:49:29] info:    at System.Object.InvokeStub_ArraySinglePrimitiveRecordTests.CanReadArrayOfAnySize_Bool(Object , Span`1 )

Most likely it's trying to load "mscorlib" which is not available for these platforms:

https://github.com/dotnet/runtime/blob/d6f4beeb26812b82bea4f6300471998a6b92718b/src/libraries/System.Runtime.Serialization.Formatters/src/System/Runtime/Serialization/Formatters/Binary/Converter.cs#L38

Prior to #103255 BF would simply throw PNSE for them:

https://github.com/dotnet/runtime/blob/a82225ee6e7c2f1cc8e064b6320756b547faa358/src/libraries/System.Runtime.Serialization.Formatters/src/System.Runtime.Serialization.Formatters.csproj#L3

https://github.com/dotnet/runtime/blob/a82225ee6e7c2f1cc8e064b6320756b547faa358/src/libraries/System.Runtime.Serialization.Formatters/src/System.Runtime.Serialization.Formatters.csproj#L77-L79

@bartonjs should we change the OOB package to keep throwing PNSE for WASM/Mobile?

dotnet-policy-service[bot] commented 1 week ago

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

jkotas commented 1 week ago

Most likely it's trying to load "mscorlib" which is not available for these platforms

mscorlib is available for these platforms.

I think that the problem that you are seeing is that these tests have trimming enabled when running on mobile/WASM, but binary serialization is incompatible with trimming. The trimming strips mscorlib since it does not see it used anywhere. I expect that you would see similar problems if you enabled these tests e.g. on native AOT on any platforms.