# Idea for shortening stacktraces

**URL:** <https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994>\
**Category:** General Usage\
**Created:** [September 18, 2023, 4:13pm UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994 "2023-09-18T16:13:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 18, 2023, 4:13pm UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994/1 "2023-09-18T16:13:01Z")

</div>

There are already approaches for shortening stack traces in external packages, but I’ve been wanting something a little less based on others’ heuristics and more on my own knowledge about the functions I write.

So the idea is to add a function to the stack trace machinery that can determine for a given method signature whether / how many of the following stack frames can be skipped. Here’s how it works in a mockup I have. Let’s say we’re annoyed by broadcasting machinery noise in the stack trace. This is always the same I think, unless we’re dealing with some overloaded special behavior.

```julia
f(args...) = error("Some error")
f.([1,2])

```

gives:

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/7/7/779928a823b9d28d791da9276271bfdd2c6c634b.png)

Now I define my special function for the `materialize` method that always starts the broadcasting machinery:

```julia
function Base.stackframe_shortening(::Type{Tuple{typeof(Base.Broadcast.materialize), Base.Broadcast.Broadcasted}})
    ("broadcast internals", 5)
end

```

For my mockup, I just counted stack frames and didn’t do any validation logic. But in principle the function could check if the next stack frames have specific properties that allow skipping. Then this results in this shortened stack trace:

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/9/c/9c523b76533061008b5c737bcfba6606c13bfd41.png)

I wanted to know if there were already others exploring manual approaches like this, what pros and cons might be, if you think it’s worth pursuing etc. For example I thought this could be useful for the noise that `Observable`s add to Makie’s stack traces.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 18, 2023, 6:34pm UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994/2 "2023-09-18T18:34:51Z")

</div>

This is along the same lines I was thinking with [Making stacktraces shorter (reducing the number of frames) · JuliaLang/julia · Discussion #49044 · GitHub](https://github.com/JuliaLang/julia/discussions/49044). The discussion raises a diversity of examples, some of which are reputed to be hard to shorten (I haven’t gotten back to that yet…)

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [September 18, 2023, 7:12pm UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994/3 "2023-09-18T19:12:31Z")

</div>

In the meantime, check out my package here [GitHub - BioTurboNick/AbbreviatedStackTraces.jl](https://github.com/BioTurboNick/AbbreviatedStackTraces.jl)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [September 19, 2023, 7:00am UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994/4 "2023-09-19T07:00:15Z")

</div>

@BioTurboNick Yes I knew AbbreviatedStackTraces of course 🙂 I tend to find its outputs a bit _too_ short for my liking, because it operates not on a per-function base but on a boundary between user and package code base. Which is nice in that it highlights user code, but I still want to understand in what way I traverse through other functions, just not the parts that are redundant and therefore don’t add information for me. In the `sum` example that’s in the readme, I would want to know that we got to the error by mapreducing over an empty collection for which I need `mapreduce` and `mapreduce_empty_iter`.

@tim.holy I’ll have a look at that issue, thanks!

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [September 19, 2023, 1:54pm UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994/5 "2023-09-19T13:54:50Z")

</div>

The upcoming `public` keyword may be able to help here. I haven’t looked at how that could work yet though.

Though, needing to see an internal function name to know why a failure occurred IMO points to the error being bad.

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [September 19, 2023, 9:36pm UTC](https://discourse.julialang.org/t/idea-for-shortening-stacktraces/103994/6 "2023-09-19T21:36:16Z")

</div>

I was inspired. Made a PR to AbbreviatedStackTraces to explore using the public names API: [Test of relying on new public api definitions in Julia by BioTurboNick · Pull Request #64 · BioTurboNick/AbbreviatedStackTraces.jl · GitHub](https://github.com/BioTurboNick/AbbreviatedStackTraces.jl/pull/64)

Currently, just throwing back in any frames from `public` functions.
