Skip to content

Commit

Permalink
Fix possible windows bugs in incremental compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
fhammerschmidt committed Nov 21, 2024
1 parent 2837e08 commit b70de44
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion analysis/src/Cmt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
31 changes: 23 additions & 8 deletions server/src/incrementalCompilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
Expand Down Expand Up @@ -190,11 +193,11 @@ function getBscArgs(
): Promise<Array<string> | 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;

Expand Down Expand Up @@ -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,
Expand All @@ -256,6 +265,7 @@ function getBscArgs(
let stopped = false;
const captured: Array<string> = [];
rl.on("line", (line) => {
line = line.trim(); // Normalize line endings
if (stopped) {
return;
}
Expand All @@ -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;
Expand All @@ -278,6 +289,10 @@ function getBscArgs(
done = true;
}
});
rl.on("error", (err) => {
console.error("Readline error:", err);
resolveResult([]);
});
rl.on("close", () => {
resolveResult(captured);
});
Expand Down Expand Up @@ -399,7 +414,7 @@ function triggerIncrementalCompilationOfFile(

let originalTypeFileLocation = path.resolve(
projectRootPath,
"lib/bs",
c.compilerDirPartialPath,
path.relative(projectRootPath, filePath)
);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b70de44

Please sign in to comment.