elixir icon indicating copy to clipboard operation
elixir copied to clipboard

Code.eval_quoted does not respect `on_undefined_variable: :warn`

Open MileaRobertStefan opened this issue 1 year ago • 7 comments

Elixir and Erlang/OTP versions

Erlang/OTP 26 [erts-14.2.1] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [jit:ns]

Elixir 1.15.4 (compiled with Erlang/OTP 26)

Operating system

Windows 10

Current behavior

    try do
        Code.eval_quoted(quoted, Map.to_list(item), __ENV__) 
    rescue
      e ->
        {:error,
         "#{Map.get(e, :description) || Exception.message(e)}"}
    end

I tried to access from a map a key that does not exist. And I get: cannot compile module (errors have been logged)

The error logged is:

 error: undefined variable "fake_key"
  (library 0.1.0) path: function/2

Expected behavior

In Elixir 14 and older I would get:

undefined variable \"fake_key\" or expected \"fake_key\" to expand to an existing variable or be part of a match"

MileaRobertStefan avatar Feb 13 '24 15:02 MileaRobertStefan

Can you please provide a complete code snippet that reproduces the error? Also keep in mind that on_undefined_variable: :warn exists for compatibility purpose and it will be deprecated in future releases.

josevalim avatar Feb 13 '24 16:02 josevalim

Sure let's use this code for an example. In elixir 15.4:

q = IO.inspect(Code.string_to_quoted!("Access.get(aux, :key1)"))
x = Code.eval_quoted(q,[b: b])
IO.inspect(x)

This code produces the following errors:

error: undefined variable "aux"
  nofile:1

** (CompileError) nofile: cannot compile file (errors have been logged)

In elixir 14.2:

warning: variable "aux" does not exist and is being expanded to "aux()", please use parentheses to remove the ambiguity or change the variable name
  nofile:1

** (CompileError) nofile:1: undefined function aux/0 (there is no such import)

MileaRobertStefan avatar Feb 13 '24 17:02 MileaRobertStefan

Thank you. I understand it. The on_undefined_variable is meant to address this, how are you using (in code), how it is behaving and how do you expect it to behave?

josevalim avatar Feb 13 '24 18:02 josevalim

I've added Code.compiler_options(on_undefined_variable: :warn) at the top of my mix.ex file. In code I'm using the error text to return a nice error message that states the variable in question. I would like it to be treated as a new variable with nil or how it was in 14.2.

" Parser error: undefined variable \"fake_key\"",
" Parser error: expected \"fake_key\" to expand to an existing variable or be part of a match"

MileaRobertStefan avatar Feb 13 '24 18:02 MileaRobertStefan

In code I'm using the error text to return a nice error message that states the variable in question. I would like it to be treated as a new variable with nil or how it was in 14.2.

Yes, I am asking for this code. I need the code that reproduces exactly the failure that you are seeing, including the call to Code.compiler_options(on_undefined_variable: :warn). You can also push a small project to GitHub if you prefer.

josevalim avatar Feb 13 '24 18:02 josevalim

I'm sorry but I can not provide more code. The example is the best I could do and I think sufficient for my use case.

MileaRobertStefan avatar Feb 13 '24 18:02 MileaRobertStefan

Unless I have code that I can run, I cannot help or debug further, because when I add Code.compiler_options(on_undefined_variable: :warn) to my mix.exs, everything in the project works as you expected. If you cannot provide a minimal case, it doesn't have to be your app, then we will have to eventually close the issue. Thanks.

josevalim avatar Feb 13 '24 18:02 josevalim

Closing this for now. If you can provide a small Elixir file or project that i can either elixir run.exs or mix compile, I will be glad to take a further look. Thanks.

josevalim avatar Mar 21 '24 09:03 josevalim