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>
  • Loading branch information
Alizter committed Oct 23, 2023
1 parent 8c91d66 commit ed57ce5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
34 changes: 28 additions & 6 deletions bin/alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ let dep_on_alias_multi_contexts ~dir ~name ~contexts =
let dep_on_alias_rec_multi_contexts ~dir:src_dir ~name ~contexts =
let open Action_builder.O in
let* dir = Action_builder.of_memo (find_dir_specified_on_command_line ~dir:src_dir) in
let+ alias_statuses =
let* alias_statuses =
Action_builder.all
(List.map contexts ~f:(fun ctx ->
let dir =
Expand All @@ -94,15 +94,37 @@ let dep_on_alias_rec_multi_contexts ~dir:src_dir ~name ~contexts =
in
Dune_rules.Alias_rec.dep_on_alias_rec name dir))
in
let is_nonempty =
List.exists alias_statuses ~f:(fun (x : Action_builder.Alias_status.t) ->
match
Alias.is_standard name
|| List.exists alias_statuses ~f:(fun (x : Action_builder.Alias_status.t) ->
match x with
| Defined -> true
| Not_defined -> false)
in
if (not is_nonempty) && not (Alias.is_standard name)
then
with
| true -> Action_builder.return ()
| false ->
let* load_dir =
Action_builder.all
@@ List.map contexts ~f:(fun ctx ->
let dir =
Source_tree.Dir.path dir
|> Path.Build.append_source (Context_name.build_dir ctx)
|> Path.build
in
Action_builder.of_memo @@ Load_rules.load_dir ~dir)
in
let hints =
let candidates =
Alias.Name.Set.union_map load_dir ~f:(function
| Load_rules.Loaded.Build build -> Alias.Name.Set.of_keys build.aliases
| _ -> Alias.Name.Set.empty)
in
User_message.did_you_mean
(Alias.Name.to_string name)
~candidates:(Alias.Name.Set.to_list_map ~f:Alias.Name.to_string candidates)
in
User_error.raise
~hints
[ Pp.textf
"Alias %S specified on the command line is empty."
(Alias.Name.to_string name)
Expand Down
1 change: 1 addition & 0 deletions doc/changes/alias_hint.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Dune now suggests other aliases when an alias is not found (#7004, @Alizter)
18 changes: 18 additions & 0 deletions test/blackbox-tests/test-cases/alias-candidates.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Dune should suggest similar aliases when it cannot find one.

$ cat > dune-project << EOF
> (lang dune 3.7)
> EOF
$ cat > dune << EOF
> (rule
> (alias foo)
> (action
> (echo "Hello, world from \"foo\"!")))
> EOF

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.
Hint: did you mean fmt or foo?
[1]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ If a generated directory is "overlaid" by a source dir, then things break.
$ dune build @foo
Error: Alias "foo" specified on the command line is empty.
It is not defined in . or any of its descendants.
Hint: did you mean fmt?
[1]

The command above claims that @foo isn't defined, but it clearly is if we
Expand Down

0 comments on commit ed57ce5

Please sign in to comment.