Bug 58504 - Unable to build iOS project that references a NET Standard 2.0 library
Summary: Unable to build iOS project that references a NET Standard 2.0 library
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: MSBuild ()
Version: XI 10.12 (d15-3)
Hardware: Macintosh Mac OS
: Highest blocker
Target Milestone: 15.4
Assignee: Ankit Jain
URL:
Depends on:
Blocks:
 
Reported: 2017-07-31 17:57 UTC by John Miller [MSFT]
Modified: 2017-09-21 09:31 UTC (History)
12 users (show)

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


Attachments
Test Project (13.37 KB, application/zip)
2017-07-31 18:10 UTC, John Miller [MSFT]
Details
ConsoleApp (7.55 KB, application/zip)
2017-08-14 20:42 UTC, Chris Hamons
Details
XI working (54.36 KB, image/png)
2017-08-14 21:14 UTC, Chris Hamons
Details


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:
Status:
VERIFIED FIXED

Description John Miller [MSFT] 2017-07-31 17:57:17 UTC
## Overview

Referencing a .NET Standard 2.0 library project from an iOS project and using some code in the library will result in a build error: 

Error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

## Steps to Reproduce

1. Build the attached sample in VS for Mac

## Actual Results

The above build error. 

## Expected Results

No Build errors

## Version Info:

=== Visual Studio Community 2017 for Mac (Preview) ===

Version 7.1 Preview (7.1 build 1289)
Installation UUID: b968d862-551c-4a33-ab4e-9666400e357c
Runtime:
	Mono 5.2.0.213 (2017-04/ef39d08) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 502000213

=== NuGet ===

Version: 4.3.0.2418

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.0-preview2-25407-01
SDK: /usr/local/share/dotnet/sdk/2.0.0-preview2-006497/Sdks
SDK Version: 2.0.0-preview2-006497
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.2.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

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

=== Apple Developer Tools ===

Xcode 8.3.3 (12175.1)
Build 8E3004b

=== Xamarin.iOS ===

Version: 10.12.0.14 (Visual Studio Community)
Hash: bf350ba5
Branch: d15-3
Build date: 2017-07-05 11:08:04-0400

=== Xamarin.Android ===

Version: 7.4.0.17 (Visual Studio Community)
Android SDK: /Users/johnmiller/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		6.0 (API level 23)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 26.0.0
SDK Build Tools Version: 25.0.3

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin.Mac ===

Version: 3.6.0.13 (Visual Studio Community)

=== Xamarin Inspector ===

Not Installed

=== Build Information ===

Release ID: 701001289
Git revision: 6a34636ae67255dab639f2589ee25bfc76693228
Build date: 2017-07-12 14:06:52-04
Xamarin addins: 310d55d575c7d3248a145747ffdc9cb3a9d7c64c
Build lane: monodevelop-lion-d15-3

=== Operating System ===

Mac OS X 10.12.5
Darwin 16.6.0 Darwin Kernel Version 16.6.0
    Fri Apr 14 16:13:31 PDT 2017
    root:xnu-3789.60.24~4/RELEASE_X86_64 x86_64

## Additional Information

If you reference the netstandard.dll file in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/lib/mono/Xamarin.iOS/Facades/ from the iOS project it will build successfully.
Comment 1 John Miller [MSFT] 2017-07-31 18:10:27 UTC
Created attachment 23918 [details]
Test Project
Comment 2 Sebastien Pouliot 2017-07-31 18:35:40 UTC
Could be related to https://bugzilla.xamarin.com/show_bug.cgi?id=57842
Comment 3 Jeffrey Stedfast 2017-07-31 19:09:27 UTC

*** This bug has been marked as a duplicate of bug 57842 ***
Comment 4 Matt Ward 2017-07-31 19:33:23 UTC
Are we sure this is the same as bug #57842? That bug seems to be about having the project files (.csproj) in the same directory - and indicates that when this is not the case the build works. The title of the bug is the same as this one though.

The attached project has the .NET Standard project in a different directory than the Xamarin.iOS project. Also the error message is different.

I can repro this particular bug using msbuild from the command line and with VS Mac 7.1.0.1281 and later with Mono 5.4.0.130 (2017-06/e9d3a69) and Xamarin.iOS 10.99.1.7.  Note that you need to remove the netstandard assembly reference from the Xamarin.iOS project to see the error.
Comment 5 Sebastien Pouliot 2017-08-08 13:47:22 UTC
@Matt, from the description I'm not sure this is the same issue (but it does seems related). I'm re-opening the issue.

It's a compiler error so it happens quite early in the build process (and before reaching the SDK tools). My guess it's something that the msbuild tasks should handle _or_ it's the .csproj that is incorrect (template? IDE issue?)

Where is the netstandard.dll reference added for _normal_ netstandard projects ? e.g. if I reference some NS2 assemblies from a console app ?


@John can you attach the build log ? in such cases it's a lot faster to look at it and assign/c.c. people than having everyone download and try it by themselves. thanks!
Comment 6 Matt Ward 2017-08-08 14:08:39 UTC
I suspect this is an MSBuild targets/tasks problem.

The error occurs if you run msbuild from the command line. I am guessing that the Xamarin.iOS MSBuild target may have to reference the netstandard.dll, which seems to be for .NET Standard 2.0, automagically like it does for the other facade dlls.

Failing diagnostic MSBuild build output:

https://gist.github.com/mrward/a3c57f5be0b0bb4b628a70964e52af4d

Note that I had to remove the explicit netstandard.dll reference from the project file in the attached zip for the build to fail.
Comment 7 Matt Ward 2017-08-08 14:22:09 UTC
If I create a normal .NET Console project (.NET 4.6.1) that references a Sdk style project that targets .NET Standard 2.0 I can see that the following netstandard.dll is referenced and used by csc.exe:

/Library/Frameworks/Mono.framework/Versions/5.4.0/lib/mono/xbuild/Microsoft/Microsoft.NET.Build.Extensions/net461/ref/netstandard.dll
Comment 8 Ankit Jain 2017-08-08 17:41:24 UTC
@Matt, could you attach the diag build log from comment #7 ?
Comment 9 Matt Ward 2017-08-08 18:00:38 UTC
.NET 4.6.1 referencing .NET Standard 2.0 - msbuild output:

https://gist.github.com/mrward/abe72a5e70005c3b866159d3491067a6
Comment 10 Matt Ward 2017-08-08 19:53:12 UTC
Adding version info where I can repro this problem:

Mono 5.4.0.130 (2017-06/e9d3a69 Tue Jul 25 05:56:19 EDT 2017)
Xamarin.iOS 10.99.1.7

Mono 5.2.0.215 (d15-3/da80840 Thu Jul 20 16:43:07 EDT 2017)
Xamarin.iOS 10.12.0.14 

.NET Core SDKs: 1.0.4 and 2.0.0-preview2-006497
Comment 11 Vincent Dondain [MSFT] 2017-08-11 00:09:22 UTC
@matt provided the required information asked in https://bugzilla.xamarin.com/show_bug.cgi?id=58504#c8

However I'm not able to reproduce the issue using the test case provided by John.

See my versions information: https://gist.github.com/VincentDondain/4929ad0add1132d3be726752142e00ff

@matt, is your test case slightly different?

Note: I'm using a more recent XI + mono
Comment 12 Matt Ward 2017-08-11 08:08:47 UTC
Yeah John's test case includes the workaround where it directly references the netstandard.dll. With that reference it compiles. I removed that reference since it should not be needed and the build should then fail.
Comment 13 Chris Hamons 2017-08-14 20:42:45 UTC
Created attachment 24184 [details]
ConsoleApp
Comment 14 Chris Hamons 2017-08-14 20:43:47 UTC
I am having difficulties getting this working even on a console app, with today's d15-3 build.

https://gist.github.com/chamons/76e17b264729dfcdb3aca3cb244b81c6

/Users/donblas/Library/Caches/VisualStudio/7.0/MSBuild/78994_1/Sdks/Microsoft.NET.Sdk/build/Microsoft.NET.Sdk.Common.targets(5,5): Error: Project '/Users/donblas/Projects/MySpecialConsoleProject/NetStandardLib/NetStandardLib.csproj' targets '.NETStandard,Version=v2.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.6.1'. (MySpecialConsoleProject)

Attached project above.
Comment 15 Chris Hamons 2017-08-14 20:53:47 UTC
Turns out you need to install this:

https://www.microsoft.com/net/download/core

to get it to work at all. Filing a bug against XS for this...
Comment 16 Chris Hamons 2017-08-14 20:56:55 UTC
https://bugzilla.xamarin.com/show_bug.cgi?id=58756
Comment 17 Chris Hamons 2017-08-14 21:14:29 UTC
Created attachment 24185 [details]
XI working
Comment 18 Chris Hamons 2017-08-14 21:15:15 UTC
This works for me on today's released 15.3 for XI (see screen shot).

15.3 XM fails for both supported profiles with

/usr/local/share/dotnet/sdk/2.0.0/Sdks/Microsoft.NET.Sdk/build/Microsoft.NET.Sdk.Common.targets(5,5): Error: Project '/Users/donblas/Downloads/TestNetStandard.iOS 2/NetStandardLib/NetStandardLib.csproj' targets '.NETStandard,Version=v2.0'. It cannot be referenced by a project that targets '.NETFramework,Version=v4.5'. (XMStd)
Comment 19 Chris Hamons 2017-08-14 21:18:19 UTC
Ok, scratch that (and apologies for the spam). I did not realize @John's repro had the work around applied.
Comment 20 Chris Hamons 2017-08-14 21:28:27 UTC
The simplified hack

    <Reference Include="netstandard" />

Fixes XI and XM Modern.

XM Full requires a second hack

    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> 

in the top property group, but then works as well.
Comment 21 Sebastien Pouliot 2017-08-28 15:40:22 UTC
@Ankit I think you have (or are working on) a PR for fixing this right ?
Comment 22 Ankit Jain 2017-08-28 15:59:09 UTC
Yeah, I am testing the fix.
Comment 23 Sebastien Pouliot 2017-09-06 19:49:39 UTC
@Ankit, is this still on the radar for 15.4 ?
Comment 24 Ankit Jain 2017-09-06 19:56:11 UTC
@Sebastien, it is. I am waiting to get an update for some of the sdks for 15.4, which might affect this.
Comment 26 Chris Hamons 2017-09-13 15:54:53 UTC
Landed in master and PR out to 15.4:

https://github.com/xamarin/xamarin-macios/pull/2680
Comment 29 Neeraj Deshmukh 2017-09-21 09:31:48 UTC
Verified the bug on VSfM 15.4 build, issue is not reproducible.

Steps Performed:
For iOS Project
1. Created single view ios app & have reference of .net library 2.0 project. 
2. Build & deployed the project successfully.

For Mac Project
1. Created Mac app & have reference of .net library 2.0 project. 
2. Build & deployed the project successfully.

Verified with test project attached and provided .NetCore 2.0 refernce

Environment : 
Visual Studio Enterprise 2017 for Mac (Preview)
Version 7.2 Preview (7.2 build 617)
Xamarin.Android Version: 8.0.0.21 (Visual Studio Enterprise)
Xamarin.iOS Version: 11.2.0.4 (Visual Studio Enterprise)
Xamarin.Mac Version: 3.8.0.44 (Visual Studio Enterprise)
Operating System Mac OS X 10.12.6

Build Info : https://gist.github.com/neerajdeshmukh/10e01593e96222b944c30f2f35999de3

Screen cast : https://www.screencast.com/t/6WDm8FnTZnl3