From b70de445648307b3c7b8c010514337a2f0a74e39 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Thu, 21 Nov 2024 15:19:07 +0100 Subject: [PATCH] Fix possible windows bugs in incremental compilation --- analysis/src/Cmt.ml | 4 +++- server/src/constants.ts | 2 ++ server/src/incrementalCompilation.ts | 31 +++++++++++++++++++++------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/analysis/src/Cmt.ml b/analysis/src/Cmt.ml index abc93e4ee..8ae08cba3 100644 --- a/analysis/src/Cmt.ml +++ b/analysis/src/Cmt.ml @@ -8,6 +8,8 @@ let fullForCmt ~moduleName ~package ~uri cmt = let extra = ProcessExtra.getExtra ~file ~infos in Some {file; extra; package} +let ( /+ ) = Filename.concat + let fullFromUri ~uri = let path = Uri.toPath uri in match Packages.getPackage ~uri with @@ -19,7 +21,7 @@ let fullFromUri ~uri = let incremental = if !Cfg.inIncrementalTypecheckingMode then let incrementalCmtPath = - package.rootPath ^ "/lib/bs/___incremental" ^ "/" ^ moduleName + (package.rootPath /+ "lib" /+ "bs" /+ "___incremental" /+ moduleName) ^ match Files.classifySourceFile path with | Resi -> ".cmti" diff --git a/server/src/constants.ts b/server/src/constants.ts index a08525523..7c10cecf9 100644 --- a/server/src/constants.ts +++ b/server/src/constants.ts @@ -43,8 +43,10 @@ export let bsbLock = ".bsb.lock"; export let bsconfigPartialPath = "bsconfig.json"; export let rescriptJsonPartialPath = "rescript.json"; export let compilerDirPartialPath = path.join("lib", "bs"); +export let compilerOcamlDirPartialPath = path.join("lib", "ocaml"); export let compilerLogPartialPath = path.join("lib", "bs", ".compiler.log"); export let buildNinjaPartialPath = path.join("lib", "bs", "build.ninja"); +export let rewatchLockPartialPath = path.join("lib", "rewatch.lock"); export let resExt = ".res"; export let resiExt = ".resi"; export let cmiExt = ".cmi"; diff --git a/server/src/incrementalCompilation.ts b/server/src/incrementalCompilation.ts index 0fad494a6..576e87cec 100644 --- a/server/src/incrementalCompilation.ts +++ b/server/src/incrementalCompilation.ts @@ -19,7 +19,10 @@ function debug() { } const INCREMENTAL_FOLDER_NAME = "___incremental"; -const INCREMENTAL_FILE_FOLDER_LOCATION = `lib/bs/${INCREMENTAL_FOLDER_NAME}`; +const INCREMENTAL_FILE_FOLDER_LOCATION = path.join( + c.compilerDirPartialPath, + INCREMENTAL_FOLDER_NAME +); type RewatchCompilerArgs = { compiler_args: Array; @@ -190,11 +193,11 @@ function getBscArgs( ): Promise | RewatchCompilerArgs | null> { const buildNinjaPath = path.resolve( entry.project.rootPath, - "lib/bs/build.ninja" + c.buildNinjaPartialPath ); const rewatchLockfile = path.resolve( entry.project.workspaceRootPath, - "lib/rewatch.lock" + c.rewatchLockPartialPath ); let buildSystem: "bsb" | "rewatch" | null = null; @@ -246,7 +249,13 @@ function getBscArgs( } if (buildSystem === "bsb") { - const fileStream = fs.createReadStream(buildNinjaPath); + const fileStream = fs.createReadStream(buildNinjaPath, { + encoding: "utf8", + }); + fileStream.on("error", (err) => { + console.error("File stream error:", err); + resolveResult([]); + }); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity, @@ -256,6 +265,7 @@ function getBscArgs( let stopped = false; const captured: Array = []; rl.on("line", (line) => { + line = line.trim(); // Normalize line endings if (stopped) { return; } @@ -264,7 +274,8 @@ function getBscArgs( captureNextLine = false; } if (done) { - fileStream.destroy(); + // Not sure if fileStream.destroy is necessary, rl.close() will handle it gracefully. + // fileStream.destroy(); rl.close(); resolveResult(captured); stopped = true; @@ -278,6 +289,10 @@ function getBscArgs( done = true; } }); + rl.on("error", (err) => { + console.error("Readline error:", err); + resolveResult([]); + }); rl.on("close", () => { resolveResult(captured); }); @@ -399,7 +414,7 @@ function triggerIncrementalCompilationOfFile( let originalTypeFileLocation = path.resolve( projectRootPath, - "lib/bs", + c.compilerDirPartialPath, path.relative(projectRootPath, filePath) ); @@ -512,13 +527,13 @@ async function figureOutBscArgs(entry: IncrementallyCompiledFileInfo) { if (isBsb) { callArgs.push( "-I", - path.resolve(entry.project.rootPath, "lib/bs", value) + path.resolve(entry.project.rootPath, c.compilerDirPartialPath, value) ); } else { if (value === ".") { callArgs.push( "-I", - path.resolve(entry.project.rootPath, "lib/ocaml") + path.resolve(entry.project.rootPath, c.compilerOcamlDirPartialPath) ); } else { callArgs.push("-I", value);