From 627cc6e79a5f7d418b85e92c16c4107088a4ab2e Mon Sep 17 00:00:00 2001 From: Forgind Date: Thu, 16 Mar 2023 11:53:01 -0700 Subject: [PATCH 1/4] Validate dotnet executable exists --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 3e6e86cc..982bd9e7 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -102,14 +102,17 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) string filePath = Path.Combine(dir, exeName); if (File.Exists(Path.Combine(dir, exeName))) { - dotnetPath = filePath; - break; + dotnetPath = Path.GetDirectoryName(isWindows ? filePath : realpath(filePath) ?? filePath); + if (File.Exists(dotnetPath)) + { + break; + } } } - if (dotnetPath != null) + if (dotnetPath is null) { - dotnetPath = Path.GetDirectoryName(isWindows ? dotnetPath : realpath(dotnetPath) ?? dotnetPath); + throw new InvalidOperationException("Could not find the dotnet executable. Is it on the PATH?"); } string bestSDK = null; From 77acedd03958d45ab2905e9a211c5401a9b23595 Mon Sep 17 00:00:00 2001 From: Forgind Date: Thu, 16 Mar 2023 12:57:50 -0700 Subject: [PATCH 2/4] PR comment --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 982bd9e7..302e16e0 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -102,9 +102,10 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) string filePath = Path.Combine(dir, exeName); if (File.Exists(Path.Combine(dir, exeName))) { - dotnetPath = Path.GetDirectoryName(isWindows ? filePath : realpath(filePath) ?? filePath); - if (File.Exists(dotnetPath)) + filePath = Path.GetDirectoryName(isWindows ? filePath : realpath(filePath) ?? filePath); + if (File.Exists(Path.Combine(filePath, exeName))) { + dotnetPath = filePath; break; } } From 8c4fa97a5aad77d8e40f8430b374f6cbebb2369d Mon Sep 17 00:00:00 2001 From: Forgind <12969783+Forgind@users.noreply.github.com> Date: Mon, 20 Mar 2023 08:08:51 -0700 Subject: [PATCH 3/4] Update src/MSBuildLocator/DotNetSdkLocationHelper.cs Co-authored-by: Ladi Prosek --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 302e16e0..535a83fe 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -100,7 +100,7 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) foreach (string dir in Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator)) { string filePath = Path.Combine(dir, exeName); - if (File.Exists(Path.Combine(dir, exeName))) + if (File.Exists(filePath)) { filePath = Path.GetDirectoryName(isWindows ? filePath : realpath(filePath) ?? filePath); if (File.Exists(Path.Combine(filePath, exeName))) From df5174ca442e356e1df5a40f1cec798a5829d7ae Mon Sep 17 00:00:00 2001 From: Forgind Date: Mon, 3 Apr 2023 11:43:26 -0700 Subject: [PATCH 4/4] Simplify logic Also avoids an unnecessary File.Exists check on Windows --- src/MSBuildLocator/DotNetSdkLocationHelper.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/MSBuildLocator/DotNetSdkLocationHelper.cs b/src/MSBuildLocator/DotNetSdkLocationHelper.cs index 535a83fe..116ab6cf 100644 --- a/src/MSBuildLocator/DotNetSdkLocationHelper.cs +++ b/src/MSBuildLocator/DotNetSdkLocationHelper.cs @@ -102,12 +102,17 @@ private static IEnumerable GetDotNetBasePaths(string workingDirectory) string filePath = Path.Combine(dir, exeName); if (File.Exists(filePath)) { - filePath = Path.GetDirectoryName(isWindows ? filePath : realpath(filePath) ?? filePath); - if (File.Exists(Path.Combine(filePath, exeName))) + if (!isWindows) { - dotnetPath = filePath; - break; + filePath = realpath(filePath) ?? filePath; + if (!File.Exists(filePath)) + { + continue; + } } + + dotnetPath = Path.GetDirectoryName(filePath); + break; } }