Skip to content

Commit

Permalink
Fix review comment - remove extra method, cleanup and fix bug with do…
Browse files Browse the repository at this point in the history
…tnet.exe name comparison

Remove extra spacing in Microsoft.Build.Locator.csproj

Remove extra changes in Microsoft.Build.Locator.csproj
  • Loading branch information
YuliiaKovalova committed Aug 23, 2023
1 parent 330ac55 commit b452d73
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 72 deletions.
1 change: 0 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
6.0.x
- name: Restore
run: dotnet restore -bl:restore.binlog
Expand Down
2 changes: 1 addition & 1 deletion samples/BuilderApp/BuilderApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472;net7.0</TargetFrameworks>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;net7.0</TargetFrameworks>
<TargetFrameworks>net472;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\MSBuildLocator\key.snk</AssemblyOriginatorKeyFile>
Expand Down
102 changes: 35 additions & 67 deletions src/MSBuildLocator/DotNetSdkLocationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal static class DotNetSdkLocationHelper
private static readonly string ExeName = IsWindows ? "dotnet.exe" : "dotnet";
private static readonly string? DotnetPath = ResolveDotnetPath();

static DotNetSdkLocationHelper() => LoadHostFxr();

public static VisualStudioInstance? GetInstance(string dotNetSdkPath)
{
if (string.IsNullOrWhiteSpace(dotNetSdkPath) || !File.Exists(Path.Combine(dotNetSdkPath, "Microsoft.Build.dll")))
Expand Down Expand Up @@ -69,8 +71,6 @@ public static IEnumerable<VisualStudioInstance> GetInstances(string workingDirec

private static IEnumerable<string> GetDotNetBasePaths(string workingDirectory)
{
LoadHostFxr();

string? bestSDK = GetSdkFromGlobalSettings(workingDirectory);
if (!string.IsNullOrEmpty(bestSDK))
yield return bestSDK;
Expand Down Expand Up @@ -99,9 +99,9 @@ private static void LoadHostFxr()

private static IntPtr HostFxrResolver(Assembly assembly, string libraryName)
{
var hostFxrRegex = new Regex("(lib)?hostfxr.dylib", RegexOptions.IgnoreCase);
var hostFxrLibName = "libhostfxr.dylib";

if (!hostFxrRegex.IsMatch(libraryName) || string.IsNullOrEmpty(DotnetPath))
if (!hostFxrLibName.Equals(libraryName, StringComparison.Ordinal) || string.IsNullOrEmpty(DotnetPath))
return IntPtr.Zero;

var hostFxrRoot = Path.Combine(DotnetPath, "host", "fxr");
Expand All @@ -115,7 +115,7 @@ private static IntPtr HostFxrResolver(Assembly assembly, string libraryName)
if (hostFxrAssemblyDirectory != null)
{
var hostfxrAssembly = Directory.GetFiles(hostFxrAssemblyDirectory)
.Where(filePath => hostFxrRegex.IsMatch(Path.GetFileName(filePath)))
.Where(filePath => hostFxrLibName.Equals(libraryName, StringComparison.Ordinal))
.FirstOrDefault();

if (hostfxrAssembly != null)
Expand Down Expand Up @@ -157,16 +157,15 @@ private static string ResolveDotnetPath()
{
string? dotnetExePath = GetCurrentProcessPath();
var isRunFromDotnetExecutable = !string.IsNullOrEmpty(dotnetExePath)
&& Path.GetFileNameWithoutExtension(dotnetExePath).Equals(ExeName, StringComparison.InvariantCultureIgnoreCase);
&& Path.GetFileName(dotnetExePath).Equals(ExeName, StringComparison.InvariantCultureIgnoreCase);
if (isRunFromDotnetExecutable)
dotnetPath = Path.GetDirectoryName(dotnetExePath);

else
{
dotnetPath = GetDotnetPathFromDefaultPaths();

if (string.IsNullOrEmpty(dotnetPath))
dotnetPath = GetDotnetPathFromPATH();
dotnetPath = FindDotnetPathFromEnvVariable("DOTNET_HOST_PATH")
?? FindDotnetPathFromEnvVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR")
?? GetDotnetPathFromPATH();
}
}

Expand All @@ -176,61 +175,26 @@ private static string ResolveDotnetPath()
return dotnetPath;
}

private static void LoadHostFxr(string dotnetPath)
{
var isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
if (isOSX)
{
var hostFxrRoot = Path.Combine(dotnetPath, "host", "fxr");
if (Directory.Exists(hostFxrRoot))
{
// Agreed to load hostfxr from the highest version
var hostFxrAssemblyDirectory = Directory.GetDirectories(hostFxrRoot)
.OrderByDescending(d => d)
.FirstOrDefault();

if (hostFxrAssemblyDirectory != null)
{
Regex regex = new Regex("(lib)?hostfxr.dylib", RegexOptions.IgnoreCase);

var matchingAssembly = Directory.GetFiles(hostFxrAssemblyDirectory)
.Where(filePath => regex.IsMatch(Path.GetFileName(filePath)))
.FirstOrDefault();

if (matchingAssembly != null)
{
var isHostFxrLoaded = AppDomain.CurrentDomain.GetAssemblies().Any(assembly =>
string.Equals(assembly.GetName().Name, matchingAssembly, StringComparison.OrdinalIgnoreCase));

if (!isHostFxrLoaded)
Assembly.Load(matchingAssembly);
}
}
}
}
}

private static string? GetDotnetPathFromROOT()
{
// 32-bit architecture has (x86) suffix
string envVarName = (IntPtr.Size == 4) ? "DOTNET_ROOT(x86)" : "DOTNET_ROOT";
var dotnetPath = FindDotnetPathFromEnvVariable(envVarName, ExeName);
var dotnetPath = FindDotnetPathFromEnvVariable(envVarName);

return dotnetPath;
}

private static string? GetDotnetPathFromDefaultPaths()
private static string? GetDotnetPathFromHOST()
{
var dotnetPath = FindDotnetPathFromEnvVariable("DOTNET_HOST_PATH", ExeName);
var dotnetPath = FindDotnetPathFromEnvVariable("DOTNET_HOST_PATH");
if (dotnetPath == null)
{
dotnetPath ??= FindDotnetPathFromEnvVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR", ExeName);
dotnetPath ??= FindDotnetPathFromEnvVariable("DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR");
}

return dotnetPath;
}


private static string? GetCurrentProcessPath()
{
string? processPath = null;
Expand All @@ -254,18 +218,11 @@ private static void LoadHostFxr(string dotnetPath)
var paths = Environment.GetEnvironmentVariable("PATH")?.Split(Path.PathSeparator) ?? Array.Empty<string>();
foreach (string dir in paths)
{
string filePath = Path.Combine(dir, ExeName);
if (File.Exists(filePath))
{
if (!IsWindows)
{
filePath = realpath(filePath) ?? filePath;
if (!File.Exists(filePath))
continue;
}

dotnetPath = dir;
}
string? filePath = ValidatePath(dir);
if (string.IsNullOrEmpty(filePath))
continue;

dotnetPath = dir;
}

return dotnetPath;
Expand Down Expand Up @@ -298,14 +255,25 @@ private static string[] GetAllAvailableSDKs()
return result;
}

private static string? FindDotnetPathFromEnvVariable(string environmentVariable, string exeName)
private static string? FindDotnetPathFromEnvVariable(string environmentVariable)
{
string? dotnet_root = Environment.GetEnvironmentVariable(environmentVariable);
if (!string.IsNullOrEmpty(dotnet_root))
string? dotnetPath = Environment.GetEnvironmentVariable(environmentVariable);

return string.IsNullOrEmpty(dotnetPath) ? null : ValidatePath(dotnetPath);
}

private static string? ValidatePath(string dotnetPath)
{
string fullPathToDotnetFromRoot = Path.Combine(dotnetPath, ExeName);
if (File.Exists(fullPathToDotnetFromRoot))
{
string fullPathToDotnetFromRoot = Path.Combine(dotnet_root, exeName);
if (File.Exists(fullPathToDotnetFromRoot))
return realpath(fullPathToDotnetFromRoot) ?? fullPathToDotnetFromRoot;
if (!IsWindows)
{
fullPathToDotnetFromRoot = realpath(fullPathToDotnetFromRoot) ?? fullPathToDotnetFromRoot;
return File.Exists(fullPathToDotnetFromRoot) ? Path.GetDirectoryName(fullPathToDotnetFromRoot) : null;
}

return dotnetPath;
}

return null;
Expand Down
5 changes: 3 additions & 2 deletions src/MSBuildLocator/Microsoft.Build.Locator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net46;net7.0</TargetFrameworks>
<TargetFrameworks>net46;net6.0</TargetFrameworks>
<DebugType>full</DebugType>

<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
<AssemblyName>Microsoft.Build.Locator</AssemblyName>
<RootNamespace>Microsoft.Build.Locator</RootNamespace>
<SignAssembly>true</SignAssembly>
Expand Down Expand Up @@ -36,6 +36,7 @@
<PackagePath>build\</PackagePath>
</Content>
</ItemGroup>

<ItemGroup>
<FilesToSign Include="$(OutDir)\Microsoft.Build.Locator.dll">
<Authenticode>Microsoft400</Authenticode>
Expand Down

0 comments on commit b452d73

Please sign in to comment.