Skip to content

Commit

Permalink
expand checks
Browse files Browse the repository at this point in the history
  • Loading branch information
IanButterworth committed Oct 8, 2023
1 parent 32a0a52 commit 7f8cb5d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7f8cb5d

Please sign in to comment.