@@ -2246,12 +2246,29 @@ function load_path_setup_code(load_path::Bool=true)
22462246 return code
22472247end
22482248
2249+ """
2250+ check_src_module_wrap(srcpath::String)
2251+
2252+ Checks that a package entry file `srcpath` has a module declaration, and that it is before any using/import statements.
2253+ """
2254+ function check_src_module_wrap (srcpath:: String )
2255+ module_rgx = r" ^\s *(?:@\w *\s *)*(?:bare)?module"
2256+ load_rgx = r" \b (?:using|import)\s "
2257+ for s in eachline (srcpath)
2258+ contains (s, module_rgx) && return
2259+ if contains (s, load_rgx)
2260+ throw (ErrorException (" Package $pkg source file $input has a using/import before a module declaration." ))
2261+ end
2262+ end
2263+ throw (ErrorException (" Package $pkg source file $input does not contain a module declaration." ))
2264+ end
2265+
22492266# this is called in the external process that generates precompiled package files
22502267function include_package_for_output (pkg:: PkgId , input:: String , depot_path:: Vector{String} , dl_load_path:: Vector{String} , load_path:: Vector{String} ,
22512268 concrete_deps:: typeof (_concrete_dependencies), source:: Union{Nothing,String} )
2252- if ! occursin ( " module " , readuntil (input, " module " , keep = true ))
2253- throw ( ErrorException ( " Package $pkg source file $ input does not contain a module. " ) )
2254- end
2269+
2270+ check_src_module_wrap ( input)
2271+
22552272 append! (empty! (Base. DEPOT_PATH ), depot_path)
22562273 append! (empty! (Base. DL_LOAD_PATH), dl_load_path)
22572274 append! (empty! (Base. LOAD_PATH ), load_path)
0 commit comments