Skip to content

Commit 59b3b3f

Browse files
committed
fix: make reanalyze work with lib/lsp build artifacts
- Write .sourcedirs.json to lib/lsp/ during full typecheck so the analysis layer can discover LSP build artifacts - Compare .sourcedirs.json mtime (not directory mtime) when choosing between lib/bs and lib/lsp — directory mtime only reflects entry adds/removes, not content updates - Pass -absname to bsc in both single-file and batched LSP typecheck paths so .cmt files embed absolute source paths for reanalyze - Use -o and ../../-prefixed source path in single-file typecheck so .cmt output lands in the correct build directory
1 parent f178c45 commit 59b3b3f

5 files changed

Lines changed: 26 additions & 14 deletions

File tree

analysis/reanalyze/src/Paths.ml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,13 @@ let newerFile a b =
226226
| false, false -> a
227227

228228
let readCmtScan () =
229-
let bsFile =
230-
["lib"; "bs"; ".sourcedirs.json"]
231-
|> List.fold_left Filename.concat runConfig.bsbProjectRoot
232-
in
233-
let lspFile =
234-
["lib"; "lsp"; ".sourcedirs.json"]
235-
|> List.fold_left Filename.concat runConfig.bsbProjectRoot
229+
let sourceDirsFile =
230+
newerFile
231+
(["lib"; "bs"; ".sourcedirs.json"]
232+
|> List.fold_left Filename.concat runConfig.bsbProjectRoot)
233+
(["lib"; "lsp"; ".sourcedirs.json"]
234+
|> List.fold_left Filename.concat runConfig.bsbProjectRoot)
236235
in
237-
let sourceDirsFile = newerFile bsFile lspFile in
238236
let entries = ref [] in
239237
let read_entry (json : Ext_json_types.t) =
240238
match json with

analysis/src/BuildSystem.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ let getRuntimeDir rootPath =
4343
let getLibBs path =
4444
let bs = path /+ "lib" /+ "bs" in
4545
let lsp = path /+ "lib" /+ "lsp" in
46-
match (Sys.file_exists bs, Sys.file_exists lsp) with
46+
let sourcedirs dir = dir /+ ".sourcedirs.json" in
47+
match (Sys.file_exists (sourcedirs bs), Sys.file_exists (sourcedirs lsp)) with
4748
| true, true ->
48-
let mtime path = (Unix.stat path).st_mtime in
49-
if mtime lsp > mtime bs then Some lsp else Some bs
49+
let mtime f = (Unix.stat f).st_mtime in
50+
if mtime (sourcedirs lsp) > mtime (sourcedirs bs) then Some lsp else Some bs
5051
| true, false -> Some bs
5152
| false, true -> Some lsp
5253
| false, false -> None

rewatch/src/build/full_typecheck.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::build_types::*;
22
use super::compile;
33
use crate::build::compiler_info::write_compiler_info;
4+
use crate::sourcedirs;
45
use tracing::{info_span, instrument};
56

67
/// Full typecheck: all dirty modules + dependents, typecheck only (no JS).
@@ -54,6 +55,7 @@ pub fn full_typecheck(
5455
})
5556
} else {
5657
write_compiler_info(build_state, OutputTarget::Lsp);
58+
sourcedirs::print(build_state, OutputTarget::Lsp);
5759
Ok(IncrementalBuildResult {
5860
diagnostics: result.to_diagnostics(),
5961
modules: params.modules,

rewatch/src/lsp/queue/file_typecheck.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,9 @@ fn typecheck_from_ast(ctx: &FileContext, parent: &tracing::Span) -> TypecheckRes
374374
)
375375
.entered();
376376

377-
let mut args = vec!["-I".to_string(), "src".to_string()];
377+
// -absname makes bsc embed absolute paths in .cmt files, which
378+
// reanalyze needs to resolve source locations correctly.
379+
let mut args = vec!["-absname".to_string(), "-I".to_string(), "src".to_string()];
378380
args.extend_from_slice(&ctx.file_args.ast_compiler_args);
379381

380382
let output = Command::new(&ctx.file_args.bsc_path)

rewatch/src/lsp/typecheck.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,18 @@ pub fn typecheck_with_args(args: &TypecheckArgs, content: &str) -> Vec<BscDiagno
4141
// reporting and output prefix derivation.
4242
let mut full_args = args.compiler_args.clone();
4343
let source_arg = full_args.pop();
44+
// -absname makes bsc embed absolute paths in .cmt files, which
45+
// reanalyze needs to resolve source locations correctly.
46+
full_args.push("-absname".into());
4447
full_args.push("-bs-read-stdin".into());
45-
if let Some(arg) = source_arg {
46-
full_args.push(arg);
48+
if let Some(source) = source_arg {
49+
// bsc runs from lib/lsp/ — use -o to keep .cmt output in the build
50+
// dir, while the ../../-prefixed source path lets -absname resolve
51+
// the correct absolute path for embedding inside the .cmt.
52+
full_args.push("-o".into());
53+
full_args.push(source.clone());
54+
let prefixed = Path::new("../..").join(&source).to_string_lossy().into_owned();
55+
full_args.push(prefixed);
4756
}
4857

4958
let mut child = match Command::new(&args.bsc_path)

0 commit comments

Comments
 (0)