Skip to content

Commit f178c45

Browse files
committed
refactor: auto-detect lib/bs or lib/lsp by newest mtime
Both reanalyze and doc/analysis tooling now pick whichever of lib/bs/.sourcedirs.json and lib/lsp/.sourcedirs.json was modified most recently, so the correct build artifacts are used regardless of whether the last build was a CLI or LSP build. This removes the --lib-dir flag from `rescript-tools doc` and the Cfg.libDir plumbing, since the mtime-based detection makes them unnecessary.
1 parent 0a81f6b commit f178c45

5 files changed

Lines changed: 32 additions & 22 deletions

File tree

analysis/reanalyze/src/Paths.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,26 @@ type cmt_scan_entry = {
215215
216216
If missing, returns the empty list and callers should fall back to legacy behavior. *)
217217

218+
let newerFile a b =
219+
match (Sys.file_exists a, Sys.file_exists b) with
220+
| true, true ->
221+
let mtime_a = (Unix.stat a).st_mtime in
222+
let mtime_b = (Unix.stat b).st_mtime in
223+
if mtime_b > mtime_a then b else a
224+
| true, false -> a
225+
| false, true -> b
226+
| false, false -> a
227+
218228
let readCmtScan () =
219-
let sourceDirsFile =
229+
let bsFile =
220230
["lib"; "bs"; ".sourcedirs.json"]
221231
|> List.fold_left Filename.concat runConfig.bsbProjectRoot
222232
in
233+
let lspFile =
234+
["lib"; "lsp"; ".sourcedirs.json"]
235+
|> List.fold_left Filename.concat runConfig.bsbProjectRoot
236+
in
237+
let sourceDirsFile = newerFile bsFile lspFile in
223238
let entries = ref [] in
224239
let read_entry (json : Ext_json_types.t) =
225240
match json with

analysis/src/BuildSystem.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,16 @@ let getRuntimeDir rootPath =
4040
None))
4141
| true -> Some rootPath
4242

43-
let getLibBs path = Files.ifExists (path /+ "lib" /+ "bs")
43+
let getLibBs path =
44+
let bs = path /+ "lib" /+ "bs" in
45+
let lsp = path /+ "lib" /+ "lsp" in
46+
match (Sys.file_exists bs, Sys.file_exists lsp) with
47+
| true, true ->
48+
let mtime path = (Unix.stat path).st_mtime in
49+
if mtime lsp > mtime bs then Some lsp else Some bs
50+
| true, false -> Some bs
51+
| false, true -> Some lsp
52+
| false, false -> None
4453

4554
let getStdlib base =
4655
match getRuntimeDir base with

analysis/src/Cfg.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ let debugFollowCtxPath = ref false
22

33
let isDocGenFromCompiler = ref false
44

5-
let libDir : string option ref = ref None
6-
75
let inIncrementalTypecheckingMode =
86
ref
97
(try

analysis/src/Packages.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ let newBsPackage ~rootPath =
3737

3838
let parseRaw raw =
3939
let libBs =
40-
match !Cfg.libDir with
41-
| Some dir -> Files.ifExists (Filename.concat rootPath dir)
42-
| None -> (
43-
match !Cfg.isDocGenFromCompiler with
44-
| true -> BuildSystem.getStdlib rootPath
45-
| false -> BuildSystem.getLibBs rootPath)
40+
match !Cfg.isDocGenFromCompiler with
41+
| true -> BuildSystem.getStdlib rootPath
42+
| false -> BuildSystem.getLibBs rootPath
4643
in
4744
match Json.parse raw with
4845
| Some config -> (

tools/bin/main.ml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ let docHelp =
33

44
Output documentation to standard output
55

6-
Usage: rescript-tools doc <FILE> [--lib-dir <DIR>]
6+
Usage: rescript-tools doc <FILE>
77

8-
Example: rescript-tools doc ./path/to/EntryPointLib.res
9-
Example: rescript-tools doc ./path/to/EntryPointLib.res --lib-dir lib/lsp|}
8+
Example: rescript-tools doc ./path/to/EntryPointLib.res|}
109

1110
let formatCodeblocksHelp =
1211
{|ReScript Tools
@@ -61,21 +60,13 @@ let main () =
6160
| "doc" :: rest -> (
6261
match rest with
6362
| ["-h"] | ["--help"] -> logAndExit (Ok docHelp)
64-
| path :: args ->
63+
| path :: _ ->
6564
(* NOTE: Internal use to generate docs from compiler *)
6665
let () =
6766
match Sys.getenv_opt "FROM_COMPILER" with
6867
| Some "true" -> Analysis.Cfg.isDocGenFromCompiler := true
6968
| _ -> ()
7069
in
71-
let rec parseArgs = function
72-
| "--lib-dir" :: dir :: remaining ->
73-
Analysis.Cfg.libDir := Some dir;
74-
parseArgs remaining
75-
| _ :: remaining -> parseArgs remaining
76-
| [] -> ()
77-
in
78-
parseArgs args;
7970
logAndExit (Tools.extractDocs ~entryPointFile:path ~debug:false)
8071
| _ -> logAndExit (Error docHelp))
8172
| "migrate" :: file :: opts -> (

0 commit comments

Comments
 (0)