From 980eb10f45cd42d2c6b9af7d804853a682d228cb Mon Sep 17 00:00:00 2001 From: Ali Caglayan Date: Fri, 12 Sep 2025 17:59:09 +0300 Subject: [PATCH 1/2] test: overlapping buildable stanzas Signed-off-by: Ali Caglayan --- .../test-cases/overlapping-modules.t | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/blackbox-tests/test-cases/overlapping-modules.t diff --git a/test/blackbox-tests/test-cases/overlapping-modules.t b/test/blackbox-tests/test-cases/overlapping-modules.t new file mode 100644 index 00000000000..2d512ccf8bd --- /dev/null +++ b/test/blackbox-tests/test-cases/overlapping-modules.t @@ -0,0 +1,65 @@ +When executables have overlapping modules we should tell the user this is the +case. + + $ cat > dune-project < (lang dune 3.21) + > EOF + + $ cat > dune < (executable + > (name baz)) + > (executable + > (name bar)) + > EOF + $ cat > baz.ml + $ cat > bar.ml + +Currently this check is missing for overlapping executable(s) stanzas. + + $ dune build @check + File "bar.ml", line 1: + Error: Could not find the .cmi file for interface bar.mli. + File "baz.ml", line 1: + Error: Could not find the .cmi file for interface baz.mli. + [1] + +Overlapping executable and library: + + $ cat > dune < (executable + > (name baz)) + > (library + > (name bar)) + > EOF + + $ dune build @check + File "dune", line 1, characters 0-0: + Error: Module "Baz" is used in several stanzas: + - dune:1 + - dune:3 + To fix this error, you must specify an explicit "modules" field in every + library, executable, and executables stanzas in this dune file. Note that + each module cannot appear in more than one "modules" field - it must belong + to a single library or executable. + [1] + +Overlapping libraries: + + $ cat > dune < (library + > (name baz)) + > (library + > (name bar)) + > EOF + + $ dune build @check + File "dune", line 1, characters 0-0: + Error: Module "Baz" is used in several stanzas: + - dune:1 + - dune:3 + To fix this error, you must specify an explicit "modules" field in every + library, executable, and executables stanzas in this dune file. Note that + each module cannot appear in more than one "modules" field - it must belong + to a single library or executable. + [1] + From 5e0da99c898a382ff3d90d05e2238b70985c57bc Mon Sep 17 00:00:00 2001 From: Ali Caglayan Date: Fri, 12 Sep 2025 18:36:20 +0300 Subject: [PATCH 2/2] fix: check for overlapping executables Signed-off-by: Ali Caglayan --- src/dune_rules/ml_sources.ml | 23 +++++++++++-------- .../test-cases/overlapping-modules.t | 12 ++++++---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/dune_rules/ml_sources.ml b/src/dune_rules/ml_sources.ml index 389f156a1a2..a697eae6565 100644 --- a/src/dune_rules/ml_sources.ml +++ b/src/dune_rules/ml_sources.ml @@ -265,20 +265,25 @@ let find_origin (t : t) ~libs path = | origins -> raise_module_conflict_error origins ~module_path:path) ;; -let modules_and_obj_dir t ~libs ~for_ = - match - match for_ with - | Library lib_id -> Lib_id.Local.Map.find t.modules.libraries lib_id - | Exe { first_exe } -> String.Map.find t.modules.executables first_exe - | Melange { target } -> String.Map.find t.modules.melange_emits target - with - | Some (Library _, modules, obj_dir) -> - let* () = +let find_and_validate_modules t ~libs ~for_ = + (match for_ with + | Library lib_id -> Lib_id.Local.Map.find t.modules.libraries lib_id + | Exe { first_exe } -> String.Map.find t.modules.executables first_exe + | Melange { target } -> String.Map.find t.modules.melange_emits target) + |> Memo.Option.map ~f:(fun (origin, modules, obj_dir) -> + let+ () = Modules.fold_user_written modules ~init:[] ~f:(fun m acc -> Module.path m :: acc) |> Memo.List.iter ~f:(fun module_path -> let+ (_origin : Origin.t option) = find_origin t ~libs module_path in ()) in + origin, modules, obj_dir) +;; + +let modules_and_obj_dir t ~libs ~for_ = + find_and_validate_modules t ~libs ~for_ + >>= function + | Some (Library _, modules, obj_dir) -> (match Path.Build.Map.find_exn t.modules.libraries_by_obj_dir (Obj_dir.obj_dir obj_dir) with diff --git a/test/blackbox-tests/test-cases/overlapping-modules.t b/test/blackbox-tests/test-cases/overlapping-modules.t index 2d512ccf8bd..03649d69154 100644 --- a/test/blackbox-tests/test-cases/overlapping-modules.t +++ b/test/blackbox-tests/test-cases/overlapping-modules.t @@ -17,10 +17,14 @@ case. Currently this check is missing for overlapping executable(s) stanzas. $ dune build @check - File "bar.ml", line 1: - Error: Could not find the .cmi file for interface bar.mli. - File "baz.ml", line 1: - Error: Could not find the .cmi file for interface baz.mli. + File "dune", line 1, characters 0-0: + Error: Module "Baz" is used in several stanzas: + - dune:1 + - dune:3 + To fix this error, you must specify an explicit "modules" field in every + library, executable, and executables stanzas in this dune file. Note that + each module cannot appear in more than one "modules" field - it must belong + to a single library or executable. [1] Overlapping executable and library: