Skip to content

Commit 412463c

Browse files
authored
Use platform specific rewatch binary (#1101)
* Use platform specific rewatch binary * Add changelog * Run bsc from `lib/bs`
1 parent 9f58cea commit 412463c

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :rocket: New Feature
1616

1717
- Find `bsc.exe` and `rescript-code-editor-analysis.exe` from platform-specific packages used by ReScript `v12.0.0-alpha.13`+.https://github.com/rescript-lang/rescript-vscode/pull/1092
18+
- Find `rewatch.exe` from platform-specific packages used by ReScript `v12.0.0-alpha.13`+. https://github.com/rescript-lang/rescript-vscode/pull/1101
1819

1920
#### :bug: Bug fix
2021

server/src/incrementalCompilation.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type IncrementallyCompiledFileInfo = {
4646
/** Location of the original type file. */
4747
originalTypeFileLocation: string;
4848
};
49+
buildSystem: "bsb" | "rewatch";
4950
/** Cache for build.ninja assets. */
5051
buildNinja: {
5152
/** When build.ninja was last modified. Used as a cache key. */
@@ -223,14 +224,16 @@ function getBscArgs(
223224
) {
224225
return Promise.resolve(rewatchCacheEntry.compilerArgs);
225226
}
226-
return new Promise((resolve, _reject) => {
227+
return new Promise(async(resolve, _reject) => {
227228
function resolveResult(result: Array<string> | RewatchCompilerArgs) {
228229
if (stat != null && Array.isArray(result)) {
230+
entry.buildSystem = "bsb";
229231
entry.buildNinja = {
230232
fileMtime: stat.mtimeMs,
231233
rawExtracted: result,
232234
};
233235
} else if (!Array.isArray(result)) {
236+
entry.buildSystem = "rewatch";
234237
entry.buildRewatch = {
235238
lastFile: entry.file.sourceFilePath,
236239
compilerArgs: result,
@@ -295,6 +298,20 @@ function getBscArgs(
295298
entry.project.workspaceRootPath,
296299
"node_modules/@rolandpeelen/rewatch/rewatch"
297300
);
301+
if (semver.valid(project.rescriptVersion) &&
302+
semver.satisfies(project.rescriptVersion as string, ">11", { includePrerelease: true })) {
303+
const rescriptRewatchPath = await utils.findRewatchBinary(entry.project.workspaceRootPath)
304+
if (rescriptRewatchPath != null) {
305+
rewatchPath = rescriptRewatchPath;
306+
if (debug()) {
307+
console.log(`Found rewatch binary bundled with v12: ${rescriptRewatchPath}`)
308+
}
309+
} else {
310+
if (debug()) {
311+
console.log("Did not find rewatch binary bundled with v12")
312+
}
313+
}
314+
}
298315
const compilerArgs = JSON.parse(
299316
cp
300317
.execFileSync(rewatchPath, [
@@ -445,6 +462,7 @@ function triggerIncrementalCompilationOfFile(
445462
bscBinaryLocation,
446463
incrementalFolderPath,
447464
},
465+
buildSystem: foundRewatchLockfileInProjectRoot ? "rewatch" : "bsb",
448466
buildRewatch: null,
449467
buildNinja: null,
450468
compilation: null,
@@ -531,6 +549,7 @@ async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) {
531549
path.resolve(entry.project.rootPath, c.compilerDirPartialPath, value)
532550
);
533551
} else {
552+
// TODO: once ReScript v12 is out we can remove this check for `.`
534553
if (value === ".") {
535554
callArgs.push(
536555
"-I",
@@ -604,11 +623,15 @@ async function compileContents(
604623

605624
try {
606625
fs.writeFileSync(entry.file.incrementalFilePath, fileContent);
607-
626+
let cwd = entry.buildSystem === "bsb" ? entry.project.rootPath : path.resolve(entry.project.rootPath, c.compilerDirPartialPath)
627+
if (debug()) {
628+
console.log(`About to invoke bsc from \"${cwd}\", used ${entry.buildSystem}`);
629+
console.log(`${entry.project.bscBinaryLocation} ${callArgs.map(c => `"${c}"`).join(" ")}`);
630+
}
608631
const process = cp.execFile(
609632
entry.project.bscBinaryLocation,
610633
callArgs,
611-
{ cwd: entry.project.rootPath },
634+
{ cwd },
612635
async (error, _stdout, stderr) => {
613636
if (!error?.killed) {
614637
if (debug())

server/src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export let findProjectRootOfFile = (
8181
// We won't know which version is in the project root until we read and parse `{project_root}/node_modules/rescript/package.json`
8282
let findBinary = async (
8383
projectRootPath: p.DocumentUri | null,
84-
binary: "bsc.exe" | "rescript-editor-analysis.exe" | "rescript"
84+
binary: "bsc.exe" | "rescript-editor-analysis.exe" | "rescript" | "rewatch.exe"
8585
) => {
8686
if (config.extensionConfiguration.platformPath != null) {
8787
return path.join(config.extensionConfiguration.platformPath, binary);
@@ -122,6 +122,8 @@ let findBinary = async (
122122
binaryPath = binPaths.bsc_exe
123123
} else if (binary == "rescript-editor-analysis.exe") {
124124
binaryPath = binPaths.rescript_editor_analysis_exe
125+
} else if (binary == "rewatch.exe") {
126+
binaryPath = binPaths.rewatch_exe
125127
}
126128
} else {
127129
binaryPath = path.join(rescriptDir, c.platformDir, binary)
@@ -143,6 +145,9 @@ export let findBscExeBinary = (projectRootPath: p.DocumentUri | null) =>
143145
export let findEditorAnalysisBinary = (projectRootPath: p.DocumentUri | null) =>
144146
findBinary(projectRootPath, "rescript-editor-analysis.exe");
145147

148+
export let findRewatchBinary = (projectRootPath: p.DocumentUri | null) =>
149+
findBinary(projectRootPath, "rewatch.exe");
150+
146151
type execResult =
147152
| {
148153
kind: "success";

0 commit comments

Comments
 (0)