@@ -1758,6 +1758,9 @@ function set_pkgorigin_version_path(pkg::PkgId, path::Union{String,Nothing})
17581758 nothing
17591759end
17601760
1761+ # A hook to allow code load to use Pkg.precompile
1762+ const PKG_PRECOMPILE_HOOK = Ref {Function} ()
1763+
17611764# Returns `nothing` or the new(ish) module
17621765function _require (pkg:: PkgId , env= nothing )
17631766 assert_havelock (require_lock)
@@ -1777,8 +1780,11 @@ function _require(pkg::PkgId, env=nothing)
17771780 end
17781781 set_pkgorigin_version_path (pkg, path)
17791782
1783+ pkg_precompile_attempted = false # being safe to avoid getting stuck in a Pkg.precompile loop
1784+
17801785 # attempt to load the module file via the precompile cache locations
17811786 if JLOptions (). use_compiled_modules != 0
1787+ @label load_from_cache
17821788 m = _require_search_from_serialized (pkg, path, UInt128 (0 ))
17831789 if m isa Module
17841790 return m
@@ -1800,6 +1806,16 @@ function _require(pkg::PkgId, env=nothing)
18001806
18011807 if JLOptions (). use_compiled_modules != 0
18021808 if (0 == ccall (:jl_generating_output , Cint, ())) || (JLOptions (). incremental != 0 )
1809+ if ! pkg_precompile_attempted && isassigned (PKG_PRECOMPILE_HOOK)
1810+ pkg_precompile_attempted = true
1811+ unlock (require_lock)
1812+ try
1813+ PKG_PRECOMPILE_HOOK[](pkg. name, _from_loading = true )
1814+ finally
1815+ lock (require_lock)
1816+ end
1817+ @goto load_from_cache
1818+ end
18031819 # spawn off a new incremental pre-compile task for recursive `require` calls
18041820 cachefile = compilecache (pkg, path)
18051821 if isa (cachefile, Exception)
0 commit comments