Skip to content

Commit 0131b59

Browse files
committed
run from appropriate bins also on the LS client
1 parent 3b57c41 commit 0131b59

File tree

3 files changed

+75
-26
lines changed

3 files changed

+75
-26
lines changed

client/src/commands/code_analysis.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
OutputChannel,
1515
StatusBarItem,
1616
} from "vscode";
17-
import { analysisProdPath, getAnalysisBinaryPath } from "../utils";
17+
import { findProjectRootOfFileInDir, getBinaryPath } from "../utils";
1818

1919
export let statusBarItem = {
2020
setToStopText: (codeAnalysisRunningStatusBarItem: StatusBarItem) => {
@@ -208,9 +208,12 @@ export const runCodeAnalysisWithReanalyze = (
208208
let currentDocument = window.activeTextEditor.document;
209209
let cwd = targetDir ?? path.dirname(currentDocument.uri.fsPath);
210210

211-
let binaryPath = getAnalysisBinaryPath();
211+
let projectRootPath: string | null = findProjectRootOfFileInDir(
212+
currentDocument.uri.fsPath
213+
);
214+
let binaryPath = getBinaryPath("rescript-tools.exe", projectRootPath);
212215
if (binaryPath === null) {
213-
window.showErrorMessage("Binary executable not found.", analysisProdPath);
216+
window.showErrorMessage("Binary executable not found.");
214217
return;
215218
}
216219

client/src/commands/dump_debug.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
ViewColumn,
88
window,
99
} from "vscode";
10-
import { createFileInTempDir, getAnalysisBinaryPath } from "../utils";
10+
import {
11+
createFileInTempDir,
12+
findProjectRootOfFileInDir,
13+
getBinaryPath,
14+
} from "../utils";
1115
import * as path from "path";
1216

1317
// Maps to Cli.ml
@@ -132,7 +136,11 @@ export const dumpDebug = async (
132136
const { line: endLine, character: endChar } = editor.selection.end;
133137
const filePath = editor.document.uri.fsPath;
134138

135-
const binaryPath = getAnalysisBinaryPath();
139+
let projectRootPath: string | null = findProjectRootOfFileInDir(filePath);
140+
const binaryPath = getBinaryPath(
141+
"rescript-editor-analysis.exe",
142+
projectRootPath
143+
);
136144
if (binaryPath === null) {
137145
window.showErrorMessage("Binary executable not found.");
138146
return;

client/src/utils.ts

+59-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
import * as path from "path";
22
import * as fs from "fs";
33
import * as os from "os";
4+
import { DocumentUri } from "vscode-languageclient";
5+
6+
/*
7+
* Much of the code in here is duplicated from the server code.
8+
* At some point we should move the functionality powered by this
9+
* to the server itself.
10+
*/
11+
12+
type binaryName = "rescript-editor-analysis.exe" | "rescript-tools.exe";
413

514
const platformDir =
615
process.arch === "arm64" ? process.platform + process.arch : process.platform;
716

8-
const analysisDevPath = path.join(
9-
path.dirname(__dirname),
10-
"..",
11-
"analysis",
12-
"rescript-editor-analysis.exe"
13-
);
14-
15-
export const analysisProdPath = path.join(
16-
path.dirname(__dirname),
17-
"..",
18-
"server",
19-
"analysis_binaries",
20-
platformDir,
21-
"rescript-editor-analysis.exe"
22-
);
23-
24-
export const getAnalysisBinaryPath = (): string | null => {
25-
if (fs.existsSync(analysisDevPath)) {
26-
return analysisDevPath;
27-
} else if (fs.existsSync(analysisProdPath)) {
28-
return analysisProdPath;
17+
const getLegacyBinaryDevPath = (b: binaryName) =>
18+
path.join(path.dirname(__dirname), "..", "analysis", b);
19+
20+
export const getLegacyBinaryProdPath = (b: binaryName) =>
21+
path.join(
22+
path.dirname(__dirname),
23+
"..",
24+
"server",
25+
"analysis_binaries",
26+
platformDir,
27+
b
28+
);
29+
30+
export const getBinaryPath = (
31+
binaryName: "rescript-editor-analysis.exe" | "rescript-tools.exe",
32+
projectRootPath: string
33+
): string | null => {
34+
const binaryFromCompilerPackage = path.join(
35+
projectRootPath,
36+
"node_modules",
37+
"rescript",
38+
platformDir,
39+
binaryName
40+
);
41+
42+
if (fs.existsSync(binaryFromCompilerPackage)) {
43+
return binaryFromCompilerPackage;
44+
} else if (fs.existsSync(getLegacyBinaryDevPath(binaryName))) {
45+
return getLegacyBinaryDevPath(binaryName);
46+
} else if (fs.existsSync(getLegacyBinaryProdPath(binaryName))) {
47+
return getLegacyBinaryProdPath(binaryName);
2948
} else {
3049
return null;
3150
}
@@ -39,3 +58,22 @@ export const createFileInTempDir = (prefix = "", extension = "") => {
3958
tempFileId = tempFileId + 1;
4059
return path.join(os.tmpdir(), tempFileName);
4160
};
61+
62+
export let findProjectRootOfFileInDir = (
63+
source: DocumentUri
64+
): null | DocumentUri => {
65+
let dir = path.dirname(source);
66+
if (
67+
fs.existsSync(path.join(dir, "rescript.json")) ||
68+
fs.existsSync(path.join(dir, "bsconfig.json"))
69+
) {
70+
return dir;
71+
} else {
72+
if (dir === source) {
73+
// reached top
74+
return null;
75+
} else {
76+
return findProjectRootOfFileInDir(dir);
77+
}
78+
}
79+
};

0 commit comments

Comments
 (0)