samuelventura
What is the proper way to evaluate a string and get a stack trace that honors the provided file: script.exs environment across all the code defined in that script regardless of whether it is executed in the current process or deferred to an async task?
The first two samples refer to the provided file: script.exs on the stack trace and the last one loses it.
Sample 1 keeps the reference to script.exs in its stacktrace
iex(1)> Code.eval_string("""
try do
raise "RAISE"
rescue
e -> {e, __STACKTRACE__}
end
""", [], file: "script.exs")
{{%RuntimeError{message: "RAISE"},
[
{:elixir_eval, :__FILE__, 1, [file: ~c"script.exs", line: 2]},
{:elixir, :eval_external_handler, 3,
[file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
...
]}, []}
Sample 2 keeps the reference to script.exs in its stacktrace
iex(2)> Code.eval_string("""
fun = fn -> raise "RAISE" end
try do
fun.()
rescue
e -> {e, __STACKTRACE__}
end
""", [], file: "script.exs")
{{%RuntimeError{message: "RAISE"},
[
{:elixir_eval, :__FILE__, 1, [file: ~c"script.exs", line: 1]},
{:elixir, :eval_external_handler, 3,
[file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
...
]}, [fun: #Function<43.81571850/0 in :erl_eval.expr/6>]}
Sample 3 loses the reference to script.exs in its stacktrace
iex(3)> Code.eval_string("""
fun = fn -> raise "RAISE" end
Task.async(fn ->
try do
fun.()
rescue
e -> {e, __STACKTRACE__}
end
end) |> Task.await(:infinity)
""", [], file: "script.exs")
{{%RuntimeError{message: "RAISE"},
[
{:elixir, :eval_external_handler, 3,
[file: ~c"src/elixir.erl", line: 386, error_info: %{module: Exception}]},
{:erl_eval, :do_apply, 7, [file: ~c"erl_eval.erl", line: 919]},
{:erl_eval, :try_clauses, 10, [file: ~c"erl_eval.erl", line: 1233]},
{Task.Supervised, :invoke_mfa, 2,
[file: ~c"lib/task/supervised.ex", line: 101]}
]}, [fun: #Function<43.81571850/0 in :erl_eval.expr/6>]}
Need this to implement scripting with accurate tracing of unhandled exceptions in async code.
Things looking into at the moment:
- Process.info(self(), :current_stacktrace)
- Last call optimisation
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
josevalim
What happens in the last example if you call
List.flatten([])or any similar example, so the last instruction is not raise, but something else to prevent last call optimization on raise?samuelventura
You mean like sample below? Apparently the reference to script.exs is lost as well.
josevalim
Yes, that’s what I meant, but I was able to reproduce the issue in the shell too.
Unfortunately evaluation has less precise stacktraces because it is literally implemented by emulating the runtime in pure Erlang/Elixir and I think splitting the anonymous function is exactly those trade-offs. I will try to see if some aspects can be improved upstream, but it may not be trivial (or it may make things more expensive).
garrison
Wait, there’s a BEAM emulator in the codebase?! That is not how I expected that to work at all, lol. Out of curiosity, where would I find that code?
josevalim
Oh, it works on Erlang AST rather than BEAM.
you can find it on erl_eval.
samuelventura
Found this notice which seems to explain it.
https://github.com/elixir-lang/elixir/blob/e27cd15715bc6997c5a51497af49934c5e9221b2/lib/elixir/src/elixir.erl#L321-L336