From 7f8cb5d0102907b91d8f142c74536d246ad23dd2 Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Sun, 8 Oct 2023 12:00:04 -0700 Subject: [PATCH] expand checks --- base/loading.jl | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/base/loading.jl b/base/loading.jl index 521719ac33961..b89ba51053d30 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)