Skip to content

Commit d4b722f

Browse files
committed
fix some issues with buildbot path not updating in stacktraces / method errors
1 parent bf886b5 commit d4b722f

File tree

5 files changed

+62
-27
lines changed

5 files changed

+62
-27
lines changed

base/errorshow.jl

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ function showerror_nostdio(err, msg::AbstractString)
371371
ccall(:jl_printf, Cint, (Ptr{Cvoid},Cstring), stderr_stream, "\n")
372372
end
373373

374+
stacktrace_expand_basepaths()::Bool =
375+
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_EXPAND_BASEPATHS", "false")) === true
376+
stacktrace_contract_userdir()::Bool =
377+
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_CONTRACT_HOMEDIR", "true")) === true
378+
stacktrace_linebreaks()::Bool =
379+
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_LINEBREAKS", "false")) === true
380+
381+
function replaceuserpath(str::String)
382+
str = replace(str, homedir() => "~")
383+
# seems to be necessary for some paths with small letter drive c:// etc
384+
str = replace(str, lowercasefirst(homedir()) => "~")
385+
return str
386+
end
387+
374388
function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=())
375389
is_arg_types = isa(ex.args, DataType)
376390
arg_types = is_arg_types ? ex.args : typesof(ex.args...)
@@ -498,7 +512,12 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
498512
end
499513
print(iob, ")")
500514
show_method_params(iob0, tv)
501-
print(iob, " at ", method.file, ":", method.line)
515+
file, line = functionloc(method)
516+
if file === nothing
517+
file = string(method.file)
518+
end
519+
stacktrace_contract_userdir() && (file = replaceuserpath(file))
520+
print(iob, " at ", file, ":", line)
502521
if !isempty(kwargs)::Bool
503522
unexpected = Symbol[]
504523
if isempty(kwords) || !(any(endswith(string(kword), "...") for kword in kwords))
@@ -555,22 +574,9 @@ end
555574
# replace `sf` as needed.
556575
const update_stackframes_callback = Ref{Function}(identity)
557576

558-
function replaceuserpath(str)
559-
str = replace(str, homedir() => "~")
560-
# seems to be necessary for some paths with small letter drive c:// etc
561-
str = replace(str, lowercasefirst(homedir()) => "~")
562-
return str
563-
end
564-
565577
const STACKTRACE_MODULECOLORS = [:light_blue, :light_yellow,
566578
:light_magenta, :light_green, :light_cyan, :light_red,
567579
:blue, :yellow, :magenta, :green, :cyan, :red]
568-
stacktrace_expand_basepaths()::Bool =
569-
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_EXPAND_BASEPATHS", "false")) === true
570-
stacktrace_contract_userdir()::Bool =
571-
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_CONTRACT_HOMEDIR", "true")) === true
572-
stacktrace_linebreaks()::Bool =
573-
tryparse(Bool, get(ENV, "JULIA_STACKTRACE_LINEBREAKS", "false")) === true
574580

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

base/methodshow.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ end
124124
# (Used by Revise and perhaps other packages.)
125125
const methodloc_callback = Ref{Union{Function, Nothing}}(nothing)
126126

127+
function updated_methodfile(file::String)
128+
# The file defining Base.Sys gets included after this file is included so make sure
129+
# this function is valid even in this intermediary state
130+
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB
131+
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
132+
file = replace(string(file), normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB))
133+
end
134+
return file
135+
end
136+
127137
# This function does the method location updating
128138
function updated_methodloc(m::Method)::Tuple{String, Int32}
129139
file, line = m.file, m.line
@@ -133,13 +143,8 @@ function updated_methodloc(m::Method)::Tuple{String, Int32}
133143
catch
134144
end
135145
end
136-
# The file defining Base.Sys gets included after this file is included so make sure
137-
# this function is valid even in this intermediary state
138-
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB
139-
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
140-
file = replace(string(file), normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB))
141-
end
142-
return string(file), line
146+
file = updated_methodfile(string(file))
147+
return file, line
143148
end
144149

145150
functionloc(m::Core.MethodInstance) = functionloc(m.def)

stdlib/InteractiveUtils/test/runtests.jl

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,29 @@ end
505505
@test_throws ArgumentError("Windows clipboard strings cannot contain NUL character") clipboard("abc\0")
506506
end
507507

508-
# buildbot path updating
509-
file, ln = functionloc(versioninfo, Tuple{})
510-
@test isfile(file)
508+
@testset "buildbot path updating" begin
509+
file, ln = functionloc(versioninfo, Tuple{})
510+
@test isfile(file)
511+
512+
e = try versioninfo("wat")
513+
catch e
514+
e
515+
end
516+
@test e isa MethodError
517+
s = sprint(showerror, e)
518+
m = match(r"at (.*?):", s)
519+
@test isfile(expanduser(m.captures[1]))
520+
521+
g() = x
522+
e, bt = try code_llvm(g, Tuple{Int})
523+
catch e
524+
e, catch_backtrace()
525+
end
526+
@test e isa Exception
527+
s = sprint(showerror, e, bt)
528+
m = match(r"(\S*InteractiveUtils\/src\S*):", s)
529+
@test isfile(expanduser(m.captures[1]))
530+
end
511531

512532
@testset "Issue #34434" begin
513533
io = IOBuffer()

test/errorshow.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ include("testenv.jl")
4848
end
4949
end
5050

51-
52-
cfile = " at $(@__FILE__):"
51+
file = @__FILE__
52+
Base.stacktrace_contract_userdir() && (file = Base.replaceuserpath(file))
53+
cfile = " at $file:"
5354
c1line = @__LINE__() + 1
5455
method_c1(x::Float64, s::AbstractString...) = true
5556

test/worlds.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ f265(::Int) = 1
136136

137137
# test for method errors
138138
h265() = true
139-
loc_h265 = "$(@__FILE__):$(@__LINE__() - 1)"
139+
file = @__FILE__
140+
Base.stacktrace_contract_userdir() && (file = Base.replaceuserpath(file))
141+
loc_h265 = "$file:$(@__LINE__() - 1)"
140142
@test h265()
141143
@test_throws TaskFailedException(t265) put_n_take!(h265, ())
142144
@test_throws TaskFailedException(t265) fetch(t265)

0 commit comments

Comments
 (0)