Skip to content

Use platform specific rewatch binary #1101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion server/src/incrementalCompilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function getBscArgs(
) {
return Promise.resolve(rewatchCacheEntry.compilerArgs);
}
return new Promise((resolve, _reject) => {
return new Promise(async(resolve, _reject) => {
function resolveResult(result: Array<string> | RewatchCompilerArgs) {
if (stat != null && Array.isArray(result)) {
entry.buildNinja = {
Expand Down Expand Up @@ -295,6 +295,20 @@ function getBscArgs(
entry.project.workspaceRootPath,
"node_modules/@rolandpeelen/rewatch/rewatch"
);
if (semver.valid(project.rescriptVersion) &&
semver.satisfies(project.rescriptVersion as string, ">11", { includePrerelease: true })) {
const rescriptRewatchPath = await utils.findRewatchBinary(entry.project.workspaceRootPath)
if (rescriptRewatchPath != null) {
rewatchPath = rescriptRewatchPath;
if (debug()) {
console.log(`Found rewatch binary bundled with v12: ${rescriptRewatchPath}`)
}
} else {
if (debug()) {
console.log("Did not find rewatch binary bundled with v12")
}
}
}
const compilerArgs = JSON.parse(
cp
.execFileSync(rewatchPath, [
Expand Down Expand Up @@ -536,6 +550,10 @@ async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) {
"-I",
path.resolve(entry.project.rootPath, c.compilerOcamlDirPartialPath)
);
} else if (value.startsWith("..") && value.endsWith("ocaml")) {
// This should be the lib/ocaml folder of the project
// This is a hack to support incremental compilation in monorepos
callArgs.push("-I", path.resolve(entry.project.incrementalFolderPath, "..", "..", "ocaml"));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not getting any diagnostics for the following file:

  • rescript.json
  • packages
    • samples
      • rescript.json
      • src/FlappyBird.res

bunx rewatch --rescript-version 12.0.0-alpha.14 --compiler-args packages/samples/src/FlappyBird.res

gave:

{
  "compiler_args": [
    "-I",
    "../ocaml",
    "-I",
    "/Users/nojaf/Projects/rescript-kaplay/packages/rescript-kaplay/lib/ocaml",
    "-uncurried",
    "-bs-package-name",
    "@nojaf/samples",
    "-bs-package-output",
    "esmodule:src:.res.mjs",
    "-bs-v",
    "12.0.0-alpha.14",
    "src/FlappyBird.ast"
  ],
  "parser_args": [
    "-bs-v",
    "12.0.0-alpha.14",
    "-uncurried",
    "-absname",
    "-bs-ast",
    "-o",
    "src/FlappyBird.ast",
    "../../src/FlappyBird.res"
  ]
}

Where -I "../ocaml" is supposed to be packages/samples/lib/ocaml.
I was getting error for my other modules (inside the same project) not being found.

@jfrolich any ideas why this happens?

With this fix, everything works like a charm again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zth as Jaap mentioned on Discord, we should run this from bs/lib.
I changed this for Rewatch only, to not impact any existing bsb users.

} else {
callArgs.push("-I", value);
}
Expand Down
7 changes: 6 additions & 1 deletion server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export let findProjectRootOfFile = (
// We won't know which version is in the project root until we read and parse `{project_root}/node_modules/rescript/package.json`
let findBinary = async (
projectRootPath: p.DocumentUri | null,
binary: "bsc.exe" | "rescript-editor-analysis.exe" | "rescript"
binary: "bsc.exe" | "rescript-editor-analysis.exe" | "rescript" | "rewatch.exe"
) => {
if (config.extensionConfiguration.platformPath != null) {
return path.join(config.extensionConfiguration.platformPath, binary);
Expand Down Expand Up @@ -122,6 +122,8 @@ let findBinary = async (
binaryPath = binPaths.bsc_exe
} else if (binary == "rescript-editor-analysis.exe") {
binaryPath = binPaths.rescript_editor_analysis_exe
} else if (binary == "rewatch.exe") {
binaryPath = binPaths.rewatch_exe
}
} else {
binaryPath = path.join(rescriptDir, c.platformDir, binary)
Expand All @@ -143,6 +145,9 @@ export let findBscExeBinary = (projectRootPath: p.DocumentUri | null) =>
export let findEditorAnalysisBinary = (projectRootPath: p.DocumentUri | null) =>
findBinary(projectRootPath, "rescript-editor-analysis.exe");

export let findRewatchBinary = (projectRootPath: p.DocumentUri | null) =>
findBinary(projectRootPath, "rewatch.exe");

type execResult =
| {
kind: "success";
Expand Down