Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look up and propagate ReScript version #867

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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