Bug 43515 - Linker error when using Roslyn
Summary: Linker error when using Roslyn
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 6.1.0 (C7)
Hardware: PC Windows
: High normal
Target Milestone: 7.1 (C9)
Assignee: Radek Doulik
Depends on:
Reported: 2016-08-18 18:01 UTC by 0xFireball
Modified: 2016-10-14 06:52 UTC (History)
6 users (show)

Tags: roslyn
Is this bug a regression?: ---
Last known good build:

test case (1.29 MB, application/zip)
2016-09-27 14:46 UTC, Radek Doulik

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description 0xFireball 2016-08-18 18:01:56 UTC
I am getting a linker error when using Roslyn:

>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1616,5): error MSB4018: The "LinkAssemblies" task failed unexpectedly.
3>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1616,5): error MSB4018: Xamarin.Android.XamarinAndroidException: error XA2006: Reference to metadata item 'System.Runtime.Loader.AssemblyLoadContext' (defined in 'Microsoft.CodeAnalysis.Scripting, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35') from 'Microsoft.CodeAnalysis.Scripting, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be resolved. ---> Mono.Cecil.ResolutionException: Failed to resolve System.Runtime.Loader.AssemblyLoadContext

Diagnostic build log link:

Instructions to reproduce:

XA version: 6.1
1. Make a Xamarin.Android project, and install System.Runtime.Loader (4.0.0)
2. Make a random dll, and add Microsoft.CodeAnalysis.CSharp.Scripting from nuget
3. reference the randomdll from the XA project
4. Build the XA project in Release
Comment 2 Jonathan Pryor 2016-08-19 14:22:25 UTC
Discussion: https://gitter.im/xamarin/xamarin-android?at=57b5eb3bb64a3a016f4896e1
Comment 3 Radek Doulik 2016-08-26 15:22:33 UTC
I am unable to reproduce it so far. Could you please attach the whole build log?
Comment 4 Radek Doulik 2016-08-26 15:39:00 UTC
OK, I am now able to reproduce.

To clarify the repro steps, the Microsoft.CodeAnalysis.CSharp.Scripting nuget package needs to be added to the App project and not the library project.
Comment 6 Jonathan Pryor 2016-09-09 14:31:19 UTC
The problem is that Microsoft.CodeAnalysis.Scripting.dll (or one of its dependencies) has a reference to System.Runtime.Loader.AssemblyLoadContext, which can't be found.

The reason it can't be found is because Mono 4.4 doesn't provide that type, and Xamarin.Android 6.1 is based on Mono 4.4.

Thus, this assembly cannot be used with Xamarin.Android 6.1.

Xamarin.Android 7.0, on the other hand, is based on Mono 4.6, which *does* provide the System.Runtime.Loader.AssemblyLoadContext type, and thus it *should* work there.

Thus, there are two "answers" to this bug:

1. It's "upstream" on mono. (Mono has it *now*, but if Mono *didn't* provide it, we couldn't provide it until Mono did...).

2. The error message needs *serious* help:

> error XA2006:
> Reference to metadata item 'System.Runtime.Loader.AssemblyLoadContext'
> (defined in 'Microsoft.CodeAnalysis.Scripting, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35')
> from 'Microsoft.CodeAnalysis.Scripting, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
> could not be resolved.
> ---> Mono.Cecil.ResolutionException: Failed to resolve System.Runtime.Loader.AssemblyLoadContext

Nowhere in there do we get understanding that (1) System.Runtime.Loader.AssemblyLoadContext is a type forwarded reference (from? to?), and the message itself is confusing around the use of 'defined'. ("Defined" *usually* means "contains the definition", e.g. "System.String is defined in mscorlib.dll. That is *not* how it is used in this message.)

Additionally, the message doesn't even provide all information it's supposed to:


> Diagnostic.Error (2006, ex,
>     "Reference to metadata item '{0}' (defined in '{1}') from '{1}' could not be resolved.",
>     ex.Member,
>     ex.Member.Module.Assembly,
>     ex.Scope)

Three parameters are provided, but only two are used.

This error message should be improved.
Comment 7 Radek Doulik 2016-09-19 14:12:46 UTC
The error message is now improved. https://github.com/xamarin/xamarin-android/commit/e176d19e6dd92c88c08a1943c8739d4f841b9463
Comment 9 Radek Doulik 2016-09-27 14:46:23 UTC
Created attachment 17765 [details]
test case
Comment 10 Radek Doulik 2016-09-27 14:50:15 UTC
I have attached a test case. It is a bit tricky though. You will need to:

1) build lib project
2) change name of the EmptyClass1 type and constructor to EmptyClass
3) build testfwd
4) build/deploy testlinkerforwardes (in Release configuration) - that should trigger the bug
Comment 13 Shruti 2016-10-14 06:52:18 UTC
Verification Status:

I have checked this issue on master XVS build version (a866670) as per comment0, following by the instruction to reproduce and also with the test project attached in comment9, following by the instruction in comment10; and observed that the issue is fixed as no linker error is seen and the project successfully build and deployed in release configuration. 

Supplement Info:
Screencast: http://www.screencast.com/t/5UEkeckcjs
Build Output:
IDE Log:

=====Environment Information=====
Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01590

Installed Version: Enterprise

Xamarin (a866670)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android (736438a)
Visual Studio extension to enable development for Xamarin.Android.

Xamarin.iOS (0760023)
Visual Studio extension to enable development for Xamarin.iOS.

Hence, marking this issue as verified fixed.