Skip to content

Commit f9975d1

Browse files
authored
workaround wrong paths reported for stdlib functions (#32763)
* workaround wrong paths reported for stdlib functions in less and edit * improve stdlib path rewriting * fixups
1 parent e5c9a43 commit f9975d1

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

base/methodshow.jl

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,25 @@ end
119119
# In case the line numbers in the source code have changed since the code was compiled,
120120
# allow packages to set a callback function that corrects them.
121121
# (Used by Revise and perhaps other packages.)
122-
default_methodloc(method::Method) = method.file, method.line
123-
const methodloc_callback = Ref{Function}(default_methodloc)
122+
const methodloc_callback = Ref{Union{Function, Nothing}}(nothing)
123+
124+
# This function does the method location updating
125+
function updated_methodloc(m::Method)::Tuple{String, Int32}
126+
file, line = m.file, m.line
127+
if methodloc_callback[] !== nothing
128+
try
129+
file, line = invokelatest(methodloc_callback[], m)
130+
catch
131+
end
132+
end
133+
# The file defining Base.Sys gets included after this file is included so make sure
134+
# this function is valid even in this intermediary state
135+
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB
136+
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
137+
file = replace(string(file), normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB))
138+
end
139+
return string(file), line
140+
end
124141

125142
functionloc(m::Core.MethodInstance) = functionloc(m.def)
126143

@@ -130,7 +147,7 @@ functionloc(m::Core.MethodInstance) = functionloc(m.def)
130147
Returns a tuple `(filename,line)` giving the location of a `Method` definition.
131148
"""
132149
function functionloc(m::Method)
133-
file, ln = invokelatest(methodloc_callback[], m)
150+
file, ln = updated_methodloc(m)
134151
if ln <= 0
135152
error("could not determine location of method definition")
136153
end
@@ -195,10 +212,7 @@ function show(io::IO, m::Method)
195212
show_method_params(io, tv)
196213
print(io, " in ", m.module)
197214
if line > 0
198-
try
199-
file, line = invokelatest(methodloc_callback[], m)
200-
catch
201-
end
215+
file, line = updated_methodloc(m)
202216
print(io, " at ", file, ":", line)
203217
end
204218
end
@@ -247,11 +261,7 @@ function show_method_table(io::IO, ms::MethodList, max::Int=-1, header::Bool=tru
247261
println(io)
248262
print(io, "[$(n)] ")
249263
show(io, meth)
250-
file, line = meth.file, meth.line
251-
try
252-
file, line = invokelatest(methodloc_callback[], meth)
253-
catch
254-
end
264+
file, line = updated_methodloc(meth)
255265
push!(LAST_SHOWN_LINE_INFOS, (string(file), line))
256266
else
257267
rest += 1
@@ -361,10 +371,7 @@ function show(io::IO, ::MIME"text/html", m::Method)
361371
end
362372
print(io, " in ", m.module)
363373
if line > 0
364-
try
365-
file, line = invokelatest(methodloc_callback[], m)
366-
catch
367-
end
374+
file, line = updated_methodloc(m)
368375
u = url(m)
369376
if isempty(u)
370377
print(io, " at ", file, ":", line)
@@ -398,11 +405,7 @@ function show(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method})
398405
first = false
399406
print(io, "[$(i)] ")
400407
show(io, m)
401-
file, line = m.file, m.line
402-
try
403-
file, line = invokelatest(methodloc_callback[], m)
404-
catch
405-
end
408+
file, line = updated_methodloc(m)
406409
push!(LAST_SHOWN_LINE_INFOS, (string(file), line))
407410
end
408411
end

base/sysinfo.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ A string containing the full path to the directory containing the `julia` execut
4949
A string containing the full path to the directory containing the `stdlib` packages.
5050
"""
5151
STDLIB = "$BINDIR/../share/julia/stdlib/v$(VERSION.major).$(VERSION.minor)" # for bootstrap
52+
# In case STDLIB change after julia is built, the variable below can be used
53+
# to update cached method locations to updated ones.
54+
const BUILD_STDLIB_PATH = STDLIB
5255

5356
# helper to avoid triggering precompile warnings
5457

stdlib/InteractiveUtils/test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,7 @@ if Sys.iswindows() || Sys.isapple()
389389
@test clipboard() == str
390390
end
391391
end
392+
393+
# buildbot path updating
394+
file, ln = functionloc(versioninfo, Tuple{})
395+
@test isfile(file)

test/show.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,6 @@ else
637637
end
638638

639639
# Method location correction (Revise integration)
640-
methloc = Base.methodloc_callback[]
641640
dummyloc(m::Method) = :nofile, 123456789
642641
Base.methodloc_callback[] = dummyloc
643642
let repr = sprint(show, "text/plain", methods(Base.inbase))
@@ -646,7 +645,7 @@ end
646645
let repr = sprint(show, "text/html", methods(Base.inbase))
647646
@test occursin("nofile:123456789", repr)
648647
end
649-
Base.methodloc_callback[] = methloc
648+
Base.methodloc_callback[] = nothing
650649

651650
@testset "matrix printing" begin
652651
# print_matrix should be able to handle small and large objects easily, test by

0 commit comments

Comments
 (0)