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

precompile: add error for using require_stdlib during precompile #56233

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
precompile: add error for using require_stdlib during precompile
This function could accidentally add a dependency on the stdlib in the
user's package, which would make it immediately stale.
  • Loading branch information
vtjnash committed Oct 18, 2024
commit 6b3cb180d49480781371abb541e7073afd5a1d81
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 @@ -2269,11 +2268,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 @@ -2282,6 +2276,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 @@ -2690,6 +2688,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 @@ -3023,7 +3025,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