Skip to content

Commit 27412bc

Browse files
committed
Big refactoring in Rpc_common to unify all RPC calls, and fix incorrect error messages
Signed-off-by: Ambre Austen Suhamy <[email protected]>
1 parent 4ad9347 commit 27412bc

File tree

18 files changed

+242
-259
lines changed

18 files changed

+242
-259
lines changed

bin/build.ml

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,6 @@ let run_build_command ~(common : Common.t) ~config ~request =
142142
~request
143143
;;
144144

145-
let build_via_rpc_server ~print_on_success ~targets =
146-
Rpc.Rpc_common.wrap_build_outcome_exn
147-
~print_on_success
148-
(Rpc.Group.Build.build ~wait:true)
149-
targets
150-
()
151-
;;
152-
153145
let build =
154146
let doc = "Build the given targets, or the default ones if none are given." in
155147
let man =
@@ -202,13 +194,20 @@ let build =
202194
an RPC server in the background to schedule the fiber which will
203195
perform the RPC call.
204196
*)
205-
Rpc.Rpc_common.run_via_rpc
206-
builder
207-
lock_held_by
208-
common
209-
config
210-
(Rpc.Group.Build.build ~wait:true)
211-
targets
197+
Scheduler.go_without_rpc_server ~common ~config (fun () ->
198+
let open Fiber.O in
199+
let targets = Rpc.Group.Build.prepare_targets targets in
200+
let+ build_outcome =
201+
Rpc.Rpc_common.fire_message
202+
~name:"build"
203+
~wait:false
204+
~lock_held_by
205+
builder
206+
(Rpc.Rpc_common.Request
207+
(Dune_rpc.Decl.Request.witness Dune_rpc_impl.Decl.build))
208+
targets
209+
in
210+
Rpc.Rpc_common.wrap_build_outcome_exn ~print_on_success:true build_outcome)
212211
| Ok () ->
213212
let request setup =
214213
Target.interpret_targets (Common.root common) config setup targets

bin/build.mli

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
open Import
22

3-
(** Connect to an RPC server (waiting for the server to start if necessary) and
4-
then send a request to the server to build the specified targets. If the
5-
build fails then a diagnostic error message is printed. If
6-
[print_on_success] is true then this function will also print a message
7-
after the build succeeds. *)
8-
val build_via_rpc_server
9-
: print_on_success:bool
10-
-> targets:Dune_lang.Dep_conf.t list
11-
-> unit Fiber.t
12-
133
val run_build_system
144
: common:Common.t
155
-> request:(Dune_rules.Main.build_system -> unit Action_builder.t)

bin/diagnostics.ml

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
open Import
22

3-
let exec () =
4-
let open Fiber.O in
5-
let where = Rpc.Rpc_common.active_server_exn () in
6-
let module Client = Dune_rpc_client.Client in
7-
let+ errors =
8-
let* connect = Client.Connection.connect_exn where in
9-
Dune_rpc_impl.Client.client
10-
connect
11-
(Dune_rpc_private.Initialize.Request.create
12-
~id:(Dune_rpc_private.Id.make (Sexp.Atom "diagnostics_cmd")))
13-
~f:(fun cli ->
14-
let* decl =
15-
Client.Versioned.prepare_request cli Dune_rpc_private.Public.Request.diagnostics
16-
in
17-
match decl with
18-
| Error e -> raise (Dune_rpc_private.Version_error.E e)
19-
| Ok decl -> Client.request cli decl ())
20-
in
21-
match errors with
22-
| Ok errors ->
23-
List.iter errors ~f:(fun err ->
24-
Console.print_user_message (Dune_rpc.Diagnostic.to_user_message err))
25-
| Error e -> Rpc.Rpc_common.raise_rpc_error e
26-
;;
27-
283
let info =
294
let doc = "Fetch and return errors from the current build." in
305
Cmd.info "diagnostics" ~doc
316
;;
327

338
let term =
34-
let+ (builder : Common.Builder.t) = Common.Builder.term in
35-
Rpc.Rpc_common.client_term builder exec
9+
let+ builder = Common.Builder.term in
10+
Rpc.Rpc_common.client_term builder (fun () ->
11+
let open Fiber.O in
12+
let+ errors =
13+
Rpc.Rpc_common.fire_message
14+
~name:"diagnostics_cmd"
15+
~wait:false
16+
builder
17+
(Rpc.Rpc_common.Request Dune_rpc_private.Public.Request.diagnostics)
18+
()
19+
in
20+
List.iter errors ~f:(fun err ->
21+
Console.print_user_message (Dune_rpc.Diagnostic.to_user_message err)))
3622
;;
3723

3824
let command = Cmd.v info term

bin/exec.ml

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,12 @@ let step ~prog ~args ~common ~no_rebuild ~context ~on_exit () =
187187
directory lock.
188188
189189
Returns the absolute path to the executable. *)
190-
let build_prog_via_rpc_if_necessary ~dir ~no_rebuild prog =
190+
let build_prog_via_rpc_if_necessary ~dir ~no_rebuild ~prog builder lock_held_by =
191191
match Filename.analyze_program_name prog with
192192
| In_path ->
193193
(* This case is reached if [dune exec] is passed the name of an
194194
executable (rather than a path to an executable). When dune is running
195-
directly, dune will try to resolve the executbale name within the public
195+
directly, dune will try to resolve the executable name within the public
196196
executables defined in the current project and its dependencies, and
197197
only if no executable with the given name is found will dune then
198198
resolve the name within the $PATH variable instead. Looking up an
@@ -225,7 +225,18 @@ let build_prog_via_rpc_if_necessary ~dir ~no_rebuild prog =
225225
Dune_lang.Dep_conf.File
226226
(Dune_lang.String_with_vars.make_text Loc.none (Path.to_string path))
227227
in
228-
Build.build_via_rpc_server ~print_on_success:false ~targets:[ target ])
228+
let targets = Rpc.Group.Build.prepare_targets [ target ] in
229+
let+ build_outcome =
230+
Rpc.Rpc_common.fire_message
231+
~name:"build"
232+
~wait:true
233+
~lock_held_by
234+
builder
235+
(Rpc.Rpc_common.Request
236+
(Dune_rpc.Decl.Request.witness Dune_rpc_impl.Decl.build))
237+
targets
238+
in
239+
Rpc.Rpc_common.wrap_build_outcome_exn ~print_on_success:false build_outcome)
229240
in
230241
Path.to_absolute_filename path
231242
| Absolute ->
@@ -234,7 +245,7 @@ let build_prog_via_rpc_if_necessary ~dir ~no_rebuild prog =
234245
else not_found ~hints:[] ~prog
235246
;;
236247

237-
let exec_building_via_rpc_server ~common ~prog ~args ~no_rebuild =
248+
let exec_building_via_rpc_server ~common ~prog ~args ~no_rebuild builder lock_held_by =
238249
let open Fiber.O in
239250
let ensure_terminal v =
240251
match (v : Cmd_arg.t) with
@@ -252,7 +263,9 @@ let exec_building_via_rpc_server ~common ~prog ~args ~no_rebuild =
252263
let dir = Context_name.build_dir context in
253264
let prog = ensure_terminal prog in
254265
let args = List.map args ~f:ensure_terminal in
255-
let+ prog = build_prog_via_rpc_if_necessary ~dir ~no_rebuild prog in
266+
let+ prog =
267+
build_prog_via_rpc_if_necessary ~dir ~no_rebuild ~prog builder lock_held_by
268+
in
256269
restore_cwd_and_execve (Common.root common) prog args Env.initial
257270
;;
258271

@@ -311,18 +324,9 @@ let term : unit Term.t =
311324
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
312325
]
313326
| No ->
314-
if not (Common.Builder.equal builder Common.Builder.default)
315-
then
316-
User_warning.emit
317-
[ Pp.textf
318-
"Your build request is being forwarded to a running Dune instance%s. Note \
319-
that certain command line arguments may be ignored."
320-
(match lock_held_by with
321-
| Unknown -> ""
322-
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
323-
];
324327
Scheduler.go_without_rpc_server ~common ~config
325-
@@ fun () -> exec_building_via_rpc_server ~common ~prog ~args ~no_rebuild)
328+
@@ fun () ->
329+
exec_building_via_rpc_server ~common ~prog ~args ~no_rebuild builder lock_held_by)
326330
| Ok () -> exec_building_directly ~common ~config ~context ~prog ~args ~no_rebuild
327331
;;
328332

bin/fmt.ml

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let lock_ocamlformat () =
2626
else Fiber.return ()
2727
;;
2828

29-
let run_fmt_command ~common ~config ~preview =
29+
let run_fmt_command ~common ~config ~preview builder =
3030
let open Fiber.O in
3131
let once () =
3232
let* () = lock_ocamlformat () in
@@ -45,22 +45,17 @@ let run_fmt_command ~common ~config ~preview =
4545
| Error lock_held_by ->
4646
(* The --preview flag is being ignored by the RPC server, warn the user. *)
4747
if preview then Rpc.Rpc_common.warn_ignore_arguments lock_held_by;
48-
let response =
49-
Scheduler.go_without_rpc_server ~common ~config (fun () ->
50-
Rpc.Rpc_common.fire_request
51-
~name:"format"
52-
~wait:false
53-
Dune_rpc.Procedures.Public.format
54-
())
55-
in
56-
(match response with
57-
| Ok () -> ()
58-
| Error error ->
59-
User_error.raise
60-
[ Pp.paragraphf
61-
"Error: %s\n%!"
62-
(Dyn.to_string (Dune_rpc.Response.Error.to_dyn error))
63-
])
48+
Scheduler.go_without_rpc_server
49+
~common
50+
~config
51+
(Rpc.Rpc_common.fire_message
52+
~name:"format"
53+
~wait:false
54+
~warn_forwarding:false
55+
~lock_held_by
56+
builder
57+
(Rpc.Rpc_common.Request
58+
(Dune_rpc.Decl.Request.witness Dune_rpc.Procedures.Public.format)))
6459
;;
6560

6661
let command =
@@ -81,7 +76,7 @@ let command =
8176
Common.Builder.set_promote builder (if preview then Never else Automatically)
8277
in
8378
let common, config = Common.init builder in
84-
run_fmt_command ~common ~config ~preview
79+
run_fmt_command ~common ~config ~preview builder
8580
in
8681
Cmd.v (Cmd.info "fmt" ~doc ~man ~envs:Common.envs) term
8782
;;

bin/promotion.ml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,20 @@ module Apply = struct
6262
let+ () = Fiber.return () in
6363
Diff_promotion.promote_files_registered_in_last_run files_to_promote)
6464
| Error lock_held_by ->
65-
Rpc.Rpc_common.run_via_rpc
66-
builder
67-
lock_held_by
68-
common
69-
config
70-
(Rpc.Rpc_common.fire_request
71-
~name:"promote_many"
72-
~wait:false
73-
Dune_rpc_private.Procedures.Public.promote_many)
74-
files_to_promote
65+
Scheduler.go_without_rpc_server ~common ~config (fun () ->
66+
let open Fiber.O in
67+
let+ build_outcome =
68+
Rpc.Rpc_common.fire_message
69+
~name:"promote_many"
70+
~wait:false
71+
~lock_held_by
72+
builder
73+
(Rpc.Rpc_common.Request
74+
(Dune_rpc.Decl.Request.witness
75+
Dune_rpc_private.Procedures.Public.promote_many))
76+
files_to_promote
77+
in
78+
Rpc.Rpc_common.wrap_build_outcome_exn ~print_on_success:true build_outcome)
7579
;;
7680

7781
let command = Cmd.v info term

bin/rpc/rpc_build.ml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
open Import
22

3-
let build ~wait targets =
4-
let targets =
5-
List.map targets ~f:(fun target ->
6-
let sexp = Dune_lang.Dep_conf.encode target in
7-
Dune_lang.to_string sexp)
8-
in
9-
Rpc_common.fire_request ~name:"build" ~wait Dune_rpc_impl.Decl.build targets
3+
let prepare_targets targets =
4+
List.map targets ~f:(fun target ->
5+
let sexp = Dune_lang.Dep_conf.encode target in
6+
Dune_lang.to_string sexp)
107
;;
118

129
let term =
@@ -17,14 +14,18 @@ let term =
1714
Rpc_common.client_term builder
1815
@@ fun () ->
1916
let open Fiber.O in
20-
let+ response =
21-
Rpc_common.fire_request ~name:"build" ~wait Dune_rpc_impl.Decl.build targets
17+
let+ build_outcome =
18+
Rpc_common.fire_message
19+
~name:"build"
20+
~wait
21+
builder
22+
(Rpc_common.Request (Dune_rpc.Decl.Request.witness Dune_rpc_impl.Decl.build))
23+
targets
2224
in
23-
match response with
24-
| Error (error : Dune_rpc.Response.Error.t) ->
25-
Printf.eprintf "Error: %s\n%!" (Dyn.to_string (Dune_rpc.Response.Error.to_dyn error))
26-
| Ok Success -> print_endline "Success"
27-
| Ok (Failure _) -> print_endline "Failure"
25+
match build_outcome with
26+
(* This is the only output that the client will see, details are in the server. *)
27+
| Success -> print_endline "Success"
28+
| Failure _ -> print_endline "Failure"
2829
;;
2930

3031
let info =

bin/rpc/rpc_build.mli

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
open! Import
22

3-
(** Sends a command to an RPC server to build the specified targets and wait
4-
for the build to complete or fail. If [wait] is true then wait until an RPC
5-
server is running before making the request. Otherwise if no RPC server is
6-
running then raise a [User_error]. *)
7-
val build
8-
: wait:bool
9-
-> Dune_lang.Dep_conf.t list
10-
-> (Dune_rpc.Build_outcome_with_diagnostics.t, Dune_rpc.Response.Error.t) result Fiber.t
3+
(** Encode the targets as [Dune_lang.t], and then as strings suitable to
4+
be sent via RPC. *)
5+
val prepare_targets : Dune_lang.Dep_conf.t list -> string list
116

127
(** dune rpc build command *)
138
val cmd : unit Cmdliner.Cmd.t

0 commit comments

Comments
 (0)