From cec93428993980b417c0a5cff3399edb3e085afe Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 27 May 2025 17:11:37 +0200 Subject: [PATCH 1/3] Use platform specific rewatch binary --- server/src/incrementalCompilation.ts | 20 +++++++++++++++++++- server/src/utils.ts | 7 ++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/server/src/incrementalCompilation.ts b/server/src/incrementalCompilation.ts index ec7d90335..7166cbd25 100644 --- a/server/src/incrementalCompilation.ts +++ b/server/src/incrementalCompilation.ts @@ -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 | RewatchCompilerArgs) { if (stat != null && Array.isArray(result)) { entry.buildNinja = { @@ -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, [ @@ -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")); } else { callArgs.push("-I", value); } diff --git a/server/src/utils.ts b/server/src/utils.ts index fd704a5ba..0cd3ae336 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -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); @@ -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) @@ -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"; From 87a598231547f24d47d7ae7e26216cac17a15acb Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 27 May 2025 17:23:29 +0200 Subject: [PATCH 2/3] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 87f66158c..d3cf0928d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ #### :rocket: New Feature - 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 +- Find `rewatch.exe` from platform-specific packages used by ReScript `v12.0.0-alpha.13`+. https://github.com/rescript-lang/rescript-vscode/pull/1101 #### :bug: Bug fix From 0e6a9785d03d01fc108c17214f4a5312396ef8c0 Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 27 May 2025 18:20:18 +0200 Subject: [PATCH 3/3] Run bsc from `lib/bs` --- server/src/incrementalCompilation.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/server/src/incrementalCompilation.ts b/server/src/incrementalCompilation.ts index 7166cbd25..bc56ed19a 100644 --- a/server/src/incrementalCompilation.ts +++ b/server/src/incrementalCompilation.ts @@ -46,6 +46,7 @@ type IncrementallyCompiledFileInfo = { /** Location of the original type file. */ originalTypeFileLocation: string; }; + buildSystem: "bsb" | "rewatch"; /** Cache for build.ninja assets. */ buildNinja: { /** When build.ninja was last modified. Used as a cache key. */ @@ -226,11 +227,13 @@ function getBscArgs( return new Promise(async(resolve, _reject) => { function resolveResult(result: Array | RewatchCompilerArgs) { if (stat != null && Array.isArray(result)) { + entry.buildSystem = "bsb"; entry.buildNinja = { fileMtime: stat.mtimeMs, rawExtracted: result, }; } else if (!Array.isArray(result)) { + entry.buildSystem = "rewatch"; entry.buildRewatch = { lastFile: entry.file.sourceFilePath, compilerArgs: result, @@ -459,6 +462,7 @@ function triggerIncrementalCompilationOfFile( bscBinaryLocation, incrementalFolderPath, }, + buildSystem: foundRewatchLockfileInProjectRoot ? "rewatch" : "bsb", buildRewatch: null, buildNinja: null, compilation: null, @@ -545,15 +549,12 @@ async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) { path.resolve(entry.project.rootPath, c.compilerDirPartialPath, value) ); } else { + // TODO: once ReScript v12 is out we can remove this check for `.` if (value === ".") { callArgs.push( "-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")); } else { callArgs.push("-I", value); } @@ -622,11 +623,15 @@ async function compileContents( try { fs.writeFileSync(entry.file.incrementalFilePath, fileContent); - + let cwd = entry.buildSystem === "bsb" ? entry.project.rootPath : path.resolve(entry.project.rootPath, c.compilerDirPartialPath) + if (debug()) { + console.log(`About to invoke bsc from \"${cwd}\", used ${entry.buildSystem}`); + console.log(`${entry.project.bscBinaryLocation} ${callArgs.map(c => `"${c}"`).join(" ")}`); + } const process = cp.execFile( entry.project.bscBinaryLocation, callArgs, - { cwd: entry.project.rootPath }, + { cwd }, async (error, _stdout, stderr) => { if (!error?.killed) { if (debug())