Skip to content

Commit

Permalink
Upstream JS changes to Dune engine
Browse files Browse the repository at this point in the history
  • Loading branch information
d-kalinichenko committed Aug 10, 2023
1 parent 758e370 commit 4682db1
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 54 deletions.
4 changes: 4 additions & 0 deletions otherlibs/stdune/src/table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ let clear (type input output) ((module T) : (input, output) t) = T.H.clear T.val
let mem (type input output) ((module T) : (input, output) t) k = T.H.mem T.value k
let keys (type input output) ((module T) : (input, output) t) = T.H.keys T.value

let values (type input output) ((module T) : (input, output) t) =
T.H.to_seq_values T.value |> List.of_seq
;;

let foldi (type input output) ((module T) : (input, output) t) ~init ~f =
T.H.foldi T.value ~init ~f
;;
Expand Down
10 changes: 6 additions & 4 deletions otherlibs/stdune/src/table.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ val remove : ('k, _) t -> 'k -> unit
val iter : (_, 'v) t -> f:('v -> unit) -> unit
val filteri_inplace : ('a, 'b) t -> f:(key:'a -> data:'b -> bool) -> unit
val length : (_, _) t -> int
val values : (_, 'a) t -> 'a list

module Multi : sig
type ('k, 'v) t := ('k, 'v list) t
type ('k, 'v) t

val cons : ('k, 'v) t -> 'k -> 'v -> unit
val find : ('k, 'v) t -> 'k -> 'v list
end
val cons : ('k, 'v) t -> 'k -> 'v -> unit
val find : ('k, 'v) t -> 'k -> 'v list
end
with type ('k, 'v) t := ('k, 'v list) t
60 changes: 58 additions & 2 deletions src/dune_engine/action_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,54 @@ let to_dune_dep_set =
;;

module Exec_result = struct
type t = { dynamic_deps_stages : (Dep.Set.t * Dep.Facts.t) list }
module Error = struct
type t =
| User of User_message.t
| Code of Code_error.t
| Sys of string
| Unix of Unix.error * string * string
| Nonreproducible_build_cancelled

(* We can't capture raw backtraces since they are not marshallable.
We can convert those to marshallable backtrace slots, but we can't convert them
back to re-raise exceptions with preserved backtraces. *)
let of_exn (e : exn) =
match e with
| User_error.E msg -> User msg
| Code_error.E err -> Code err
| Sys_error msg -> Sys msg
| Unix.Unix_error (err, call, args) -> Unix (err, call, args)
| Memo.Non_reproducible Scheduler.Run.Build_cancelled ->
Nonreproducible_build_cancelled
| e ->
Code
{ message = "unable to serialize exception"
; data = [ "exn", Exn.to_dyn e ]
; loc = None
}
;;

let to_exn (t : t) =
match t with
| User msg -> User_error.E msg
| Code err -> Code_error.E err
| Sys msg -> Sys_error msg
| Unix (err, call, args) -> Unix.Unix_error (err, call, args)
| Nonreproducible_build_cancelled ->
Memo.Non_reproducible Scheduler.Run.Build_cancelled
;;
end

type ok = { dynamic_deps_stages : (Dep.Set.t * Dep.Facts.t) list }
type t = (ok, Error.t list) Result.t

let ok_exn (t : t) =
match t with
| Ok t -> Fiber.return t
| Error errs ->
Fiber.reraise_all
(List.map errs ~f:(fun e -> Exn_with_backtrace.capture (Error.to_exn e)))
;;
end

type done_or_more_deps =
Expand Down Expand Up @@ -582,5 +629,14 @@ let exec
; exit_codes = Predicate.create (Int.equal 0)
}
in
exec_until_all_deps_ready t ~display:!Clflags.display ~ectx ~eenv
let open Fiber.O in
let+ result =
Fiber.collect_errors (fun () ->
exec_until_all_deps_ready t ~display:!Clflags.display ~ectx ~eenv)
in
match result with
| Ok res -> Ok res
| Error exns ->
Error
(List.map exns ~f:(fun (e : Exn_with_backtrace.t) -> Exec_result.Error.of_exn e.exn))
;;
18 changes: 17 additions & 1 deletion src/dune_engine/action_exec.mli
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
open Import

module Exec_result : sig
type t =
(* Exceptions that can be raised by action execution. We catch those and
use a variant type so we can marshal them across processes. We lose backtraces,
but we don't print them for most exceptions. *)
module Error : sig
type t =
| User of User_message.t
| Code of Code_error.t
| Sys of string
| Unix of Unix.error * string * string
| Nonreproducible_build_cancelled
end

type ok =
{ dynamic_deps_stages :
(* The set can be derived from the facts by getting the keys of the
facts map. We don't do it because conversion isn't free *)
(Dep.Set.t * Dep.Facts.t) list
}

type t = (ok, Error.t list) Result.t

val ok_exn : t -> ok Fiber.t
end

type input =
Expand Down
Loading

0 comments on commit 4682db1

Please sign in to comment.