Xamarin-macios: System.IO.FileNotFoundException: Could not load the file 'System.Runtime.CompilerServices.Unsafe'

Created on 3 Aug 2020  路  6Comments  路  Source: xamarin/xamarin-macios

Steps to Reproduce

TODO: I will fill this out in more detail.

In my case I'm converting a Xamarin.Mac project to the sdk-style project format. So, after I've uploaded a sample project, the repro step will be to build and run it.

Expected Behavior

Project runs and shows the window.

Actual Behavior

Xamarin.Mac project crashes with the following exception:

Could not find `System.Runtime.CompilerServices.Unsafe` referenced by assembly `System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51`.
Could not find `System.Runtime.CompilerServices.Unsafe` referenced by assembly `CSharpMath, Version=0.4.2.0, Culture=neutral, PublicKeyToken=null`.
Could not find `System.Runtime.CompilerServices.Unsafe` referenced by assembly `CSharpMath.SkiaSharp, Version=0.4.2.0, Culture=neutral, PublicKeyToken=null`.
Could not find `System.Runtime.CompilerServices.Unsafe` referenced by assembly `41ViewModels, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null`.
Could not find `System.Runtime.CompilerServices.Unsafe` referenced by assembly `42XFViews, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null`.
Could not find `System.Runtime.CompilerServices.Unsafe` referenced by assembly `MathSpire, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null`.
2020-08-03 13:31:40.359 MathSpire[74364:1307532]
Unhandled Exception:
System.IO.FileNotFoundException: Could not load the file 'System.Runtime.CompilerServices.Unsafe'.
File name: 'System.Runtime.CompilerServices.Unsafe'
  at System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity) [0x00073] in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/mcs/class/corlib/System/AppDomain.cs:758
  at System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/mcs/class/corlib/System/AppDomain.cs:724
  at (wrapper remoting-invoke-with-check) System.AppDomain.Load(System.Reflection.AssemblyName)
  at System.Reflection.Assembly.Load (System.Reflection.AssemblyName assemblyRef) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/mcs/class/corlib/System.Reflection/Assembly.cs:440
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.RegisterEntryAssembly (System.Reflection.Assembly entry_assembly) [0x00032] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:505
  at ObjCRuntime.Runtime.RegisterAssemblies () [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:481
  at AppKit.NSApplication.Init () [0x00022] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/AppKit/NSApplication.cs:56
  at Mac.MainClass.Main (System.String[] args) [0x00001] in /Users/jwostenberg/Code/MathSpireApp/Platforms/50Mac/Main.cs:22
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load the file 'System.Runtime.CompilerServices.Unsafe'.
File name: 'System.Runtime.CompilerServices.Unsafe'
  at System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity) [0x00073] in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/mcs/class/corlib/System/AppDomain.cs:758
  at System.AppDomain.Load (System.Reflection.AssemblyName assemblyRef) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/mcs/class/corlib/System/AppDomain.cs:724
  at (wrapper remoting-invoke-with-check) System.AppDomain.Load(System.Reflection.AssemblyName)
  at System.Reflection.Assembly.Load (System.Reflection.AssemblyName assemblyRef) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/src/Xamarin.Mac/mcs/class/corlib/System.Reflection/Assembly.cs:440
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.CollectReferencedAssemblies (System.Collections.Generic.List`1[T] assemblies, System.Reflection.Assembly assembly) [0x00085] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:544
  at ObjCRuntime.Runtime.RegisterEntryAssembly (System.Reflection.Assembly entry_assembly) [0x00032] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:505
  at ObjCRuntime.Runtime.RegisterAssemblies () [0x00001] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/ObjCRuntime/Runtime.cs:481
  at AppKit.NSApplication.Init () [0x00022] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.18.3.2/src/Xamarin.Mac/AppKit/NSApplication.cs:56
  at Mac.MainClass.Main (System.String[] args) [0x00001] in /Users/jwostenberg/Code/MathSpireApp/Platforms/50Mac/Main.cs:22

The mac host app references a shared .net standard 2.0 library, which references System.Memory, which in turn references System.Runtime.CompilerServices.Unsafe. I have verified that System.Runtime.CompilerServices.Unsafe.dll is present in the output directory (and the application bundle), but that it's actually the reference assembly and not the implementation assembly (i.e. the one found in the ref/ folder of the package). I checked this by comparing the assemblies' hashes. If I manually replace the dll with the correct one from the lib/ folder, and the exception went away (it was replaced by a similar issue with a different assembly, but that's a separate problem).

Environment

=== Visual Studio Community 2019 for Mac ===

Version 8.6.8 (build 2)
Installation UUID: 9eab047c-fd3e-410a-9089-5effacc474d4
GTK+ 2.24.23 (Raleigh theme)
Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

Package version: 610000106

=== Mono Framework MDK ===

Runtime:
Mono 6.10.0.106 (2019-12/77769615db1) (64-bit)
Package version: 610000106

=== Roslyn (Language Service) ===

3.6.0-3.20210.9+4eafdcb1bcbd8d3573f2ba6065e56d9b9ce4f8a3

=== NuGet ===

Version: 5.6.0.6591

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/3.1.302/Sdks
SDK Versions:
3.1.302
3.1.300
2.2.300
2.1.505
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.10.0/lib/mono/msbuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
3.1.6
3.1.4
2.2.5
2.1.20
2.1.18
2.1.9

=== Xamarin.Profiler ===

Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 10.3.1.4 (Visual Studio Community)
Commit: xamarin-android/d16-6/3a10de9
Android SDK: /Users/jwostenberg/Library/Developer/Xamarin/android-sdk-macosx
Supported Android versions:
None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 29.0.5
SDK Build Tools Version: 29.0.2

Build Information:
Mono: 165f4b0
Java.Interop: xamarin/java.interop/d16-6@2cab35c
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.31.1@49232bc
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-6@bfb66f3

=== Microsoft OpenJDK for Mobile ===

Java SDK: /Users/jwostenberg/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.6.0.50
Hash: 5901879
Branch: remotes/origin/d16-6
Build date: 2020-06-10 22:42:50 UTC

=== Android Device Manager ===

Version: 16.6.0.96
Hash: 6e8b80b
Branch: remotes/origin/d16-6
Build date: 2020-06-10 22:43:28 UTC

=== Xamarin Designer ===

Version: 16.6.0.329
Hash: d4f8bcd13
Branch: remotes/origin/d16-6
Build date: 2020-04-24 02:16:02 UTC

=== Apple Developer Tools ===

Xcode 11.6 (16141)
Build 11E708

=== Xamarin.Mac ===

Version: 6.18.3.2 (Visual Studio Community)
Hash: ce0cc74a3
Branch: d16-6-xcode11.6
Build date: 2020-07-16 18:15:27-0400

=== Xamarin.iOS ===

Version: 13.18.3.2 (Visual Studio Community)
Hash: ce0cc74a3
Branch: d16-6-xcode11.6
Build date: 2020-07-16 18:15:27-0400

=== Build Information ===

Release ID: 806080002
Git revision: e999e2934e8c771fdf6494b80361dacef9427565
Build date: 2020-07-24 15:17:52-04
Build branch: release-8.6
Xamarin extensions: e999e2934e8c771fdf6494b80361dacef9427565

=== Operating System ===

Mac OS X 10.15.4
Darwin 19.4.0 Darwin Kernel Version 19.4.0
    Wed Mar  4 22:28:40 PST 2020
    root:xnu-6153.101.6~15/RELEASE_X86_64 x86_64


bug macOS

Most helpful comment

I can reproduce and a fix is in progress.

All 6 comments

A sample project is really necessary here.

We have not received the requested information.

If you are still experiencing this issue please provide all the requested information then click the Reopen Issue button.

Thanks!

@whitneyschmidt Can you reopen the issue? It seems I can reproduce it quite easily.
See an attached project:
XamarinMacRefOnly.zip

Application will start only if I remove \ and uses of types from that assembly.

Unhandled Exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
File name: 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
[ERROR] FATAL UNHANDLED EXCEPTION: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
File name: 'System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Bundle contains assembly System.Runtime.CompilerServices.Unsafe.dll, but it's a reference assembly

monodis System.Runtime.CompilerServices.Unsafe.dll | grep -i assembly
.assembly extern netstandard
.assembly 'System.Runtime.CompilerServices.Unsafe'
  .custom instance void class [mscorlib]System.Runtime.CompilerServices.ReferenceAssemblyAttribute::'.ctor'() =  (01 00 00 00 ) // ....
  .custom instance void class [mscorlib]System.Reflection.AssemblyDefaultAliasAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyMetadataAttribute::'.ctor'(string, string) =  (
        41 73 73 65 6D 62 6C 79 00 00 00                ) // Assembly...
  .custom instance void class [mscorlib]System.Reflection.AssemblyMetadataAttribute::'.ctor'(string, string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyMetadataAttribute::'.ctor'(string, string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyCompanyAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyCopyrightAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyDescriptionAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyFileVersionAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyInformationalVersionAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyProductAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyTitleAttribute::'.ctor'(string) =  (
  .custom instance void class [mscorlib]System.Reflection.AssemblyMetadataAttribute::'.ctor'(string, string) =  (

Visual Studio for Mac:

Visual Studio Enterprise 2019 for Mac
Version 8.7.9 (build 9)
Installation UUID: c414f231-db83-468b-81b7-de8ab339624c
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

    Package version: 612000093

Mono Framework MDK
Runtime:
    Mono 6.12.0.93 (2020-02/620cf538206) (64-bit)
    Package version: 612000093

Thanks for following up on this issue!

cc @dalexsoto for further investigation

I can reproduce and a fix is in progress.

Was this page helpful?
0 / 5 - 0 ratings