From 2268d505970604f38738bda758ffac44a2a77e54 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Sat, 12 Aug 2023 14:00:18 +0800 Subject: [PATCH] Fsdk: fix VsWhere when no entries are returned Otherwise First() would fail with IOE: Sequence contains no elements. --- Fsdk/Process.fs | 11 ++++++----- fsxc/Fsxc.fs | 5 ++++- scripts/make.fsx | 4 +++- scripts/runTests.fsx | 5 ++++- test/testProcess.fsx | 4 +++- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Fsdk/Process.fs b/Fsdk/Process.fs index 9b88ba59..7da5298b 100644 --- a/Fsdk/Process.fs +++ b/Fsdk/Process.fs @@ -496,7 +496,7 @@ module Process = commandNamesByOrderOfPreference // FIXME: it returns the first result, but we should return all (array) - let VsWhere(searchPattern: string) : string = + let VsWhere(searchPattern: string) : Option = if Misc.GuessPlatform() <> Misc.Platform.Windows then failwith "vswhere.exe doesn't exist in other platforms than Windows" @@ -523,14 +523,15 @@ module Process = let procResult = Execute(vswhereCmd, Echo.Off) - let firstResult = + let entries = procResult .UnwrapDefault() .Split( Array.singleton Environment.NewLine, StringSplitOptions.RemoveEmptyEntries ) - .First() - .Trim() - firstResult + if entries.Any() then + entries.First().Trim() |> Some + else + None diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 8f92ea8d..77cb91e8 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -788,7 +788,10 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } "dotnet" #else match Misc.GuessPlatform() with - | Misc.Platform.Windows -> Process.VsWhere "**\\fsc.exe" + | Misc.Platform.Windows -> + match Process.VsWhere "**\\fsc.exe" with + | None -> failwith "fsc.exe not found" + | Some fscExe -> fscExe | _ -> "fsharpc" #endif diff --git a/scripts/make.fsx b/scripts/make.fsx index 99cab4d4..42fc565e 100644 --- a/scripts/make.fsx +++ b/scripts/make.fsx @@ -91,7 +91,9 @@ let FindBuildTool() : string * string = #if !LEGACY_FRAMEWORK "dotnet", "build" #else - (Process.VsWhere "MSBuild\\**\\Bin\\MSBuild.exe"), String.Empty + match Process.VsWhere "MSBuild\\**\\Bin\\MSBuild.exe" with + | None -> failwith "msbuild not found?" + | Some msbuildExe -> msbuildExe, String.Empty #endif let BuildSolution diff --git a/scripts/runTests.fsx b/scripts/runTests.fsx index dc1a6da8..102ccd4a 100755 --- a/scripts/runTests.fsx +++ b/scripts/runTests.fsx @@ -72,7 +72,10 @@ let fsharpCompilerCommand = "dotnet" #else match Misc.GuessPlatform() with - | Misc.Platform.Windows -> Process.VsWhere "**\\fsc.exe" + | Misc.Platform.Windows -> + match Process.VsWhere "**\\fsc.exe" with + | None -> failwith "fsc.exe not found" + | Some fscExe -> fscExe | _ -> "fsharpc" #endif diff --git a/test/testProcess.fsx b/test/testProcess.fsx index a6e28a52..3dc35aab 100755 --- a/test/testProcess.fsx +++ b/test/testProcess.fsx @@ -26,7 +26,9 @@ let command = if Misc.GuessPlatform() = Misc.Platform.Windows then // HACK: we should call fsx here but then we would get this problem in // the tests: error FS0193: The process cannot access the file 'D:\a\fsx\fsx\test\bin\FSharp.Core.dll' because it is being used by another process. - Process.VsWhere "**\\fsi.exe" + match Process.VsWhere "**\\fsi.exe" with + | None -> failwith "fsi.exe not found" + | Some fsiExe -> fsiExe else // FIXME: extract PREFIX from build.config instead of assuming default "/usr/local/bin/fsx"