Skip to content

Commit a24e500

Browse files
committed
Parse alerts as alerts directly
Signed-off-by: Ambre Austen Suhamy <[email protected]>
1 parent 64a1c1c commit a24e500

File tree

12 files changed

+380
-138
lines changed

12 files changed

+380
-138
lines changed

bin/rpc/rpc_common.ml

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -151,50 +151,97 @@ let fire_notification
151151
send_request connection name ~f:(fun client -> notify_exn client notification arg)
152152
;;
153153

154+
let print_err_warn_alert =
155+
let plural x = if x = 1 then "" else "s" in
156+
function
157+
| 0, 0, 0 ->
158+
Code_error.raise
159+
"Build via RPC failed, but the RPC server did not send an error message."
160+
[]
161+
| 0, 0, a ->
162+
User_warning.emit
163+
[ Pp.paragraphf "Build completed with %d alert%s." a (plural a)
164+
|> Pp.tag User_message.Style.Warning
165+
]
166+
| 0, w, 0 ->
167+
User_warning.emit
168+
[ Pp.paragraphf "Build completed with %d warning%s." w (plural w)
169+
|> Pp.tag User_message.Style.Warning
170+
]
171+
| 0, w, a ->
172+
User_warning.emit
173+
[ Pp.paragraphf
174+
"Build completed with %d warning%s and %d alert%s."
175+
w
176+
(plural w)
177+
a
178+
(plural a)
179+
|> Pp.tag User_message.Style.Warning
180+
]
181+
| e, 0, 0 ->
182+
User_error.raise
183+
[ Pp.paragraphf "Build failed with %d error%s." e (plural e)
184+
|> Pp.tag User_message.Style.Error
185+
]
186+
| e, 0, a ->
187+
User_error.raise
188+
[ Pp.paragraphf
189+
"Build failed with %d error%s and %d alert%s."
190+
e
191+
(plural e)
192+
a
193+
(plural a)
194+
|> Pp.tag User_message.Style.Error
195+
]
196+
| e, w, 0 ->
197+
User_error.raise
198+
[ Pp.paragraphf
199+
"Build failed with %d error%s and %d warning%s."
200+
e
201+
(plural e)
202+
w
203+
(plural w)
204+
|> Pp.tag User_message.Style.Error
205+
]
206+
| e, w, a ->
207+
User_error.raise
208+
[ Pp.paragraphf
209+
"Build failed with %d error%s, %d warning%s, and %d alert%s."
210+
e
211+
(plural e)
212+
w
213+
(plural w)
214+
a
215+
(plural a)
216+
|> Pp.tag User_message.Style.Error
217+
]
218+
;;
219+
154220
let wrap_build_outcome_exn ~print_on_success build_outcome =
155221
match build_outcome with
156222
| Dune_rpc.Build_outcome_with_diagnostics.Success ->
157223
if print_on_success
158224
then Console.print [ Pp.text "Success" |> Pp.tag User_message.Style.Success ]
159225
| Failure errors ->
160-
let nb_errors, nb_warnings =
226+
let nb_errors, nb_warnings, nb_alerts =
161227
List.fold_left
162228
errors
163-
~init:(0, 0)
229+
~init:(0, 0, 0)
164230
~f:
165231
(fun
166-
(nb_errors, nb_warnings) { Dune_rpc.Compound_user_error.main; severity; _ } ->
232+
(nb_errors, nb_warnings, nb_alerts)
233+
{ Dune_rpc.Compound_user_error.main; severity; _ }
234+
->
167235
match severity with
168236
| Error ->
169237
Console.print_user_message main;
170-
nb_errors + 1, nb_warnings
238+
nb_errors + 1, nb_warnings, nb_alerts
171239
| Warning ->
172240
User_warning.emit_message main;
173-
nb_errors, nb_warnings + 1)
241+
nb_errors, nb_warnings + 1, nb_alerts
242+
| Alert ->
243+
Console.print_user_message main;
244+
nb_errors, nb_warnings, nb_alerts + 1)
174245
in
175-
(match nb_errors, nb_warnings with
176-
| 0, 0 ->
177-
Code_error.raise
178-
"Build via RPC failed, but the RPC server did not send an error message."
179-
[]
180-
| 0, n ->
181-
User_warning.emit
182-
[ Pp.paragraphf
183-
"Build completed with %d warning%s."
184-
n
185-
(if n = 1 then "" else "s")
186-
|> Pp.tag User_message.Style.Warning
187-
]
188-
| n, m ->
189-
User_error.raise
190-
[ Pp.paragraphf
191-
"Build failed with %d error%s%s."
192-
n
193-
(if n = 1 then "" else "s")
194-
(match m with
195-
| 0 -> ""
196-
| 1 -> "and 1 warning"
197-
| m -> "and " ^ string_of_int m ^ " warnings")
198-
|> Pp.tag User_message.Style.Error
199-
])
246+
print_err_warn_alert (nb_errors, nb_warnings, nb_alerts)
200247
;;

otherlibs/dune-rpc/private/diagnostics_v1.ml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
open Import
22
open Exported_types
33

4+
type severity =
5+
| Error
6+
| Warning
7+
48
module Related = struct
59
type t =
610
{ message : unit Pp.t
@@ -32,15 +36,15 @@ type t =
3236
; id : Diagnostic.Id.t
3337
; message : unit Pp.t
3438
; loc : Loc.t option
35-
; severity : Diagnostic.severity option
39+
; severity : severity option
3640
; promotion : Diagnostic.Promotion.t list
3741
; directory : string option
3842
; related : Related.t list
3943
}
4044

4145
let sexp_severity =
4246
let open Conv in
43-
enum [ "error", Diagnostic.Error; "warning", Warning ]
47+
enum [ "error", Error; "warning", Warning ]
4448
;;
4549

4650
let sexp =
@@ -69,7 +73,10 @@ let to_diagnostic t : Diagnostic.t =
6973
{ targets = t.targets
7074
; message = t.message |> Pp.map_tags ~f:(fun _ -> User_message.Style.Details)
7175
; loc = t.loc
72-
; severity = t.severity
76+
; severity =
77+
Option.map t.severity ~f:(function
78+
| Error -> Diagnostic.Error
79+
| Warning -> Warning)
7380
; promotion = t.promotion
7481
; directory = t.directory
7582
; id = t.id
@@ -81,7 +88,11 @@ let of_diagnostic (t : Diagnostic.t) =
8188
{ targets = t.targets
8289
; message = t.message |> Pp.map_tags ~f:(fun _ -> ())
8390
; loc = t.loc
84-
; severity = t.severity
91+
; severity =
92+
Option.map t.severity ~f:(function
93+
| Diagnostic.Error -> Error
94+
| Warning -> Warning
95+
| Alert -> Warning)
8596
; promotion = t.promotion
8697
; directory = t.directory
8798
; id = t.id
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
(** V1 of the diagnostics module. *)
22

3+
type severity =
4+
| Error
5+
| Warning
6+
37
module Related : sig
48
type t
59

@@ -9,14 +13,14 @@ end
913

1014
type t
1115

12-
val sexp : (t, Conv.values) Conv.t
16+
val sexp : t Conv.value
1317
val to_diagnostic : t -> Exported_types.Diagnostic.t
1418
val of_diagnostic : Exported_types.Diagnostic.t -> t
1519

1620
module Event : sig
1721
type t
1822

19-
val sexp : (t, Conv.values) Conv.t
23+
val sexp : t Conv.value
2024
val to_event : t -> Exported_types.Diagnostic.Event.t
2125
val of_event : Exported_types.Diagnostic.Event.t -> t
2226
end
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
open Import
2+
open Exported_types
3+
4+
type t =
5+
{ targets : Target.t list
6+
; id : Diagnostic.Id.t
7+
; message : User_message.Style.t Pp.t
8+
; loc : Loc.t option
9+
; severity : Diagnostics_v1.severity option
10+
; promotion : Diagnostic.Promotion.t list
11+
; directory : string option
12+
; related : Diagnostic.Related.t list
13+
}
14+
15+
let sexp_severity =
16+
let open Conv in
17+
enum [ "error", Diagnostics_v1.Error; "warning", Warning ]
18+
;;
19+
20+
let sexp =
21+
let open Conv in
22+
let from { targets; message; loc; severity; promotion; directory; id; related } =
23+
targets, message, loc, severity, promotion, directory, id, related
24+
in
25+
let to_ (targets, message, loc, severity, promotion, directory, id, related) =
26+
{ targets; message; loc; severity; promotion; directory; id; related }
27+
in
28+
let loc = field "loc" (optional Loc.sexp) in
29+
let message = field "message" (required (Pp.sexp User_message.Style.sexp)) in
30+
let targets = field "targets" (required (list Target.sexp)) in
31+
let severity = field "severity" (optional sexp_severity) in
32+
let directory = field "directory" (optional string) in
33+
let promotion = field "promotion" (required (list Diagnostic.Promotion.sexp)) in
34+
let id = field "id" (required Diagnostic.Id.sexp) in
35+
let related = field "related" (required (list Diagnostic.Related.sexp)) in
36+
iso
37+
(record (eight targets message loc severity promotion directory id related))
38+
to_
39+
from
40+
;;
41+
42+
let to_diagnostic t : Diagnostic.t =
43+
{ targets = t.targets
44+
; message = t.message
45+
; loc = t.loc
46+
; severity =
47+
Option.map t.severity ~f:(function
48+
| Error -> Diagnostic.Error
49+
| Warning -> Warning)
50+
; promotion = t.promotion
51+
; directory = t.directory
52+
; id = t.id
53+
; related = t.related
54+
}
55+
;;
56+
57+
let of_diagnostic (t : Diagnostic.t) =
58+
{ targets = t.targets
59+
; message = t.message
60+
; loc = t.loc
61+
; severity =
62+
Option.map t.severity ~f:(function
63+
| Diagnostic.Error -> Diagnostics_v1.Error
64+
| Warning -> Warning
65+
| Alert -> Warning)
66+
; promotion = t.promotion
67+
; directory = t.directory
68+
; id = t.id
69+
; related = t.related
70+
}
71+
;;
72+
73+
module Event = struct
74+
type nonrec t =
75+
| Add of t
76+
| Remove of t
77+
78+
let sexp =
79+
let diagnostic = sexp in
80+
let open Conv in
81+
let add = constr "Add" diagnostic (fun a -> Add a) in
82+
let remove = constr "Remove" diagnostic (fun a -> Remove a) in
83+
sum
84+
[ econstr add; econstr remove ]
85+
(function
86+
| Add t -> case t add
87+
| Remove t -> case t remove)
88+
;;
89+
90+
let to_event : t -> Diagnostic.Event.t = function
91+
| Add t -> Add (to_diagnostic t)
92+
| Remove t -> Remove (to_diagnostic t)
93+
;;
94+
95+
let of_event : Diagnostic.Event.t -> t = function
96+
| Add t -> Add (of_diagnostic t)
97+
| Remove t -> Remove (of_diagnostic t)
98+
;;
99+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(** V2 of the diagnostics module. *)
2+
3+
type t
4+
5+
val sexp : (t, Conv.values) Conv.t
6+
val to_diagnostic : t -> Exported_types.Diagnostic.t
7+
val of_diagnostic : Exported_types.Diagnostic.t -> t
8+
9+
module Event : sig
10+
type t
11+
12+
val sexp : t Conv.value
13+
val to_event : t -> Exported_types.Diagnostic.Event.t
14+
val of_event : Exported_types.Diagnostic.Event.t -> t
15+
end

otherlibs/dune-rpc/private/exported_types.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ module Ansi_color = struct
206206
end
207207

208208
module Pp = struct
209-
include Pp
209+
include Stdune.Pp
210210

211211
let sexp (conv_tag : 'a Conv.value) : 'a Pp.t Conv.value =
212212
let open Conv in
@@ -470,6 +470,7 @@ module Diagnostic = struct
470470
type severity =
471471
| Error
472472
| Warning
473+
| Alert
473474

474475
module Promotion = struct
475476
type t =
@@ -542,12 +543,13 @@ module Diagnostic = struct
542543

543544
let sexp_severity =
544545
let open Conv in
545-
enum [ "error", Error; "warning", Warning ]
546+
enum [ "error", Error; "warning", Warning; "alert", Alert ]
546547
;;
547548

548549
let severity_to_dyn = function
549550
| Error -> Dyn.string "error"
550551
| Warning -> Dyn.string "warning"
552+
| Alert -> Dyn.string "alert"
551553
;;
552554

553555
let sexp =
@@ -790,9 +792,7 @@ module Compound_user_error = struct
790792
match report.severity with
791793
| Error _ -> Error
792794
| Warning _ -> Warning
793-
| Alert _ ->
794-
(* FIXME: tests expect this, but it's unclear if that should change. *)
795-
Error
795+
| Alert _ -> Alert
796796
in
797797
make_with_severity ~main ~related ~severity)
798798
;;

otherlibs/dune-rpc/private/exported_types.mli

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
(** Types exposed to end-user consumers of [dune_rpc.mli]. *)
22

3+
module Pp : sig
4+
include module type of Stdune.Pp
5+
6+
val sexp : 'a Conv.value -> 'a t Conv.value
7+
end
8+
39
module Loc : sig
410
type t = Stdune.Lexbuf.Loc.t =
511
{ start : Lexing.position
@@ -80,6 +86,8 @@ module User_message : sig
8086
| Debug
8187
| Success
8288
| Ansi_styles of Ansi_color.Style.t list
89+
90+
val sexp : t Conv.value
8391
end
8492

8593
type t = Stdune.User_message.t
@@ -94,6 +102,7 @@ module Diagnostic : sig
94102
type severity =
95103
| Error
96104
| Warning
105+
| Alert
97106

98107
module Promotion : sig
99108
type t =

0 commit comments

Comments
 (0)