From 999279473c0cd4a25b54edb9e1ca1887d75e6a60 Mon Sep 17 00:00:00 2001 From: Alpha Issiaga DIALLO Date: Thu, 10 Oct 2024 00:06:01 +0200 Subject: [PATCH] fix(pkg): print depexts message when a program is not found (#11005) * fix(pkg): print depexts message when a program is not found Signed-off-by: Alpha DIALLO --- src/dune_rules/pkg_rules.ml | 43 ++++++++++++------- .../test-cases/pkg/depexts/error-message.t | 13 +++--- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/dune_rules/pkg_rules.ml b/src/dune_rules/pkg_rules.ml index 34512a4f4da..cd9d6046efb 100644 --- a/src/dune_rules/pkg_rules.ml +++ b/src/dune_rules/pkg_rules.ml @@ -579,6 +579,16 @@ module Substitute = struct let action expander ~src ~dst = A.action { Spec.expander; src; dst } end +let depexts_hint = function + | [] -> None + | depexts -> + [ Pp.textf "You may want to verify the following depexts are installed:" + ; Pp.enumerate ~f:Pp.verbatim depexts + ] + |> Pp.concat_map ~sep:Pp.cut ~f:(fun pp -> Pp.box pp) + |> Option.some +;; + module Run_with_path = struct module Output : sig type error @@ -616,30 +626,28 @@ module Run_with_path = struct ;; let to_paragraphs t error = - let pkg_name, loc = t.pkg in - let depexts_warning = - match t.depexts with - | [] -> [] - | _ :: _ -> - [ Pp.textf "You may want to verify the following depexts are installed:" - ; Pp.enumerate ~f:Pp.verbatim t.depexts - ] - in - let pp_pkg = Pp.textf "Logs for package %s" (Package.Name.to_string pkg_name) in - [ pp_pkg; Pp.verbatim error ] @ depexts_warning, loc + let pp_pkg = Pp.textf "Logs for package %s" (Package.Name.to_string (fst t.pkg)) in + [ pp_pkg; Pp.verbatim error ] ;; let prerr ~rc error = + let hints = + lazy + (match depexts_hint error.depexts with + | None -> [] + | Some h -> [ h ]) + in + let loc = snd error.pkg in match Predicate.test error.accepted_exit_codes rc, error.display with | false, _ -> - let paragraphs, loc = Stdune.Io.read_file error.filename |> to_paragraphs error in - User_warning.emit ~loc ~is_error:true paragraphs + let paragraphs = Stdune.Io.read_file error.filename |> to_paragraphs error in + User_warning.emit ~hints:(Lazy.force hints) ~loc ~is_error:true paragraphs | true, Display.Verbose -> let content = Stdune.Io.read_file error.filename in if not (String.is_empty content) then ( - let paragraphs, loc = to_paragraphs error content in - User_warning.emit ~loc paragraphs) + let paragraphs = to_paragraphs error content in + User_warning.emit ~hints:(Lazy.force hints) ~loc paragraphs) | true, _ -> () ;; end @@ -943,8 +951,13 @@ module Action_expander = struct >>| (function | Some p -> Ok p | None -> + let hint = + depexts_hint t.depexts + |> Option.map ~f:(fun pp -> Format.asprintf "%a" Pp.to_fmt pp) + in Error (Action.Prog.Not_found.create + ?hint ~program ~context:t.context ~loc:(Some loc) diff --git a/test/blackbox-tests/test-cases/pkg/depexts/error-message.t b/test/blackbox-tests/test-cases/pkg/depexts/error-message.t index ce4807ce4f4..8051f73ec8b 100644 --- a/test/blackbox-tests/test-cases/pkg/depexts/error-message.t +++ b/test/blackbox-tests/test-cases/pkg/depexts/error-message.t @@ -37,8 +37,8 @@ Make dune.lock files with known program "dune". > (checksum md5=$(md5sum foo.tar | cut -f1 -d' ')))) > EOF -Build the project, when it fails building 'foo' package, it shows -the depexts error message. +Build the project, when it fails building 'foo' package, it shows the depexts +error message. $ dune build File "dune.lock/foo.pkg", line 3, characters 6-10: 3 | (run dune build)) @@ -47,7 +47,7 @@ the depexts error message. File "dune-project", line 1, characters 0-0: Error: Invalid first line, expected: (lang ) - You may want to verify the following depexts are installed: + Hint: You may want to verify the following depexts are installed: - unzip - gnupg [1] @@ -65,13 +65,14 @@ Make dune.lock files with unknown program and unknown package. > (checksum md5=$(md5sum foo.tar | cut -f1 -d' ')))) > EOF -Doing the same build which is supposed to show the depexts message at the end. -There is a bug at the moment, it shows the correct error message but without -depexts message. +Running the same build. It is supposed to show the depexts message at the end, +when the program is not found. $ dune build File "dune.lock/foo.pkg", line 3, characters 6-21: 3 | (run unknown-program)) ^^^^^^^^^^^^^^^ Error: Program unknown-program not found in the tree or in PATH (context: default) + Hint: You may want to verify the following depexts are installed: + - unknown-package [1]