diff --git a/src/MSBuildLocator/MSBuildLocator.cs b/src/MSBuildLocator/MSBuildLocator.cs index 40b27e81..5b447bab 100644 --- a/src/MSBuildLocator/MSBuildLocator.cs +++ b/src/MSBuildLocator/MSBuildLocator.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices; using System.Text; #if NETCOREAPP @@ -353,6 +354,29 @@ private static bool IsMSBuildAssembly(AssemblyName assemblyName) return sb.ToString().Equals(MSBuildPublicKeyToken, StringComparison.OrdinalIgnoreCase); } + +#if NETCOREAPP + private static IntPtr HostFxrResolver(Assembly assembly, string libraryName) + { + if (libraryName != "hostfxr") + { + return IntPtr.Zero; + } + + var path = Environment.GetEnvironmentVariable("HOSTFXR_PATH"); + if (path == null) + { + return IntPtr.Zero; + } + + if (!NativeLibrary.TryLoad(path, out var handle)) + { + throw new Exception(); + } + + return handle; + } +#endif private static IEnumerable GetInstances(VisualStudioInstanceQueryOptions options) { @@ -368,6 +392,7 @@ private static IEnumerable GetInstances(VisualStudioInstan #endif #if NETCOREAPP + AssemblyLoadContext.GetLoadContext(Assembly.GetExecutingAssembly()).ResolvingUnmanagedDll += HostFxrResolver; foreach (var dotnetSdk in DotNetSdkLocationHelper.GetInstances(options.WorkingDirectory)) yield return dotnetSdk; #endif