Skip to content

Commit 3159674

Browse files
IanButterworthstevengj
authored andcommitted
precompilepkgs: respect loaded dependencies when precompiling for load (#56901)
1 parent 3b618cb commit 3159674

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

base/loading.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ function compilecache_path(pkg::PkgId;
18281828
path = nothing
18291829
isnothing(sourcepath) && error("Cannot locate source for $(repr("text/plain", pkg))")
18301830
for path_to_try in cachepaths
1831-
staledeps = stale_cachefile(sourcepath, path_to_try, ignore_loaded = true, requested_flags=flags)
1831+
staledeps = stale_cachefile(sourcepath, path_to_try; ignore_loaded, requested_flags=flags)
18321832
if staledeps === true
18331833
continue
18341834
end
@@ -2649,7 +2649,7 @@ function __require_prelocked(pkg::PkgId, env)
26492649
parallel_precompile_attempted = true
26502650
unlock(require_lock)
26512651
try
2652-
Precompilation.precompilepkgs([pkg.name]; _from_loading=true)
2652+
Precompilation.precompilepkgs([pkg.name]; _from_loading=true, ignore_loaded=false)
26532653
finally
26542654
lock(require_lock)
26552655
end

base/precompilation.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,12 @@ function precompilepkgs(pkgs::Vector{String}=String[];
418418
io::IO=stderr,
419419
# asking for timing disables fancy mode, as timing is shown in non-fancy mode
420420
fancyprint::Bool = can_fancyprint(io) && !timing,
421-
manifest::Bool=false,)
421+
manifest::Bool=false,
422+
ignore_loaded::Bool=true)
422423
# monomorphize this to avoid latency problems
423424
_precompilepkgs(pkgs, internal_call, strict, warn_loaded, timing, _from_loading,
424425
configs isa Vector{Config} ? configs : [configs],
425-
IOContext{IO}(io), fancyprint, manifest)
426+
IOContext{IO}(io), fancyprint, manifest, ignore_loaded)
426427
end
427428

428429
function _precompilepkgs(pkgs::Vector{String},
@@ -434,7 +435,8 @@ function _precompilepkgs(pkgs::Vector{String},
434435
configs::Vector{Config},
435436
io::IOContext{IO},
436437
fancyprint::Bool,
437-
manifest::Bool)
438+
manifest::Bool,
439+
ignore_loaded::Bool)
438440
requested_pkgs = copy(pkgs) # for understanding user intent
439441

440442
time_start = time_ns()
@@ -918,7 +920,7 @@ function _precompilepkgs(pkgs::Vector{String},
918920
wait(was_processed[(dep,config)])
919921
end
920922
circular = pkg in circular_deps
921-
is_stale = !Base.isprecompiled(pkg; ignore_loaded=true, stale_cache, cachepath_cache, cachepaths, sourcepath, flags=cacheflags)
923+
is_stale = !Base.isprecompiled(pkg; ignore_loaded, stale_cache, cachepath_cache, cachepaths, sourcepath, flags=cacheflags)
922924
if !circular && is_stale
923925
Base.acquire(parallel_limiter)
924926
is_project_dep = pkg in project_deps
@@ -944,10 +946,10 @@ function _precompilepkgs(pkgs::Vector{String},
944946
try
945947
# allows processes to wait if another process is precompiling a given package to
946948
# a functionally identical package cache (except for preferences, which may differ)
947-
t = @elapsed ret = precompile_pkgs_maybe_cachefile_lock(io, print_lock, fancyprint, pkg_config, pkgspidlocked, hascolor, parallel_limiter) do
949+
t = @elapsed ret = precompile_pkgs_maybe_cachefile_lock(io, print_lock, fancyprint, pkg_config, pkgspidlocked, hascolor, parallel_limiter, ignore_loaded) do
948950
Base.with_logger(Base.NullLogger()) do
949-
# The false here means we ignore loaded modules, so precompile for a fresh session
950-
keep_loaded_modules = false
951+
# whether to respect already loaded dependency versions
952+
keep_loaded_modules = !ignore_loaded
951953
# for extensions, any extension in our direct dependencies is one we have a right to load
952954
# for packages, we may load any extension (all possible triggers are accounted for above)
953955
loadable_exts = haskey(ext_to_parent, pkg) ? filter((dep)->haskey(ext_to_parent, dep), direct_deps[pkg]) : nothing
@@ -1037,9 +1039,11 @@ function _precompilepkgs(pkgs::Vector{String},
10371039
plural1 = length(configs) > 1 ? "dependency configurations" : n_loaded == 1 ? "dependency" : "dependencies"
10381040
plural2 = n_loaded == 1 ? "a different version is" : "different versions are"
10391041
plural3 = n_loaded == 1 ? "" : "s"
1042+
plural4 = n_loaded == 1 ? "this package" : "these packages"
10401043
print(iostr, "\n ",
10411044
color_string(string(n_loaded), Base.warn_color()),
1042-
" $(plural1) precompiled but $(plural2) currently loaded. Restart julia to access the new version$(plural3)"
1045+
" $(plural1) precompiled but $(plural2) currently loaded. Restart julia to access the new version$(plural3). \
1046+
Otherwise, loading dependents of $(plural4) may trigger further precompilation to work with the unexpected version$(plural3)."
10431047
)
10441048
end
10451049
if !isempty(precomperr_deps)
@@ -1130,7 +1134,7 @@ function _color_string(cstr::String, col::Union{Int64, Symbol}, hascolor)
11301134
end
11311135

11321136
# Can be merged with `maybe_cachefile_lock` in loading?
1133-
function precompile_pkgs_maybe_cachefile_lock(f, io::IO, print_lock::ReentrantLock, fancyprint::Bool, pkg_config, pkgspidlocked, hascolor, parallel_limiter::Base.Semaphore)
1137+
function precompile_pkgs_maybe_cachefile_lock(f, io::IO, print_lock::ReentrantLock, fancyprint::Bool, pkg_config, pkgspidlocked, hascolor, parallel_limiter::Base.Semaphore, ignore_loaded::Bool)
11341138
if !(isdefined(Base, :mkpidlock_hook) && isdefined(Base, :trymkpidlock_hook) && Base.isdefined(Base, :parse_pidfile_hook))
11351139
return f()
11361140
end
@@ -1158,7 +1162,7 @@ function precompile_pkgs_maybe_cachefile_lock(f, io::IO, print_lock::ReentrantLo
11581162
# wait until the lock is available
11591163
@invokelatest Base.mkpidlock_hook(() -> begin
11601164
# double-check in case the other process crashed or the lock expired
1161-
if Base.isprecompiled(pkg; ignore_loaded=true, flags=cacheflags) # don't use caches for this as the env state will have changed
1165+
if Base.isprecompiled(pkg; ignore_loaded, flags=cacheflags) # don't use caches for this as the env state will have changed
11621166
return nothing # returning nothing indicates a process waited for another
11631167
else
11641168
delete!(pkgspidlocked, pkg_config)

0 commit comments

Comments
 (0)