diff --git a/src/dune_engine/build_system.ml b/src/dune_engine/build_system.ml index 8b59841ee042..102febcb0587 100644 --- a/src/dune_engine/build_system.ml +++ b/src/dune_engine/build_system.ml @@ -92,6 +92,15 @@ module Error = struct | Diagnostic d -> `Diagnostic d.diagnostic ;; + module For_tests = struct + let make ~id ~description () = + match description with + | `Exn exn -> Exn { id; exn } + | `Diagnostic diagnostic -> + Diagnostic { id; diagnostic; dir = None; promotion = None } + ;; + end + module Set : sig type error := t diff --git a/src/dune_engine/build_system.mli b/src/dune_engine/build_system.mli index c86e939d053a..27c14d57a93a 100644 --- a/src/dune_engine/build_system.mli +++ b/src/dune_engine/build_system.mli @@ -88,6 +88,7 @@ module Error : sig module Map : Map.S with type key = t + val gen : unit -> t val compare : t -> t -> Ordering.t val to_int : t -> int val to_dyn : t -> Dyn.t @@ -126,6 +127,18 @@ module Error : sig val current : t -> error Id.Map.t val empty : t end + + module For_tests : sig + (** Internal helpers for testing purposes. Do not use. *) + + (** Construct an [Error.t] *) + val make + : id:Id.t + -> description: + [ `Exn of Exn_with_backtrace.t | `Diagnostic of Compound_user_error.t ] + -> unit + -> t + end end (** The current set of active errors. *) diff --git a/src/dune_rpc_impl/dune_rpc_impl.ml b/src/dune_rpc_impl/dune_rpc_impl.ml index a6eb06bdde2a..c685ca918576 100644 --- a/src/dune_rpc_impl/dune_rpc_impl.ml +++ b/src/dune_rpc_impl/dune_rpc_impl.ml @@ -6,6 +6,12 @@ module Private = Dune_rpc_client.Private module Watch_mode_config = Watch_mode_config module Where = Dune_rpc_client.Where +module Diagnostics = struct + module For_tests = struct + let diagnostic_of_error = Diagnostics.diagnostic_of_error + end +end + module Poll_active = Dune_rpc_private.Registry.Poll (Fiber) diff --git a/test/expect-tests/dune_rpc/dune b/test/expect-tests/dune_rpc/dune index fb5593916ae5..77b3d7fcf356 100644 --- a/test/expect-tests/dune_rpc/dune +++ b/test/expect-tests/dune_rpc/dune @@ -5,10 +5,12 @@ (libraries ocaml_config dune_util + dune_console dune_rpc_private dune_rpc_server dune_rpc_impl dune_rpc_client + dune_engine stdune test_scheduler csexp diff --git a/test/expect-tests/dune_rpc/dune_rpc_tests.ml b/test/expect-tests/dune_rpc/dune_rpc_tests.ml index 1dcacfc169e9..fe5e7cf7e2da 100644 --- a/test/expect-tests/dune_rpc/dune_rpc_tests.ml +++ b/test/expect-tests/dune_rpc/dune_rpc_tests.ml @@ -925,3 +925,19 @@ let%expect_test "print digests for all public RPCs" = Request: Sexp Response: 33528f248084297d123a6ebd4c3ddee0 |}] ;; + +let%expect_test "serialize and then deserialize error messages" = + let id = Dune_engine.Build_system.Error.Id.gen () in + let main = User_error.make [ Pp.verbatim "Oh no!" ] in + Dune_console.print_user_message main; + let description = + `Diagnostic (Dune_engine.Compound_user_error.make ~main ~related:[]) + in + Dune_engine.Build_system.Error.For_tests.make ~id ~description () + |> Dune_rpc_impl.Diagnostics.For_tests.diagnostic_of_error + |> Dune_rpc_private.Diagnostic.to_user_message + |> Dune_console.print_user_message; + [%expect {| + Error: Oh no! + Error: Error: Oh no! |}] +;;