# Julia 1.6 Stacktrace

**URL:** <https://discourse.julialang.org/t/julia-1-6-stacktrace/57981>\
**Category:** General Usage\
**Created:** [March 25, 2021, 11:39pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981 "2021-03-25T23:39:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kruxigt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kruxigt/32/11694_2.png) [@Kruxigt](https://discourse.julialang.org/u/Kruxigt)\
**Post date:** [March 25, 2021, 11:39pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/1 "2021-03-25T23:39:30Z")

</div>

Hi good folks!

I like the idea of trying to make the fairly cluttered stacktrace more readable by “syntax” highlighting. However, I don’t get the choices introduced in 1.6 at all. The filename and row number are probably what I look for most often but they are greyed out to the point of hardly being visible. On the other hand all the types are highlighted (not something I look at too much in a stacktrace).

Can this be modified by me? Am I using the stacktrace wrong?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 26, 2021, 12:41am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/2 "2021-03-26T00:41:14Z")

</div>

Personally, I tend to agree that file name + line number (not the full path) could be highlighted differently. Something else that we could improve in my opinion is the error bit itself:

 ![Screenshot 2021-03-26 at 01.36.14](https://global.discourse-cdn.com/julialang/original/3X/b/9/b9b7d4a5dba5c94d18027eba8d2cd7be7dc1a92f.png)

IMO, rather than the types of the closest candidate methods `::Type{String}` should be highlighted in red. After all it is the type that caused the error. The closest candidate types could get a different color, say, orange.

Also, I would like to have “MethodError”, that is the kind of error, in red as well. But that is less important.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 26, 2021, 12:58am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/3 "2021-03-26T00:58:48Z")

</div>

I would go even further to say that file name and line number could be the first thing at all. Something as

```julia
ERROR:....
At line XXX of test.jl.

```

I don’t know if there are technical difficulties involved though. But at least for me, the actual error message is only useful after I looked into the code and didn’t find a trivial error, which is what happens most times.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 26, 2021, 1:18am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/4 "2021-03-26T01:18:48Z")

</div>

Just for reference, at some point all the possibilities were discussed in this extreme case of bikeshedding:

[https://github.com/JuliaLang/julia/pull/36134](https://github.com/JuliaLang/julia/pull/36134)  
[https://github.com/JuliaLang/julia/issues/36026](https://github.com/JuliaLang/julia/issues/36026)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 26, 2021, 1:23am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/5 "2021-03-26T01:23:54Z")

</div>

> [@lmiq](#):
>
> I would go even further to say that file name and line number could be the first thing at all.

> [@lmiq](#):
>
> I don’t know if there are technical difficulties involved though.

For me, at least, the exact line that the error happens is not that useful. Because in half of the cases, it is not a line of my code, but instead a line of `Base` or a third-party library, because I have broken some assumption and passed garbage to a lower level function and this will break an error outside of my sources. It would be very interesting if the stacktrace emphasized strongly the first line from a source that does not come from `Base` or a “normally” installed library (for the case you are using `dev` to test your code). The heuristic probably could be improved, but if it was right on about 80% of the cases it would already save considerable time.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 26, 2021, 1:31am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/6 "2021-03-26T01:31:24Z")

</div>

> [@Henrique\_Becker](#):
>
> It would be very interesting if the stacktrace emphasized strongly the first line from a source

Yes, that is what I meant. The line of “my” code. But I understand that is not easily defined in Julia. When using `Revise`, the tracked files maybe? (Is revise tracking everything down to base? Off topic, but may make my way of thinking meaningless).

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 26, 2021, 1:31am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/7 "2021-03-26T01:31:27Z")

</div>

This second link seem to have been closed because the PR was accepted, am I wrong? Why the stacktrace is not printed as it appears there?

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [March 26, 2021, 1:36am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/8 "2021-03-26T01:36:41Z")

</div>

Yes, second link is like planning and discussion based on the previous package ClearStacktraces.jl and the first one is the actual PR with another fair amount of bikeshedding. I probably should’ve pasted them in inverted order.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [March 26, 2021, 11:55am UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/9 "2021-03-26T11:55:44Z")

</div>

Getting people to agree on “optimal” stacktrace formatting is like getting them to agree on editor themes.  
Perhaps a solution might be for `Base` to save the `n` most recent stacktraces to a file (rather like a REPL history). The user could then run a separate Julia instance with a stacktrace viewer / explorer of their choice.  
I would see three benefits:

1. When stacktraces come from unit tests, there are often several of them. It is tedious to scroll back through the REPL to view them all.
2. While fixing errors, one has to type into the REPL, which scrolls stacktraces off the screen. This argues in favor of separating a stacktrace viewer from the REPL.
3. User defined packages (or apps) can be installed. Each user can pick their favorite display format and customize it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 26, 2021, 12:11pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/10 "2021-03-26T12:11:34Z")

</div>

> [@hendri54](#):
>
> Getting people to agree on “optimal” stacktrace formatting is like getting them to agree on editor themes.

OhMyREPL allows you to customize syntax highlighting. Maybe it could do the same for stacktraces?

---

<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:** [March 26, 2021, 12:43pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/11 "2021-03-26T12:43:59Z")

</div>

The color is hardcoded at

[https://github.com/JuliaLang/julia/blob/91c297b6c983aed2828600bcd9b8ba6494f747b6/base/errorshow.jl#L735](https://github.com/JuliaLang/julia/blob/91c297b6c983aed2828600bcd9b8ba6494f747b6/base/errorshow.jl#L735)

so it is not easy to change it right now

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 26, 2021, 1:12pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/12 "2021-03-26T13:12:36Z")

</div>

Would it be possible to store the stack trace in a default global environment variable, such that packages could be written to read that variable and print different kind of information?

---

<div class="post-metadata">

**Author:** ![Kruxigt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kruxigt/32/11694_2.png) [@Kruxigt](https://discourse.julialang.org/u/Kruxigt)\
**Post date:** [March 26, 2021, 1:17pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/13 "2021-03-26T13:17:19Z")

</div>

> [@kristoffer.carlsson](#):
>
> so it is not easy to change it right now

This might very well mean that I stick to Julia 1.5 for now. Looking up the file+line number and entering in my editor is my preferred way of working as is. Especially because of multiple dispatch (so just going to one of the functions with the correct name won’t do).

By the way, I like the suggestion earlier in this thread about trying to put extra focus on the last part of the stack trace before it “leaves your own code” since this is usually the part of interest for me.

---

<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:** [March 26, 2021, 1:27pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/14 "2021-03-26T13:27:23Z")

</div>

For some sort of workaround, you can put this in your `.julia/config/startup.jl` file and tweak the color of the paths on the second line:

```julia
@eval Base begin

path_color = :red

function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolor)
    file, line = string(frame.file), frame.line
    stacktrace_expand_basepaths() && (file = something(find_source_file(file), file))
    stacktrace_contract_userdir() && (file = replaceuserpath(file))

    # Used by the REPL to make it possible to open
    # the location of a stackframe/method in the editor.
    if haskey(io, :last_shown_line_infos)
        push!(io[:last_shown_line_infos], (string(frame.file), frame.line))
    end

    inlined = getfield(frame, :inlined)
    modul = parentmodule(frame)

    # frame number
    print(io, " ", lpad("[" * string(i) * "]", digit_align_width + 2))
    print(io, " ")

    StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), frame)
    if n > 1
        printstyled(io, " (repeats $n times)"; color=:light_black)
    end
    println(io)

    # @
    printstyled(io, " " ^ (digit_align_width + 2) * "@ ", color = :light_black)

    # module
    if modul !== nothing
        printstyled(io, modul, color = modulecolor)
        print(io, " ")
    end

    # filepath
    pathparts = splitpath(file)
    folderparts = pathparts[1:end-1]
    if !isempty(folderparts)
        printstyled(io, joinpath(folderparts...) * (Sys.iswindows() ? "\\" : "/"), color = path_color)
    end

    # filename, separator, line
    # use escape codes for formatting, printstyled can't do underlined and color
    # codes are bright black (90) and underlined (4)
    function print_underlined(io::IO, s...)
        colored = get(io, :color, false)::Bool
        start_s = colored ? text_colors[path_color] * "\033[4m" : ""
        end_s = colored ? "\033[0m" : ""
        print(io, start_s, s..., end_s)
    end
    print_underlined(io, pathparts[end], ":", line)

    # inlined
    printstyled(io, inlined ? " [inlined]" : "", color = path_color)
end

end

```

 ![bild](https://global.discourse-cdn.com/julialang/original/3X/8/e/8e103f57fb0740d704c3c606efce901369971223.png)

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [March 26, 2021, 3:28pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/15 "2021-03-26T15:28:19Z")

</div>

Note that for the example `eigvals(fill("test", 3,3))` , the part under “Closest candidates are:” (in the screenshot) hasn’t changed in Julia 1.6. The big change (starting from ClearStacktraces.jl) was what’s printed under “Stacktrace:”. Not making “MethodError: no method…” red by default was another small PR, partly to make it easier for custom errors to decide their own colours.

 ![Screenshot 2021-03-26 at 11.27.11](https://global.discourse-cdn.com/julialang/original/3X/d/4/d40a7d14cb83fd127330d6905a115c664934f6a6.jpeg)

Improving the “Closest candidates” printing to better highlight relevant details would be great. And that for `methods(eigvals)` too, perhaps. Presumably both would, like the new stacktrace, follow how `@warn` prints module, file & line numbers on a separate line.

Restoring more colour to e.g. MethodError would also be nice, I think.

---

<div class="post-metadata">

**Author:** ![Kruxigt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kruxigt/32/11694_2.png) [@Kruxigt](https://discourse.julialang.org/u/Kruxigt)\
**Post date:** [March 26, 2021, 8:11pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/16 "2021-03-26T20:11:41Z")

</div>

> [@kristoffer.carlsson](#):
>
> For some sort of workaround, you can put this in your `.julia/config/startup.jl` file

Thank you for that!

Tackar så mycket! Hälsningar från andra kusten!

---

<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:** [March 26, 2021, 8:40pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/17 "2021-03-26T20:40:43Z")

</div>

One problem with finding a good general solution was that I’ve treated the available colors simply as less or more salient, but some themes just make them quite hard to read. So if your favorite theme has very low contrast light black text, for example, there’s not much one can do to remedy that. At least it seemed to me at the time the PR was discussed, a lot of diverging opinions stemmed from people using more or less contrasty themes.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 26, 2021, 12:57pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/18 "2021-04-26T12:57:04Z")

</div>

This now (Julia 1.6.1) gives the error `replaceuserpath not defined`.  
What might I be doing wrong here?  
Example:

```julia
julia> sin("x")
ERROR: MethodError: no method matching sin(::String)
Closest candidates are:
  sin(::Float16) at math.jl:1159
  sin(::ComplexF16) at math.jl:1160
  sin(::Complex{T}) where T at complex.jl:831
  ...
Stacktrace:

SYSTEM (REPL): showing an error caused an error
ERROR: UndefVarError: replaceuserpath not defined
Stacktrace:

SYSTEM (REPL): caught exception of type UndefVarError while trying to handle a nested exception; giving up

```

---

<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:** [April 26, 2021, 1:15pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/19 "2021-04-26T13:15:03Z")

</div>

Change `replaceuserpath` to `contractuser`.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [April 26, 2021, 5:49pm UTC](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981/20 "2021-04-26T17:49:21Z")

</div>

Thank you!

[Next page](https://discourse.julialang.org/t/julia-1-6-stacktrace/57981.md?page=2)
