Bug 43744 - Reference to metadata item WriteEventCore could not be resolved
Summary: Reference to metadata item WriteEventCore could not be resolved
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 6.1.2 (C7SR1)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2016-08-25 13:37 UTC by Hrvoje
Modified: 2016-08-31 07:46 UTC (History)
3 users (show)

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

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 Hrvoje 2016-08-25 13:37:23 UTC
I'm getting "LinkAssemblies" task failed unexpectedly with error output:

Xamarin.Android.XamarinAndroidException: error XA2006: 
Reference to metadata item 
	'System.Void System.Diagnostics.Tracing.EventSource::WriteEventCore(System.Int32,System.Int32,System.Diagnostics.Tracing.EventSource/EventData*)' 
	(defined in 'System.Buffers, Version=, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51') 
	from 'System.Buffers, Version=, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' could not be resolved. -
	--> Mono.Cecil.ResolutionException: Failed to resolve System.Void System.Diagnostics.Tracing.EventSource::WriteEventCore(System.Int32,System.Int32,System.Diagnostics.Tracing.EventSource/EventData*)
   at Mono.Linker.Steps.MarkStep.MarkMethod(MethodReference reference)
   at Mono.Linker.Steps.MarkStep.MarkInstruction(Instruction instruction)
   at Mono.Linker.Steps.MarkStep.MarkMethodBody(MethodBody body)
   at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
   at Mono.Linker.Steps.MarkStep.ProcessQueue()
   at Mono.Linker.Steps.MarkStep.Process()
   at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
   at Mono.Linker.Pipeline.Process(LinkContext context)
   at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
   at Xamarin.Android.Tasks.LinkAssemblies.Execute()
   --- End of inner exception stack trace ---
   at Xamarin.Android.Diagnostic.Error(Int32 code, Exception innerException, String message, Object[] args)
   at Xamarin.Android.Tasks.LinkAssemblies.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()	Orderion.App.Droid	C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets	1616	Build
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2016-08-30 03:04:54 UTC
Thanks for the report!

I believe this has been resolved in the latest Beta versions by https://github.com/mono/mono/commit/3c3ef17376f5b4e71ac024210672e2d0484f3419.

I made a first attempt at trying to hit this error myself on the current Beta version of Xamarin.Android ( I added the System.Buffers 4.0.0 NuGet package to a template Android application, added a couple uses of `ArrayPool` to the application, and tried building the .apk with the managed linker set to either "all" or "SDK only".  I wasn't able to hit the error in those conditions.

I also quickly checked that `void System.Diagnostics.Tracing.EventSource.WriteEventCore(int, int, EventSource.EventData* data)` did exist in the Xamarin.Android version of `mscorlib.dll`, and it was present as expected.

If those results don't match up with your testing, then to help the Xamarin team address this issue as efficiently as possible, please provide additional information, such as:

- If possible, please attach a small, self-contained test case that reproduces the bug with as little code as possible.

- Briefly describe the steps you followed to encounter the error.

- Include the full log context of the error messages you are seeing.  This looks like a build time error, so please attach back the complete diagnostic MSBuild output [1].

[1] http://developer.xamarin.com/guides/android/troubleshooting/troubleshooting/#Diagnostic_MSBuild_OutputFor

- Describe the environment where the bug was encountered, including the operating system and versions of Xamarin and any dependencies.

See https://kb.xamarin.com/customer/en/portal/articles/1910343 for additional details on what to include.  Thanks in advance!
Comment 2 joshua 2016-08-31 02:21:36 UTC
I am also getting this linker error when I try to build in RELEASE mode only.

By the way, this error occurs only when linker setting is set to SDK assemblies only.

When it's set to SDK and user assemblies, project builds successfully.

You said it's been fixed in the latest beta versions, does that mean I have to download the latest beta xamarin to resolve this error?

Can you tell me exactly what should I do to workaround this bug?

Comment 3 Brendan Zagaeski (Xamarin Team, assistant) 2016-08-31 03:23:20 UTC
The error is caused by a method declaration that is missing from Xamarin.Android's version of `mscorlib`.  The linker is looking for `System.Diagnostics.Tracing.EventSource.WriteEventCore()`, but that method declaration did not exist in Mono (and Xamarin.Android) until it was added in [1].

The linker [2] is looking for that method as it walks through the tree of all the types and methods it needs to preserve.  The "System.Buffers" NuGet package includes a code path that uses `System.Diagnostics.Tracing.EventSource.WriteEventCore()`.  When you set the linker to "SDK and user assemblies", the "System.Buffers" NuGet package itself becomes eligible for linking.  So what I suspect is happening is that the code path that uses `WriteEventCore()` in "System.Buffers" is being removed by the linker.  Once that code path is gone, the linker no longer needs to worry about walking down _into_ `WriteEventCore()` (to preserve the types used by _that_ method), so the fact that `WriteEventCore()` doesn't exist in Xamarin.Android is no longer a problem.

Updating to the Beta version is indeed the most straightforward approach to resolve the issue.  The Beta version of `mscorlib` now includes the `WriteEventCore()` method declaration.  But based on your observations, setting the linker to "SDK and user assemblies" seems like it would be a valid approach for you too (at least, as long as you don't change your app's usage of "System.Buffers" in such a way that it starts hitting the `WriteEventCore()` code path).  Of course, you might wish to add most of your assemblies to the linker's "skip" field so that you don't have to worry about making sure _all_ of them are safe for the linker.  You could probably skip all of your external libraries and user assemblies _except_ for "System.Buffers" and that would still be sufficient to avoid the error.

[1] https://github.com/mono/mono/commit/3c3ef17376f5b4e71ac024210672e2d0484f3419
[2] https://developer.xamarin.com/guides/android/advanced_topics/linking/
Comment 4 Hrvoje 2016-08-31 07:46:55 UTC
I can confirm that this issue is resolved by installing latest beta