Skip to content

Commit 304d9ac

Browse files
Alizterrgrinberg
andauthored
test: include_subdirs unqualified duplicates cram rules (#12142)
* test: include_subdirs unqualified and directory cram test Signed-off-by: Ali Caglayan <[email protected]> * fix: cram tests with (inlude_subdirs ..) Pass the source directory of each directory contents. Otherwise, we end up generating rules for one dir more than once. Signed-off-by: Rudi Grinberg <[email protected]> * test(cram): update test documentation Signed-off-by: Ali Caglayan <[email protected]> --------- Signed-off-by: Ali Caglayan <[email protected]> Signed-off-by: Rudi Grinberg <[email protected]> Co-authored-by: Rudi Grinberg <[email protected]>
1 parent 9ca47a7 commit 304d9ac

File tree

11 files changed

+97
-33
lines changed

11 files changed

+97
-33
lines changed

src/dune_rules/coq/coq_sources.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ let empty =
2828
;;
2929

3030
let coq_modules_of_files ~dirs =
31-
let filter_v_files ({ Source_file_dir.dir = _; path_to_root = _; files } as sd) =
31+
let filter_v_files
32+
({ Source_file_dir.dir = _; path_to_root = _; files; source_dir = _ } as sd)
33+
=
3234
{ sd with files = String.Set.filter files ~f:(fun f -> Filename.check_suffix f ".v") }
3335
in
3436
let dirs = List.map dirs ~f:filter_v_files in
35-
let build_mod_dir { Source_file_dir.dir; path_to_root = prefix; files } =
37+
let build_mod_dir { Source_file_dir.dir; path_to_root = prefix; files; source_dir = _ } =
3638
String.Set.to_list_map files ~f:(fun file ->
3739
let name, _ = Filename.split_extension file in
3840
let name = Coq_module.Name.make name in

src/dune_rules/dir_contents.ml

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ type t =
2525
; mlds : (Documentation.t * Doc_sources.mld list) list Memo.Lazy.t
2626
; coq : Coq_sources.t Memo.Lazy.t
2727
; ml : Ml_sources.t Memo.Lazy.t
28+
; source_dir : Source_tree.Dir.t option
2829
}
2930

3031
and kind =
3132
| Standalone
3233
| Group_root of t list
3334
| Group_part
3435

35-
let empty kind ~dir =
36+
let empty kind ~dir ~source_dir =
3637
{ kind
3738
; dir
39+
; source_dir
3840
; text_files = Filename.Set.empty
3941
; ml = Memo.Lazy.of_val Ml_sources.empty
4042
; mlds = Memo.Lazy.of_val []
@@ -52,11 +54,11 @@ module Standalone_or_root = struct
5254

5355
type nonrec t = { contents : standalone_or_root Memo.Lazy.t }
5456

55-
let empty ~dir =
57+
let empty ~dir ~source_dir =
5658
{ contents =
5759
Memo.Lazy.create (fun () ->
5860
Memo.return
59-
{ root = empty Standalone ~dir
61+
{ root = empty Standalone ~dir ~source_dir
6062
; rules = Rules.empty
6163
; subdirs = Path.Build.Map.empty
6264
})
@@ -84,6 +86,7 @@ type triage =
8486
| Group_part of Path.Build.t
8587

8688
let dir t = t.dir
89+
let source_dir t = t.source_dir
8790
let coq t = Memo.Lazy.force t.coq
8891
let ocaml t = Memo.Lazy.force t.ml
8992

@@ -235,7 +238,10 @@ end = struct
235238
let src_dir = Dune_file.dir d in
236239
stanzas >>= load_text_files sctx st_dir ~src_dir ~dir)
237240
in
238-
let dirs = [ { Source_file_dir.dir; path_to_root = []; files } ] in
241+
let dirs =
242+
[ { Source_file_dir.dir; path_to_root = []; files; source_dir = Some st_dir }
243+
]
244+
in
239245
let ml =
240246
Memo.lazy_ (fun () ->
241247
let lookup_vlib = lookup_vlib sctx ~current_dir:dir in
@@ -257,6 +263,7 @@ end = struct
257263
let mlds = mlds ~sctx ~dir ~dune_file:d ~files in
258264
{ Standalone_or_root.root =
259265
{ kind = Standalone
266+
; source_dir = Some st_dir
260267
; dir
261268
; text_files = files
262269
; ml
@@ -315,9 +322,20 @@ end = struct
315322
~src_dir:(Source_tree.Dir.path source_dir)
316323
~dir
317324
in
318-
{ Source_file_dir.dir; path_to_root = path_to_group_root; files })))
325+
{ Source_file_dir.dir
326+
; path_to_root = path_to_group_root
327+
; files
328+
; source_dir = Some source_dir
329+
})))
330+
in
331+
let dirs =
332+
{ Source_file_dir.dir
333+
; path_to_root = []
334+
; files
335+
; source_dir = Some source_dir
336+
}
337+
:: subdirs
319338
in
320-
let dirs = { Source_file_dir.dir; path_to_root = []; files } :: subdirs in
321339
let lib_config =
322340
let+ ocaml = Context.ocaml ctx in
323341
ocaml.lib_config
@@ -350,18 +368,22 @@ end = struct
350368
in
351369
let mlds = mlds ~sctx ~dir ~dune_file ~files in
352370
let subdirs =
353-
List.map subdirs ~f:(fun { Source_file_dir.dir; path_to_root = _; files } ->
354-
{ kind = Group_part
355-
; dir
356-
; text_files = files
357-
; ml
358-
; foreign_sources
359-
; mlds
360-
; coq
361-
})
371+
List.map
372+
subdirs
373+
~f:(fun { Source_file_dir.dir; path_to_root = _; files; source_dir } ->
374+
{ kind = Group_part
375+
; source_dir
376+
; dir
377+
; text_files = files
378+
; ml
379+
; foreign_sources
380+
; mlds
381+
; coq
382+
})
362383
in
363384
let root =
364385
{ kind = Group_root subdirs
386+
; source_dir = Some source_dir
365387
; dir
366388
; text_files = files
367389
; ml
@@ -383,8 +405,11 @@ end = struct
383405
>>= function
384406
| Is_component_of_a_group_but_not_the_root { group_root; stanzas = _ } ->
385407
Memo.return @@ Group_part group_root
386-
| Lock_dir | Generated | Source_only _ ->
387-
Memo.return @@ Standalone_or_root (Standalone_or_root.empty ~dir)
408+
| Generated ->
409+
Memo.return @@ Standalone_or_root (Standalone_or_root.empty ~dir ~source_dir:None)
410+
| Lock_dir source_dir | Source_only source_dir ->
411+
Memo.return
412+
@@ Standalone_or_root (Standalone_or_root.empty ~dir ~source_dir:(Some source_dir))
388413
| Standalone (st_dir, d) ->
389414
Memo.return @@ Standalone_or_root (make_standalone sctx st_dir ~dir d)
390415
| Group_root root ->

src/dune_rules/dir_contents.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ open Import
1212
type t
1313

1414
val dir : t -> Path.Build.t
15+
val source_dir : t -> Source_tree.Dir.t option
1516

1617
(** Files in this directory. At the moment, this doesn't include all generated
1718
files, just the ones generated by [rule], [ocamllex], [ocamlyacc], [menhir]

src/dune_rules/dir_status.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ end
2828

2929
module T = struct
3030
type t =
31-
| Lock_dir
31+
| Lock_dir of Source_tree.Dir.t
3232
| Generated
3333
| Source_only of Source_tree.Dir.t
3434
| (* Directory not part of a multi-directory group *)
@@ -47,7 +47,7 @@ type enclosing_group =
4747
| Group_root of Path.Build.t
4848

4949
let current_group dir = function
50-
| Lock_dir | Generated | Source_only _ | Standalone _ -> No_group
50+
| Lock_dir _ | Generated | Source_only _ | Standalone _ -> No_group
5151
| Group_root _ -> Group_root dir
5252
| Is_component_of_a_group_but_not_the_root { group_root; _ } -> Group_root group_root
5353
;;
@@ -230,7 +230,7 @@ end = struct
230230
let rec walk st_dir ~dir ~local =
231231
DB.get ~dir
232232
>>= function
233-
| Lock_dir | Generated | Source_only _ | Standalone _ | Group_root _ ->
233+
| Lock_dir _ | Generated | Source_only _ | Standalone _ | Group_root _ ->
234234
Memo.return Appendable_list.empty
235235
| Is_component_of_a_group_but_not_the_root { stanzas; group_root = _ } ->
236236
let* stanzas =
@@ -323,7 +323,7 @@ end = struct
323323
| None -> false
324324
| Some of_ -> Path.Source.is_descendant ~of_ src_dir)
325325
>>= (function
326-
| true -> Memo.return Lock_dir
326+
| true -> Memo.return (Lock_dir st_dir)
327327
| false ->
328328
let build_dir_is_project_root = build_dir_is_project_root st_dir in
329329
Dune_load.stanzas_in_dir dir
@@ -348,7 +348,7 @@ end
348348

349349
let directory_targets t ~jsoo_enabled ~dir =
350350
match t with
351-
| Lock_dir | Generated | Source_only _ | Is_component_of_a_group_but_not_the_root _ ->
351+
| Lock_dir _ | Generated | Source_only _ | Is_component_of_a_group_but_not_the_root _ ->
352352
Memo.return Path.Build.Map.empty
353353
| Standalone (_, dune_file) ->
354354
Dune_file.stanzas dune_file >>= extract_directory_targets ~jsoo_enabled ~dir

src/dune_rules/dir_status.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ module Group_root : sig
2626
end
2727

2828
type t =
29-
| Lock_dir
29+
| Lock_dir of
30+
Source_tree.Dir.t (* XXX Will have to be modified once lock dirs are targets *)
3031
| Generated
3132
| Source_only of Source_tree.Dir.t
3233
| Standalone of Source_tree.Dir.t * Dune_file.t

src/dune_rules/foreign_sources.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module Unresolved = struct
9898
List.fold_left
9999
dirs
100100
~init:String.Map.empty
101-
~f:(fun acc { Source_file_dir.dir; path_to_root = _; files } ->
101+
~f:(fun acc { Source_file_dir.dir; path_to_root = _; files; source_dir = _ } ->
102102
let sources = load ~dir ~dune_version ~files in
103103
String.Map.Multi.rev_union sources acc)
104104
;;

src/dune_rules/gen_rules.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ let gen_rules_standalone_or_root sctx ~dir ~source_dir =
477477
let* cctxs = gen_rules_group_part_or_root sctx dir_contents [] ~source_dir ~dir in
478478
Dir_contents.Standalone_or_root.subdirs standalone_or_root
479479
>>= Memo.parallel_iter ~f:(fun dc ->
480+
let source_dir = Option.value_exn (Dir_contents.source_dir dc) in
480481
let+ (_ : (Loc.t * Compilation_context.t) list) =
481482
gen_rules_group_part_or_root
482483
sctx
@@ -506,7 +507,7 @@ let gen_automatic_subdir_rules sctx ~dir ~nearest_src_dir ~src_dir =
506507
let gen_rules_regular_directory (sctx : Super_context.t Memo.t) ~src_dir ~components ~dir =
507508
Dir_status.DB.get ~dir
508509
>>= function
509-
| Lock_dir -> Memo.return Gen_rules.no_rules
510+
| Lock_dir _ -> Memo.return Gen_rules.no_rules
510511
| dir_status ->
511512
let+ rules =
512513
let* st_dir = Source_tree.find_dir src_dir in
@@ -550,7 +551,7 @@ let gen_rules_regular_directory (sctx : Super_context.t Memo.t) ~src_dir ~compon
550551
Gen_rules.rules_for ~dir ~directory_targets ~allowed_subdirs rules
551552
in
552553
match dir_status with
553-
| Lock_dir -> Gen_rules.rules_here Gen_rules.Rules.empty
554+
| Lock_dir _ -> Gen_rules.rules_here Gen_rules.Rules.empty
554555
| Source_only source_dir ->
555556
gen_rules_source_only sctx ~dir source_dir |> make_rules |> Gen_rules.rules_here
556557
| Generated | Is_component_of_a_group_but_not_the_root _ ->

src/dune_rules/ml_sources.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ let make
575575
List.fold_left
576576
dirs
577577
~init:Module_trie.empty
578-
~f:(fun acc { Source_file_dir.dir; path_to_root; files } ->
578+
~f:(fun acc { Source_file_dir.dir; path_to_root; files; source_dir = _ } ->
579579
match
580580
let path =
581581
let loc =
@@ -620,7 +620,7 @@ let make
620620
List.fold_left
621621
dirs
622622
~init:Module_name.Map.empty
623-
~f:(fun acc { Source_file_dir.dir; files; path_to_root = _ } ->
623+
~f:(fun acc { Source_file_dir.dir; files; path_to_root = _; source_dir = _ } ->
624624
let modules = modules_of_files ~dialects ~dir ~files ~path:[] in
625625
Module_name.Map.union acc modules ~f:(fun name x y ->
626626
User_error.raise

src/dune_rules/source_file_dir.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ type t =
44
{ dir : Path.Build.t
55
; path_to_root : Filename.t list
66
; files : Filename.Set.t
7+
; source_dir : Source_tree.Dir.t option
78
}

src/dune_rules/source_file_dir.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ type t =
44
{ dir : Path.Build.t
55
; path_to_root : Filename.t list
66
; files : Filename.Set.t
7+
; source_dir : Source_tree.Dir.t option
78
}

0 commit comments

Comments
 (0)