elixir-lang / elixir

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

raising inside dbg is not handled #14005

Open dkuku opened 3 days ago

dkuku commented 3 days ago

Elixir and Erlang/OTP versions

any recent

Operating system

any

Current behavior

Currently when there is an exception we don't get any output in dbg. When debugging pipes I need to add inspects in the pipeline idealy with labels to see where it failed.

[:a, :b]
|> then(fn _ -> raise "breaks dbg" end)
|> dbg

returns

** (RuntimeError) breaks dbg
    #cell:exemuvvgh6ktixl2:2: (file)

Expected behavior

Rendering partial result would be ideal, this way we would know what was the input to the failing function. Currently the dbg is generated in 2 steps

It may be possible if the dbg result would be generated where the code is executed and in case of exceptions we could just display what was generated up to that point.

      abc() #=> [:a, :b, :c]
     |> tl() #=> [:b, :c]
     |> then(fn _ -> raise "boom" end)
      ** (RuntimeError) boom
    #cell:exemuvvgh6ktixl2:3: (file)
sabiwara commented 3 days ago

It may be possible if the dbg result would be generated where the code is executed and in case of exceptions we could just display what was generated up to that point.

It's an interesting idea, but I think it's a trade-off though: if intermediate steps are printing to stdout too, the pipeline might become unreadable:

iex(1)> :world |> then(&IO.puts("hello #{&1}!")) |> Atom.to_string() |> dbg()
hello world!
[iex:1: (file)]
:world #=> :world
|> then(&IO.puts("hello #{&1}!")) #=> :ok
|> Atom.to_string() #=> "ok"

would become

[iex:1: (file)]
:world #=> :world
hello world!
|> then(&IO.puts("hello #{&1}!")) #=> :ok
|> Atom.to_string() #=> "ok"

We could also imagine wrapping in a try + reraise, but I'm not sure it's a good approach either. Adding more intermediate dbg before the raising step might be the best approach here?

dkuku commented 3 days ago

What motivated me to create this issue was dbg on a simple function call:

function(param1, param2)
|> dbg

The function raised and I had to add inspects before it.

josevalim commented 3 days ago

I would avoid try-catch. But I think the intermediate printing would be nice!

dkuku commented 1 day ago

Something like this example? This will allow calling dbg on the condition. It would be good to refactor it first to a new Macro.Dbg module

  defp dbg_ast_to_debuggable({:if, meta, [condition_ast, clauses]} = ast, env, options) do
    condition_result_var = unique_var(:condition_result, __MODULE__)
    result_var = unique_var(:result, __MODULE__)

    quote do
      Macro.write_underline("If condition", unquote(options))
      unquote(condition_result_var) = unquote(condition_ast)

      Macro.write_ast_value(
        unquote(escape(condition_ast)),
        unquote(condition_result_var),
        unquote(options)
      )

      Macro.write_underline("If expression", unquote(options))
      unquote(result_var) = unquote({:if, meta, [condition_result_var, clauses]})

      Macro.write_ast_value(
        unquote(escape(ast)),
        unquote(result_var),
        unquote(options)
      )

      unquote(result_var)
    end
  end