elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.26k stars 3.34k forks source link

Better error message for undefined variables within remote calls to macro's missing a corresponding `require` #13465

Open zachdaniel opened 5 months ago

zachdaniel commented 5 months ago

Current behavior

When calling macros like Ecto.Query, you often end up with an error message that can be cryptic, especially with beginners.

For example, the following code without adding require Ecto.Query.

Ecto.Query.from row in Table, where: row.name == "fred"

will produce error: undefined variable "row"

This should be an error, but I think that a hint here would go a long way towards helping users understand what needs to happen.

Desired behavior

In this situation, the compiler should check to see if it is within a remote call to a macro that has not been required, and if so provide a helpful message to the user, something along the lines of:

perhaps you are missing require Ecto.Query?

zachdaniel commented 5 months ago

I will put this on my list for ~5-6 weeks from now unless someone else gets to it :)

Here is some feedback from the mailing list from @josevalim about how this could be implemented.


We have a record that controls compilation:

https://github.com/elixir-lang/elixir/blob/main/lib/elixir/src/elixir.hrl#L8

You are going to add a new field called "debug". This field can have three values:

Whenever we raise because of an undefined variable, we will set the debug mode to pending (if it is disabled).

Now, when compiling a function, we will see if it returns debug as "pending":

https://github.com/elixir-lang/elixir/blob/main/lib/elixir/src/elixir_clauses.erl#L38

If pending, you will set "debug" to enabled and expand it again. The enabled debug mode will disable the variables from raising again and change elixir_dispatch to code:ensure_loaded modules when looking for remote functions, in an attempt to find pending macros.

viniciusmuller commented 2 months ago

Hey there! I'm taking a look at this issue and I'm currently a bit confused at this part:

change elixir_dispatch to code:ensure_loaded modules when looking for remote functions, in an attempt to find pending macros.

I see that for the Ecto example, there's no mention of Ecto at all on the %Env{} structure for example, how should I get available macros using this helper function?

Another thing that I was thinking about, would it be possible to use the AST from EBody to extract the module + macro name from the call and try to check if it exists, and if so throwing a different error message?

josevalim commented 2 months ago

Ecto's case has already been fixed, because the undefined variable warning doesn't stop compilation and we warn about unknown function. So that's no longer a concern. :)

josevalim commented 2 months ago

To be clear, the forgotten import case is addressed. Now we only need to deal with forgotten require.