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 1 commit
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
30 changes: 22 additions & 8 deletions lib/elixir/lib/exception.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1345,17 +1345,25 @@ defmodule UndefinedFunctionError do
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
candidates =
Enum.filter(:code.all_available(), fn {name, _, _} = candidate ->
if downcase_module_name(name) == downcased_module or
String.ends_with?(to_string(name), strip_elixir_prefix(module)) do
with {:module, module} <- load_module(candidate),
true <- function_exported?(module, function, arity) do
true
else
_ -> false
end
else
false
end
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"
if candidates != [] do
". Did you mean:\n#{Enum.map(candidates, fn {module, _, _} -> "\n * #{Exception.format_mfa(String.to_atom(to_string(module)), function, arity)}" end)}\n"
else
_ -> ""
""
end
end

Expand All @@ -1365,6 +1373,12 @@ defmodule UndefinedFunctionError do
|> Code.ensure_loaded()
end

defp strip_elixir_prefix(module) do
module
|> to_string()
|> String.replace_leading("Elixir.", "")
end

defp downcase_module_name(module) do
module
|> to_string()
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