From 040301836ddc1c8d63025ac5e578050b48554563 Mon Sep 17 00:00:00 2001 From: Mateo Torres-Ruiz Date: Thu, 1 Oct 2020 17:44:36 -0700 Subject: [PATCH] Add single-file app's dir to NATIVE_DLL_SEARCH_DIRECTORIES (#42876) Add two directories to NATIVE_DLL_SEARCH_DIRECTORIES to single-file bundles: 1. The bundle exe directory 2. If the bundle extracts any files, the extraction directory Fixes #42772 --- .../corehost/cli/hostpolicy/deps_resolver.cpp | 13 +++++++ .../TestProjects/SingleFileApiTests/Program.cs | 5 +++ .../SingleFileApiTests/SingleFileApiTests.csproj | 8 ++++- .../AppHost.Bundle.Tests/BundleTestBase.cs | 5 +-- .../AppHost.Bundle.Tests/SingleFileApiTests.cs | 41 +++++++++++++++++++++- .../tests/TestUtils/TestProjectFixture.cs | 8 ++++- 6 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp b/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp index d406497..31140b1 100644 --- a/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp +++ b/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp @@ -877,6 +877,19 @@ bool deps_resolver_t::resolve_probe_dirs( } } + // If this is a single-file app, add the app's dir to the native search directories. + if (bundle::info_t::is_single_file_bundle() && !is_resources) + { + auto bundle = bundle::runner_t::app(); + add_unique_path(asset_type, bundle->base_path(), &items, output, &non_serviced, core_servicing); + + // Add the extraction path if it exists. + if (pal::directory_exists(bundle->extraction_path())) + { + add_unique_path(asset_type, bundle->extraction_path(), &items, output, &non_serviced, core_servicing); + } + } + output->append(non_serviced); return true; diff --git a/src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs b/src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs index 56428b1..8fc8f12 100644 --- a/src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs +++ b/src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs @@ -71,6 +71,11 @@ namespace SingleFileApiTests Console.WriteLine("AppContext.BaseDirectory: " + AppContext.BaseDirectory); break; + case "native_search_dirs": + var native_search_dirs = AppContext.GetData("NATIVE_DLL_SEARCH_DIRECTORIES"); + Console.WriteLine("NATIVE_DLL_SEARCH_DIRECTORIES: " + native_search_dirs); + break; + default: Console.WriteLine("test failure"); return -1; diff --git a/src/installer/tests/Assets/TestProjects/SingleFileApiTests/SingleFileApiTests.csproj b/src/installer/tests/Assets/TestProjects/SingleFileApiTests/SingleFileApiTests.csproj index 9052b4e..f3c2207 100644 --- a/src/installer/tests/Assets/TestProjects/SingleFileApiTests/SingleFileApiTests.csproj +++ b/src/installer/tests/Assets/TestProjects/SingleFileApiTests/SingleFileApiTests.csproj @@ -7,4 +7,10 @@ $(MNAVersion) - \ No newline at end of file + + + PreserveNewest + + + + diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs index e01d71d..ab256e3 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs @@ -59,13 +59,14 @@ namespace AppHost.Bundle.Tests RepoDirectories = new RepoDirectoriesProvider(); } - public TestProjectFixture PreparePublishedSelfContainedTestProject(string projectName) + public TestProjectFixture PreparePublishedSelfContainedTestProject(string projectName, params string[] extraArgs) { var testFixture = new TestProjectFixture(projectName, RepoDirectories); testFixture .EnsureRestoredForRid(testFixture.CurrentRid, RepoDirectories.CorehostPackages) .PublishProject(runtime: testFixture.CurrentRid, - outputDirectory: BundleHelper.GetPublishPath(testFixture)); + outputDirectory: BundleHelper.GetPublishPath(testFixture), + extraArgs: extraArgs); return testFixture; } diff --git a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/SingleFileApiTests.cs b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/SingleFileApiTests.cs index 37f8b0f..7121ead 100644 --- a/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/SingleFileApiTests.cs +++ b/src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/SingleFileApiTests.cs @@ -85,13 +85,52 @@ namespace AppHost.Bundle.Tests .HaveStdOutContaining(appPath); } + [Fact] + public void AppContext_Native_Search_Dirs_Contains_Bundle_Dir() + { + var fixture = sharedTestState.TestFixture.Copy(); + Bundler bundler = BundleHelper.BundleApp(fixture, out string singleFile); + string extractionDir = BundleHelper.GetExtractionDir(fixture, bundler).Name; + string bundleDir = BundleHelper.GetBundleDir(fixture).FullName; + + // If we don't extract anything to disk, the extraction dir shouldn't + // appear in the native search dirs. + Command.Create(singleFile, "native_search_dirs") + .CaptureStdErr() + .CaptureStdOut() + .Execute() + .Should().Pass() + .And.HaveStdOutContaining(bundleDir) + .And.NotHaveStdOutContaining(extractionDir); + } + + [Fact] + public void AppContext_Native_Search_Dirs_Contains_Bundle_And_Extraction_Dirs() + { + var fixture = sharedTestState.TestFixture.Copy(); + Bundler bundler = BundleHelper.BundleApp(fixture, out string singleFile, BundleOptions.BundleNativeBinaries); + string extractionDir = BundleHelper.GetExtractionDir(fixture, bundler).Name; + string bundleDir = BundleHelper.GetBundleDir(fixture).FullName; + + Command.Create(singleFile, "native_search_dirs") + .CaptureStdErr() + .CaptureStdOut() + .Execute() + .Should().Pass() + .And.HaveStdOutContaining(extractionDir) + .And.HaveStdOutContaining(bundleDir); + } + public class SharedTestState : SharedTestStateBase, IDisposable { public TestProjectFixture TestFixture { get; set; } public SharedTestState() { - TestFixture = PreparePublishedSelfContainedTestProject("SingleFileApiTests"); + // We include mockcoreclr in our project to test native binaries extraction. + string mockCoreClrPath = Path.Combine(RepoDirectories.Artifacts, "corehost_test", + RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("mockcoreclr")); + TestFixture = PreparePublishedSelfContainedTestProject("SingleFileApiTests", $"/p:AddFile={mockCoreClrPath}"); } public void Dispose() diff --git a/src/installer/tests/TestUtils/TestProjectFixture.cs b/src/installer/tests/TestUtils/TestProjectFixture.cs index 25574f3..8f1c04f 100644 --- a/src/installer/tests/TestUtils/TestProjectFixture.cs +++ b/src/installer/tests/TestUtils/TestProjectFixture.cs @@ -257,7 +257,8 @@ namespace Microsoft.DotNet.CoreSetup.Test bool? selfContained = null, string outputDirectory = null, bool singleFile = false, - bool restore = false) + bool restore = false, + params string[] extraArgs) { dotnet = dotnet ?? SdkDotnet; outputDirectory = outputDirectory ?? TestProject.OutputDirectory; @@ -308,6 +309,11 @@ namespace Microsoft.DotNet.CoreSetup.Test publishArgs.Add($"/p:TestTargetRid={RepoDirProvider.TargetRID}"); publishArgs.Add($"/p:MNAVersion={RepoDirProvider.MicrosoftNETCoreAppVersion}"); + foreach (var arg in extraArgs) + { + publishArgs.Add(arg); + } + dotnet.Publish(publishArgs.ToArray()) .WorkingDirectory(TestProject.ProjectDirectory) .Environment("NUGET_PACKAGES", RepoDirProvider.NugetPackages) -- 2.7.4