Add single-file app's dir to NATIVE_DLL_SEARCH_DIRECTORIES (#42876)
authorMateo Torres-Ruiz <mateoatr@users.noreply.github.com>
Fri, 2 Oct 2020 00:44:36 +0000 (17:44 -0700)
committerGitHub <noreply@github.com>
Fri, 2 Oct 2020 00:44:36 +0000 (17:44 -0700)
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

src/installer/corehost/cli/hostpolicy/deps_resolver.cpp
src/installer/tests/Assets/TestProjects/SingleFileApiTests/Program.cs
src/installer/tests/Assets/TestProjects/SingleFileApiTests/SingleFileApiTests.csproj
src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/BundleTestBase.cs
src/installer/tests/Microsoft.NET.HostModel.Tests/AppHost.Bundle.Tests/SingleFileApiTests.cs
src/installer/tests/TestUtils/TestProjectFixture.cs

index d406497..31140b1 100644 (file)
@@ -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;
index 56428b1..8fc8f12 100644 (file)
@@ -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;
index 9052b4e..f3c2207 100644 (file)
@@ -7,4 +7,10 @@
     <RuntimeFrameworkVersion>$(MNAVersion)</RuntimeFrameworkVersion>
   </PropertyGroup>
 
-</Project>
\ No newline at end of file
+  <ItemGroup>
+    <None Condition="'$(AddFile)' != ''" Include="$(AddFile)">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
+  </ItemGroup>
+
+</Project>
index e01d71d..ab256e3 100644 (file)
@@ -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;
             }
index 37f8b0f..7121ead 100644 (file)
@@ -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()
index 25574f3..8f1c04f 100644 (file)
@@ -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)