Skip to content

Commit ccd1a83

Browse files
authored
Fix handling of root module dependencies (ocaml#12227)
* test: demonstrate pointless running of ocamldep on the root module Signed-off-by: Rudi Grinberg <[email protected]> * fix: root module should have no dependencies The root module is generated and only depends on the entry modules of the libraries field. Signed-off-by: Rudi Grinberg <[email protected]> --------- Signed-off-by: Rudi Grinberg <[email protected]>
2 parents 13da9c7 + a5d8945 commit ccd1a83

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

doc/changes/12227.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Don't run ocamldep to compute false dependencies on the `root_module`
2+
(#12227, @rgrinberg)

src/dune_rules/dep_rules.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ let rec deps_of
137137
~ml_kind
138138
(m : Modules.Sourced_module.t)
139139
=
140-
let is_alias =
140+
let is_alias_or_root =
141141
match m with
142142
| Impl_of_virtual_module _ -> false
143143
| Imported_from_vlib m | Normal m ->
144144
(match Module.kind m with
145-
| Alias _ -> true
145+
| Root | Alias _ -> true
146146
| _ -> false)
147147
in
148-
if is_alias
148+
if is_alias_or_root
149149
then Memo.return (Action_builder.return [])
150150
else (
151151
let skip_if_source_absent f sourced_module =
@@ -176,7 +176,7 @@ let has_single_file modules = Option.is_some @@ Modules.With_vlib.as_singleton m
176176

177177
let immediate_deps_of unit modules ~obj_dir ~ml_kind =
178178
match Module.kind unit with
179-
| Alias _ -> Action_builder.return []
179+
| Root | Alias _ -> Action_builder.return []
180180
| Wrapped_compat ->
181181
let interface_module =
182182
match Modules.With_vlib.lib_interface modules with

src/dune_rules/obj_dir.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ module Module = struct
618618
match (dep : Dep.t) with
619619
| Immediate (m, _) | Transitive (m, _) ->
620620
(match Module.kind m with
621-
| Module.Kind.Alias _ -> None
621+
| Module.Kind.Alias _ | Root -> None
622622
| _ ->
623623
let dir = obj_dir t in
624624
let name = Dep.basename dep in

src/dune_rules/ocamldep.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let parse_deps_exn =
109109
let transitive_deps =
110110
let transive_dep obj_dir m =
111111
(match Module.kind m with
112-
| Alias _ -> None
112+
| Root | Alias _ -> None
113113
| _ -> if Module.has m ~ml_kind:Intf then Some Ml_kind.Intf else Some Impl)
114114
|> Option.map ~f:(fun ml_kind ->
115115
Obj_dir.Module.dep obj_dir (Transitive (m, ml_kind))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Demonstrate if we're running ocamldep for the root module. There should be no
2+
need to do so since this module cannot depend on any other module in the same
3+
compilation unit.
4+
5+
$ cat >dune-project <<EOF
6+
> (lang dune 3.20)
7+
> EOF
8+
9+
$ mkdir lib/
10+
$ cat >lib/dune <<EOF
11+
> (library
12+
> (name bar))
13+
> EOF
14+
$ touch lib/bar.ml
15+
16+
$ cat >dune <<EOF
17+
> (library
18+
> (name foo)
19+
> (libraries bar)
20+
> (root_module root))
21+
> EOF
22+
23+
$ dune build foo.cma
24+
25+
$ find _build/default/.foo.objs | grep -i root | sort -u
26+
_build/default/.foo.objs/byte/foo__Root.cmi
27+
_build/default/.foo.objs/byte/foo__Root.cmo
28+
_build/default/.foo.objs/byte/foo__Root.cmt
29+
30+
Not only is running ocamldep wasteful, but it can also lead to cycles:
31+
32+
$ cat >bar.ml <<EOF
33+
> module X = Root.Bar
34+
> EOF
35+
36+
$ dune build foo.cma

0 commit comments

Comments
 (0)