diff --git a/base/loading.jl b/base/loading.jl index 521719ac33961f..b89ba51053d307 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -2246,12 +2246,29 @@ function load_path_setup_code(load_path::Bool=true) return code end +""" + check_src_module_wrap(srcpath::String) + +Checks that a package entry file `srcpath` has a module declaration, and that it is before any using/import statements. +""" +function check_src_module_wrap(srcpath::String) + module_rgx = r"^\s*(?:@\w*\s*)*(?:bare)?module" + load_rgx = r"\b(?:using|import)\s" + for s in eachline(srcpath) + contains(s, module_rgx) && return + if contains(s, load_rgx) + throw(ErrorException("Package $pkg source file $input has a using/import before a module declaration.")) + end + end + throw(ErrorException("Package $pkg source file $input does not contain a module declaration.")) +end + # this is called in the external process that generates precompiled package files function include_package_for_output(pkg::PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::typeof(_concrete_dependencies), source::Union{Nothing,String}) - if !occursin("module ", readuntil(input, "module ", keep = true)) - throw(ErrorException("Package $pkg source file $input does not contain a module.")) - end + + check_src_module_wrap(input) + append!(empty!(Base.DEPOT_PATH), depot_path) append!(empty!(Base.DL_LOAD_PATH), dl_load_path) append!(empty!(Base.LOAD_PATH), load_path)