diff --git a/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp b/src/installer/corehost/cli/hostpolicy/deps_resolver.cpp index d406497e7be1b3..31140b19c0daf8 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 56428b14cacad4..8fc8f1228aed2b 100644 --- a/src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs +++ b/src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs @@ -71,6 +71,11 @@ public static int Main(string[] args) 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 9052b4e7ce36f7..f3c2207fd24f26 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 e01d71d8dcb352..ab256e36bec25a 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 @@ public SharedTestStateBase() 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 37f8b0fff088f0..7121eadb33af3e 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 @@ public void GetCommandLineArgs_0_Non_Bundled_App() .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 25574f3a2936db..8f1c04f83467bb 100644 --- a/src/installer/tests/TestUtils/TestProjectFixture.cs +++ b/src/installer/tests/TestUtils/TestProjectFixture.cs @@ -257,7 +257,8 @@ public TestProjectFixture PublishProject( 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 @@ public TestProjectFixture PublishProject( 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)