Skip to content

Add :dbg_callback option to Code eval functions#15189

Merged
josevalim merged 1 commit intoelixir-lang:mainfrom
jonatanklosko:jk-eval-dbg-callback
Mar 13, 2026
Merged

Add :dbg_callback option to Code eval functions#15189
josevalim merged 1 commit intoelixir-lang:mainfrom
jonatanklosko:jk-eval-dbg-callback

Conversation

@jonatanklosko
Copy link
Copy Markdown
Member

This adds :dbg_callback option to Code.eval_* functions, which overrides the dbg/2 behaviour specifically for the evaluation.

The dbg callback is a compile time config. When doing dynamic evaluation, it may be desirable to override the dbg behaviour. While Application.put_env(:elixir, :dbg_callback, ...) works, overriding application compile env at runtime shouldn't be done and this approach leads to issues (for example #15178). By having :dbg_callback on the eval functions, we effectively configure the compilation of that dynamic code, without changes to the application environment.

This also fixes a bug where :prune_binding was documented not supported by eval_string/3 and eval_quoted/3.

Comment thread lib/elixir/lib/code.ex
"""
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | env_eval_opts) :: {term, binding}
def eval_string(string, binding \\ [], opts \\ [])
@spec eval_string(List.Chars.t(), binding, Macro.Env.t() | [eval_opt | env_eval_opt]) ::
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that it's not possible to pass %Macro.Env{} and eval option such as :prune_binding, but that's an inherent limitation of the current API. We could add opts \\ [] as another argument, but having three arguments with default values is likely too much.

It's not necessarily an issue though, because Code.eval_quoted_with_env/4 already provides more control by having separate env and opts options.

@jonatanklosko jonatanklosko force-pushed the jk-eval-dbg-callback branch 2 times, most recently from c5fb074 to ec19505 Compare March 13, 2026 00:07
assert Macro.Env.vars(env) == []
end

test "with :dbg_callback" do
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a single test for :dbg_callback is enough, since I added a test for :prune_binding to all of the functions to make sure the operations are propagated. Let me know if you have a preference!

@josevalim josevalim merged commit 7967ff9 into elixir-lang:main Mar 13, 2026
15 checks passed
@josevalim
Copy link
Copy Markdown
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants