Skip to content

Commit 2258d8e

Browse files
committed
Make server.findRescriptBinary async
1 parent 190696d commit 2258d8e

File tree

3 files changed

+21
-35
lines changed

3 files changed

+21
-35
lines changed

server/src/constants.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ export let bscBinName = "bsc";
2929

3030
export let nodeModulesBinDir = path.join("node_modules", ".bin");
3131

32-
// can't use the native bsb/rescript since we might need the watcher -w flag, which is only in the JS wrapper
33-
export let rescriptNodePartialPath = path.join(
34-
nodeModulesBinDir,
35-
rescriptBinName
36-
);
37-
3832
export let bsbLock = ".bsb.lock";
3933
export let bsconfigPartialPath = "bsconfig.json";
4034
export let rescriptJsonPartialPath = "rescript.json";

server/src/server.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,14 @@ let codeActionsFromDiagnostics: codeActions.filesCodeActions = {};
5656
// will be properly defined later depending on the mode (stdio/node-rpc)
5757
let send: (msg: p.Message) => void = (_) => {};
5858

59-
let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
60-
config.extensionConfiguration.binaryPath == null
61-
? lookup.findFilePathFromProjectRoot(
62-
projectRootPath,
63-
path.join(c.nodeModulesBinDir, c.rescriptBinName)
64-
)
65-
: utils.findBinary(
66-
config.extensionConfiguration.binaryPath,
67-
c.rescriptBinName
68-
);
59+
let findRescriptBinary = async (projectRootPath: p.DocumentUri | null): Promise<string | null> => {
60+
if (config.extensionConfiguration.binaryPath != null &&
61+
fs.existsSync(path.join(config.extensionConfiguration.binaryPath, "rescript"))) {
62+
return path.join(config.extensionConfiguration.binaryPath, "rescript")
63+
}
64+
65+
return utils.findRescriptBinary(projectRootPath)
66+
}
6967

7068
let createInterfaceRequest = new v.RequestType<
7169
p.TextDocumentIdentifier,
@@ -305,7 +303,7 @@ let openedFile = async (fileUri: string, fileContent: string) => {
305303
// TODO: sometime stale .bsb.lock dangling. bsb -w knows .bsb.lock is
306304
// stale. Use that logic
307305
// TODO: close watcher when lang-server shuts down
308-
if (findRescriptBinary(projectRootPath) != null) {
306+
if (await findRescriptBinary(projectRootPath) != null) {
309307
let payload: clientSentBuildAction = {
310308
title: c.startBuildAction,
311309
projectRootPath: projectRootPath,
@@ -1311,7 +1309,7 @@ async function onMessage(msg: p.Message) {
13111309
// TODO: close watcher when lang-server shuts down. However, by Node's
13121310
// default, these subprocesses are automatically killed when this
13131311
// language-server process exits
1314-
let rescriptBinaryPath = findRescriptBinary(projectRootPath);
1312+
let rescriptBinaryPath = await findRescriptBinary(projectRootPath);
13151313
if (rescriptBinaryPath != null) {
13161314
let bsbProcess = utils.runBuildWatcherUsingValidBuildPath(
13171315
rescriptBinaryPath,

server/src/utils.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,12 @@ export let findProjectRootOfFile = (
7676
}
7777
};
7878

79-
// Check if binaryName exists inside binaryDirPath and return the joined path.
80-
export let findBinary = (
81-
binaryDirPath: p.DocumentUri | null,
82-
binaryName: string
83-
): p.DocumentUri | null => {
84-
if (binaryDirPath == null) {
85-
return null;
86-
}
87-
let binaryPath: p.DocumentUri = path.join(binaryDirPath, binaryName);
88-
if (fs.existsSync(binaryPath)) {
89-
return binaryPath;
90-
} else {
91-
return null;
92-
}
93-
};
9479
// If ReScript < 12.0.0-alpha.13, then we want `{project_root}/node_modules/rescript/{c.platformDir}/{binary}`.
9580
// Otherwise, we want to dynamically import `{project_root}/node_modules/rescript` and from `binPaths` get the relevant binary.
9681
// We won't know which version is in the project root until we read and parse `{project_root}/node_modules/rescript/package.json`
9782
let findBinaryAsync = async (
9883
projectRootPath: p.DocumentUri | null,
99-
binary: "bsc.exe" | "rescript-editor-analysis.exe"
84+
binary: "bsc.exe" | "rescript-editor-analysis.exe" | "rescript"
10085
) => {
10186
if (config.extensionConfiguration.platformPath != null) {
10287
return path.join(config.extensionConfiguration.platformPath, binary);
@@ -111,16 +96,22 @@ let findBinaryAsync = async (
11196
}
11297

11398
let rescriptVersion = null;
99+
let rescriptJSWrapperPath = null
114100
try {
115101
const rescriptPackageJSONPath = path.join(rescriptDir, "package.json");
116102
const rescriptPackageJSON = JSON.parse(await fsAsync.readFile(rescriptPackageJSONPath, "utf-8"));
117103
rescriptVersion = rescriptPackageJSON.version
104+
rescriptJSWrapperPath = rescriptPackageJSON.bin.rescript
118105
} catch (error) {
119106
return null
120107
}
121108

122109
let binaryPath: string | null = null
123-
if (semver.gte(rescriptVersion, "12.0.0-alpha.13")) {
110+
if (binary == "rescript") {
111+
// Can't use the native bsb/rescript since we might need the watcher -w
112+
// flag, which is only in the JS wrapper
113+
binaryPath = path.join(rescriptDir, rescriptJSWrapperPath)
114+
} else if (semver.gte(rescriptVersion, "12.0.0-alpha.13")) {
124115
// TODO: export `binPaths` from `rescript` package so that we don't need to
125116
// copy the logic for figuring out `target`.
126117
const target = `${process.platform}-${process.arch}`;
@@ -143,6 +134,9 @@ let findBinaryAsync = async (
143134
}
144135
}
145136

137+
export let findRescriptBinary = (projectRootPath: p.DocumentUri | null) =>
138+
findBinaryAsync(projectRootPath, "rescript");
139+
146140
export let findBscExeBinary = (projectRootPath: p.DocumentUri | null) =>
147141
findBinaryAsync(projectRootPath, "bsc.exe");
148142

0 commit comments

Comments
 (0)