Skip to content

Commit 29b9b3e

Browse files
committed
Refactor bsc finding logic
1 parent 4f59208 commit 29b9b3e

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

server/src/constants.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import * as path from "path";
33
// See https://microsoft.github.io/language-server-protocol/specification Abstract Message
44
// version is fixed to 2.0
55
export let jsonrpcVersion = "2.0";
6-
export let bscExePartialPath = path.join(
6+
export let bscNativeReScriptPartialPath = path.join(
77
"node_modules",
8-
"bs-platform",
8+
"rescript",
99
process.platform,
1010
"bsc.exe"
1111
);
12-
export let bscExeReScriptPartialPath = path.join(
12+
export let bscNativePartialPath = path.join(
1313
"node_modules",
14-
"rescript",
14+
"bs-platform",
1515
process.platform,
1616
"bsc.exe"
1717
);

server/src/server.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,13 @@ function onMessage(msg: m.Message) {
466466
send(fakeSuccessResponse);
467467
send(response);
468468
} else {
469-
// See comment on findBscExeDirOfFile for why we need
469+
// See comment on findBscNativeDirOfFile for why we need
470470
// to recursively search for bsc.exe upward
471-
let bscExeDir = utils.findBscExeDirOfFile(filePath);
472-
if (bscExeDir === null) {
471+
let bscNativePath = utils.findBscNativeOfFile(filePath);
472+
if (bscNativePath === null) {
473473
let params: p.ShowMessageParams = {
474474
type: p.MessageType.Error,
475-
message: `Cannot find a nearby bsc.exe in bs-platform or rescript. It's needed for formatting.`,
475+
message: `Cannot find a nearby bsc.exe in rescript or bs-platform. It's needed for formatting.`,
476476
};
477477
let response: m.NotificationMessage = {
478478
jsonrpc: c.jsonrpcVersion,
@@ -482,16 +482,11 @@ function onMessage(msg: m.Message) {
482482
send(fakeSuccessResponse);
483483
send(response);
484484
} else {
485-
let bscExePath1 = path.join(bscExeDir, c.bscExeReScriptPartialPath);
486-
let bscExePath2 = path.join(bscExeDir, c.bscExePartialPath);
487-
let resolvedBscExePath = fs.existsSync(bscExePath1)
488-
? bscExePath1
489-
: bscExePath2;
490485
// code will always be defined here, even though technically it can be undefined
491486
let code = getOpenedFileContent(params.textDocument.uri);
492-
let formattedResult = utils.formatUsingValidBscExePath(
487+
let formattedResult = utils.formatUsingValidBscNativePath(
493488
code,
494-
resolvedBscExePath,
489+
bscNativePath,
495490
extension === c.resiExt
496491
);
497492
if (formattedResult.kind === "success") {

server/src/utils.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import * as path from "path";
55
import * as t from "vscode-languageserver-types";
66
import fs from "fs";
77
import * as os from "os";
8-
import { fileURLToPath } from "url";
9-
import { RequestMessage } from "vscode-languageserver-protocol";
108

119
let tempFilePrefix = "rescript_format_file_" + process.pid + "_";
1210
let tempFileId = 0;
@@ -45,21 +43,25 @@ export let findProjectRootOfFile = (
4543
// Also, if someone's ever formatting a regular project setup's dependency
4644
// (which is weird but whatever), they'll at least find an upward bs-platform
4745
// from the dependent.
48-
export let findBscExeDirOfFile = (
46+
export let findBscNativeOfFile = (
4947
source: p.DocumentUri
5048
): null | p.DocumentUri => {
5149
let dir = path.dirname(source);
52-
let bscExePath1 = path.join(dir, c.bscExeReScriptPartialPath);
53-
let bscExePath2 = path.join(dir, c.bscExePartialPath);
54-
if (fs.existsSync(bscExePath1) || fs.existsSync(bscExePath2)) {
55-
return dir;
50+
// The rescript package's rescript command is a JS wrapper. `rescript format`
51+
// also invokes another JS wrapper. _That_ JS wrapper ultimately calls the
52+
// (unexposed) bsc -format anyway.
53+
let bscNativeReScriptPath = path.join(dir, c.bscNativeReScriptPartialPath);
54+
let bscNativePath = path.join(dir, c.bscNativePartialPath);
55+
56+
if (fs.existsSync(bscNativeReScriptPath)) {
57+
return bscNativeReScriptPath
58+
} else if (fs.existsSync(bscNativePath)) {
59+
return bscNativePath
60+
} else if (dir === source) {
61+
// reached the top
62+
return null
5663
} else {
57-
if (dir === source) {
58-
// reached the top
59-
return null;
60-
} else {
61-
return findBscExeDirOfFile(dir);
62-
}
64+
return findBscNativeOfFile(dir);
6365
}
6466
};
6567

@@ -72,9 +74,9 @@ type execResult =
7274
kind: "error";
7375
error: string;
7476
};
75-
export let formatUsingValidBscExePath = (
77+
export let formatUsingValidBscNativePath = (
7678
code: string,
77-
bscExePath: p.DocumentUri,
79+
bscNativePath: p.DocumentUri,
7880
isInterface: boolean
7981
): execResult => {
8082
let extension = isInterface ? c.resiExt : c.resExt;
@@ -83,7 +85,7 @@ export let formatUsingValidBscExePath = (
8385
encoding: "utf-8",
8486
});
8587
try {
86-
let result = childProcess.execFileSync(bscExePath, [
88+
let result = childProcess.execFileSync(bscNativePath, [
8789
"-color",
8890
"never",
8991
"-format",

0 commit comments

Comments
 (0)