Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5055 from mono/add-facades-for-netstandard-assembly
Browse files Browse the repository at this point in the history
[Core] Fix editor errors when .NET Standard assembly referenced
  • Loading branch information
slluis committed Jun 14, 2018
2 parents 6914496 + 279b084 commit d500e76
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,38 @@ static bool ContainsReferenceToSystemRuntimeInternal (string fileName)
return false;
}

static Dictionary<string, bool> facadeReferenceDict = new Dictionary<string, bool> ();

static bool RequiresFacadeAssembliesInternal (string fileName)
{
bool result;
if (facadeReferenceDict.TryGetValue (fileName, out result))
return result;

AssemblyDefinition assembly = null;
try {
try {
assembly = Mono.Cecil.AssemblyDefinition.ReadAssembly (fileName);
} catch {
return false;
}
foreach (var r in assembly.MainModule.AssemblyReferences) {
// Don't compare the version number since it may change depending on the version of .net standard
if (r.Name.Equals ("System.Runtime") || r.Name.Equals ("netstandard")) {
facadeReferenceDict [fileName] = true; ;
return true;
}
}
} finally {
assembly?.Dispose ();
}
facadeReferenceDict [fileName] = false;
return false;
}

static object referenceLock = new object ();

[Obsolete ("Use RequiresFacadeAssemblies (string fileName)")]
public static bool ContainsReferenceToSystemRuntime (string fileName)
{
lock (referenceLock) {
Expand All @@ -469,6 +500,8 @@ public static bool ContainsReferenceToSystemRuntime (string fileName)
}

static SemaphoreSlim referenceLockAsync = new SemaphoreSlim (1, 1);

[Obsolete ("Use RequiresFacadeAssembliesAsync (string fileName)")]
public static async System.Threading.Tasks.Task<bool> ContainsReferenceToSystemRuntimeAsync (string filename)
{
try {
Expand All @@ -479,6 +512,23 @@ public static async System.Threading.Tasks.Task<bool> ContainsReferenceToSystemR
}
}

internal static bool RequiresFacadeAssemblies (string fileName)
{
lock (referenceLock) {
return RequiresFacadeAssembliesInternal (fileName);
}
}

internal static async System.Threading.Tasks.Task<bool> RequiresFacadeAssembliesAsync (string filename)
{
try {
await referenceLockAsync.WaitAsync ().ConfigureAwait (false);
return RequiresFacadeAssembliesInternal (filename);
} finally {
referenceLockAsync.Release ();
}
}

public class ManifestResource
{
public string Name {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ internal protected virtual async Task<List<AssemblyReference>> OnGetReferencedAs
} else {
fullPath = Path.GetFullPath (refFilename.FilePath);
}
if (await SystemAssemblyService.ContainsReferenceToSystemRuntimeAsync (fullPath)) {
if (await SystemAssemblyService.RequiresFacadeAssembliesAsync (fullPath)) {
addFacadeAssemblies = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public async Task ImmutableCollectionsContainReferenceToSystemRuntimeAsync (bool
Assert.That(result, Is.EqualTo(withSystemRuntime));
}

[TestCase (true, "System.Collections.Immutable.dll")]
[TestCase (false, "MonoDevelop.Core.dll")]
public void RequiresFacadeAssemblies (bool addFacades, string relativeDllPath)
{
var result = SystemAssemblyService.RequiresFacadeAssemblies (relativeDllPath);
Assert.That (result, Is.EqualTo (addFacades));
}

[TestCase (true, "System.Collections.Immutable.dll")]
[TestCase (false, "MonoDevelop.Core.dll")]
public async Task RequiresFacadeAssembliesAsync (bool addFacades, string relativeDllPath)
{
var result = await SystemAssemblyService.RequiresFacadeAssembliesAsync (relativeDllPath);
Assert.That (result, Is.EqualTo (addFacades));
}

[Test]
public void CheckReferencesAreOk()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
using UnitTests;
Expand Down Expand Up @@ -816,6 +817,49 @@ public void GetDefaultNamespaceWhenProjectRootNamespaceContainsHyphen ()
Assert.AreEqual (expectedDefaultNamespace, result);
}

[Test]
public async Task XamarinIOSProjectReferencesCollectionsImmutableNetStandardAssembly_GetReferencedAssembliesShouldIncludeNetStandard ()
{
if (!Platform.IsMac) {
// NUnit platform attribute does not seem to work.
Assert.Ignore ("Only supported on Mac.");
}

FilePath solFile = Util.GetSampleProject ("iOSImmutableCollections", "iOSImmutableCollections.sln");
CreateNuGetConfigFile (solFile.ParentDirectory);

var process = Process.Start ("msbuild", $"/t:Restore /p:RestoreDisableParallel=true \"{solFile}\"");
Assert.IsTrue (process.WaitForExit (120000), "Timeout restoring NuGet packages.");
Assert.AreEqual (0, process.ExitCode);

using (var sol = (Solution) await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solFile)) {
var p = (DotNetProject) sol.Items [0];

var refs = (await p.GetReferencedAssemblies (ConfigurationSelector.Default)).ToArray ();

Assert.IsTrue (refs.Any (r => r.FilePath.FileName == "netstandard.dll"));
}
}

/// <summary>
/// Clear all other package sources and just use the main NuGet package source when
/// restoring the packages for the project tests.
/// </summary>
static void CreateNuGetConfigFile (FilePath directory)
{
var fileName = directory.Combine ("NuGet.Config");

string xml =
"<configuration>\r\n" +
" <packageSources>\r\n" +
" <clear />\r\n" +
" <add key=\"NuGet v3 Official\" value=\"https://api.nuget.org/v3/index.json\" />\r\n" +
" </packageSources>\r\n" +
"</configuration>";

File.WriteAllText (fileName, xml);
}

[Test]
public async Task ProjectExtensionOnModifiedCalledWhenProjectModified ()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "iOSImmutableCollections", "iOSImmutableCollections\iOSImmutableCollections.csproj", "{548A1099-6C66-410D-9D72-CD5105E51D9D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{548A1099-6C66-410D-9D72-CD5105E51D9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{548A1099-6C66-410D-9D72-CD5105E51D9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{548A1099-6C66-410D-9D72-CD5105E51D9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{548A1099-6C66-410D-9D72-CD5105E51D9D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

using System;
using System.Collections.Immutable;

namespace iOSImmutableCollections
{
public class MyClass
{
ImmutableArray<int> array = ImmutableArray<int>.Empty;

public MyClass()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

using System.Reflection;
using System.Runtime.CompilerServices;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.

[assembly: AssemblyTitle("iOSImmutableCollections")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("Microsoft")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{548A1099-6C66-410D-9D72-CD5105E51D9D}</ProjectGuid>
<ProjectTypeGuids>{FEACFBD2-3405-455C-9665-78FE426C6842};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>iOSImmutableCollections</RootNamespace>
<AssemblyName>iOSImmutableCollections</AssemblyName>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchDebug>true</MtouchDebug>
<MtouchNoSymbolStrip>true</MtouchNoSymbolStrip>
<MtouchFastDev>true</MtouchFastDev>
<IOSDebuggerPort>50484</IOSDebuggerPort>
<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchNoSymbolStrip>true</MtouchNoSymbolStrip>
<MtouchLink>SdkOnly</MtouchLink>
<MtouchHttpClientHandler>NSUrlSessionHandler</MtouchHttpClientHandler>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Xamarin.iOS" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="MyClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
</Project>

0 comments on commit d500e76

Please sign in to comment.