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

System.IO.FileNotFoundException: Could not load the file 'System.Runtime.CompilerServices.Unsafe' #9266

Closed
jwosty opened this issue Aug 3, 2020 · 6 comments · Fixed by #10197
Labels
bug If an issue is a bug or a pull request a bug fix macOS Issues affecting macOS
Milestone

Comments

@jwosty
Copy link

jwosty commented Aug 3, 2020

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


@jwosty
Copy link
Author

jwosty commented Aug 3, 2020

Possibly related to:

dotnet/runtime#24769
#3804
dotnet/standard#481

@chamons chamons added the need-info Waiting for more information before the bug can be investigated label Aug 4, 2020
@chamons chamons added this to the Future milestone Aug 4, 2020
@chamons chamons added the macOS Issues affecting macOS label Aug 4, 2020
@chamons
Copy link
Contributor

chamons commented Aug 4, 2020

A sample project is really necessary here.

@whitneyschmidt
Copy link
Contributor

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!

@AndirNotes
Copy link

AndirNotes commented Dec 1, 2020

@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 <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" /> 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

@whitneyschmidt
Copy link
Contributor

Thanks for following up on this issue!

cc @dalexsoto for further investigation

@whitneyschmidt whitneyschmidt reopened this Dec 1, 2020
@rolfbjarne rolfbjarne added bug If an issue is a bug or a pull request a bug fix and removed need-info Waiting for more information before the bug can be investigated labels Dec 2, 2020
@rolfbjarne
Copy link
Member

I can reproduce and a fix is in progress.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Dec 3, 2020
…ce assemblies as well.

The build logic will put reference assemblies in
'ResolvedCompileFileDefinitions' instead of '_ReferencesFromNuGetPackages'
when using the new-style packageref format, so we need to handle
'ResolvedCompileFileDefinitions' the same way we handle
'_ReferencesFromNuGetPackages'.

I was unfortunately not able to create a unit test for this scenario, because
xibuild sets MSBUILD_EXE_PATH to make msbuild look in our local build files,
and then the restore fails with:

> Issue9266.csproj : error MSB4236: The SDK 'Msbuild.Sdk.Extras/2.1.2' specified could not be found.

If I don't use "Msbuild.Sdk.Extras" in the test, then it doesn't hit the
broken path this PR is fixing. If I change xibuild to not set
MSBUILD_EXE_PATH, we're not testing our local bits anymore, but the system
Xamarin.Mac.

Fixes xamarin#9266.
rolfbjarne added a commit that referenced this issue Dec 3, 2020
…ce assemblies as well. (#10197)

The build logic will put reference assemblies in
'ResolvedCompileFileDefinitions' instead of '_ReferencesFromNuGetPackages'
when using the new-style packageref format, so we need to handle
'ResolvedCompileFileDefinitions' the same way we handle
'_ReferencesFromNuGetPackages'.

I was unfortunately not able to create a unit test for this scenario, because
xibuild sets MSBUILD_EXE_PATH to make msbuild look in our local build files,
and then the restore fails with:

> Issue9266.csproj : error MSB4236: The SDK 'Msbuild.Sdk.Extras/2.1.2' specified could not be found.

If I don't use "Msbuild.Sdk.Extras" in the test, then it doesn't hit the
broken path this PR is fixing. If I change xibuild to not set
MSBUILD_EXE_PATH, we're not testing our local bits anymore, but the system
Xamarin.Mac.

Fixes #9266.
@ghost ghost locked as resolved and limited conversation to collaborators Apr 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug If an issue is a bug or a pull request a bug fix macOS Issues affecting macOS
Projects
None yet
5 participants