Skip to content

Commit

Permalink
precompile: add error for using require_stdlib during precompile (Jul…
Browse files Browse the repository at this point in the history
…iaLang#56233)

This function could accidentally add a dependency on the stdlib in the
user's package, which would make it immediately stale. As pointed out to
me by topolarity
  • Loading branch information
vtjnash authored Oct 21, 2024
1 parent cba1cc0 commit 2188ba4
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,6 @@ function _insert_extension_triggers(parent::PkgId, extensions::Dict{String, Any}
end
end

precompiling_package::Bool = false
loading_extension::Bool = false
precompiling_extension::Bool = false
function run_extension_callbacks(extid::ExtensionId)
Expand Down Expand Up @@ -2288,11 +2287,6 @@ For more details regarding code loading, see the manual sections on [modules](@r
[parallel computing](@ref code-availability).
"""
function require(into::Module, mod::Symbol)
if into === Base.__toplevel__ && precompiling_package
# this error type needs to match the error type compilecache throws for non-125 errors.
error("`using/import $mod` outside of a Module detected. Importing a package outside of a module \
is not allowed during package precompilation.")
end
if _require_world_age[] != typemax(UInt)
Base.invoke_in_world(_require_world_age[], __require, into, mod)
else
Expand All @@ -2301,6 +2295,10 @@ function require(into::Module, mod::Symbol)
end

function __require(into::Module, mod::Symbol)
if into === Base.__toplevel__ && generating_output(#=incremental=#true)
error("`using/import $mod` outside of a Module detected. Importing a package outside of a module \
is not allowed during package precompilation.")
end
@lock require_lock begin
LOADING_CACHE[] = LoadingCache()
try
Expand Down Expand Up @@ -2709,6 +2707,10 @@ end
[2] https://github.com/JuliaLang/StyledStrings.jl/issues/91#issuecomment-2379602914
"""
function require_stdlib(package_uuidkey::PkgId, ext::Union{Nothing, String}=nothing)
if generating_output(#=incremental=#true)
# Otherwise this would lead to awkward dependency issues by loading a package that isn't in the Project/Manifest
error("This interactive function requires a stdlib to be loaded, and package code should instead use it directly from that stdlib.")
end
@lock require_lock begin
# the PkgId of the ext, or package if not an ext
this_uuidkey = ext isa String ? PkgId(uuid5(package_uuidkey.uuid, ext), ext) : package_uuidkey
Expand Down Expand Up @@ -3048,7 +3050,6 @@ function create_expr_cache(pkg::PkgId, input::String, output::String, output_o::
empty!(Base.EXT_DORMITORY) # If we have a custom sysimage with `EXT_DORMITORY` prepopulated
Base.track_nested_precomp($precomp_stack)
Base.precompiling_extension = $(loading_extension | isext)
Base.precompiling_package = true
Base.include_package_for_output($(pkg_str(pkg)), $(repr(abspath(input))), $(repr(depot_path)), $(repr(dl_load_path)),
$(repr(load_path)), $deps, $(repr(source_path(nothing))))
""")
Expand Down

0 comments on commit 2188ba4

Please sign in to comment.