Skip to content

Commit c9e8fa3

Browse files
committed
Fix promotion not having warnings
Signed-off-by: Ambre Austen Suhamy <[email protected]>
1 parent f6f6331 commit c9e8fa3

File tree

20 files changed

+277
-153
lines changed

20 files changed

+277
-153
lines changed

bin/promotion.ml

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,14 @@ let files_to_promote ~common files : Dune_rpc.Files_to_promote.t =
88
let files =
99
List.map files ~f:(fun fn -> Path.Source.of_string (Common.prefix_target common fn))
1010
in
11-
let on_missing fn =
12-
User_warning.emit
13-
[ Pp.textf "Nothing to promote for %s." (Path.Source.to_string_maybe_quoted fn) ]
14-
in
15-
These (files, on_missing)
11+
These files
1612
;;
1713

18-
let display_files files_to_promote =
19-
let open Fiber.O in
20-
Diff_promotion.load_db ()
21-
|> Diff_promotion.filter_db files_to_promote
22-
|> Fiber.parallel_map ~f:(fun file ->
23-
Diff_promotion.diff_for_file file
24-
>>| function
25-
| Ok _ -> Some file
26-
| Error _ -> None)
27-
>>| List.filter_opt
28-
>>| List.sort ~compare:(fun file file' -> Diff_promotion.File.compare file file')
29-
>>| List.iter ~f:(fun (file : Diff_promotion.File.t) ->
30-
Console.printf "%s" (Diff_promotion.File.source file |> Path.Source.to_string))
14+
let on_missing fn =
15+
User_warning.emit
16+
[ Pp.paragraphf "Nothing to promote for %s." (Path.Source.to_string_maybe_quoted fn)
17+
|> Pp.tag User_message.Style.Warning
18+
]
3119
;;
3220

3321
module Apply = struct
@@ -60,7 +48,10 @@ module Apply = struct
6048
Scheduler.go_with_rpc_server ~common ~config (fun () ->
6149
let open Fiber.O in
6250
let+ () = Fiber.return () in
63-
Diff_promotion.promote_files_registered_in_last_run files_to_promote)
51+
let missing =
52+
Diff_promotion.promote_files_registered_in_last_run files_to_promote
53+
in
54+
List.iter ~f:on_missing missing)
6455
| Error lock_held_by ->
6556
Scheduler.go_without_rpc_server ~common ~config (fun () ->
6657
let open Fiber.O in
@@ -86,7 +77,11 @@ module Diff = struct
8677
let common, config = Common.init builder in
8778
let files_to_promote = files_to_promote ~common files in
8879
Scheduler.go_with_rpc_server ~common ~config (fun () ->
89-
Diff_promotion.display files_to_promote)
80+
let open Fiber.O in
81+
let db = Diff_promotion.load_db () in
82+
let* missing = Diff_promotion.missing ~db files_to_promote in
83+
List.iter ~f:on_missing missing;
84+
Diff_promotion.display_diffs ~db files_to_promote)
9085
;;
9186

9287
let command = Cmd.v info term
@@ -101,7 +96,11 @@ module Files = struct
10196
let common, config = Common.init builder in
10297
let files_to_promote = files_to_promote ~common files in
10398
Scheduler.go_with_rpc_server ~common ~config (fun () ->
104-
display_files files_to_promote)
99+
let open Fiber.O in
100+
let db = Diff_promotion.load_db () in
101+
let* missing = Diff_promotion.missing ~db files_to_promote in
102+
List.iter ~f:on_missing missing;
103+
Diff_promotion.display_files ~db files_to_promote)
105104
;;
106105

107106
let command = Cmd.v info term

bin/rpc/rpc_common.ml

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ let warn_ignore_arguments lock_held_by =
106106
(match lock_held_by with
107107
| Dune_util.Global_lock.Lock_held_by.Unknown -> ""
108108
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
109+
|> Pp.tag User_message.Style.Warning
109110
]
110111
;;
111112

@@ -156,16 +157,44 @@ let wrap_build_outcome_exn ~print_on_success build_outcome =
156157
if print_on_success
157158
then Console.print [ Pp.text "Success" |> Pp.tag User_message.Style.Success ]
158159
| Failure errors ->
159-
let error_msg =
160-
match List.length errors with
161-
| 0 ->
162-
Code_error.raise
163-
"Build via RPC failed, but the RPC server did not send an error message."
164-
[]
165-
| 1 -> Pp.paragraph "Build failed with 1 error."
166-
| n -> Pp.paragraphf "Build failed with %d errors." n
160+
let nb_errors, nb_warnings =
161+
List.fold_left
162+
errors
163+
~init:(0, 0)
164+
~f:
165+
(fun
166+
(nb_errors, nb_warnings) { Dune_rpc.Compound_user_error.main; severity; _ } ->
167+
match severity with
168+
| Error ->
169+
Console.print_user_message main;
170+
nb_errors + 1, nb_warnings
171+
| Warning ->
172+
User_warning.emit_message main;
173+
nb_errors, nb_warnings + 1)
167174
in
168-
List.iter errors ~f:(fun { Dune_rpc.Compound_user_error.main; _ } ->
169-
Console.print_user_message main);
170-
User_error.raise [ error_msg |> Pp.tag User_message.Style.Error ]
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+
])
171200
;;

otherlibs/dune-rpc/private/exported_types.ml

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,11 @@ module Diagnostic = struct
545545
enum [ "error", Error; "warning", Warning ]
546546
;;
547547

548+
let severity_to_dyn = function
549+
| Error -> Dyn.string "error"
550+
| Warning -> Dyn.string "warning"
551+
;;
552+
548553
let sexp =
549554
let open Conv in
550555
let from { targets; message; loc; severity; promotion; directory; id; related } =
@@ -708,9 +713,10 @@ module Compound_user_error = struct
708713
type t =
709714
{ main : User_message.t
710715
; related : User_message.t list
716+
; severity : Diagnostic.severity
711717
}
712718

713-
let create ~main ~related =
719+
let create ~main ~related ~severity =
714720
let () =
715721
List.iter related ~f:(fun (related : User_message.t) ->
716722
match related.loc with
@@ -720,31 +726,33 @@ module Compound_user_error = struct
720726
"related messages must have locations"
721727
[ "related", String (Stdune.User_message.to_string related) ])
722728
in
723-
{ main; related }
729+
{ main; related; severity }
724730
;;
725731

726732
let sexp =
727733
let open Conv in
728-
let from { main; related } = main, related in
729-
let to_ (main, related) = create ~main ~related in
734+
let from { main; related; severity } = main, related, severity in
735+
let to_ (main, related, severity) = create ~main ~related ~severity in
730736
let main = field "main" (required User_message.sexp_without_annots) in
731737
let related = field "related" (required (list User_message.sexp_without_annots)) in
732-
iso (record (both main related)) to_ from
738+
let severity = field "severity" (required Diagnostic.sexp_severity) in
739+
iso (record (three main related severity)) to_ from
733740
;;
734741

735-
let to_dyn { main; related } =
742+
let to_dyn { main; related; severity } =
736743
let open Dyn in
737744
record
738745
[ "main", string (Stdune.User_message.to_string main)
739746
; "related", (list string) (List.map related ~f:Stdune.User_message.to_string)
747+
; "severity", Diagnostic.severity_to_dyn severity
740748
]
741749
;;
742750

743751
let annot =
744752
Stdune.User_message.Annots.Key.create ~name:"compound-user-error" (Dyn.list to_dyn)
745753
;;
746754

747-
let make ~main ~related = create ~main ~related
755+
let make ~main ~related ~severity = create ~main ~related ~severity
748756

749757
let make_loc ~dir { Ocamlc_loc.path; chars; lines } : Stdune.Loc.t =
750758
let pos_fname =
@@ -777,7 +785,15 @@ module Compound_user_error = struct
777785
in
778786
let main = make_message (report.loc, report.message) in
779787
let related = List.map report.related ~f:make_message in
780-
make ~main ~related)
788+
let severity : Diagnostic.severity =
789+
match report.severity with
790+
| Error _ -> Error
791+
| Warning _ -> Warning
792+
| Alert _ ->
793+
(* FIXME: tests expect this, but it's unclear if that should change. *)
794+
Error
795+
in
796+
make ~main ~related ~severity)
781797
;;
782798
end
783799

@@ -814,25 +830,17 @@ end
814830
module Files_to_promote = struct
815831
type t =
816832
| All
817-
| These of Stdune.Path.Source.t list * (Stdune.Path.Source.t -> unit)
818-
819-
let on_missing fn =
820-
Stdune.User_warning.emit
821-
[ Pp.paragraphf
822-
"Nothing to promote for %s."
823-
(Stdune.Path.Source.to_string_maybe_quoted fn)
824-
]
825-
;;
833+
| These of Stdune.Path.Source.t list
826834

827835
let sexp =
828836
let open Conv in
829837
let to_ = function
830838
| [] -> All
831-
| paths -> These (List.map ~f:Stdune.Path.Source.of_string paths, on_missing)
839+
| paths -> These (List.map ~f:Stdune.Path.Source.of_string paths)
832840
in
833841
let from = function
834842
| All -> []
835-
| These (paths, _) -> List.map ~f:Stdune.Path.Source.to_string paths
843+
| These paths -> List.map ~f:Stdune.Path.Source.to_string paths
836844
in
837845
iso (list Path.sexp) to_ from
838846
;;

otherlibs/dune-rpc/private/exported_types.mli

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,19 @@ module Compound_user_error : sig
227227
type t = private
228228
{ main : User_message.t
229229
; related : User_message.t list
230+
; severity : Diagnostic.severity
230231
}
231232

232233
val sexp : t Conv.value
233234
val to_dyn : t -> Dyn.t
234235
val annot : t list Stdune.User_message.Annots.Key.t
235-
val make : main:User_message.t -> related:User_message.t list -> t
236+
237+
val make
238+
: main:User_message.t
239+
-> related:User_message.t list
240+
-> severity:Diagnostic.severity
241+
-> t
242+
236243
val parse_output : dir:Stdune.Path.t -> string -> t list
237244
end
238245

@@ -246,12 +253,11 @@ module Build_outcome_with_diagnostics : sig
246253
val sexp : t Conv.value
247254
end
248255

249-
(** Describe what files should be promoted. The second argument of [These] is a
250-
function that is called on files that cannot be promoted. *)
256+
(** Describe what files should be promoted. *)
251257
module Files_to_promote : sig
252258
type t =
253259
| All
254-
| These of Stdune.Path.Source.t list * (Stdune.Path.Source.t -> unit)
260+
| These of Stdune.Path.Source.t list
255261

256262
val sexp : t Conv.value
257263
end

src/dune_engine/build_system_error.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ let of_exn (exn : Exn_with_backtrace.t) =
3434
[ Diagnostic
3535
{ dir
3636
; id = Id.gen ()
37-
; diagnostic = Compound_user_error.make ~main ~related:[]
37+
; diagnostic =
38+
Compound_user_error.make
39+
~main
40+
~related:[]
41+
~severity:Dune_rpc_private.Diagnostic.Error
3842
; promotion
3943
}
4044
]

src/dune_lang/dune_project.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,11 @@ let make_packages
788788
let related = [ message loc1; message loc2 ] in
789789
User_message.Annots.singleton
790790
Compound_user_error.annot
791-
[ Compound_user_error.make ~main:(User_message.make main_message) ~related ]
791+
[ Compound_user_error.make
792+
~main:(User_message.make main_message)
793+
~related
794+
~severity:Dune_rpc_private.Diagnostic.Error
795+
]
792796
in
793797
User_error.raise
794798
~annots

src/dune_rpc_impl/diagnostics.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ let diagnostic_of_error : Build_system_error.t -> Dune_rpc_private.Diagnostic.t
2424
let dir = Option.value ~default:Path.root dir in
2525
absolutize_paths ~dir loc
2626
in
27-
let message, related =
27+
let message, related, severity =
2828
match Build_system_error.description m with
2929
| `Exn e ->
3030
(* CR-someday jeremiedimino: Use [Report_error.get_user_message] here. *)
31-
User_message.make [ Pp.text (Printexc.to_string e.exn) ], []
32-
| `Diagnostic { Compound_user_error.main = message; related } -> message, related
31+
User_message.make [ Pp.text (Printexc.to_string e.exn) ], [], Some Diagnostic.Error
32+
| `Diagnostic { Compound_user_error.main = message; related; severity } ->
33+
message, related, Some severity
3334
in
3435
let loc = Option.map message.loc ~f:make_loc in
3536
let id =
@@ -65,7 +66,7 @@ let diagnostic_of_error : Build_system_error.t -> Dune_rpc_private.Diagnostic.t
6566
in
6667
List.map paragraphs ~f:Pp.box |> Pp.concat ~sep:Pp.cut |> Pp.vbox
6768
in
68-
{ Dune_rpc_private.Diagnostic.severity = Some Dune_rpc_private.Diagnostic.Error
69+
{ Dune_rpc_private.Diagnostic.severity
6970
; id
7071
; targets = []
7172
; message

src/dune_rpc_impl/server.ml

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,16 @@ let handler (t : _ t Fdecl.t) handle : 'build_arg Dune_rpc_server.Handler.t =
369369
(* A 'successful' formatting means there is nothing to promote. *)
370370
| Success -> ()
371371
| Failure ->
372-
Promote.Diff_promotion.promote_files_registered_in_last_run
373-
Dune_rpc.Files_to_promote.All
372+
let missing =
373+
Promote.Diff_promotion.promote_files_registered_in_last_run
374+
Dune_rpc.Files_to_promote.All
375+
in
376+
if List.is_non_empty missing
377+
then
378+
Code_error.raise
379+
"promote_files_registered_in_last_run All should always return an empty list"
380+
[];
381+
()
374382
in
375383
Handler.implement_request rpc Procedures.Public.format f
376384
in
@@ -423,16 +431,31 @@ let handler (t : _ t Fdecl.t) handle : 'build_arg Dune_rpc_server.Handler.t =
423431
in
424432
let () =
425433
let f _ files =
426-
Promote.Diff_promotion.promote_files_registered_in_last_run files;
427-
Fiber.return Dune_rpc.Build_outcome_with_diagnostics.Success
434+
match Promote.Diff_promotion.promote_files_registered_in_last_run files with
435+
| [] -> Fiber.return Dune_rpc.Build_outcome_with_diagnostics.Success
436+
| missing ->
437+
let warnings =
438+
List.map missing ~f:(fun fn ->
439+
Dune_rpc.Compound_user_error.make
440+
~main:
441+
(User_message.make
442+
[ Pp.paragraphf
443+
"Nothing to promote for %s."
444+
(Path.Source.to_string_maybe_quoted fn)
445+
])
446+
~related:[]
447+
~severity:Dune_rpc.Diagnostic.Warning)
448+
in
449+
Fiber.return (Dune_rpc.Build_outcome_with_diagnostics.Failure warnings)
428450
in
429451
Handler.implement_request rpc Procedures.Public.promote_many f
430452
in
431453
let () =
432454
let f _ path =
433455
let files = For_handlers.source_path_of_string path in
434-
Promote.Diff_promotion.promote_files_registered_in_last_run
435-
(These ([ files ], ignore));
456+
let _ignored : Path.Source.t list =
457+
Promote.Diff_promotion.promote_files_registered_in_last_run (These [ files ])
458+
in
436459
Fiber.return ()
437460
in
438461
Handler.implement_request rpc Procedures.Public.promote f

src/dune_rules/cram/cram_rules.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ let rules ~sctx ~dir tests =
266266
in
267267
User_message.Annots.singleton
268268
Compound_user_error.annot
269-
[ Compound_user_error.make ~main ~related ]
269+
[ Compound_user_error.make ~main ~related ~severity:Error ]
270270
in
271271
User_error.raise
272272
~annots

0 commit comments

Comments
 (0)