From 3d3c97ff3d4aa231c55ce02528f1a5c39dd50752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ch=C3=A1varri?= Date: Thu, 28 Mar 2024 10:18:28 +0000 Subject: [PATCH 1/2] merlin: add rules regardless of (merlin) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Javier Chávarri --- src/dune_rules/action_unexpanded.ml | 10 ++---- src/dune_rules/buildable_rules.ml | 18 ++++------ src/dune_rules/buildable_rules.mli | 3 +- src/dune_rules/check_rules.ml | 47 ++++++++++--------------- src/dune_rules/check_rules.mli | 16 ++------- src/dune_rules/context.ml | 20 +++-------- src/dune_rules/context.mli | 1 - src/dune_rules/copy_line_directive.ml | 36 ++++++------------- src/dune_rules/copy_line_directive.mli | 5 ++- src/dune_rules/exe_rules.ml | 8 ++--- src/dune_rules/lib_rules.ml | 10 +++--- src/dune_rules/melange/melange_rules.ml | 5 ++- src/dune_rules/merlin/merlin.ml | 4 +-- src/dune_rules/simple_rules.ml | 3 +- 14 files changed, 62 insertions(+), 124 deletions(-) diff --git a/src/dune_rules/action_unexpanded.ml b/src/dune_rules/action_unexpanded.ml index 0a009be9a91..88abcf6faf0 100644 --- a/src/dune_rules/action_unexpanded.ml +++ b/src/dune_rules/action_unexpanded.ml @@ -507,13 +507,9 @@ let rec expand (t : Dune_lang.Action.t) : Action.t Action_expander.t = and+ y = E.target y in O.Symlink (x, y) | Copy_and_add_line_directive (x, y) -> - A.with_expander (fun expander -> - Expander.context expander - |> Context.DB.get - |> Memo.map ~f:(fun context -> - let+ x = E.dep x - and+ y = E.target y in - Copy_line_directive.action context ~src:x ~dst:y)) + let+ x = E.dep x + and+ y = E.target y in + Copy_line_directive.action ~src:x ~dst:y | System x -> let+ x = E.string x in O.System x diff --git a/src/dune_rules/buildable_rules.ml b/src/dune_rules/buildable_rules.ml index 953388444b5..328ea40706f 100644 --- a/src/dune_rules/buildable_rules.ml +++ b/src/dune_rules/buildable_rules.ml @@ -36,20 +36,16 @@ let gen_select_rules sctx ~dir compile_info = let* src_fn = Resolve.read src_fn in let src = Path.build (Path.Build.relative dir src_fn) in let+ () = Action_builder.path src in - let context = Super_context.context sctx in - Action.Full.make (Copy_line_directive.action context ~src ~dst)))) + Action.Full.make (Copy_line_directive.action ~src ~dst)))) ;; -let with_lib_deps (t : Context.t) compile_info ~dir ~f = +let with_lib_deps compile_info ~dir ~f = let prefix = - if Context.merlin t - then - Lib.Compile.merlin_ident compile_info - |> Merlin_ident.merlin_file_path dir - |> Path.build - |> Action_builder.path - |> Action_builder.goal - else Action_builder.return () + Lib.Compile.merlin_ident compile_info + |> Merlin_ident.merlin_file_path dir + |> Path.build + |> Action_builder.path + |> Action_builder.goal in Rules.prefix_rules prefix ~f ;; diff --git a/src/dune_rules/buildable_rules.mli b/src/dune_rules/buildable_rules.mli index 227d33b3549..f536fcfbca2 100644 --- a/src/dune_rules/buildable_rules.mli +++ b/src/dune_rules/buildable_rules.mli @@ -14,8 +14,7 @@ val gen_select_rules : Super_context.t -> dir:Path.Build.t -> Lib.Compile.t -> u (** Generate the rules for the [(select ...)] forms in library dependencies *) val with_lib_deps - : Context.t - -> Lib.Compile.t + : Lib.Compile.t -> dir:Path.Build.t -> f:(unit -> 'a Memo.t) -> 'a Memo.t diff --git a/src/dune_rules/check_rules.ml b/src/dune_rules/check_rules.ml index 3f861205b57..a2c12cb1916 100644 --- a/src/dune_rules/check_rules.ml +++ b/src/dune_rules/check_rules.ml @@ -6,37 +6,28 @@ let dev_files = |> Glob.matching_extensions ;; -let add_obj_dir sctx ~obj_dir mode = - if Super_context.context sctx |> Context.merlin - then ( - let dir_glob = - let dir = - Path.build - (match mode with - | Lib_mode.Melange -> Obj_dir.melange_dir obj_dir - | Ocaml _ -> Obj_dir.byte_dir obj_dir) - in - File_selector.of_glob ~dir dev_files +let add_obj_dir ~obj_dir mode = + let dir_glob = + let dir = + Path.build + (match mode with + | Lib_mode.Melange -> Obj_dir.melange_dir obj_dir + | Ocaml _ -> Obj_dir.byte_dir obj_dir) in - Rules.Produce.Alias.add_deps - (Alias.make Alias0.check ~dir:(Obj_dir.dir obj_dir)) - (Action_builder.paths_matching_unit ~loc:(Loc.of_pos __POS__) dir_glob)) - else Memo.return () + File_selector.of_glob ~dir dev_files + in + Rules.Produce.Alias.add_deps + (Alias.make Alias0.check ~dir:(Obj_dir.dir obj_dir)) + (Action_builder.paths_matching_unit ~loc:(Loc.of_pos __POS__) dir_glob) ;; -let add_files sctx ~dir files = - if Super_context.context sctx |> Context.merlin - then ( - let alias = Alias.make Alias0.check ~dir in - let files = Path.Set.of_list files in - Rules.Produce.Alias.add_deps alias (Action_builder.path_set files)) - else Memo.return () +let add_files ~dir files = + let alias = Alias.make Alias0.check ~dir in + let files = Path.Set.of_list files in + Rules.Produce.Alias.add_deps alias (Action_builder.path_set files) ;; -let add_cycle_check sctx ~dir modules = - if Super_context.context sctx |> Context.merlin - then ( - let alias = Alias.make Alias0.check ~dir in - Rules.Produce.Alias.add_deps alias (Action_builder.ignore modules)) - else Memo.return () +let add_cycle_check ~dir modules = + let alias = Alias.make Alias0.check ~dir in + Rules.Produce.Alias.add_deps alias (Action_builder.ignore modules) ;; diff --git a/src/dune_rules/check_rules.mli b/src/dune_rules/check_rules.mli index 72d648c6473..3332a3fe605 100644 --- a/src/dune_rules/check_rules.mli +++ b/src/dune_rules/check_rules.mli @@ -1,15 +1,5 @@ open Import -val add_obj_dir - : Super_context.t - -> obj_dir:Path.Build.t Obj_dir.t - -> Lib_mode.t - -> unit Memo.t - -val add_files : Super_context.t -> dir:Path.Build.t -> Path.t list -> unit Memo.t - -val add_cycle_check - : Super_context.t - -> dir:Path.Build.t - -> Module.t list Action_builder.t - -> unit Memo.t +val add_obj_dir : obj_dir:Path.Build.t Obj_dir.t -> Lib_mode.t -> unit Memo.t +val add_files : dir:Path.Build.t -> Path.t list -> unit Memo.t +val add_cycle_check : dir:Path.Build.t -> Module.t list Action_builder.t -> unit Memo.t diff --git a/src/dune_rules/context.ml b/src/dune_rules/context.ml index 9dcbc39d18f..c3ea83fd826 100644 --- a/src/dune_rules/context.ml +++ b/src/dune_rules/context.ml @@ -67,7 +67,6 @@ end type builder = { profile : Profile.t - ; merlin : bool ; instrument_with : Lib_name.t list ; fdo_target_exe : Path.t option ; dynamically_linked_foreign_archives : bool @@ -96,7 +95,6 @@ module Builder = struct let empty = { profile = Profile.Dev - ; merlin = false ; instrument_with = [] ; fdo_target_exe = None ; dynamically_linked_foreign_archives = false @@ -142,7 +140,7 @@ module Builder = struct ; fdo_target_exe ; dynamically_linked_foreign_archives ; instrument_with - ; merlin + ; merlin = _ } = let env = @@ -150,8 +148,7 @@ module Builder = struct extend_paths ~env paths in { t with - merlin - ; profile + profile ; dynamically_linked_foreign_archives ; instrument_with ; fdo_target_exe @@ -182,7 +179,6 @@ let dynamically_linked_foreign_archives t = let fdo_target_exe t = t.builder.fdo_target_exe let instrument_with t = t.builder.instrument_with -let merlin t = t.builder.merlin let profile t = t.builder.profile let equal x y = Context_name.equal x.builder.name y.builder.name let hash t = Context_name.hash t.builder.name @@ -207,7 +203,6 @@ let to_dyn t : Dyn.t = [ "name", Context_name.to_dyn t.builder.name ; "kind", Kind.to_dyn t.kind ; "profile", Profile.to_dyn t.builder.profile - ; "merlin", Bool t.builder.merlin ; "fdo_target_exe", option path t.builder.fdo_target_exe ; "build_dir", Path.Build.to_dyn t.build_dir ; "instrument_with", (list Lib_name.to_dyn) t.builder.instrument_with @@ -522,11 +517,7 @@ module Group = struct in let targets = let builder = - { builder with - implicit = false - ; merlin = false - ; for_host = Some (name, Memo.Lazy.force native) - } + { builder with implicit = false; for_host = Some (name, Memo.Lazy.force native) } in List.filter_map targets ~f:(function | Native -> None @@ -608,10 +599,7 @@ module Group = struct in match context with | Opam opam -> Builder.set_workspace_base builder opam.base - | Default default -> - let builder = Builder.set_workspace_base builder default.base in - let merlin = workspace.merlin_context = Some (Workspace.Context.name context) in - { builder with merlin } + | Default default -> Builder.set_workspace_base builder default.base in match context with | Opam { base; switch } -> diff --git a/src/dune_rules/context.mli b/src/dune_rules/context.mli index c97052aafa4..5a8363ba855 100644 --- a/src/dune_rules/context.mli +++ b/src/dune_rules/context.mli @@ -50,7 +50,6 @@ val default_ocamlpath : t -> Path.t list Memo.t val findlib_toolchain : t -> Context_name.t option val instrument_with : t -> Lib_name.t list val profile : t -> Profile.t -val merlin : t -> bool val equal : t -> t -> bool val hash : t -> int val to_dyn : t -> Dyn.t diff --git a/src/dune_rules/copy_line_directive.ml b/src/dune_rules/copy_line_directive.ml index a6acc865946..8311f2bbc6d 100644 --- a/src/dune_rules/copy_line_directive.ml +++ b/src/dune_rules/copy_line_directive.ml @@ -58,62 +58,46 @@ let line_directive ~filename:fn ~line_number = ;; module Spec = struct - type merlin = - | Yes - | No - - let bool_of_merlin = function - | Yes -> true - | No -> false - ;; - - type ('path, 'target) t = 'path * 'target * merlin + type ('path, 'target) t = 'path * 'target let name = "copy-line-directive" let version = 1 - let bimap (src, dst, merlin) f g = f src, g dst, merlin + let bimap (src, dst) f g = f src, g dst let is_useful_to ~memoize = memoize - let encode (src, dst, merlin) path target : Dune_lang.t = - List - [ Dune_lang.atom_or_quoted_string "copy-line-directive" - ; path src - ; target dst - ; Dune_lang.atom_or_quoted_string (Bool.to_string (bool_of_merlin merlin)) - ] + let encode (src, dst) path target : Dune_lang.t = + List [ Dune_lang.atom_or_quoted_string "copy-line-directive"; path src; target dst ] ;; - let action (src, dst, merlin) ~ectx:_ ~eenv:_ = + let action (src, dst) ~ectx:_ ~eenv:_ = Io.with_file_in src ~f:(fun ic -> Path.build dst |> Io.with_file_out ~f:(fun oc -> let fn = Path.drop_optional_build_context_maybe_sandboxed src in output_string oc (line_directive ~filename:(Path.to_string fn) ~line_number:1); Io.copy_channels ic oc)); - (match merlin with - | No -> () - | Yes -> Path.as_in_build_dir src |> Option.iter ~f:(fun src -> DB.set ~src ~dst)); + Path.as_in_build_dir src |> Option.iter ~f:(fun src -> DB.set ~src ~dst); Fiber.return () ;; end -let action (context : Context.t) ~src ~dst = +let action ~src ~dst = let module M = struct type path = Path.t type target = Path.Build.t module Spec = Spec - let v = src, dst, if Context.merlin context then Spec.Yes else No + let v = src, dst end in Action.Extension (module M) ;; -let builder context ~src ~dst = +let builder ~src ~dst = let open Action_builder.O in Action_builder.with_file_targets ~file_targets:[ dst ] (Action_builder.path src - >>> Action_builder.return (Action.Full.make (action context ~src ~dst))) + >>> Action_builder.return (Action.Full.make (action ~src ~dst))) ;; diff --git a/src/dune_rules/copy_line_directive.mli b/src/dune_rules/copy_line_directive.mli index e05cfb5a5bb..3179cf67c07 100644 --- a/src/dune_rules/copy_line_directive.mli +++ b/src/dune_rules/copy_line_directive.mli @@ -4,10 +4,9 @@ module DB : sig val follow_while : Path.Build.t -> f:(Path.Build.t -> 'a option) -> 'a option end -val action : Context.t -> src:Path.t -> dst:Path.Build.t -> Action.t +val action : src:Path.t -> dst:Path.Build.t -> Action.t val builder - : Context.t - -> src:Path.t + : src:Path.t -> dst:Path.Build.t -> Action.Full.t Action_builder.With_targets.t diff --git a/src/dune_rules/exe_rules.ml b/src/dune_rules/exe_rules.ml index 33a40f54019..f382a03f463 100644 --- a/src/dune_rules/exe_rules.ml +++ b/src/dune_rules/exe_rules.ml @@ -129,7 +129,7 @@ let executables_rules Dir_contents.ocaml dir_contents >>| Ml_sources.modules_and_obj_dir ~for_:(Exe { first_exe }) in - let* () = Check_rules.add_obj_dir sctx ~obj_dir (Ocaml Byte) in + let* () = Check_rules.add_obj_dir ~obj_dir (Ocaml Byte) in let ctx = Super_context.context sctx in let* ocaml = Context.ocaml ctx in let project = Scope.project scope in @@ -220,7 +220,7 @@ let executables_rules let* o_files = o_files sctx ~dir ~expander ~exes ~linkages ~dir_contents ~requires_compile in - let* () = Check_rules.add_files sctx ~dir @@ Mode.Map.Multi.to_flat_list o_files in + let* () = Check_rules.add_files ~dir @@ Mode.Map.Multi.to_flat_list o_files in let buildable = exes.buildable in match buildable.ctypes with | None -> @@ -257,7 +257,7 @@ let executables_rules link in let+ () = - Memo.parallel_iter dep_graphs.for_exes ~f:(Check_rules.add_cycle_check sctx ~dir) + Memo.parallel_iter dep_graphs.for_exes ~f:(Check_rules.add_cycle_check ~dir) in ( cctx , Merlin.make @@ -315,5 +315,5 @@ let rules ~sctx ~dir ~dir_contents ~scope ~expander (exes : Executables.t) = let requires_link = Lib.Compile.requires_link compile_info in Bootstrap_info.gen_rules sctx exes ~dir ~requires_link in - Buildable_rules.with_lib_deps (Super_context.context sctx) compile_info ~dir ~f + Buildable_rules.with_lib_deps compile_info ~dir ~f ;; diff --git a/src/dune_rules/lib_rules.ml b/src/dune_rules/lib_rules.ml index adf93213539..7b2fe36da1c 100644 --- a/src/dune_rules/lib_rules.ml +++ b/src/dune_rules/lib_rules.ml @@ -257,7 +257,7 @@ let foreign_rules (library : Foreign.Library.t) ~sctx ~expander ~dir ~dir_conten in Mode.Map.Multi.for_all_modes o_files_by_mode in - let* () = Check_rules.add_files sctx ~dir o_files in + let* () = Check_rules.add_files ~dir o_files in let* standard = let+ project = Dune_load.find_project ~dir in match Dune_project.use_standard_c_and_cxx_flags project with @@ -305,7 +305,7 @@ let build_stubs lib ~cctx ~dir ~expander ~requires ~dir_contents ~vlib_stubs_o_f Mode.Map.Multi.add_all tbl Mode.Select.All lib_foreign_o_files in let all_o_files = Mode.Map.Multi.to_flat_list o_files in - let* () = Check_rules.add_files sctx ~dir all_o_files in + let* () = Check_rules.add_files ~dir all_o_files in if List.for_all ~f:List.is_empty [ all_o_files; vlib_stubs_o_files ] then Memo.return () else ( @@ -588,7 +588,7 @@ let library_rules Memo.Option.iter vimpl ~f:(Virtual_rules.setup_copy_rules_for_impl ~sctx ~dir) in let* expander = Super_context.expander sctx ~dir in - let* () = Check_rules.add_cycle_check sctx ~dir top_sorted_modules in + let* () = Check_rules.add_cycle_check ~dir top_sorted_modules in let* () = gen_wrapped_compat_modules lib cctx and* () = Module_compilation.build_all cctx and* lib_info = @@ -601,7 +601,7 @@ let library_rules ~lib_config in let mode = Lib_mode.Map.Set.for_merlin (Lib_info.modes info) in - let+ () = Check_rules.add_obj_dir sctx ~obj_dir mode in + let+ () = Check_rules.add_obj_dir ~obj_dir mode in info in let+ () = @@ -673,5 +673,5 @@ let rules (lib : Library.t) ~sctx ~dir_contents ~dir ~expander ~scope = ~ctx_dir:dir in let* () = Buildable_rules.gen_select_rules sctx compile_info ~dir in - Buildable_rules.with_lib_deps (Super_context.context sctx) compile_info ~dir ~f + Buildable_rules.with_lib_deps compile_info ~dir ~f ;; diff --git a/src/dune_rules/melange/melange_rules.ml b/src/dune_rules/melange/melange_rules.ml index 778005b4cb0..3a194997f08 100644 --- a/src/dune_rules/melange/melange_rules.ml +++ b/src/dune_rules/melange/melange_rules.ml @@ -257,7 +257,6 @@ let setup_emit_cmj_rules = let open Memo.O in let* compile_info = compile_info ~scope mel in - let ctx = Super_context.context sctx in let f () = (* Use "mobjs" rather than "objs" to avoid a potential conflict with a library of the same name *) @@ -265,7 +264,7 @@ let setup_emit_cmj_rules Dir_contents.ocaml dir_contents >>| Ml_sources.modules_and_obj_dir ~for_:(Melange { target = mel.target }) in - let* () = Check_rules.add_obj_dir sctx ~obj_dir Melange in + let* () = Check_rules.add_obj_dir ~obj_dir Melange in let* modules, pp = Buildable_rules.modules_rules sctx @@ -351,7 +350,7 @@ let setup_emit_cmj_rules ~modes:`Melange_emit ) in let* () = Buildable_rules.gen_select_rules sctx compile_info ~dir in - Buildable_rules.with_lib_deps ctx compile_info ~dir ~f + Buildable_rules.with_lib_deps compile_info ~dir ~f ;; module Runtime_deps = struct diff --git a/src/dune_rules/merlin/merlin.ml b/src/dune_rules/merlin/merlin.ml index 95ff0eb879f..628bb1686e9 100644 --- a/src/dune_rules/merlin/merlin.ml +++ b/src/dune_rules/merlin/merlin.ml @@ -600,9 +600,7 @@ let dot_merlin sctx ~dir ~more_src_dirs ~expander (t : Unprocessed.t) = ;; let add_rules sctx ~dir ~more_src_dirs ~expander merlin = - Memo.when_ - (Context.merlin (Super_context.context sctx)) - (fun () -> dot_merlin sctx ~more_src_dirs ~expander ~dir merlin) + dot_merlin sctx ~more_src_dirs ~expander ~dir merlin ;; let more_src_dirs dir_contents ~source_dirs = diff --git a/src/dune_rules/simple_rules.ml b/src/dune_rules/simple_rules.ml index bee79dd7453..90692f617c1 100644 --- a/src/dune_rules/simple_rules.ml +++ b/src/dune_rules/simple_rules.ml @@ -237,14 +237,13 @@ let copy_files sctx ~dir ~expander ~src_dir (def : Copy_files.t) = ~f:(fun basename -> let file_src = Path.relative src_in_build basename in let file_dst = Path.Build.relative dir basename in - let context = Super_context.context sctx in Super_context.add_rule sctx ~loc ~dir ~mode:def.mode ((if def.add_line_directive - then Copy_line_directive.builder context + then Copy_line_directive.builder else Action_builder.copy) ~src:file_src ~dst:file_dst)) From 0c00e11686245d7ff3490151e8582ece57d12d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ch=C3=A1varri?= Date: Thu, 28 Mar 2024 10:51:19 +0000 Subject: [PATCH 2/2] merlin: update tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Javier Chávarri --- .../ppx-cross-context-issue.t/run.t | 3 ++- .../merlin/default-based-context.t/run.t | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/test/blackbox-tests/test-cases/custom-cross-compilation/ppx-cross-context-issue.t/run.t b/test/blackbox-tests/test-cases/custom-cross-compilation/ppx-cross-context-issue.t/run.t index 05cf21797f8..b56dedf1bda 100644 --- a/test/blackbox-tests/test-cases/custom-cross-compilation/ppx-cross-context-issue.t/run.t +++ b/test/blackbox-tests/test-cases/custom-cross-compilation/ppx-cross-context-issue.t/run.t @@ -6,7 +6,8 @@ ^^^^^^ Error: Library "fooppx" in _build/cross-environment/ppx is hidden (unsatisfied 'enabled_if'). - -> required by _build/cross-environment/lib/lib.pp.ml + -> required by _build/cross-environment/lib/.merlin-conf/lib-foolib + -> required by _build/cross-environment/lib/foolib.a -> required by alias lib/all (context cross-environment) -> required by alias default (context cross-environment) [1] diff --git a/test/blackbox-tests/test-cases/merlin/default-based-context.t/run.t b/test/blackbox-tests/test-cases/merlin/default-based-context.t/run.t index d29e5ee4eee..c6a3b81728f 100644 --- a/test/blackbox-tests/test-cases/merlin/default-based-context.t/run.t +++ b/test/blackbox-tests/test-cases/merlin/default-based-context.t/run.t @@ -13,14 +13,20 @@ If Merlin field is absent, default context is chosen $ dune build - $ [ ! -d _build/cross/.merlin-conf ] && echo "No config in cross" - No config in cross +The rules are generated for all contexts, regardless which one is chosen + + $ ls -a _build/cross/.merlin-conf + . + .. + lib-foo $ ls -a _build/default/.merlin-conf . .. lib-foo +But the default context is used by default + $ dune ocaml merlin dump-config "$PWD" Foo: _build/default/foo ((STDLIB OPAM_PREFIX) @@ -56,8 +62,10 @@ If Merlin field is present, this context is chosen .. lib-foo - $ [ ! -d _build/default/.merlin-conf ] && echo "No config in default" - No config in default + $ ls -a _build/default/.merlin-conf + . + .. + lib-foo $ dune ocaml merlin dump-config "$PWD" Foo: _build/cross/foo