Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix different .cachepath initialization points for precompile load #45149

Merged
merged 1 commit into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ end
# these return either the array of modules loaded from the path / content given
# or an Exception that describes why it couldn't be loaded
# and it reconnects the Base.Docs.META
function _include_from_serialized(path::String, depmods::Vector{Any})
function _include_from_serialized(pkg::PkgId, path::String, depmods::Vector{Any})
sv = ccall(:jl_restore_incremental, Any, (Cstring, Any), path, depmods)
if isa(sv, Exception)
return sv
Expand All @@ -805,6 +805,11 @@ function _include_from_serialized(path::String, depmods::Vector{Any})
register_root_module(M)
end
end

# Register this cache path now - If Requires.jl is loaded, Revise may end
# up looking at the cache path during the init callback.
get!(PkgOrigin, pkgorigins, pkg).cachepath = path

inits = sv[2]::Vector{Any}
if !isempty(inits)
unlock(require_lock) # temporarily _unlock_ during these callbacks
Expand Down Expand Up @@ -859,7 +864,7 @@ function _tryrequire_from_serialized(modkey::PkgId, build_id::UInt64, modpath::U
return nothing
end

function _require_from_serialized(path::String)
function _require_from_serialized(pkg::PkgId, path::String)
# loads a precompile cache file, ignoring stale_cachfile tests
# load all of the dependent modules first
local depmodnames
Expand All @@ -880,7 +885,7 @@ function _require_from_serialized(path::String)
depmods[i] = dep::Module
end
# then load the file
return _include_from_serialized(path, depmods)
return _include_from_serialized(pkg, path, depmods)
end

# use an Int counter so that nested @time_imports calls all remain open
Expand Down Expand Up @@ -924,7 +929,7 @@ const TIMING_IMPORTS = Threads.Atomic{Int}(0)
if staledeps === true
continue
end
restored = _include_from_serialized(path_to_try, staledeps)
restored = _include_from_serialized(pkg, path_to_try, staledeps)
if isa(restored, Exception)
@debug "Deserialization checks failed while attempting to load cache from $path_to_try" exception=restored
else
Expand Down Expand Up @@ -1104,10 +1109,7 @@ require(uuidkey::PkgId) = @lock require_lock _require_prelocked(uuidkey)
function _require_prelocked(uuidkey::PkgId)
just_loaded_pkg = false
if !root_module_exists(uuidkey)
cachefile = _require(uuidkey)
if cachefile !== nothing
get!(PkgOrigin, pkgorigins, uuidkey).cachepath = cachefile
end
_require(uuidkey)
# After successfully loading, notify downstream consumers
run_package_callbacks(uuidkey)
just_loaded_pkg = true
Expand Down Expand Up @@ -1244,11 +1246,11 @@ function _require(pkg::PkgId)
end
# fall-through to loading the file locally
else
m = _require_from_serialized(cachefile)
m = _require_from_serialized(pkg, cachefile)
if isa(m, Exception)
@warn "The call to compilecache failed to create a usable precompiled cache file for $pkg" exception=m
else
return cachefile
return
end
end
end
Expand Down Expand Up @@ -2037,8 +2039,6 @@ get_compiletime_preferences(::Nothing) = String[]
@debug "Rejecting cache file $cachefile because preferences hash does not match 0x$(string(prefs_hash, base=16)) != 0x$(string(curr_prefs_hash, base=16))"
return true
end

get!(PkgOrigin, pkgorigins, id).cachepath = cachefile
end

return depmods # fresh cachefile
Expand Down
14 changes: 13 additions & 1 deletion test/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ precompile_test_harness(false) do dir
# the module doesn't reload from the image:
@test_warn "@ccallable was already defined for this method name" begin
@test_logs (:warn, "Replacing module `$Foo_module`") begin
ms = Base._require_from_serialized(cachefile)
ms = Base._require_from_serialized(Base.PkgId(Foo), cachefile)
@test isa(ms, Array{Any,1})
end
end
Expand Down Expand Up @@ -1278,3 +1278,15 @@ end
@test any(mi -> mi.specTypes.parameters[2] === Any, mis)
@test all(mi -> isa(mi.cache, Core.CodeInstance), mis)
end

# Test that the cachepath is available in pkgorigins during the
# __init__ callback
precompile_test_harness("__init__ cachepath") do load_path
write(joinpath(load_path, "InitCachePath.jl"),
"""
module InitCachePath
__init__() = Base.pkgorigins[Base.PkgId(InitCachePath)]
end
""")
@test isa((@eval (using InitCachePath; InitCachePath)), Module)
end