From 62567257d7bcbc408a4d830c73a21d34b7ce3be0 Mon Sep 17 00:00:00 2001 From: YuliiaKovalova Date: Wed, 23 Aug 2023 10:33:22 +0200 Subject: [PATCH 1/2] upgrade framework version + fix issue with path extraction from environment variable --- samples/BuilderApp/BuilderApp.csproj | 2 +- .../Microsoft.Build.Locator.Tests.csproj | 2 +- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 14 ++++++++++---- src/MSBuildLocator/Microsoft.Build.Locator.csproj | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/samples/BuilderApp/BuilderApp.csproj b/samples/BuilderApp/BuilderApp.csproj index f8adc233..9035d7de 100644 --- a/samples/BuilderApp/BuilderApp.csproj +++ b/samples/BuilderApp/BuilderApp.csproj @@ -2,7 +2,7 @@ Exe - net472;netcoreapp3.1;net6.0 + net472;net6.0 false false diff --git a/src/MSBuildLocator.Tests/Microsoft.Build.Locator.Tests.csproj b/src/MSBuildLocator.Tests/Microsoft.Build.Locator.Tests.csproj index efc837d5..4114e127 100644 --- a/src/MSBuildLocator.Tests/Microsoft.Build.Locator.Tests.csproj +++ b/src/MSBuildLocator.Tests/Microsoft.Build.Locator.Tests.csproj @@ -1,7 +1,7 @@ - net472;netcoreapp3.1 + net472;net6.0 false true ..\MSBuildLocator\key.snk diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 12f65aad..044216bd 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -17,6 +17,7 @@ internal static class DotNetSdkLocationHelper private static readonly Regex DotNetBasePathRegex = new Regex("Base Path:(.*)$", RegexOptions.Multiline); private static readonly Regex VersionRegex = new Regex(@"^(\d+)\.(\d+)\.(\d+)", RegexOptions.Multiline); private static readonly Regex SdkRegex = new Regex(@"(\S+) \[(.*?)]$", RegexOptions.Multiline); + private static bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); public static VisualStudioInstance GetInstance(string dotNetSdkPath) { @@ -98,7 +99,13 @@ private static string FindDotnetFromEnvironmentVariable(string environmentVariab 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) ? dotnet_root : null; + } + + return dotnet_root; } } @@ -108,8 +115,7 @@ private static string FindDotnetFromEnvironmentVariable(string environmentVariab private static IEnumerable GetDotNetBasePaths(string workingDirectory) { string dotnetPath = null; - bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - string exeName = isWindows ? "dotnet.exe" : "dotnet"; + string exeName = IsWindows ? "dotnet.exe" : "dotnet"; // First check for the DOTNET_ROOT environment variable, as it's often there as with, for example, dotnet format. if (IntPtr.Size == 4) @@ -135,7 +141,7 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) string filePath = Path.Combine(dir, exeName); if (File.Exists(filePath)) { - if (!isWindows) + if (!IsWindows) { filePath = realpath(filePath) ?? filePath; if (!File.Exists(filePath)) diff --git a/src/MSBuildLocator/Microsoft.Build.Locator.csproj b/src/MSBuildLocator/Microsoft.Build.Locator.csproj index bdc05282..056f7ca5 100644 --- a/src/MSBuildLocator/Microsoft.Build.Locator.csproj +++ b/src/MSBuildLocator/Microsoft.Build.Locator.csproj @@ -2,7 +2,7 @@ Library - net46;netcoreapp3.1 + net46;net6.0 full false From 20af00a8a63c4ffd366aeed035c7c1c960f183a8 Mon Sep 17 00:00:00 2001 From: YuliiaKovalova Date: Wed, 23 Aug 2023 11:22:28 +0200 Subject: [PATCH 2/2] fix review comments --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 044216bd..fa41f87d 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -17,7 +17,7 @@ internal static class DotNetSdkLocationHelper private static readonly Regex DotNetBasePathRegex = new Regex("Base Path:(.*)$", RegexOptions.Multiline); private static readonly Regex VersionRegex = new Regex(@"^(\d+)\.(\d+)\.(\d+)", RegexOptions.Multiline); private static readonly Regex SdkRegex = new Regex(@"(\S+) \[(.*?)]$", RegexOptions.Multiline); - private static bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + private static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); public static VisualStudioInstance GetInstance(string dotNetSdkPath) { @@ -102,7 +102,7 @@ private static string FindDotnetFromEnvironmentVariable(string environmentVariab if (!IsWindows) { fullPathToDotnetFromRoot = realpath(fullPathToDotnetFromRoot) ?? fullPathToDotnetFromRoot; - return File.Exists(fullPathToDotnetFromRoot) ? dotnet_root : null; + return File.Exists(fullPathToDotnetFromRoot) ? Path.GetDirectoryName(fullPathToDotnetFromRoot) : null; } return dotnet_root; @@ -144,7 +144,12 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) if (!IsWindows) { filePath = realpath(filePath) ?? filePath; - if (!File.Exists(filePath)) + if (File.Exists(filePath)) + { + dotnetPath = Path.GetDirectoryName(filePath); + break; + } + else { continue; }