From 87d52daf4bc9c263f8af1949de598cf384e217a3 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Tue, 9 Dec 2025 21:25:22 +0000 Subject: [PATCH 1/2] dev Signed-off-by: Rudi Grinberg From 5714fde54d384d69281d8d579407194060c4b451 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 14 Dec 2025 10:06:44 +0000 Subject: [PATCH 2/2] feature: target resolution trace event Replaces the less useful log message Signed-off-by: Rudi Grinberg --- bin/build.ml | 13 ++- bin/common.ml | 2 +- bin/print_rules.ml | 3 +- bin/target.ml | 47 ++++++----- bin/target.mli | 1 - doc/changes/changed/12955.md | 2 + src/dune_trace/category.ml | 7 +- src/dune_trace/category.mli | 1 + src/dune_trace/dune_trace.mli | 10 +++ src/dune_trace/event.ml | 34 ++++++++ .../artifact-variables.t/run.t | 83 +++++++++++++------ .../test-cases/variables-for-artifacts/dune | 3 + 12 files changed, 143 insertions(+), 63 deletions(-) create mode 100644 doc/changes/changed/12955.md create mode 100644 test/blackbox-tests/test-cases/variables-for-artifacts/dune diff --git a/bin/build.ml b/bin/build.ml index a4c51ed27f8..214d3079ccf 100644 --- a/bin/build.ml +++ b/bin/build.ml @@ -83,7 +83,7 @@ let run_build_system ~common ~request = Fiber.return ()) ;; -let poll_handling_rpc_build_requests ~(common : Common.t) ~config = +let poll_handling_rpc_build_requests ~(common : Common.t) = let open Fiber.O in let rpc = match Common.rpc common with @@ -96,8 +96,7 @@ let poll_handling_rpc_build_requests ~(common : Common.t) ~config = let request setup = let root = Common.root common in match kind with - | Build targets -> - Target.interpret_targets (Common.root common) config setup targets + | Build targets -> Target.interpret_targets (Common.root common) setup targets | Runtest test_paths -> Runtest_common.make_request ~contexts:setup.contexts @@ -114,7 +113,7 @@ let run_build_command_poll_eager ~(common : Common.t) ~config ~request : unit = named on the command line in reaction to file system changes. The other is responsible for building targets named in RPC build requests. *) let+ () = Dune_engine.Scheduler.Run.poll (run_build_system ~common ~request) - and+ () = poll_handling_rpc_build_requests ~common ~config in + and+ () = poll_handling_rpc_build_requests ~common in ()) ;; @@ -122,7 +121,7 @@ let run_build_command_poll_passive ~common ~config ~request:_ : unit = (* CR-someday aalekseyev: It would've been better to complain if [request] is non-empty, but we can't check that here because [request] is a function.*) Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config (fun () -> - poll_handling_rpc_build_requests ~common ~config) + poll_handling_rpc_build_requests ~common) ;; let run_build_command_once ~(common : Common.t) ~config ~request = @@ -235,9 +234,7 @@ let build = targets >>| Rpc.Rpc_common.wrap_build_outcome_exn ~print_on_success:true) | Ok () -> - let request setup = - Target.interpret_targets (Common.root common) config setup targets - in + let request setup = Target.interpret_targets (Common.root common) setup targets in run_build_command ~common ~config ~request in Cmd.v (Cmd.info "build" ~doc ~man ~envs:Common.envs) term diff --git a/bin/common.ml b/bin/common.ml index cdd22ce06bd..d2f2b612e4a 100644 --- a/bin/common.ml +++ b/bin/common.ml @@ -1272,7 +1272,7 @@ let build (root : Workspace_root.t) (builder : Builder.t) = let cats = match Sys.getenv_opt "DUNE_TRACE" with | None -> - Dune_trace.Category.[ Sandbox; Persistent; Process; Rules; Pkg; Promote ] + Dune_trace.Category.[ Sandbox; Persistent; Process; Rules; Pkg; Promote; Build ] | Some s -> String.split ~on:',' s |> List.map ~f:(fun x -> diff --git a/bin/print_rules.ml b/bin/print_rules.ml index ac72d162267..e2df78893bb 100644 --- a/bin/print_rules.ml +++ b/bin/print_rules.ml @@ -291,8 +291,7 @@ let term = Target.all_direct_targets None >>| Path.Build.Map.foldi ~init:[] ~f:(fun p _ acc -> Path.build p :: acc) >>| Action_builder.paths - | _ -> - Memo.return (Target.interpret_targets (Common.root common) config setup targets) + | _ -> Memo.return (Target.interpret_targets (Common.root common) setup targets) in let+ rules = Dune_engine.Reflection.eval ~request ~recursive in let print oc = diff --git a/bin/target.ml b/bin/target.ml index e7d5ee30dcd..d50aa134ac4 100644 --- a/bin/target.ml +++ b/bin/target.ml @@ -220,34 +220,35 @@ let resolve_target root ~setup target = | dep -> Action_builder.return (Error (dep, [])) ;; -let resolve_targets - root - (config : Dune_config.t) - (setup : Dune_rules.Main.build_system) - user_targets - = +let resolve_targets root (setup : Dune_rules.Main.build_system) user_targets = match user_targets with | [] -> Action_builder.return [] | _ -> let+ targets = Action_builder.List.map user_targets ~f:(resolve_target root ~setup) in - (match config.display with - | Simple { verbosity = Verbose; _ } -> - Log.info - [ Pp.text "Actual targets:" - ; Pp.enumerate - (List.concat_map targets ~f:(function - | Ok targets -> targets - | Error _ -> [])) - ~f:(function - | File p -> Pp.verbatim (Path.to_string_maybe_quoted p) - | Alias a -> Alias.pp a) - ] - | _ -> ()); + Dune_trace.emit Build (fun () -> + let files, aliases = + List.concat_map targets ~f:(function + | Ok targets -> targets + | Error _ -> []) + |> List.partition_map ~f:(function + | Request.File p -> Left p + | Alias { name; recursive; dir; contexts } -> + Right + ({ name = Dune_engine.Alias.Name.to_string name + ; recursive + ; dir + ; contexts = + List.map contexts ~f:(fun ctx -> + Dune_rules.Context.name ctx |> Context_name.to_string) + } + : Dune_trace.Event.alias)) + in + Dune_trace.Event.resolve_targets files aliases); targets ;; -let resolve_targets_exn root config setup user_targets = - resolve_targets root config setup user_targets +let resolve_targets_exn root setup user_targets = + resolve_targets root setup user_targets >>| List.concat_map ~f:(function | Error (dep, hints) -> User_error.raise @@ -256,9 +257,9 @@ let resolve_targets_exn root config setup user_targets = | Ok targets -> targets) ;; -let interpret_targets root config setup user_targets = +let interpret_targets root setup user_targets = let* () = Action_builder.return () in - resolve_targets_exn root config setup user_targets >>= request + resolve_targets_exn root setup user_targets >>= request ;; type target_type = Target_type.t = diff --git a/bin/target.mli b/bin/target.mli index 03a1e55115c..bced4e525fc 100644 --- a/bin/target.mli +++ b/bin/target.mli @@ -13,7 +13,6 @@ val all_direct_targets : Path.Source.t option -> target_type Path.Build.Map.t Me val interpret_targets : Workspace_root.t - -> Dune_config.t -> Dune_rules.Main.build_system -> Arg.Dep.t list -> unit Dune_engine.Action_builder.t diff --git a/doc/changes/changed/12955.md b/doc/changes/changed/12955.md new file mode 100644 index 00000000000..1606fd226b4 --- /dev/null +++ b/doc/changes/changed/12955.md @@ -0,0 +1,2 @@ +- Add a target resolution event to replace the equivalent log message (#12955, + @rgrinberg) diff --git a/src/dune_trace/category.ml b/src/dune_trace/category.ml index 11b8c3c1ccb..37dca5eb8bf 100644 --- a/src/dune_trace/category.ml +++ b/src/dune_trace/category.ml @@ -11,8 +11,11 @@ type t = | Pkg | Scheduler | Promote + | Build -let all = [ Rpc; Gc; Fd; Sandbox; Persistent; Process; Rules; Pkg; Scheduler; Promote ] +let all = + [ Rpc; Gc; Fd; Sandbox; Persistent; Process; Rules; Pkg; Scheduler; Promote; Build ] +;; let to_string = function | Rpc -> "rpc" @@ -25,6 +28,7 @@ let to_string = function | Pkg -> "pkg" | Scheduler -> "scheduler" | Promote -> "promote" + | Build -> "build" ;; let of_string = @@ -51,5 +55,6 @@ module Set = Bit_set.Make (struct | Pkg -> 7 | Scheduler -> 8 | Promote -> 9 + | Build -> 10 ;; end) diff --git a/src/dune_trace/category.mli b/src/dune_trace/category.mli index a4c233e0bbd..2aeec4a0e80 100644 --- a/src/dune_trace/category.mli +++ b/src/dune_trace/category.mli @@ -9,6 +9,7 @@ type t = | Pkg | Scheduler | Promote + | Build val to_string : t -> string val of_string : string -> t option diff --git a/src/dune_trace/dune_trace.mli b/src/dune_trace/dune_trace.mli index 5333a8341df..3c640716885 100644 --- a/src/dune_trace/dune_trace.mli +++ b/src/dune_trace/dune_trace.mli @@ -12,6 +12,7 @@ module Category : sig | Pkg | Scheduler | Promote + | Build val of_string : string -> t option end @@ -73,6 +74,15 @@ module Event : sig val fd_count : unit -> t option val promote : Path.Build.t -> Path.Source.t -> t + type alias = + { dir : Path.Source.t + ; name : string + ; recursive : bool + ; contexts : string list + } + + val resolve_targets : Path.t list -> alias list -> t + module Rpc : sig type stage = | Start diff --git a/src/dune_trace/event.ml b/src/dune_trace/event.ml index f23ceb5e324..8a13e525e0d 100644 --- a/src/dune_trace/event.ml +++ b/src/dune_trace/event.ml @@ -354,3 +354,37 @@ let promote src dst = in Event.instant ~args common ;; + +type alias = + { dir : Path.Source.t + ; name : string + ; recursive : bool + ; contexts : string list + } + +let json_of_alias { dir; name; recursive; contexts } = + `Assoc + [ "dir", `String (Path.Source.to_string dir) + ; "name", `String name + ; "recursive", `Bool recursive + ; "contexts", `List (List.map contexts ~f:Json.string) + ] +;; + +let resolve_targets targets aliases = + let module Event = Chrome_trace.Event in + let ts = Event.Timestamp.of_float_seconds (Time.now () |> Time.to_secs) in + let args = + [ "targets", List.map targets ~f:(fun p -> `String (Path.to_string p)) + ; "aliases", List.map aliases ~f:json_of_alias + ] + |> List.filter_map ~f:(fun (k, v) -> + match v with + | [] -> None + | _ :: _ -> Some (k, `List v)) + in + let common = + Event.common_fields ~cat:[ Category.to_string Build ] ~name:"targets" ~ts () + in + Event.instant ~args common +;; diff --git a/test/blackbox-tests/test-cases/variables-for-artifacts/artifact-variables.t/run.t b/test/blackbox-tests/test-cases/variables-for-artifacts/artifact-variables.t/run.t index 7b51d518c01..4bdf97c7647 100644 --- a/test/blackbox-tests/test-cases/variables-for-artifacts/artifact-variables.t/run.t +++ b/test/blackbox-tests/test-cases/variables-for-artifacts/artifact-variables.t/run.t @@ -15,13 +15,21 @@ prefixed and unprefixed modules are built. $ dune clean $ dune build @t1 + $ buildAndShow() { + > dune build --trace-file trace.json $@ + > jq '.[] | select(.name == "targets") | .args' trace.json + > } + Command line version. - $ dune build --verbose %{cmo:a} %{cmo:b} %{cmo:c} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/.a1.objs/byte/a1__A.cmo - - _build/default/.b.eobjs/byte/dune__exe__B.cmo - - _build/default/.c1.objs/byte/c.cmo + $ buildAndShow %{cmo:a} %{cmo:b} %{cmo:c} + { + "targets": [ + "_build/default/.a1.objs/byte/a1__A.cmo", + "_build/default/.b.eobjs/byte/dune__exe__B.cmo", + "_build/default/.c1.objs/byte/c.cmo" + ] + } The next test tries to build a .cmi file (of a module in a wrapped library). @@ -30,9 +38,12 @@ The next test tries to build a .cmi file (of a module in a wrapped library). Command line version. - $ dune build --verbose %{cmi:a} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/.a1.objs/byte/a1__A.cmi + $ buildAndShow %{cmi:a} + { + "targets": [ + "_build/default/.a1.objs/byte/a1__A.cmi" + ] + } Command line version; note that the error message is slightly different. @@ -48,9 +59,12 @@ The next test builds a native .cmxa. Command line version. - $ dune build --verbose %{cmxa:a1} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/a1.cmxa + $ buildAndShow %{cmxa:a1} + { + "targets": [ + "_build/default/a1.cmxa" + ] + } Command line version. @@ -67,9 +81,12 @@ defined. The library is public in this case, but we use the local name. Command line version. - $ dune build --verbose %{cma:sub2/bar2} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/sub2/bar2.cma + $ buildAndShow %{cma:sub2/bar2} + { + "targets": [ + "_build/default/sub2/bar2.cma" + ] + } This test builds a .cmo in a subdirectory (same project). @@ -78,9 +95,12 @@ This test builds a .cmo in a subdirectory (same project). Command line version. - $ dune build --verbose %{cmo:sub/x} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/sub/.bar.objs/byte/bar__X.cmo + $ buildAndShow %{cmo:sub/x} + { + "targets": [ + "_build/default/sub/.bar.objs/byte/bar__X.cmo" + ] + } This test builds a module in a subdirectory (different project) belonging to a private library. @@ -88,11 +108,14 @@ private library. $ dune clean $ dune build @t8 -COmmand line version. +Command line version. - $ dune build --verbose %{cmo:sub3/x} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/sub3/.c1.objs/byte/c1__X.cmo + $ buildAndShow %{cmo:sub3/x} + { + "targets": [ + "_build/default/sub3/.c1.objs/byte/c1__X.cmo" + ] + } This test builds a private library in a subdirectory belonging to a different project. @@ -102,9 +125,12 @@ project. Command line version. - $ dune build --verbose %{cma:sub3/c1} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/sub3/c1.cma + $ buildAndShow %{cma:sub3/c1} + { + "targets": [ + "_build/default/sub3/c1.cma" + ] + } This test builds a library in the current directory that has the same name as a public library defined in a subdirectory. @@ -114,9 +140,12 @@ public library defined in a subdirectory. Command line version. - $ dune build --verbose %{cma:c1} 2>&1 | grep -A100 'Actual targets' - Actual targets: - - _build/default/c1.cma + $ buildAndShow %{cma:c1} + { + "targets": [ + "_build/default/c1.cma" + ] + } This test checks error handling. diff --git a/test/blackbox-tests/test-cases/variables-for-artifacts/dune b/test/blackbox-tests/test-cases/variables-for-artifacts/dune new file mode 100644 index 00000000000..63f737927db --- /dev/null +++ b/test/blackbox-tests/test-cases/variables-for-artifacts/dune @@ -0,0 +1,3 @@ +(cram + (applies_to artifact-variables) + (deps %{bin:jq}))