Skip to content

Commit

Permalink
fix some issues with buildbot path not updating in stacktraces / meth…
Browse files Browse the repository at this point in the history
…od errors
  • Loading branch information
KristofferC committed Sep 7, 2020
1 parent bf886b5 commit c997286
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 27 deletions.
35 changes: 21 additions & 14 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,20 @@ function showerror_nostdio(err, msg::AbstractString)
ccall(:jl_printf, Cint, (Ptr{Cvoid},Cstring), stderr_stream, "\n")
end

stacktrace_expand_basepaths()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_EXPAND_BASEPATHS", "false")) === true
stacktrace_contract_userdir()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_CONTRACT_HOMEDIR", "true")) === true
stacktrace_linebreaks()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_LINEBREAKS", "false")) === true

function replaceuserpath(str::String)
str = replace(str, homedir() => "~")
# seems to be necessary for some paths with small letter drive c:// etc
str = replace(str, lowercasefirst(homedir()) => "~")
return str
end

function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=())
is_arg_types = isa(ex.args, DataType)
arg_types = is_arg_types ? ex.args : typesof(ex.args...)
Expand Down Expand Up @@ -498,7 +512,12 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
end
print(iob, ")")
show_method_params(iob0, tv)
print(iob, " at ", method.file, ":", method.line)
file, line = functionloc(method)
if file === nothing
file = string(method.file)
end
stacktrace_contract_userdir() && (file = replaceuserpath(file))
print(iob, " at ", file, ":", line)
if !isempty(kwargs)::Bool
unexpected = Symbol[]
if isempty(kwords) || !(any(endswith(string(kword), "...") for kword in kwords))
Expand Down Expand Up @@ -555,22 +574,9 @@ end
# replace `sf` as needed.
const update_stackframes_callback = Ref{Function}(identity)

function replaceuserpath(str)
str = replace(str, homedir() => "~")
# seems to be necessary for some paths with small letter drive c:// etc
str = replace(str, lowercasefirst(homedir()) => "~")
return str
end

const STACKTRACE_MODULECOLORS = [:light_blue, :light_yellow,
:light_magenta, :light_green, :light_cyan, :light_red,
:blue, :yellow, :magenta, :green, :cyan, :red]
stacktrace_expand_basepaths()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_EXPAND_BASEPATHS", "false")) === true
stacktrace_contract_userdir()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_CONTRACT_HOMEDIR", "true")) === true
stacktrace_linebreaks()::Bool =
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_LINEBREAKS", "false")) === true

function show_full_backtrace(io::IO, trace::Vector; print_linebreaks::Bool)
n = length(trace)
Expand Down Expand Up @@ -700,6 +706,7 @@ end
# Print a stack frame where the module color is set manually with `modulecolor`.
function print_stackframe(io, i, frame, n, digit_align_width, modulecolor)
file, line = string(frame.file), frame.line
file = updated_methodfile(file)
stacktrace_expand_basepaths() && (file = something(find_source_file(file), file))
stacktrace_contract_userdir() && (file = replaceuserpath(file))

Expand Down
19 changes: 12 additions & 7 deletions base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ end
# (Used by Revise and perhaps other packages.)
const methodloc_callback = Ref{Union{Function, Nothing}}(nothing)

function updated_methodfile(file::String)
# The file defining Base.Sys gets included after this file is included so make sure
# this function is valid even in this intermediary state
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
file = replace(string(file), normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB))
end
return file
end

# This function does the method location updating
function updated_methodloc(m::Method)::Tuple{String, Int32}
file, line = m.file, m.line
Expand All @@ -133,13 +143,8 @@ function updated_methodloc(m::Method)::Tuple{String, Int32}
catch
end
end
# The file defining Base.Sys gets included after this file is included so make sure
# this function is valid even in this intermediary state
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
file = replace(string(file), normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB))
end
return string(file), line
file = updated_methodfile(string(file))
return file, line
end

functionloc(m::Core.MethodInstance) = functionloc(m.def)
Expand Down
26 changes: 23 additions & 3 deletions stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,29 @@ end
@test_throws ArgumentError("Windows clipboard strings cannot contain NUL character") clipboard("abc\0")
end

# buildbot path updating
file, ln = functionloc(versioninfo, Tuple{})
@test isfile(file)
@testset "buildbot path updating" begin
file, ln = functionloc(versioninfo, Tuple{})
@test isfile(file)

e = try versioninfo("wat")
catch e
e
end
@test e isa MethodError
s = sprint(showerror, e)
m = match(r"at (.*?):[0-9]*", s)
@test isfile(expanduser(m.captures[1]))

g() = x
e, bt = try code_llvm(g, Tuple{Int})
catch e
e, catch_backtrace()
end
@test e isa Exception
s = sprint(showerror, e, bt)
m = match(r"(\S*InteractiveUtils[\/\\]src\S*):", s)
@test isfile(expanduser(m.captures[1]))
end

@testset "Issue #34434" begin
io = IOBuffer()
Expand Down
5 changes: 3 additions & 2 deletions test/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ include("testenv.jl")
end
end


cfile = " at $(@__FILE__):"
file = @__FILE__
Base.stacktrace_contract_userdir() && (file = Base.replaceuserpath(file))
cfile = " at $file:"
c1line = @__LINE__() + 1
method_c1(x::Float64, s::AbstractString...) = true

Expand Down
4 changes: 3 additions & 1 deletion test/worlds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ f265(::Int) = 1

# test for method errors
h265() = true
loc_h265 = "$(@__FILE__):$(@__LINE__() - 1)"
file = @__FILE__
Base.stacktrace_contract_userdir() && (file = Base.replaceuserpath(file))
loc_h265 = "$file:$(@__LINE__() - 3)"
@test h265()
@test_throws TaskFailedException(t265) put_n_take!(h265, ())
@test_throws TaskFailedException(t265) fetch(t265)
Expand Down

0 comments on commit c997286

Please sign in to comment.