From 47d497260cbf7ac98f2cf3937292464e9a46d8b4 Mon Sep 17 00:00:00 2001 From: Severin Sverdvik Date: Wed, 5 Dec 2018 21:45:06 +0100 Subject: [PATCH 1/2] Take version into account when determining dotnet cli path when global.json is present --- src/app/Fake.DotNet.Cli/DotNet.fs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/app/Fake.DotNet.Cli/DotNet.fs b/src/app/Fake.DotNet.Cli/DotNet.fs index ed29898d802..f3d2eb08340 100644 --- a/src/app/Fake.DotNet.Cli/DotNet.fs +++ b/src/app/Fake.DotNet.Cli/DotNet.fs @@ -487,9 +487,17 @@ module DotNet = } static member Create() = { DotNetCliPath = + let version = try Some <| getSDKVersionFromGlobalJson() with _ -> None findPossibleDotnetCliPaths None - |> Seq.tryHead - // shouldn't hit this one because the previous two probe PATH... + |> Seq.tryFind (fun cliPath -> + match version with + | Some version -> + version + |> Path.combine "sdk" + |> Path.combine (Path.getDirectory cliPath) + |> Directory.Exists + | None -> true + ) |> Option.defaultWith (fun () -> if Environment.isUnix then "dotnet" else "dotnet.exe") WorkingDirectory = Directory.GetCurrentDirectory() CustomParams = None From cf3e157b340e62eab97a83f66ba1b044e4a14a25 Mon Sep 17 00:00:00 2001 From: Severin Sverdvik Date: Fri, 21 Dec 2018 15:51:44 +0100 Subject: [PATCH 2/2] Introduce tryGetSDKVersionFromGlobalJsonDir --- src/app/Fake.DotNet.Cli/DotNet.fs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/app/Fake.DotNet.Cli/DotNet.fs b/src/app/Fake.DotNet.Cli/DotNet.fs index f3d2eb08340..3b5324a9ef1 100644 --- a/src/app/Fake.DotNet.Cli/DotNet.fs +++ b/src/app/Fake.DotNet.Cli/DotNet.fs @@ -29,8 +29,8 @@ module DotNet = then "/usr/local/share/dotnet" else @"C:\Program Files\dotnet" - /// Gets the DotNet SDK from the global.json, starts searching in the given directory. - let internal getSDKVersionFromGlobalJsonDir startDir : string = + /// Tries to get the DotNet SDK from the global.json, starts searching in the given directory. Returns None if global.json is not found + let internal tryGetSDKVersionFromGlobalJsonDir startDir : string option = let globalJsonPaths rootDir = let rec loop (dir: DirectoryInfo) = seq { match dir.GetFiles "global.json" with @@ -43,21 +43,35 @@ module DotNet = match Seq.tryHead (globalJsonPaths startDir) with | None -> - failwithf "global.json not found" + None | Some globalJson -> try let content = File.ReadAllText globalJson.FullName let json = JObject.Parse content let sdk = json.Item("sdk") :?> JObject let version = sdk.Property("version").Value.ToString() - version + Some version with | exn -> failwithf "Could not parse `sdk.version` from global.json at '%s': %s" globalJson.FullName exn.Message + + /// Gets the DotNet SDK from the global.json, starts searching in the given directory. + let internal getSDKVersionFromGlobalJsonDir startDir : string = + tryGetSDKVersionFromGlobalJsonDir startDir + |> function + | Some version -> version + | None -> failwithf "global.json not found" + + /// Tries the DotNet SDK from the global.json + /// This file can exist in the working directory or any of the parent directories + /// Returns None if global.json is not found + let tryGetSDKVersionFromGlobalJson() : string option = + tryGetSDKVersionFromGlobalJsonDir "." + /// Gets the DotNet SDK from the global.json /// This file can exist in the working directory or any of the parent directories - let getSDKVersionFromGlobalJson() : string = getSDKVersionFromGlobalJsonDir "." - + let getSDKVersionFromGlobalJson() : string = + getSDKVersionFromGlobalJsonDir "." /// Get dotnet cli executable path. Probes the provided path first, then as a fallback tries the system PATH /// ## Parameters @@ -487,7 +501,7 @@ module DotNet = } static member Create() = { DotNetCliPath = - let version = try Some <| getSDKVersionFromGlobalJson() with _ -> None + let version = tryGetSDKVersionFromGlobalJson() findPossibleDotnetCliPaths None |> Seq.tryFind (fun cliPath -> match version with