Skip to content

Commit 1b2651a

Browse files
committed
Enable create-sourcedirs by default in rewatch
- Change --create-sourcedirs to default to true (always create .sourcedirs.json) - Hide the flag from help since it's now always enabled - Add deprecation warning when flag is explicitly used - Fix package name mismatches in test projects: - deadcode rescript.json: sample-typescript-app -> @tests/reanalyze-deadcode - rescript-react package.json: @tests/rescript-react -> @rescript/react
1 parent 4a8a036 commit 1b2651a

File tree

11 files changed

+90
-80
lines changed

11 files changed

+90
-80
lines changed

analysis/reanalyze/src/CmtCache.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,10 @@ let clear () = Marshal_cache.clear ()
2929
The next read will re-load the file from disk. *)
3030
let invalidate path = Marshal_cache.invalidate path
3131

32+
type stats = {entry_count: int; mapped_bytes: int}
3233
(** Cache statistics *)
33-
type stats = {
34-
entry_count: int;
35-
mapped_bytes: int;
36-
}
3734

3835
(** Get cache statistics *)
3936
let stats () : stats =
4037
let s = Marshal_cache.stats () in
41-
{ entry_count = s.entry_count; mapped_bytes = s.mapped_bytes }
42-
38+
{entry_count = s.entry_count; mapped_bytes = s.mapped_bytes}

analysis/reanalyze/src/CmtCache.mli

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ val clear : unit -> unit
1717
val invalidate : string -> unit
1818
(** Invalidate a specific path in the cache. *)
1919

20-
type stats = {
21-
entry_count: int;
22-
mapped_bytes: int;
23-
}
20+
type stats = {entry_count: int; mapped_bytes: int}
2421
(** Cache statistics *)
2522

2623
val stats : unit -> stats
2724
(** Get cache statistics *)
28-

analysis/reanalyze/src/ReactiveAnalysis.ml

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,25 @@
66

77
[@@@alert "-unsafe"]
88

9-
(** Result of processing a single CMT file *)
109
type cmt_file_result = {
1110
dce_data: DceFileProcessing.file_data option;
1211
exception_data: Exception.file_result option;
1312
}
13+
(** Result of processing a single CMT file *)
1414

15-
(** Result of processing all CMT files *)
1615
type all_files_result = {
1716
dce_data_list: DceFileProcessing.file_data list;
1817
exception_results: Exception.file_result list;
1918
}
19+
(** Result of processing all CMT files *)
2020

21-
(** Cached file_data for a single CMT file.
22-
We cache the processed result, not just the raw CMT data. *)
2321
type cached_file = {
2422
path: string;
2523
file_data: DceFileProcessing.file_data option;
2624
exception_data: Exception.file_result option;
2725
}
26+
(** Cached file_data for a single CMT file.
27+
We cache the processed result, not just the raw CMT data. *)
2828

2929
(** The file cache - maps CMT paths to processed results *)
3030
let file_cache : (string, cached_file) Hashtbl.t = Hashtbl.create 1024
@@ -81,60 +81,62 @@ let process_cmt_infos ~config ~cmtFilePath cmt_infos : cmt_file_result option =
8181
Returns the cached result if the file hasn't changed since last access. *)
8282
let process_cmt_cached ~config cmtFilePath : cmt_file_result option =
8383
match CmtCache.read_cmt_if_changed cmtFilePath with
84-
| None ->
84+
| None -> (
8585
(* File unchanged - return cached result *)
86-
(match Hashtbl.find_opt file_cache cmtFilePath with
87-
| Some cached ->
88-
Some { dce_data = cached.file_data; exception_data = cached.exception_data }
89-
| None ->
90-
(* First time seeing this file - shouldn't happen, but handle gracefully *)
91-
None)
86+
match Hashtbl.find_opt file_cache cmtFilePath with
87+
| Some cached ->
88+
Some {dce_data = cached.file_data; exception_data = cached.exception_data}
89+
| None ->
90+
(* First time seeing this file - shouldn't happen, but handle gracefully *)
91+
None)
9292
| Some cmt_infos ->
9393
(* File changed or new - process it *)
9494
let result = process_cmt_infos ~config ~cmtFilePath cmt_infos in
9595
(* Cache the result *)
9696
(match result with
97-
| Some r ->
98-
Hashtbl.replace file_cache cmtFilePath {
99-
path = cmtFilePath;
100-
file_data = r.dce_data;
101-
exception_data = r.exception_data;
102-
}
103-
| None -> ());
97+
| Some r ->
98+
Hashtbl.replace file_cache cmtFilePath
99+
{
100+
path = cmtFilePath;
101+
file_data = r.dce_data;
102+
exception_data = r.exception_data;
103+
}
104+
| None -> ());
104105
result
105106

106107
(** Process all files incrementally.
107108
First run processes all files. Subsequent runs only process changed files. *)
108109
let process_files_incremental ~config cmtFilePaths : all_files_result =
109110
Timing.time_phase `FileLoading (fun () ->
110-
let dce_data_list = ref [] in
111-
let exception_results = ref [] in
112-
let processed = ref 0 in
113-
let from_cache = ref 0 in
114-
115-
cmtFilePaths |> List.iter (fun cmtFilePath ->
116-
(* Check if file was in cache *before* processing *)
117-
let was_cached = Hashtbl.mem file_cache cmtFilePath in
118-
match process_cmt_cached ~config cmtFilePath with
119-
| Some {dce_data; exception_data} ->
120-
(match dce_data with
121-
| Some data -> dce_data_list := data :: !dce_data_list
122-
| None -> ());
123-
(match exception_data with
124-
| Some data -> exception_results := data :: !exception_results
125-
| None -> ());
126-
(* Track whether it was from cache *)
127-
if was_cached then
128-
incr from_cache
129-
else
130-
incr processed
131-
| None -> ()
132-
);
133-
134-
if !Cli.timing then
135-
Printf.eprintf "Reactive: %d files processed, %d from cache\n%!" !processed !from_cache;
136-
137-
{dce_data_list = List.rev !dce_data_list; exception_results = List.rev !exception_results})
111+
let dce_data_list = ref [] in
112+
let exception_results = ref [] in
113+
let processed = ref 0 in
114+
let from_cache = ref 0 in
115+
116+
cmtFilePaths
117+
|> List.iter (fun cmtFilePath ->
118+
(* Check if file was in cache *before* processing *)
119+
let was_cached = Hashtbl.mem file_cache cmtFilePath in
120+
match process_cmt_cached ~config cmtFilePath with
121+
| Some {dce_data; exception_data} ->
122+
(match dce_data with
123+
| Some data -> dce_data_list := data :: !dce_data_list
124+
| None -> ());
125+
(match exception_data with
126+
| Some data -> exception_results := data :: !exception_results
127+
| None -> ());
128+
(* Track whether it was from cache *)
129+
if was_cached then incr from_cache else incr processed
130+
| None -> ());
131+
132+
if !Cli.timing then
133+
Printf.eprintf "Reactive: %d files processed, %d from cache\n%!"
134+
!processed !from_cache;
135+
136+
{
137+
dce_data_list = List.rev !dce_data_list;
138+
exception_results = List.rev !exception_results;
139+
})
138140

139141
(** Clear all cached file data *)
140142
let clear () =
@@ -146,4 +148,3 @@ let stats () =
146148
let file_count = Hashtbl.length file_cache in
147149
let cmt_stats = CmtCache.stats () in
148150
(file_count, cmt_stats)
149-

analysis/reanalyze/src/Reanalyze.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,14 @@ let processCmtFiles ~config ~cmtRoot : all_files_result =
211211
let cmtFilePaths = collectCmtFilePaths ~cmtRoot in
212212
(* Reactive mode: use incremental processing that skips unchanged files *)
213213
if !Cli.reactive then
214-
let result = ReactiveAnalysis.process_files_incremental ~config cmtFilePaths in
215-
{dce_data_list = result.dce_data_list; exception_results = result.exception_results}
216-
else begin
214+
let result =
215+
ReactiveAnalysis.process_files_incremental ~config cmtFilePaths
216+
in
217+
{
218+
dce_data_list = result.dce_data_list;
219+
exception_results = result.exception_results;
220+
}
221+
else
217222
let numDomains =
218223
match !Cli.parallel with
219224
| n when n > 0 -> n
@@ -228,7 +233,6 @@ let processCmtFiles ~config ~cmtRoot : all_files_result =
228233
(List.length cmtFilePaths);
229234
processFilesParallel ~config ~numDomains cmtFilePaths)
230235
else processFilesSequential ~config cmtFilePaths
231-
end
232236

233237
(* Shuffle a list using Fisher-Yates algorithm *)
234238
let shuffle_list lst =
@@ -362,11 +366,10 @@ let runAnalysisAndReport ~cmtRoot =
362366
if numRuns > 1 && !Cli.timing then
363367
Printf.eprintf "\n=== Run %d/%d ===\n%!" run numRuns;
364368
runAnalysis ~dce_config ~cmtRoot;
365-
if run = numRuns then begin
369+
if run = numRuns then (
366370
(* Only report on last run *)
367371
Log_.Stats.report ~config:dce_config;
368-
Log_.Stats.clear ()
369-
end;
372+
Log_.Stats.clear ());
370373
Timing.report ()
371374
done;
372375
if !Cli.json then EmitJson.finish ()

analysis/vendor/skip-lite/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
; skip-lite vendor directory
2+
23
(dirs marshal_cache reactive_file_collection)
34

45
; Test executable for CMT file support
6+
57
(executable
68
(name test_cmt)
79
(modules test_cmt)

analysis/vendor/skip-lite/marshal_cache/dune

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
(foreign_stubs
44
(language cxx)
55
(names marshal_cache_stubs)
6-
(flags (:standard -std=c++17)))
6+
(flags
7+
(:standard -std=c++17)))
78
(c_library_flags (-lstdc++)))

rewatch/src/cli.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,9 @@ pub struct AfterBuildArg {
197197

198198
#[derive(Args, Debug, Clone, Copy)]
199199
pub struct CreateSourceDirsArg {
200-
/// Create a source_dirs.json file at the root of the monorepo, needed for Reanalyze.
201-
#[arg(short, long, default_value_t = false, num_args = 0..=1)]
202-
pub create_sourcedirs: bool,
200+
/// Deprecated: source_dirs.json is now always created.
201+
#[arg(short, long, num_args = 0..=1, default_missing_value = "true", hide = true)]
202+
pub create_sourcedirs: Option<bool>,
203203
}
204204

205205
#[derive(Args, Debug, Clone, Copy)]
@@ -488,11 +488,10 @@ impl Deref for AfterBuildArg {
488488
}
489489
}
490490

491-
impl Deref for CreateSourceDirsArg {
492-
type Target = bool;
493-
494-
fn deref(&self) -> &Self::Target {
495-
&self.create_sourcedirs
491+
impl CreateSourceDirsArg {
492+
/// Returns true if the flag was explicitly passed on the command line.
493+
pub fn was_explicitly_set(&self) -> bool {
494+
self.create_sourcedirs.is_some()
496495
}
497496
}
498497

rewatch/src/main.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ fn main() -> Result<()> {
4646
);
4747
}
4848

49+
if build_args.create_sourcedirs.was_explicitly_set() {
50+
log::warn!(
51+
"`--create-sourcedirs` is deprecated: source_dirs.json is now always created. Please remove this flag from your command."
52+
);
53+
}
54+
4955
match build::build(
5056
&build_args.filter,
5157
Path::new(&build_args.folder as &str),
5258
show_progress,
5359
build_args.no_timing,
54-
*build_args.create_sourcedirs,
60+
true, // create_sourcedirs is now always enabled
5561
plain_output,
5662
(*build_args.warn_error).clone(),
5763
) {
@@ -76,12 +82,18 @@ fn main() -> Result<()> {
7682
);
7783
}
7884

85+
if watch_args.create_sourcedirs.was_explicitly_set() {
86+
log::warn!(
87+
"`--create-sourcedirs` is deprecated: source_dirs.json is now always created. Please remove this flag from your command."
88+
);
89+
}
90+
7991
match watcher::start(
8092
&watch_args.filter,
8193
show_progress,
8294
&watch_args.folder,
8395
(*watch_args.after_build).clone(),
84-
*watch_args.create_sourcedirs,
96+
true, // create_sourcedirs is now always enabled
8597
plain_output,
8698
(*watch_args.warn_error).clone(),
8799
) {

tests/analysis_tests/tests-reanalyze/deadcode/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "@tests/reanalyze-deadcode",
33
"private": true,
44
"scripts": {
5-
"build": "rescript-legacy build",
6-
"clean": "rescript-legacy clean"
5+
"build": "rescript build",
6+
"clean": "rescript clean"
77
},
88
"dependencies": {
99
"@rescript/react": "link:../../../dependencies/rescript-react",

tests/analysis_tests/tests-reanalyze/deadcode/rescript.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"unsuppress": [],
66
"transitive": true
77
},
8-
"name": "sample-typescript-app",
8+
"name": "@tests/reanalyze-deadcode",
99
"jsx": { "version": 4 },
1010
"dependencies": ["@rescript/react"],
1111
"sources": [

0 commit comments

Comments
 (0)