Skip to content

Commit

Permalink
Look up and propagate ReScript version (#867)
Browse files Browse the repository at this point in the history
* look up and propagate ReScript version to analysis so we can default uncurried by default properly

* revert test changes

* changelog
  • Loading branch information
zth authored Dec 15, 2023
1 parent 23bcade commit 04ff9aa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion analysis/src/Packages.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions analysis/src/SharedTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ type package = {
builtInCompletionModules: builtInCompletionModules;
opens: path list;
uncurried: bool;
rescriptVersion: int * int;
}

let allFilesInPackage package =
Expand Down
28 changes: 28 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>,
Expand All @@ -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);

Expand Down

0 comments on commit 04ff9aa

Please sign in to comment.