xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.48k stars 514 forks source link

Missing Method exception on iOS #17584

Open jasells opened 1 year ago

jasells commented 1 year ago

a variant of the issue was already logged in the .net runtime repo: (#61737),

Steps to Reproduce

  1. Build a Xamarin.Forms v 5 application that uses System.Text.Json and build the iOS application package on Windows 10 (using a Mac as server for iOS simulator, also manifests on physical iOS device). I discovered the issue trying to use LaunchDarkly client SDK.
  2. Debug/release builds make no difference.
  3. LaunchDarkly SDK works in Xamarin for UWP/Android as expected.

Expected Behavior

Json text parsing should work without exceptions.

Actual Behavior

I am seeing the following at runtime, on iOS only. Windows .Net 6 cli and Android applications work without issue:

 System.MissingMethodException: Method not found: void System.Text.Json.Utf8JsonReader..ctor(System.ReadOnlySpan `1<byte>,System.Text.Json.JsonReaderOptions)

Environment

Version information Windows 10 Installed SDK/toolset from VS 2022->about: ``` Microsoft Visual Studio Professional 2022 Version 17.4.5 VisualStudio.17.Release/17.4.5+33403.182 Microsoft .NET Framework Version 4.8.04084 Installed Version: Professional Visual C++ 2022 00476-80000-00000-AA175 Microsoft Visual C++ 2022 ASP.NET and Web Tools 17.4.337.25083 ASP.NET and Web Tools Azure App Service Tools v3.0.0 17.4.337.25083 Azure App Service Tools v3.0.0 C# Tools 4.4.0-6.23101.15+72dbad1d47f6065730a65c97621a4e93dc3efdf2 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Extensibility Message Bus 1.4.1 (main@2ee106a) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.4.19 (8c0a575) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 6.4.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Razor (ASP.NET Core) 17.4.5.2306901+ce80d539513291a2065f47028bef14b497dc2443 Provides languages services for ASP.NET Core Razor. TypeScript Tools 17.0.10921.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.4.0-6.23101.15+72dbad1d47f6065730a65c97621a4e93dc3efdf2 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info Xamarin 17.4.0.315 (d17-4@beed03a) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.4.0.138 (remotes/origin/d17-4@d36bba3cc9) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.4.2 (c457c97) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 13.1.0.1 (d17-4/13ba222) Xamarin.Android Reference Assemblies and MSBuild support. Mono: a96bde9 Java.Interop: xamarin/java.interop/d17-4@fcc33ce2 SQLite: xamarin/sqlite/3.39.3@23e1ae7 Xamarin.Android Tools: xamarin/xamarin-android-tools/main@0be567a Xamarin.iOS and Xamarin.Mac SDK 16.2.0.2 (956a059ba) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ``` The same behavior is seen when built on Mac OS 11 (azure hosted agent) using : ``` Xamarin SDK version: 6_12_16 Xamarin.Mono: 6.12 Xamarin.iOS: 15.8 ``` Workaround: Building iOS application package on Mac OS 12 or newer with ``` Xamarin SDK version: 6_12_21 Xamarin.Mono: 6.12 Xamarin.iOS: 16.0 ``` Prevents the issue from showing. I assume it is most directly linked to Xamarin.iOS SDK? I haven't tried `6.12.16 < Xamarin SDK < 6.12.21`

Build Logs

Example Project (If Possible)

https://github.com/jasells/Json-MissingMethodException/tree/master

rolfbjarne commented 1 year ago

I tried this using VSMac, and it worked fine. Could you try directly from a Mac as well, and see if that works for you?

ghost commented 1 year ago

Hi @jasells. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jasells commented 1 year ago

I tried this using VSMac, and it worked fine. Could you try directly from a Mac as well, and see if that works for you?

Yes, it does work on Mac 12 or newer, see Version information above (both Rider and VS Mac 2019/2022 Preview).

Workaround: Building iOS application package on Mac OS 12 or newer with

Xamarin SDK version: 6_12_21 Xamarin.Mono: 6.12 Xamarin.iOS: 16.0

jasells commented 1 year ago

Not sure if it is really Xamarin.iOS, or compiler , or System.Text.Json issue?

rolfbjarne commented 1 year ago

My guess is that the build finds a different/wrong version of the System.Text.Json assembly when building on Windows.

Would you be able to get a binary build log from your (failing) Windows build: https://github.com/xamarin/xamarin-macios/wiki/Diagnosis#binary-build-logs?

ghost commented 1 year ago

Hi @jasells. We have added the "need-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

jasells commented 1 year ago

Here is the bin log file (zipped) Json.Error.Demo.iOS_Debug_iPhoneSimulator_Build_2023-03-02T10_46_53.7898769-06_00.zip

rolfbjarne commented 1 year ago

This is a rather wild guess, but does adding this to your csproj make it work?

  <ItemGroup>
    <FrameworkReference Include="NETStandard.Library" IsImplicitlyDefined="true" Pack="false" PrivateAssets="All"/>
  </ItemGroup>
jasells commented 1 year ago

I does not seem to make a difference. Here is the build log with above added to the iOS project. Json.Error.Demo.iOS_Debug_iPhoneSimulator_Build_2023-03-03T10_18_02.0669479-06_00.zip

rolfbjarne commented 1 year ago

I think I found a workaround/fix:

Replace the contents of this file on Windows:

C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\Xamarin.iOS\v1.0\RedistList\FrameworkList.xml

with the contents of: https://gist.github.com/rolfbjarne/bbc494cda71ce64e6d729cd4a8f82296

The cause of the problem seems to be that the build chooses the System.Memory.dll assembly from the System.Memory NuGet, instead of the override assembly we provide (and then things break because the assembly from the NuGet references things in other assemblies that we don't have).

@emaf do you remember/know why the contents of the FrameworksList.xml file is different between Mac and Windows (or where the Windows version of FrameworkList.xml comes from, I couldn't find i)?

Windows:

<FileList Redist="Xamarin.iOS Base Class Libraries" Name="Xamarin.iOS" />

Mac: https://gist.github.com/rolfbjarne/bbc494cda71ce64e6d729cd4a8f82296

jasells commented 1 year ago

Kool, I'll give this workaround a try!

jasells commented 1 year ago

Woot! Woot!

I made the change suggested, and now the repro code works, as well as my original LaunchDarkly client test app. :D

Let me know if you want another build log.

jasells commented 1 year ago

So, here's another similar issue I found in some unit tests when I run on my windows box:

When built on MacOS the following snippet

 var money = -528.49m;
 Debug.WriteLine($"{money:C2}");

Produces the output "-$528.49"

When built on Windows, the output is "($528.49)".

In both cases the Culture is en-US.

rolfbjarne commented 1 year ago

So, here's another similar issue I found in some unit tests when I run on my windows box:

When built on MacOS the following snippet

 var money = -528.49m;
 Debug.WriteLine($"{money:C2}");

Produces the output "-$528.49"

When built on Windows, the output is "($528.49)".

In both cases the Culture is en-US.

Are you saying that the same iOS app behaves differently depending on whether it was built on Windows or on Mac?

jasells commented 1 year ago

Yes, at least in Nunit tests built against .Net Core 3.1 on windows vs Mac. I have delayed responding because my MacBook has been a little troublesome this week, and I can no longer deploy from windows onto my iOS sims on my MacBook to get a log or verify it does the same on iOS/mono runtime....