# How to "fix" stacktraces for macros?

**URL:** <https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454>\
**Category:** General Usage\
**Tags:** macros\
**Created:** [February 11, 2020, 7:18am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454 "2020-02-11T07:18:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [February 11, 2020, 7:18am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/1 "2020-02-11T07:18:49Z")

</div>

When I time a function using [TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl)’s `@timeit`, I get broken stacktraces when there is a compilation error inside the called function.

Example:

```julia
@timeit to "mesh loading" mesh = load_mesh(restart_filename) 

```

yields something like

```julia
Loading mesh... ERROR: LoadError: MethodError: no method matching Val{1}(::Int64)
Closest candidates are:
  Val{1}() where x at essentials.jl:693
Stacktrace:
 [1] load_mesh(::String) at /home/user/.julia/packages/TimerOutputs/7Id5J/src/TimerOutput.jl:214
 [2] macro expansion at /home/user/.julia/packages/TimerOutputs/7Id5J/src/TimerOutput.jl:214 [inlined]
 [3] run() at /home/user/code/src/main.jl:32
...

```

That is, while `[3]` still points to the correct source location, `[2]` and `[1]` return useless information, making errors hard to track down unless I remove the `@timeit` macro (which may be non-trivial if there are nested macro uses). What can I do to get proper error information from within macros? I found this seemingly related issue, but I guess it does not apply to my case: [Corrupt linenumber in stacktraces for functions with macros · Issue #28618 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/28618)

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [February 17, 2020, 3:41pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/2 "2020-02-17T15:41:00Z")

</div>

@kristoffer.carlsson Sorry to ping you directly, but would you be able to give me some pointers in which direction to look for more answers?

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 17, 2020, 3:55pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/3 "2020-02-17T15:55:01Z")

</div>

Instead of `:(...)` you can try `@q(...)` from MacroTools.

```julia
help?> MacroTools.@q
  @q [expression]

  Like the quote keyword but doesn't insert line numbers from the construction
  site. e.g. compare @q begin end with quote end. Line numbers of interpolated
  expressions are preserverd.

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 17, 2020, 4:44pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/4 "2020-02-17T16:44:51Z")

</div>

Macros like `@inline` doesn’t mess with the Stacktrace info so maybe it is possible to look there how it should be done?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 17, 2020, 5:02pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/5 "2020-02-17T17:02:07Z")

</div>

I will see if I can change the macro to fix this.

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [February 17, 2020, 7:28pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/6 "2020-02-17T19:28:17Z")

</div>

I checked the `@inline` macro, but it is so stupidly simple that it is hard to tell for me where the issue in the `@timeit` macro arises.

Compare ([julia/expr.jl at 2d5741174ce3e6a394010d2e470e4269ca54607f · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/2d5741174ce3e6a394010d2e470e4269ca54607f/base/expr.jl#L192-L194)):

```julia
macro inline(ex)
    esc(isa(ex, Expr) ? pushmeta!(ex, :inline) : ex)
end

```

to [TimerOutputs.jl/TimerOutput.jl at master · KristofferC/TimerOutputs.jl · GitHub](https://github.com/KristofferC/TimerOutputs.jl/blob/master/src/TimerOutput.jl#L218-L244) (not included due to excessive length)

If there is any way I can help, though, please let me know. We are using `@timeit` to both identify and optimize hotspots, and the combination of run time and allocations is just perfect for that.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 17, 2020, 7:32pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/7 "2020-02-17T19:32:03Z")

</div>

Well, the devil in the `pushmeta!` I would guess, heh.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 17, 2020, 9:43pm UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/8 "2020-02-17T21:43:47Z")

</div>

The core problem is not really the example here but rather that you probably have

```julia
@timeit function load_mesh(...)
    ...
end

```

which creates a new function with the timings enabled from within TimerOutputs.jl and ruins the stacktrace.

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [February 18, 2020, 6:37am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/9 "2020-02-18T06:37:48Z")

</div>

No, it actually is my example (although your example might have similar issues 😉 ). Here’s an MWE:

**mwe.jl** :

```julia
using TimerOutputs

function foo(::Float64) end

@timeit "foo" foo(1)

```

Running `julia mwe.jl` then yields

```julia
ERROR: LoadError: MethodError: no method matching foo(::Int64)
Closest candidates are:
  foo(::Float64) at /path/to/mwe.jl:3
Stacktrace:
 [1] top-level scope at /home/user/.julia/packages/TimerOutputs/7Id5J/src/TimerOutput.jl:214
 [2] include at ./boot.jl:328 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1105
 [4] include(::Module, ::String) at ./Base.jl:31
 [5] exec_options(::Base.JLOptions) at ./client.jl:287
 [6] _start() at ./client.jl:460
in expression starting at /path/to/mwe.jl:5

```

However, while constructing this MWE, I came across another (probably unrelated error) that I have encountered before: If I change the first line of `mwe.jl` to `using TimerOutputs: @timeit`, I get the following error:

```julia
ERROR: LoadError: UndefVarError: TimerOutputs not defined
Stacktrace:
 [1] top-level scope at /home/user/.julia/packages/TimerOutputs/7Id5J/src/TimerOutput.jl:210
 [2] include at ./boot.jl:328 [inlined]
 [3] include_relative(::Module, ::String) at ./loading.jl:1105
 [4] include(::Module, ::String) at ./Base.jl:31
 [5] exec_options(::Base.JLOptions) at ./client.jl:287
 [6] _start() at ./client.jl:460
in expression starting at /path/to/mwe.jl:5

```

I am not an expert in Julia, but from intuition I feel that if I make available all symbols that I am using in my user code through `using`/`import`, I should not get any errors about undefined symbols.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 18, 2020, 7:02am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/10 "2020-02-18T07:02:00Z")

</div>

Yeah that looks like a bug

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [February 18, 2020, 7:02am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/11 "2020-02-18T07:02:42Z")

</div>

If it helps, I could open two separate issues for it?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 18, 2020, 7:24am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/12 "2020-02-18T07:24:37Z")

</div>

Sure

---

<div class="post-metadata">

**Author:** ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)\
**Post date:** [February 18, 2020, 7:44am UTC](https://discourse.julialang.org/t/how-to-fix-stacktraces-for-macros/34454/13 "2020-02-18T07:44:05Z")

</div>

Broken stacktraces: [Compilation errors in timed function calls yield "broken" stacktraces · Issue #77 · KristofferC/TimerOutputs.jl · GitHub](https://github.com/KristofferC/TimerOutputs.jl/issues/77)  
UndefVarError for selective imports: [UndefVarError for TimerOutput when selectively using symbols · Issue #78 · KristofferC/TimerOutputs.jl · GitHub](https://github.com/KristofferC/TimerOutputs.jl/issues/78)

Thanks for providing the extremely helpful `TimerOutputs.jl` package as well as taking an interest in these issues!
