Avoid searching for a local hostfxr in component hosts (dotnet/core-setup#6254)
authorElinor Fung <47805090+elinor-fung@users.noreply.github.com>
Fri, 3 May 2019 17:47:59 +0000 (10:47 -0700)
committerGitHub <noreply@github.com>
Fri, 3 May 2019 17:47:59 +0000 (10:47 -0700)
- Stop looking for hostfxr next to the host when getting a delegate in com/ijw/winrt hosts
- Add case to comhost test to check that if hostfxr.dll is next to comhost, it is not used

Commit migrated from https://github.com/dotnet/core-setup/commit/c36c745b2e3f9163ae7afa66ad20d1bc343cd31f

src/installer/corehost/cli/fxr_resolver.cpp
src/installer/corehost/cli/fxr_resolver.h
src/installer/test/HostActivationTests/NativeHosting/Comhost.cs

index c841b64..b838e6f 100644 (file)
@@ -57,6 +57,8 @@ bool fxr_resolver::try_get_path(const pal::string_t& root_path, pal::string_t* o
 {
     pal::string_t fxr_dir;
 #if defined(FEATURE_APPHOST) || defined(FEATURE_LIBHOST)
+    // For apphost and libhost, root_path is expected to be a directory.
+    // For libhost, it may be empty if app-local search is not desired (e.g. com/ijw/winrt hosts, nethost when no assembly path is specified)
     // If a hostfxr exists in root_path, then assume self-contained.
     if (root_path.length() > 0 && library_exists_in_dir(root_path, LIBFXR_NAME, out_fxr_path))
     {
@@ -110,6 +112,7 @@ bool fxr_resolver::try_get_path(const pal::string_t& root_path, pal::string_t* o
         return false;
     }
 #else // !FEATURE_APPHOST && !FEATURE_LIBHOST
+    // For non-apphost and non-libhost (i.e. muxer), root_path is expected to be the full path to the host
     pal::string_t host_dir;
     host_dir.assign(get_directory(root_path));
 
index 38b3522..33da581 100644 (file)
@@ -38,7 +38,8 @@ int load_fxr_and_get_delegate(hostfxr_delegate_type type, THostPathToConfigCallb
     }
     else
     {
-        if (!fxr_resolver::try_get_path(get_directory(host_path), &dotnet_root, &fxr_path))
+        // Do not specify the root path. Getting a delegate does not support self-contained (app-local fxr)
+        if (!fxr_resolver::try_get_path(pal::string_t{}, &dotnet_root, &fxr_path))
         {
             return StatusCode::CoreHostLibMissingFailure;
         }
index 8cf55da..8e5a39e 100644 (file)
@@ -31,17 +31,14 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
                 return;
             }
 
-            var fixture = sharedState.ComLibraryFixture
-                .Copy();
-
             string scenario = synchronous ? "synchronous" : "concurrent";
             string args = $"comhost {scenario} {count} {sharedState.ComHostPath} {sharedState.ClsidString}";
             CommandResult result = Command.Create(sharedState.NativeHostPath, args)
                 .CaptureStdErr()
                 .CaptureStdOut()
                 .EnvironmentVariable("COREHOST_TRACE", "1")
-                .EnvironmentVariable("DOTNET_ROOT", fixture.BuiltDotnet.BinPath)
-                .EnvironmentVariable("DOTNET_ROOT(x86)", fixture.BuiltDotnet.BinPath)
+                .EnvironmentVariable("DOTNET_ROOT", sharedState.ComLibraryFixture.BuiltDotnet.BinPath)
+                .EnvironmentVariable("DOTNET_ROOT(x86)", sharedState.ComLibraryFixture.BuiltDotnet.BinPath)
                 .Execute();
 
             result.Should().Pass()
@@ -53,6 +50,37 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
             }
         }
 
+        [Fact]
+        public void ActivateClass_IgnoreAppLocalHostFxr()
+        {
+            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+            {
+                // COM activation is only supported on Windows
+                return;
+            }
+
+            var fixture = sharedState.ComLibraryFixture.Copy();
+
+            File.WriteAllText(Path.Combine(fixture.TestProject.BuiltApp.Location, "hostfxr.dll"), string.Empty);
+            var comHostWithAppLocalFxr = Path.Combine(
+                fixture.TestProject.BuiltApp.Location,
+                $"{ fixture.TestProject.AssemblyName }.comhost.dll");
+
+            string args = $"comhost synchronous 1 {comHostWithAppLocalFxr} {sharedState.ClsidString}";
+            CommandResult result = Command.Create(sharedState.NativeHostPath, args)
+                .CaptureStdErr()
+                .CaptureStdOut()
+                .EnvironmentVariable("COREHOST_TRACE", "1")
+                .EnvironmentVariable("DOTNET_ROOT", fixture.BuiltDotnet.BinPath)
+                .EnvironmentVariable("DOTNET_ROOT(x86)", fixture.BuiltDotnet.BinPath)
+                .Execute();
+
+            result.Should().Pass()
+                .And.HaveStdOutContaining("New instance of Server created")
+                .And.HaveStdOutContaining($"Activation of {sharedState.ClsidString} succeeded.")
+                .And.HaveStdErrContaining("Using environment variable DOTNET_ROOT");
+        }
+
         public class SharedTestState : SharedTestStateBase
         {
             public string ComHostPath { get; }