Skip to content

Commit 9faaa30

Browse files
authored
Merge pull request #1129 from nojaf/compiler-info
Use compiler-info.json to find runtime and bsc
2 parents 01a4a81 + 891e9e3 commit 9faaa30

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
- Find `@rescript/runtime` for Rewatch compiler-args call. https://github.com/rescript-lang/rescript-vscode/pull/1125
2727
- Use `prepareRename` command (when a new enough ReScript version is used) to speed up the `rename` command. https://github.com/rescript-lang/rescript-vscode/pull/1124
28+
- Use `compiler-info.json` to find the `@rescript/runtime` and `bsc.exe` if available.
2829

2930
## 1.64.0
3031

server/src/bsc-args/rewatch.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async function getRuntimePath(
5757

5858
export async function getRewatchBscArgs(
5959
send: (msg: p.Message) => void,
60+
bscBinaryLocation: string | null,
6061
projectsFiles: Map<string, projectFiles>,
6162
entry: IncrementallyCompiledFileInfo,
6263
): Promise<RewatchCompilerArgs | null> {
@@ -124,12 +125,10 @@ export async function getRewatchBscArgs(
124125
"--compiler-args",
125126
entry.file.sourceFilePath,
126127
];
127-
const bscExe = await utils.findBscExeBinary(
128-
entry.project.workspaceRootPath,
129-
);
128+
130129
const env: NodeJS.ProcessEnv = {};
131-
if (bscExe != null) {
132-
env["RESCRIPT_BSC_EXE"] = bscExe;
130+
if (bscBinaryLocation != null) {
131+
env["RESCRIPT_BSC_EXE"] = bscBinaryLocation;
133132
}
134133

135134
// For ReScript >= 12.0.0-beta.11 we need to set RESCRIPT_RUNTIME

server/src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export let rescriptJsonPartialPath = "rescript.json";
3333
export let compilerDirPartialPath = path.join("lib", "bs");
3434
export let compilerOcamlDirPartialPath = path.join("lib", "ocaml");
3535
export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log");
36+
export let compilerInfoPartialPath = path.join(
37+
"lib",
38+
"bs",
39+
"compiler-info.json",
40+
);
3641
export let buildNinjaPartialPath = path.join("lib", "bs", "build.ninja");
3742
export let rewatchLockPartialPath = path.join("lib", "rewatch.lock");
3843
export let rescriptLockPartialPath = path.join("lib", "rescript.lock");

server/src/find-runtime.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { readdir, stat as statAsync } from "fs/promises";
1+
import { readdir, stat as statAsync, readFile } from "fs/promises";
22
import { join, resolve } from "path";
3+
import { compilerInfoPartialPath } from "./constants";
34

45
// Efficient parallel folder traversal to find node_modules directories
56
async function findNodeModulesDirs(
@@ -91,7 +92,19 @@ async function findRescriptRuntimeInAlternativeLayout(
9192
return results;
9293
}
9394

94-
async function findRuntimePath(project: string) {
95+
async function findRuntimePath(project: string): Promise<string[]> {
96+
// Try a compiler-info.json file first
97+
const compilerInfo = resolve(project, compilerInfoPartialPath);
98+
try {
99+
const contents = await readFile(compilerInfo, "utf8");
100+
const compileInfo: { runtime_path?: string } = JSON.parse(contents);
101+
if (compileInfo && compileInfo.runtime_path) {
102+
return [compileInfo.runtime_path];
103+
}
104+
} catch {
105+
// Ignore errors, fallback to node_modules search
106+
}
107+
95108
// Find all node_modules directories using efficient traversal
96109
const node_modules = await findNodeModulesDirs(project);
97110

@@ -136,7 +149,7 @@ async function findRuntimePath(project: string) {
136149
return rescriptRuntimeDirs.map((runtime) => resolve(runtime));
137150
}
138151

139-
function findRuntimeCached() {
152+
function findRuntimeCached(): (project: string) => Promise<string[]> {
140153
const cache = new Map<string, string[]>();
141154
return async (project: string) => {
142155
if (cache.has(project)) {

server/src/incrementalCompilation.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ export async function getBscArgs(
185185
): Promise<BsbCompilerArgs | RewatchCompilerArgs | null> {
186186
return entry.buildSystem === "bsb"
187187
? await getBsbBscArgs(entry)
188-
: await getRewatchBscArgs(send, projectsFiles, entry);
188+
: await getRewatchBscArgs(
189+
send,
190+
entry.project.bscBinaryLocation,
191+
projectsFiles,
192+
entry,
193+
);
189194
}
190195

191196
function argCouples(argList: string[]): string[][] {

server/src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ let findBinary = async (
9292
return path.join(config.extensionConfiguration.platformPath, binary);
9393
}
9494

95+
if (projectRootPath !== null && binary === "bsc.exe") {
96+
try {
97+
const compilerInfo = path.resolve(
98+
projectRootPath,
99+
c.compilerInfoPartialPath,
100+
);
101+
const contents = await fsAsync.readFile(compilerInfo, "utf8");
102+
const compileInfo = JSON.parse(contents);
103+
if (compileInfo && compileInfo.runtime_path) {
104+
return compileInfo.runtime_path;
105+
}
106+
} catch {}
107+
}
108+
95109
const rescriptDir = lookup.findFilePathFromProjectRoot(
96110
projectRootPath,
97111
path.join("node_modules", "rescript"),

0 commit comments

Comments
 (0)