From 04ff9aa1dc3a24aaa57447eaac9b08176ede61ba Mon Sep 17 00:00:00 2001 From: Gabriel Nordeborn Date: Fri, 15 Dec 2023 11:49:20 +0100 Subject: [PATCH] Look up and propagate ReScript version (#867) * look up and propagate ReScript version to analysis so we can default uncurried by default properly * revert test changes * changelog --- CHANGELOG.md | 1 + analysis/src/Packages.ml | 22 +++++++++++++++++++++- analysis/src/SharedTypes.ml | 1 + server/src/utils.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec806af61..efcfa340a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ #### :bug: Bug Fix +- Proper default for `"uncurried"` in V11 projects. https://github.com/rescript-lang/rescript-vscode/pull/867 - Treat `result` type as a proper built in type. https://github.com/rescript-lang/rescript-vscode/pull/860 #### :nail_care: Polish diff --git a/analysis/src/Packages.ml b/analysis/src/Packages.ml index ae0ea1e7a..b330eac99 100644 --- a/analysis/src/Packages.ml +++ b/analysis/src/Packages.ml @@ -11,6 +11,22 @@ let makePathsForModule ~projectFilesAndPaths ~dependenciesFilesAndPaths = Hashtbl.replace pathsForModule modName paths); pathsForModule +let getReScriptVersion () = + (* TODO: Include patch stuff when needed *) + let defaultVersion = (10, 1) in + try + let value = Sys.getenv "RESCRIPT_VERSION" in + let version = + match value |> String.split_on_char '.' with + | major :: minor :: _rest -> ( + match (int_of_string_opt major, int_of_string_opt minor) with + | Some major, Some minor -> (major, minor) + | _ -> defaultVersion) + | _ -> defaultVersion + in + version + with Not_found -> defaultVersion + let newBsPackage ~rootPath = let rescriptJson = Filename.concat rootPath "rescript.json" in let bsconfigJson = Filename.concat rootPath "bsconfig.json" in @@ -27,9 +43,12 @@ let newBsPackage ~rootPath = | Some libBs -> Some (let namespace = FindFiles.getNamespace config in + let rescriptVersion = getReScriptVersion () in let uncurried = let ns = config |> Json.get "uncurried" in - Option.bind ns Json.bool + match (rescriptVersion, ns) with + | (major, _), None when major >= 11 -> Some true + | _, ns -> Option.bind ns Json.bool in let uncurried = uncurried = Some true in let sourceDirectories = @@ -93,6 +112,7 @@ let newBsPackage ~rootPath = ("Opens from ReScript config file: " ^ (opens |> List.map pathToString |> String.concat " ")); { + rescriptVersion; rootPath; projectFiles = projectFilesAndPaths |> List.map fst |> FileSet.of_list; diff --git a/analysis/src/SharedTypes.ml b/analysis/src/SharedTypes.ml index 5cbbce741..7917509f0 100644 --- a/analysis/src/SharedTypes.ml +++ b/analysis/src/SharedTypes.ml @@ -493,6 +493,7 @@ type package = { builtInCompletionModules: builtInCompletionModules; opens: path list; uncurried: bool; + rescriptVersion: int * int; } let allFilesInPackage package = diff --git a/server/src/utils.ts b/server/src/utils.ts index a6adf8ec7..0c3b27872 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -136,6 +136,29 @@ export let formatCode = ( } }; +let findReScriptVersion = (filePath: p.DocumentUri): string | undefined => { + let projectRoot = findProjectRootOfFile(filePath); + if (projectRoot == null) { + return undefined; + } + + let rescriptBinary = lookup.findFilePathFromProjectRoot( + projectRoot, + path.join(c.nodeModulesBinDir, c.rescriptBinName) + ); + + if (rescriptBinary == null) { + return undefined; + } + + try { + let version = childProcess.execSync(`${rescriptBinary} -v`); + return version.toString().trim(); + } catch (e) { + return undefined; + } +}; + export let runAnalysisAfterSanityCheck = ( filePath: p.DocumentUri, args: Array, @@ -154,9 +177,14 @@ export let runAnalysisAfterSanityCheck = ( if (projectRootPath == null && projectRequired) { return null; } + let rescriptVersion = findReScriptVersion(filePath); let options: childProcess.ExecFileSyncOptions = { cwd: projectRootPath || undefined, maxBuffer: Infinity, + env: { + ...process.env, + RESCRIPT_VERSION: rescriptVersion, + }, }; let stdout = childProcess.execFileSync(binaryPath, args, options);