Skip to content

Improve UndefinedFunctionError for unqualified module #12859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/elixir/lib/exception.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1342,20 +1342,31 @@ defmodule UndefinedFunctionError do
hint_for_loaded_module(module, function, arity, nil)
end

@max_suggestions 5
defp hint(module, function, arity, _loaded?) do
downcased_module = downcase_module_name(module)

candidate =
Enum.find(:code.all_available(), fn {name, _, _} ->
downcase_module_name(name) == downcased_module
end)

with {_, _, _} <- candidate,
{:module, module} <- load_module(candidate),
true <- function_exported?(module, function, arity) do
". Did you mean:\n\n * #{Exception.format_mfa(module, function, arity)}\n"
stripped_module = module |> Atom.to_string() |> String.replace_leading("Elixir.", "")

candidates =
for {name, _, _} = candidate <- :code.all_available(),
downcase_module_name(name) == downcased_module or
String.ends_with?(List.to_string(name), stripped_module),
{:module, module} <- [load_module(candidate)],
function_exported?(module, function, arity),
do: module

if candidates != [] do
suggestions =
candidates
|> Enum.take(@max_suggestions)
|> Enum.sort(:asc)
|> Enum.map(fn module ->
["\n * ", Exception.format_mfa(module, function, arity)]
end)

IO.iodata_to_binary([". Did you mean:\n", suggestions, "\n"])
else
_ -> ""
""
end
end

Expand Down
31 changes: 31 additions & 0 deletions lib/elixir/test/elixir/exception_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,25 @@ defmodule ExceptionTest do
end

test "annotates undefined function error with module suggestions" do
import PathHelpers

modules = [
Namespace.A.One,
Namespace.A.Two,
Namespace.A.Three,
Namespace.B.One,
Namespace.B.Two,
Namespace.B.Three
]

for module <- modules do
write_beam(
defmodule module do
def foo, do: :bar
end
)
end

assert blame_message(ENUM, & &1.map(&1, 1)) == """
function ENUM.map/2 is undefined (module ENUM is not available). Did you mean:

Expand All @@ -604,6 +623,18 @@ defmodule ExceptionTest do

assert blame_message(ENUM, & &1.not_a_function(&1, 1)) ==
"function ENUM.not_a_function/2 is undefined (module ENUM is not available)"

assert blame_message(One, & &1.foo()) == """
function One.foo/0 is undefined (module One is not available). Did you mean:

* Namespace.A.One.foo/0
* Namespace.B.One.foo/0
"""

for module <- modules do
:code.delete(module)
:code.purge(module)
end
end

test "annotates undefined function clause error with macro hints" do
Expand Down