From fad7d2f5a4590c91b49115fcb68e119b60bf71bf Mon Sep 17 00:00:00 2001 From: Nate McMaster Date: Sat, 20 Oct 2018 16:49:00 -0700 Subject: [PATCH] Fix #12 - Give base-path the lowest precedence when searching for managed libraries This fixes assembly loading for System.Drawing.Common. This package was a rare case of a package with both runtime agnostic and runtime-specific versions of the managed library. This means the .NET Core SDK will dump System.Drawing.Common.dll in the plugin base path *as well as* a version in the runtimes/ folder. In this case, corehost actually treats the runtime specific asset with higher priority. This change updates the ManagedLoadContext to imitate this behavior. --- .appveyor.yml | 2 +- .vscode/launch.json | 11 +++++++++ Directory.Build.props | 2 +- DotNetCorePlugins.sln | 15 ++++++++++++ releasenotes.props | 6 +++++ src/Plugins/Loader/ManagedLoadContext.cs | 24 +++++++++---------- .../Plugins.Tests/BasicAssemblyLoaderTests.cs | 19 ++++++++++++++- .../McMaster.NETCore.Plugins.Tests.csproj | 1 + .../TestProjects/DrawingApp/DrawingApp.csproj | 11 +++++++++ test/TestProjects/DrawingApp/Finder.cs | 11 +++++++++ 10 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 test/TestProjects/DrawingApp/DrawingApp.csproj create mode 100644 test/TestProjects/DrawingApp/Finder.cs diff --git a/.appveyor.yml b/.appveyor.yml index 555aa1f..169d7fb 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -1,4 +1,4 @@ -version: 0.2.0.{build} +version: 0.2.1.{build} build_script: - ps: .\build.ps1 -ci environment: diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..396e7ed --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,11 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ] +} diff --git a/Directory.Build.props b/Directory.Build.props index a43912b..5bce517 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -26,7 +26,7 @@ $(NoWarn);NU5105 true - true + true $(MSBuildThisFileDirectory).build\obj\$(MSBuildProjectName)\ $(MSBuildThisFileDirectory).build\bin\$(MSBuildProjectName)\ diff --git a/DotNetCorePlugins.sln b/DotNetCorePlugins.sln index e5b19da..1c5a9c8 100644 --- a/DotNetCorePlugins.sln +++ b/DotNetCorePlugins.sln @@ -57,6 +57,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plátano", "test\TestProjects\Plátano\Plátano.csproj", "{5889B0C9-50E5-4FDC-933B-8684B9FAB8E4}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DrawingApp", "test\TestProjects\DrawingApp\DrawingApp.csproj", "{8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -247,6 +249,18 @@ Global {5889B0C9-50E5-4FDC-933B-8684B9FAB8E4}.Release|x64.Build.0 = Release|Any CPU {5889B0C9-50E5-4FDC-933B-8684B9FAB8E4}.Release|x86.ActiveCfg = Release|Any CPU {5889B0C9-50E5-4FDC-933B-8684B9FAB8E4}.Release|x86.Build.0 = Release|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Debug|x64.ActiveCfg = Debug|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Debug|x64.Build.0 = Debug|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Debug|x86.ActiveCfg = Debug|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Debug|x86.Build.0 = Debug|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Release|Any CPU.Build.0 = Release|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Release|x64.ActiveCfg = Release|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Release|x64.Build.0 = Release|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Release|x86.ActiveCfg = Release|Any CPU + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -268,6 +282,7 @@ Global {78D2B20D-B173-4CF2-A763-F2AA5347947E} = {98E964A2-55DA-4740-9F2E-B64FDF6715DB} {C0BA460C-C1AC-4C54-9C88-60E4B8681E94} = {98E964A2-55DA-4740-9F2E-B64FDF6715DB} {5889B0C9-50E5-4FDC-933B-8684B9FAB8E4} = {98E964A2-55DA-4740-9F2E-B64FDF6715DB} + {8BE0F92F-F353-4D63-9C6F-50D4A0BB7FAB} = {98E964A2-55DA-4740-9F2E-B64FDF6715DB} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B1AF41DC-A03E-47B1-BBDB-3DC27ABD9F74} diff --git a/releasenotes.props b/releasenotes.props index ce47ae9..00882cb 100644 --- a/releasenotes.props +++ b/releasenotes.props @@ -1,5 +1,11 @@  + + + ())); } + [Fact] + public void ItPrefersRuntimeSpecificManagedAssetsOverRidlessOnes() + { + // System.Drawing.Common is an example of a package which has both rid-specific and ridless versions + // The package has lib/netstandard2.0/System.Drawing.Common.dll, but also has runtimes/{win,unix}/lib/netcoreapp2.0/System.Drawing.Common.dll + // In this case, the host will pick the rid-specific version + + var path = TestResources.GetTestProjectAssembly("DrawingApp"); + var loader = PluginLoader.CreateFromConfigFile(path); + var assembly = loader.LoadDefaultAssembly(); + + var type = assembly.GetType("Finder", throwOnError: true); + var method = type.GetMethod("FindDrawingAssembly", BindingFlags.Static | BindingFlags.Public); + Assert.NotNull(method); + Assert.Contains("runtimes", (string)method.Invoke(null, Array.Empty())); + } + [Fact] [UseCulture("es")] public void ItLoadsSatelliteAssemblies() @@ -71,7 +88,7 @@ namespace McMaster.NETCore.Plugins.Tests var loader = PluginLoader.CreateFromConfigFile(path, sharedTypes: new[] { typeof(IFruit) }); var assembly = loader.LoadDefaultAssembly(); var type = Assert.Single(assembly.GetTypes(), t => typeof(IFruit).IsAssignableFrom(t)); - return (IFruit) Activator.CreateInstance(type); + return (IFruit)Activator.CreateInstance(type); } } } diff --git a/test/Plugins.Tests/McMaster.NETCore.Plugins.Tests.csproj b/test/Plugins.Tests/McMaster.NETCore.Plugins.Tests.csproj index d99e3dd..e0d0313 100644 --- a/test/Plugins.Tests/McMaster.NETCore.Plugins.Tests.csproj +++ b/test/Plugins.Tests/McMaster.NETCore.Plugins.Tests.csproj @@ -15,6 +15,7 @@ + diff --git a/test/TestProjects/DrawingApp/DrawingApp.csproj b/test/TestProjects/DrawingApp/DrawingApp.csproj new file mode 100644 index 0000000..c2630d3 --- /dev/null +++ b/test/TestProjects/DrawingApp/DrawingApp.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp2.0 + + + + + + + diff --git a/test/TestProjects/DrawingApp/Finder.cs b/test/TestProjects/DrawingApp/Finder.cs new file mode 100644 index 0000000..0bc0953 --- /dev/null +++ b/test/TestProjects/DrawingApp/Finder.cs @@ -0,0 +1,11 @@ +using System.Drawing.Printing; + +public class Finder +{ + public static string FindDrawingAssembly() + { + var pd = new PrintDocument(); + return typeof(PrintDocument).Assembly.CodeBase; + } +} +