Skip to content

Commit

Permalink
feature: Suggest candidates when alias not found
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Caglayan <alizter@gmail.com>

<!-- ps-id: 8eef5d92-83a8-4141-9bc8-234ddb4136ce -->
  • Loading branch information
Alizter committed Feb 5, 2023
1 parent 9381573 commit 9e0d15e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
21 changes: 19 additions & 2 deletions bin/alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,31 @@ let dep_on_alias_rec_multi_contexts ~dir:src_dir ~name ~contexts =
let* dir =
Action_builder.of_memo (find_dir_specified_on_command_line ~dir:src_dir)
in
let+ is_nonempty_list =
let* is_nonempty_list =
Action_builder.all
(List.map contexts ~f:(fun ctx ->
Action_builder.dep_on_alias_rec name ctx dir))
in
let+ load_dir =
Action_builder.of_memo
@@ Load_rules.load_dir ~dir:(Path.source (Source_tree.Dir.path dir))
in
let is_nonempty = List.exists is_nonempty_list ~f:Fun.id in
if (not is_nonempty) && not (Dune_engine.Alias.is_standard name) then
User_error.raise
let hints =
let aliases =
match load_dir with
| Build build -> build.aliases
| _ ->
Printf.printf "Oh no, the map is not there!\n";
Alias.Name.Map.empty
in
User_message.did_you_mean
(Alias.Name.to_string name)
~candidates:
(Alias.Name.Map.keys aliases |> List.map ~f:Alias.Name.to_string)
in
User_error.raise ~hints
[ Pp.textf "Alias %S specified on the command line is empty."
(Dune_engine.Alias.Name.to_string name)
; Pp.textf "It is not defined in %s or any of its descendants."
Expand Down
4 changes: 4 additions & 0 deletions test/blackbox-tests/test-cases/alias-candidates.t/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(rule
(alias foo)
(action
(echo "Hello, world from \"foo\"!")))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
11 changes: 11 additions & 0 deletions test/blackbox-tests/test-cases/alias-candidates.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Dune should suggest similar aliases when it cannot find one.

We have an alias "foo" but let's try to build something misspeled:
$ dune build @fou
Error: Alias "fou" specified on the command line is empty.
It is not defined in . or any of its descendants.
Oh no, the map is not there!
[1]

$ dune build @foo
Hello, world from "foo"!

0 comments on commit 9e0d15e

Please sign in to comment.