Skip to content

Commit

Permalink
set rollforward args and env vars if .net 6.0 is detected (#1559)
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel authored Aug 5, 2021
1 parent 363248c commit 907d095
Show file tree
Hide file tree
Showing 6 changed files with 459 additions and 15 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
{
"scripts": {},
"scripts": {
"gen:semver": "ts2fable node_modules/@types/semver/{classes,functions,internals,ranges}/*.d.ts node_modules/@types/semver/index.d.ts src/Imports/Semver.fs"
},
"dependencies": {
"htmlparser2": "^4.1.0"
"htmlparser2": "^4.1.0",
"semver": "^7.3.5"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/plugin-transform-runtime": "^7.11.5",
"@babel/preset-env": "^7.11.5",
"@types/semver": "^7.3.8",
"@types/showdown": "^1.9.3",
"@types/vscode": "^1.52.0",
"@types/ws": "^7.2.6",
Expand All @@ -15,6 +19,7 @@
"mocha": "^8.1.3",
"showdown": "^1.9.1",
"toml": "^3.0.0",
"ts2fable": "^0.8.0-build.618",
"vscode-debugadapter": "^1.44.0",
"vscode-languageclient": "^7.0.0",
"webpack": "^4.44.1",
Expand Down
101 changes: 90 additions & 11 deletions src/Core/LanguageService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ open Fable.Import.VSCode
open Fable.Import.VSCode.Vscode
open global.Node
open Ionide.VSCode.Helpers
open Semver

open DTO
open LanguageServer
Expand Down Expand Up @@ -72,6 +73,23 @@ module LanguageService =

let private handleUntitled (fn : string) = if fn.EndsWith ".fs" || fn.EndsWith ".fsi" || fn.EndsWith ".fsx" then fn else (fn + ".fsx")

/// runs `dotnet --version` in the current rootPath to determine the resolved sdk version from the global.json file.
let runtimeVersion() = promise {
let! dotnet = Environment.dotnet
match dotnet with
| None -> return Core.Error "No dotnet binary found"
| Some dotnet ->
let! (error, stdout, stderr) = Process.exec dotnet "" "--version"
match error with
| Some e -> return Core.Error $"error while invoking: {e.message}"
| None ->
let stdoutTrimmed = stdout.TrimEnd()
let semver = semver.parse(Some (U2.Case1 stdoutTrimmed))
match semver with
| None -> return Core.Error $"unable to parse version string '{stdoutTrimmed}'"
| Some semver -> return Core.Ok semver
}


let compilerLocation () =
match client with
Expand Down Expand Up @@ -593,7 +611,7 @@ Consider:
return Environment.configFsiSdkFilePath ()
}

let private createClient opts =
let private createClient (opts: Executable) =
let options =
createObj [
"run" ==> opts
Expand Down Expand Up @@ -630,7 +648,7 @@ Consider:
client <- Some cl
cl

let getOptions () = promise {
let getOptions (): JS.Promise<Executable> = promise {

let dotnetNotFound () = promise {
let msg = """
Expand All @@ -650,13 +668,73 @@ Consider:
let fsacNetcorePath = "FSharp.fsac.netCoreDllPath" |> Configuration.get ""
let fsacSilencedLogs = "FSharp.fsac.silencedLogs" |> Configuration.get [||]
let verbose = "FSharp.verboseLogging" |> Configuration.get false
let fsacDotnetArgs = "FSharp.fsac.dotnetArgs" |> Configuration.get [||]
let spawnNetCore dotnet =



let discoverDotnetArgs () = promise {
let! (rollForwardArgs, necessaryEnvVariables) = promise {
let! sdkVersionAtRootPath = runtimeVersion()
match sdkVersionAtRootPath with
| Error e ->
printfn $"FSAC (NETCORE): {e}"
return [], []
| Ok v ->
if v.major >= 6.0
then
// when we run on a sdk higher than 5.x (aka what FSAC is currently built/targeted for),
// we have to tell the runtime to allow it to actually run on that runtime (instead of presenting 6.x as 5.x)
// in order for msbuild resolution to work
let args = ["--roll-forward"; "LatestMajor"]
let envs =
if v.prerelease <> null || v.prerelease.Count > 0
then ["DOTNET_ROLL_FORWARD_TO_PRERELEASE", box 1]
else []
return args, envs
else return [], []
}
let userDotnetArgs = "FSharp.fsac.dotnetArgs" |> Configuration.get [||]
let hasUserRollForward = userDotnetArgs |> Array.tryFindIndex (fun a -> a = "--roll-forward") |> Option.map (fun _ -> true) |> Option.defaultValue false
let hasUserFxVersion = userDotnetArgs |> Array.tryFindIndex (fun a -> a = "--fx-version") |> Option.map (fun _ -> true) |> Option.defaultValue false
let shouldApplyImplicitRollForward = not (hasUserFxVersion || hasUserRollForward)

let args =
[
if shouldApplyImplicitRollForward then yield! rollForwardArgs
yield! userDotnetArgs
]
let envVariables =
[
if shouldApplyImplicitRollForward then yield! necessaryEnvVariables
]

return args, envVariables
}

let spawnNetCore dotnet: JS.Promise<Executable> = promise {
let fsautocompletePath =
if String.IsNullOrEmpty fsacNetcorePath
then VSCodeExtension.ionidePluginPath () + "/bin/fsautocomplete.dll"
else fsacNetcorePath
printfn "FSAC (NETCORE): '%s'" fsautocompletePath

let! (fsacDotnetArgs, fsacEnvVars) = discoverDotnetArgs()

printfn $"FSAC (NETCORE): '%s{fsautocompletePath}'"

let exeOpts = createEmpty<ExecutableOptions>
let exeEnv =
match fsacEnvVars with
| [] -> None
| fsacEnvVars ->
// only need to set the process env if FSAC needs rollfoward env vars.
let keys = Node.Util.Object.keys process.env
let baseEnv = keys |> Seq.toList |> List.map (fun k -> k, process.env?(k))
let combinedEnv =
baseEnv @ fsacEnvVars
|> ResizeArray
let envObj = createObj combinedEnv
Some envObj
exeOpts.env <- exeEnv

let args =
[ yield! fsacDotnetArgs
yield fsautocompletePath
Expand All @@ -673,11 +751,12 @@ Consider:
yield! fsacSilencedLogs
] |> ResizeArray

createObj [
"command" ==> dotnet
"args" ==> args
"transport" ==> 0
]
let executable = createEmpty<Executable>
executable.command <- dotnet
executable.args <- Some args
executable.options <- Some exeOpts
return executable
}

let! dotnet = Environment.dotnet

Expand All @@ -686,7 +765,7 @@ Consider:

// dotnet SDK handling
| Some dotnet ->
return spawnNetCore dotnet
return! spawnNetCore dotnet
| None ->
return! dotnetNotFound ()

Expand Down
10 changes: 9 additions & 1 deletion src/Imports/Node.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ type IExports =
abstract member format : format : string * [<ParamArray>] args : obj[] -> string

[<Import("*", "util")>]
let Util : IExports = jsNative
let Util : IExports = jsNative


type ObjectExports =
abstract member keys: o: obj -> ResizeArray<string>

module Object =
[<Emit("Object.keys($0)")>]
let keys (o): ResizeArray<string> = jsNative
Loading

0 comments on commit 907d095

Please sign in to comment.