Skip to content

Commit a970183

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

File tree

11 files changed

+163
-104
lines changed

11 files changed

+163
-104
lines changed

bin/promotion.ml

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,12 @@ 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) ]
3117
;;
3218

3319
module Apply = struct
@@ -60,7 +46,10 @@ module Apply = struct
6046
Scheduler.go_with_rpc_server ~common ~config (fun () ->
6147
let open Fiber.O in
6248
let+ () = Fiber.return () in
63-
Diff_promotion.promote_files_registered_in_last_run files_to_promote)
49+
let missing =
50+
Diff_promotion.promote_files_registered_in_last_run files_to_promote
51+
in
52+
List.iter missing ~f:on_missing)
6453
| Error lock_held_by ->
6554
Scheduler.go_without_rpc_server ~common ~config (fun () ->
6655
let open Fiber.O in
@@ -86,7 +75,7 @@ module Diff = struct
8675
let common, config = Common.init builder in
8776
let files_to_promote = files_to_promote ~common files in
8877
Scheduler.go_with_rpc_server ~common ~config (fun () ->
89-
Diff_promotion.display files_to_promote)
78+
Diff_promotion.display_diffs ~on_missing files_to_promote)
9079
;;
9180

9281
let command = Cmd.v info term
@@ -101,7 +90,7 @@ module Files = struct
10190
let common, config = Common.init builder in
10291
let files_to_promote = files_to_promote ~common files in
10392
Scheduler.go_with_rpc_server ~common ~config (fun () ->
104-
display_files files_to_promote)
93+
Diff_promotion.display_files ~on_missing files_to_promote)
10594
;;
10695

10796
let command = Cmd.v info term

bin/rpc/rpc_build.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let term =
1313
in
1414
match response with
1515
| Success -> print_endline "Success"
16+
| Warn _ -> print_endline "Warn"
1617
| Failure _ -> print_endline "Failure"
1718
;;
1819

bin/rpc/rpc_common.ml

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ let prepare_targets targets =
100100

101101
let warn_ignore_arguments lock_held_by =
102102
User_warning.emit
103-
[ Pp.paragraphf
104-
"Your build request is being forwarded to a running Dune instance%s. Note that \
105-
certain command line arguments may be ignored."
106-
(match lock_held_by with
107-
| Dune_util.Global_lock.Lock_held_by.Unknown -> ""
108-
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
103+
[ Pp.tag User_message.Style.Warning
104+
@@ Pp.paragraphf
105+
"Your build request is being forwarded to a running Dune instance%s. Note \
106+
that certain command line arguments may be ignored."
107+
(match lock_held_by with
108+
| Dune_util.Global_lock.Lock_held_by.Unknown -> ""
109+
| Pid_from_lockfile pid -> sprintf " (pid: %d)" pid)
109110
]
110111
;;
111112

@@ -155,6 +156,20 @@ let wrap_build_outcome_exn ~print_on_success build_outcome =
155156
| Dune_rpc.Build_outcome_with_diagnostics.Success ->
156157
if print_on_success
157158
then Console.print [ Pp.text "Success" |> Pp.tag User_message.Style.Success ]
159+
| Warn warnings ->
160+
let warn_msg =
161+
match List.length warnings with
162+
| 0 ->
163+
Code_error.raise
164+
"Build via RPC returned a warning state, but the RPC server did not send any \
165+
warnings."
166+
[]
167+
| 1 -> Pp.paragraph "Build completed with 1 warning."
168+
| n -> Pp.paragraphf "Build completed with %d warnings." n
169+
in
170+
List.iter warnings ~f:(fun { Dune_rpc.Compound_user_error.main; _ } ->
171+
Console.print_user_message main);
172+
User_warning.emit [ Pp.tag User_message.Style.Warning warn_msg ]
158173
| Failure errors ->
159174
let error_msg =
160175
match List.length errors with
@@ -167,5 +182,5 @@ let wrap_build_outcome_exn ~print_on_success build_outcome =
167182
in
168183
List.iter errors ~f:(fun { Dune_rpc.Compound_user_error.main; _ } ->
169184
Console.print_user_message main);
170-
User_error.raise [ error_msg |> Pp.tag User_message.Style.Error ]
185+
User_error.raise [ Pp.tag User_message.Style.Error error_msg ]
171186
;;

otherlibs/dune-rpc/private/exported_types.ml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -784,27 +784,34 @@ end
784784
module Build_outcome_with_diagnostics = struct
785785
type t =
786786
| Success
787+
| Warn of Compound_user_error.t list
787788
| Failure of Compound_user_error.t list
788789

789790
let sexp_v1 =
790791
let open Conv in
791792
let success = constr "Success" unit (fun () -> Success) in
793+
let warn = constr "Warn" unit (fun () -> Warn []) in
792794
let failure = constr "Failure" unit (fun () -> Failure []) in
793-
let variants = [ econstr success; econstr failure ] in
795+
let variants = [ econstr success; econstr warn; econstr failure ] in
794796
sum variants (function
795797
| Success -> case () success
798+
| Warn _ -> case () warn
796799
| Failure _ -> case () failure)
797800
;;
798801

799802
let sexp_v2 =
800803
let open Conv in
801804
let success = constr "Success" unit (fun () -> Success) in
805+
let warn =
806+
constr "Warn" (list Compound_user_error.sexp) (fun warnings -> Warn warnings)
807+
in
802808
let failure =
803809
constr "Failure" (list Compound_user_error.sexp) (fun errors -> Failure errors)
804810
in
805-
let variants = [ econstr success; econstr failure ] in
811+
let variants = [ econstr success; econstr warn; econstr failure ] in
806812
sum variants (function
807813
| Success -> case () success
814+
| Warn warnings -> case warnings warn
808815
| Failure errors -> case errors failure)
809816
;;
810817

@@ -814,25 +821,17 @@ end
814821
module Files_to_promote = struct
815822
type t =
816823
| 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-
;;
824+
| These of Stdune.Path.Source.t list
826825

827826
let sexp =
828827
let open Conv in
829828
let to_ = function
830829
| [] -> All
831-
| paths -> These (List.map ~f:Stdune.Path.Source.of_string paths, on_missing)
830+
| paths -> These (List.map ~f:Stdune.Path.Source.of_string paths)
832831
in
833832
let from = function
834833
| All -> []
835-
| These (paths, _) -> List.map ~f:Stdune.Path.Source.to_string paths
834+
| These paths -> List.map ~f:Stdune.Path.Source.to_string paths
836835
in
837836
iso (list Path.sexp) to_ from
838837
;;

otherlibs/dune-rpc/private/exported_types.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,19 @@ end
239239
module Build_outcome_with_diagnostics : sig
240240
type t =
241241
| Success
242+
| Warn of Compound_user_error.t list
242243
| Failure of Compound_user_error.t list
243244

244245
val sexp_v1 : t Conv.value
245246
val sexp_v2 : t Conv.value
246247
val sexp : t Conv.value
247248
end
248249

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. *)
250+
(** Describe what files should be promoted. *)
251251
module Files_to_promote : sig
252252
type t =
253253
| All
254-
| These of Stdune.Path.Source.t list * (Stdune.Path.Source.t -> unit)
254+
| These of Stdune.Path.Source.t list
255255

256256
val sexp : t Conv.value
257257
end

src/dune_rpc_impl/server.ml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,11 @@ 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 _is_necessarily_empty =
373+
Promote.Diff_promotion.promote_files_registered_in_last_run
374+
Dune_rpc.Files_to_promote.All
375+
in
376+
()
374377
in
375378
Handler.implement_request rpc Procedures.Public.format f
376379
in
@@ -423,16 +426,30 @@ let handler (t : _ t Fdecl.t) handle : 'build_arg Dune_rpc_server.Handler.t =
423426
in
424427
let () =
425428
let f _ files =
426-
Promote.Diff_promotion.promote_files_registered_in_last_run files;
427-
Fiber.return Dune_rpc.Build_outcome_with_diagnostics.Success
429+
match Promote.Diff_promotion.promote_files_registered_in_last_run files with
430+
| [] -> Fiber.return Dune_rpc.Build_outcome_with_diagnostics.Success
431+
| missing ->
432+
let warnings =
433+
List.map missing ~f:(fun fn ->
434+
Dune_rpc.Compound_user_error.make
435+
~main:
436+
(User_message.make
437+
[ Pp.paragraphf
438+
"Nothing to promote for %s."
439+
(Path.Source.to_string_maybe_quoted fn)
440+
])
441+
~related:[])
442+
in
443+
Fiber.return (Dune_rpc.Build_outcome_with_diagnostics.Warn warnings)
428444
in
429445
Handler.implement_request rpc Procedures.Public.promote_many f
430446
in
431447
let () =
432448
let f _ path =
433449
let files = For_handlers.source_path_of_string path in
434-
Promote.Diff_promotion.promote_files_registered_in_last_run
435-
(These ([ files ], ignore));
450+
let _ignored =
451+
Promote.Diff_promotion.promote_files_registered_in_last_run (These [ files ])
452+
in
436453
Fiber.return ()
437454
in
438455
Handler.implement_request rpc Procedures.Public.promote f

src/promote/diff_promotion.ml

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -159,37 +159,41 @@ let do_promote db files_to_promote =
159159
match files_to_promote with
160160
| Dune_rpc_private.Files_to_promote.All ->
161161
Path.Source.Map.iteri by_targets ~f:promote_one;
162-
[]
163-
| These (files, on_missing) ->
164-
let by_targets =
162+
[], []
163+
| These files ->
164+
let by_targets, missing =
165165
let files = Path.Source.Set.of_list files in
166-
Path.Source.Set.fold files ~init:by_targets ~f:(fun fn map ->
167-
match Path.Source.Map.find by_targets fn with
168-
| None ->
169-
on_missing fn;
170-
map
166+
Path.Source.Set.fold files ~init:(by_targets, []) ~f:(fun fn (map, missing) ->
167+
match Path.Source.Map.find map fn with
168+
| None -> map, fn :: missing
171169
| Some srcs ->
172170
promote_one fn srcs;
173-
Path.Source.Map.remove by_targets fn)
171+
Path.Source.Map.remove map fn, missing)
174172
in
175-
Path.Source.Map.to_list by_targets
176-
|> List.concat_map ~f:(fun (dst, srcs) ->
177-
List.map srcs ~f:(fun (src, staging) -> { File.src; staging; dst }))
173+
( Path.Source.Map.to_list by_targets
174+
|> List.concat_map ~f:(fun (dst, srcs) ->
175+
List.map srcs ~f:(fun (src, staging) -> { File.src; staging; dst }))
176+
, missing )
178177
;;
179178

180179
let finalize () =
181180
let db =
182181
match !Dune_engine.Clflags.promote with
183-
| Some Automatically -> do_promote !File.db All
182+
| Some Automatically ->
183+
let _is_necessarily_empty = do_promote !File.db All in
184+
[]
184185
| Some Never | None -> !File.db
185186
in
186187
dump_db db
187188
;;
188189

190+
(* Returns the list of files that were in [files_to_promote]
191+
but not present in the promotion database. *)
189192
let promote_files_registered_in_last_run files_to_promote =
190193
let db = load_db () in
191-
let db = do_promote db files_to_promote in
192-
dump_db db
194+
let remaining, missing = do_promote db files_to_promote in
195+
dump_db remaining;
196+
missing
193197
;;
194198

195199
let diff_for_file (file : File.t) =
@@ -199,28 +203,50 @@ let diff_for_file (file : File.t) =
199203
Print_diff.get msg original correction
200204
;;
201205

202-
let filter_db files_to_promote db =
206+
(** [partition_db files_to_promote db] splits [files_to_promote] into two lists
207+
- The files present in [db] as actual [File.t]s.
208+
- The files absent from [db] as [Path]s. *)
209+
let partition_db files_to_promote db =
203210
match files_to_promote with
204-
| Dune_rpc_private.Files_to_promote.All -> db
205-
| These (files, on_missing) ->
206-
List.filter_map files ~f:(fun file ->
207-
let r = List.find db ~f:(fun (f : File.t) -> Path.Source.equal f.dst file) in
208-
if Option.is_none r then on_missing file;
209-
r)
211+
| Dune_rpc_private.Files_to_promote.All -> db, []
212+
| These paths ->
213+
List.partition_map paths ~f:(fun path ->
214+
let res = List.find db ~f:(fun (f : File.t) -> Path.Source.equal f.dst path) in
215+
match res with
216+
| Some file -> Left file
217+
| None -> Right path)
210218
;;
211219

212-
let display files_to_promote =
220+
let sort_for_display files_to_promote =
213221
let open Fiber.O in
214-
let files = load_db () |> filter_db files_to_promote in
222+
let files, missing = load_db () |> partition_db files_to_promote in
215223
let+ diff_opts =
216224
Fiber.parallel_map files ~f:(fun file ->
217225
let+ diff_opt = diff_for_file file in
218226
match diff_opt with
219227
| Ok diff -> Some (file, diff)
220228
| Error _ -> None)
221229
in
222-
diff_opts
223-
|> List.filter_opt
224-
|> List.sort ~compare:(fun (file, _) (file', _) -> File.compare file file')
225-
|> List.iter ~f:(fun (_file, diff) -> Print_diff.Diff.print diff)
230+
let sorted_diffs =
231+
diff_opts
232+
|> List.filter_opt
233+
|> List.sort ~compare:(fun (file, _) (file', _) -> File.compare file file')
234+
in
235+
let sorted_missing = List.sort missing ~compare:Path.Source.compare in
236+
sorted_diffs, sorted_missing
237+
;;
238+
239+
let display_diffs ~on_missing files_to_promote =
240+
let open Fiber.O in
241+
let+ diffs, missing = sort_for_display files_to_promote in
242+
List.iter missing ~f:on_missing;
243+
List.iter diffs ~f:(fun (_file, diff) -> Print_diff.Diff.print diff)
244+
;;
245+
246+
let display_files ~on_missing files_to_promote =
247+
let open Fiber.O in
248+
let+ diffs, missing = sort_for_display files_to_promote in
249+
List.iter missing ~f:on_missing;
250+
List.iter diffs ~f:(fun (file, _diff) ->
251+
Console.printf "%s" (File.source file |> Path.Source.to_string))
226252
;;

src/promote/diff_promotion.mli

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ end
2727
val finalize : unit -> unit
2828

2929
val load_db : unit -> File.t list
30-
val filter_db : Dune_rpc_private.Files_to_promote.t -> File.t list -> File.t list
31-
val diff_for_file : File.t -> (Print_diff.Diff.t, User_message.t) result Fiber.t
32-
val promote_files_registered_in_last_run : Dune_rpc_private.Files_to_promote.t -> unit
33-
val display : Dune_rpc_private.Files_to_promote.t -> unit Fiber.t
30+
31+
val promote_files_registered_in_last_run
32+
: Dune_rpc_private.Files_to_promote.t
33+
-> Path.Source.t list
34+
35+
val display_diffs
36+
: on_missing:(Path.Source.t -> unit)
37+
-> Dune_rpc_private.Files_to_promote.t
38+
-> unit Fiber.t
39+
40+
val display_files
41+
: on_missing:(Path.Source.t -> unit)
42+
-> Dune_rpc_private.Files_to_promote.t
43+
-> unit Fiber.t

0 commit comments

Comments
 (0)