Skip to content

Commit 86457c8

Browse files
author
Ankit Jain
committed
[msbuild][mac][ios] Fix referencing netstandard projects
Building a XI or XM (Modern) project that references a netstandard 2.0 project with msbuild fails because of a missing reference to `netstandard.dll`. AppDelegate.cs(21,52): 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'. The reason is that XI and XM (Modern) projects have `$(TargetFrameworkIdentifier) != .NETFramework`, so targets from `Microsoft.NET.Build.Extensions` which provide ns2.0 support don't get imported. `ImplicitlyExpandNETStandardFacades` in particular, which would have added a reference to `netstandard.dll`. So, instead we duplicate that in XI/XM targets. Essentially, we need to scan all the references for a `netstandard` dependency (using `GetDependsOnNETStandard` task) and if found, add the `netstandard.dll` reference. Partially fixes bxc #58504 .
1 parent 4879ea0 commit 86457c8

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

msbuild/Xamarin.Mac.Tasks/Xamarin.Mac.Common.ImplicitFacade.msbuild.targets

+23-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,35 @@ Copyright (c) 2017 Microsoft Corp. (www.microsoft.com)
3333
</PropertyGroup>
3434

3535
<!-- Implicitly references all portable design-time facades if the user is referencing a System.Runtime-based portable library -->
36+
37+
<UsingTask
38+
TaskName="GetDependsOnNETStandard"
39+
Condition="'$(IsXBuild)' != 'true'"
40+
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\Microsoft.NET.Build.Extensions\tools\net46\Microsoft.NET.Build.Extensions.Tasks.dll" />
41+
3642
<Target Name="ImplicitlyExpandDesignTimeFacades" DependsOnTargets="$(ImplicitlyExpandDesignTimeFacadesDependsOn)">
43+
<ItemGroup>
44+
<XM_CandidateNETStandardReferences Include="@(Reference);@(_ResolvedProjectReferencePaths)" />
45+
<XM_InboxNETStandardFolders Include="$(TargetFrameworkDirectory)" />
46+
</ItemGroup>
47+
48+
<!-- This file is imported for Modern projects, which have have `$(TargetFrameworkIdentifier) != .NETFramework`, so Microsoft.NET.Build.Extensions
49+
(which provides support for ns 2.0 projects) doesn't get imported. So, we need to check if any references depend on `netstandard`. And if so,
50+
add an explicit reference to `netstandard.dll` -->
51+
<GetDependsOnNETStandard
52+
Condition="'$(IsXBuild)' != 'true' and '$(DependsOnNETStandard)' == '' and '@(XM_CandidateNETStandardReferences)' != ''"
53+
References="@(XM_CandidateNETStandardReferences)">
54+
<Output TaskParameter="DependsOnNETStandard" PropertyName="XM_DependsOnNETStandard" />
55+
</GetDependsOnNETStandard>
56+
3757
<PropertyGroup>
3858
<!-- Does one of our dependencies reference a System.Runtime-based portable library? -->
3959
<_HasReferenceToSystemRuntime Condition="'$(DependsOnSystemRuntime)' == 'true' or '%(_ResolvedProjectReferencePaths.TargetPlatformIdentifier)' == 'Portable'">true</_HasReferenceToSystemRuntime>
60+
61+
<XM_NETStandardInbox Condition="'$(XM_NETStandardInbox)' == '' and Exists('%(XM_InboxNETStandardFolders.Identity)\netstandard.dll')">true</XM_NETStandardInbox>
4062
</PropertyGroup>
4163

42-
<ItemGroup Condition="'$(_HasReferenceToSystemRuntime)' == 'true'">
64+
<ItemGroup Condition="'$(_HasReferenceToSystemRuntime)' == 'true' or ('$(XM_NETStandardInbox)' == 'true' and '$(XM_DependsOnNETStandard)' == 'true')">
4365
<_DesignTimeFacadeAssemblies Include="%(DesignTimeFacadeDirectories.Identity)*.dll"/>
4466

4567
<_DesignTimeFacadeAssemblies_Names Include="@(_DesignTimeFacadeAssemblies->'%(FileName)')">

msbuild/Xamarin.iOS.Tasks.Core/Xamarin.iOS.Common.targets

+22-2
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,34 @@ Copyright (C) 2013-2016 Xamarin. All rights reserved.
155155
<Optimize />
156156
</_BundleResourceWithLogicalName>
157157
</ItemDefinitionGroup>
158-
158+
159+
<UsingTask
160+
TaskName="GetDependsOnNETStandard"
161+
Condition="'$(IsXBuild)' != 'true'"
162+
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\Microsoft.NET.Build.Extensions\tools\net46\Microsoft.NET.Build.Extensions.Tasks.dll" />
163+
159164
<Target Name="ImplicitlyExpandDesignTimeFacades" Condition="'$(ImplicitlyExpandDesignTimeFacades)' == 'true'" DependsOnTargets="$(ImplicitlyExpandDesignTimeFacadesDependsOn)">
165+
<ItemGroup>
166+
<XI_CandidateNETStandardReferences Include="@(Reference);@(_ResolvedProjectReferencePaths)" />
167+
<XI_InboxNETStandardFolders Include="$(TargetFrameworkDirectory)" />
168+
</ItemGroup>
169+
170+
<!-- XI projects have `$(TargetFrameworkIdentifier) != .NETFramework`, so Microsoft.NET.Build.Extensions (which provides support for ns 2.0 projects) doesn't get
171+
imported. So, we need to check if any references depend on `netstandard`. And if so, add an explicit reference to `netstandard.dll` -->
172+
<GetDependsOnNETStandard
173+
Condition="'$(IsXBuild)' != 'true' and '$(DependsOnNETStandard)' == '' and '@(XI_CandidateNETStandardReferences)' != ''"
174+
References="@(XI_CandidateNETStandardReferences)">
175+
<Output TaskParameter="DependsOnNETStandard" PropertyName="XI_DependsOnNETStandard" />
176+
</GetDependsOnNETStandard>
177+
160178
<PropertyGroup>
161179
<_HasReferenceToSystemRuntime Condition="'$(DependsOnSystemRuntime)' == 'true' or '%(_ResolvedProjectReferencePaths.TargetPlatformIdentifier)' == 'Portable'
162180
or '%(ReferenceDependencyPaths.Filename)' == 'System.Runtime'">true</_HasReferenceToSystemRuntime>
181+
182+
<XI_NETStandardInbox Condition="'$(XI_NETStandardInbox)' == '' and Exists('%(XI_InboxNETStandardFolders.Identity)\netstandard.dll')">true</XI_NETStandardInbox>
163183
</PropertyGroup>
164184

165-
<ItemGroup Condition="'$(_HasReferenceToSystemRuntime)' == 'true'">
185+
<ItemGroup Condition="'$(_HasReferenceToSystemRuntime)' == 'true' or ('$(XI_NETStandardInbox)' == 'true' and '$(XI_DependsOnNETStandard)' == 'true')">
166186
<_DesignTimeFacadeAssemblies Include="%(DesignTimeFacadeDirectories.Identity)*.dll"/>
167187
<ReferencePath Remove="@(_DesignTimeFacadeAssemblies)"/>
168188
<ReferencePath Include="%(_DesignTimeFacadeAssemblies.Identity)">

0 commit comments

Comments
 (0)