diff --git a/base/initdefs.jl b/base/initdefs.jl index ef223bd70a7c9..3b2c9bac4a8ca 100644 --- a/base/initdefs.jl +++ b/base/initdefs.jl @@ -54,19 +54,9 @@ isinteractive() = (is_interactive::Bool) An array of paths as strings or custom loader objects for the `require` function and `using` and `import` statements to consider when loading -code. To create a custom loader type, define the type and then add -appropriate methods to the `Base.load_hook` function with the following -signature: - - Base.load_hook(loader::Loader, name::String, found::Any) - -The `loader` argument is the current value in `LOAD_PATH`, `name` is the -name of the module to load, and `found` is the path of any previously -found code to provide `name`. If no provider has been found earlier in -`LOAD_PATH` then the value of `found` will be `nothing`. Custom loader -functionality is experimental and may break or change in Julia 1.0. +code. """ -const LOAD_PATH = Any[] +const LOAD_PATH = String[] const LOAD_CACHE_PATH = String[] function init_load_path(JULIA_HOME = JULIA_HOME) diff --git a/base/loading.jl b/base/loading.jl index 0ecdeb6e58469..16afad4e66ab4 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -80,52 +80,28 @@ else end end -function load_hook(prefix::String, name::String, ::Void) - name_jl = "$name.jl" - path = joinpath(prefix, name_jl) - isfile_casesensitive(path) && return abspath(path) - path = joinpath(prefix, name_jl, "src", name_jl) - isfile_casesensitive(path) && return abspath(path) - path = joinpath(prefix, name, "src", name_jl) - isfile_casesensitive(path) && return abspath(path) - return nothing +macro return_if_file(path) + quote + path = $(esc(path)) + isfile_casesensitive(path) && return path + end end -load_hook(prefix::String, name::String, path::String) = path -load_hook(prefix, name::String, ::Any) = - throw(ArgumentError("unrecognized custom loader in LOAD_PATH: $prefix")) -_str(x::AbstractString) = String(x) -_str(x) = x - -# `wd` is a working directory to search. defaults to current working directory. -# if `wd === nothing`, no extra path is searched. -function find_in_path(name::String, wd::Union{Void,String}) - isabspath(name) && return name - base = name - if endswith(name,".jl") - base = name[1:prevind(name, end-2)] - else - name = string(base,".jl") +function find_package(name::String) + endswith(name, ".jl") && (name = chop(name, 0, 3)) + for dir in [Pkg.dir(); LOAD_PATH] + dir = abspath(dir) + @return_if_file joinpath(dir, "$name.jl") + @return_if_file joinpath(dir, "$name.jl", "src", "$name.jl") + @return_if_file joinpath(dir, name, "src", "$name.jl") end - if wd !== nothing - isfile_casesensitive(joinpath(wd,name)) && return joinpath(wd,name) - end - path = nothing - path = _str(load_hook(_str(Pkg.dir()), base, path)) - for dir in LOAD_PATH - path = _str(load_hook(_str(dir), base, path)) - end - return path + return nothing end -find_in_path(name::AbstractString, wd::AbstractString = pwd()) = - find_in_path(String(name), String(wd)) -function find_source_file(file::String) - (isabspath(file) || isfile(file)) && return file - file2 = find_in_path(file) - file2 !== nothing && return file2 - file2 = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", file) - return isfile(file2) ? file2 : nothing +function find_source_file(path::String) + (isabspath(path) || isfile(path)) && return path + base_path = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", path) + return isfile(base_path) ? base_path : nothing end function find_all_in_cache_path(mod::Symbol) @@ -416,7 +392,7 @@ function _require(mod::Symbol) toplevel_load[] = false # perform the search operation to select the module file require intends to load name = string(mod) - path = find_in_path(name, nothing) + path = find_package(name) if path === nothing throw(ArgumentError("Module $name not found in current path.\nRun `Pkg.add(\"$name\")` to install the $name package.")) end @@ -628,7 +604,7 @@ for important notes. """ function compilecache(name::String) # decide where to get the source file from - path = find_in_path(name, nothing) + path = find_package(name) path === nothing && throw(ArgumentError("$name not found in path")) path = String(path) # decide where to put the resulting cache file @@ -778,7 +754,7 @@ function stale_cachefile(modpath::String, cachefile::String) continue end name = string(mod) - path = find_in_path(name, nothing) + path = find_package(name) if path === nothing return true # Won't be able to fullfill dependency end diff --git a/base/precompile.jl b/base/precompile.jl index 86bcc216f32c8..72ebc60a917f1 100644 --- a/base/precompile.jl +++ b/base/precompile.jl @@ -1720,7 +1720,8 @@ precompile(Tuple{typeof(Base.Filesystem.samefile), String, String}) precompile(Tuple{typeof(Base.Filesystem.unlink), String}) precompile(Tuple{typeof(Base.finalizer), Base.Distributed.Future, typeof(Base.Distributed.finalize_ref)}) precompile(Tuple{typeof(Base.find_all_in_cache_path), Symbol}) -precompile(Tuple{typeof(Base.find_in_path), String, Void}) +precompile(Tuple{typeof(Base.find_package), String}) +precompile(Tuple{typeof(Base.find_source_file), String}) precompile(Tuple{typeof(Base.getindex), Base.ObjectIdDict, Symbol}) precompile(Tuple{typeof(Base.getindex), Type{Tuple{String, Float64}}, Tuple{String, Float64}}) precompile(Tuple{typeof(Base.Grisu._show), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, Float64, Int64, Int64, Bool, Bool}) @@ -1745,8 +1746,6 @@ precompile(Tuple{typeof(Base.isequal), String, Float64}) precompile(Tuple{typeof(Base.isequal), Tuple{String, Float64}, Tuple{String, Float64}}) precompile(Tuple{typeof(Base._jl_spawn), String, Array{String, 1}, Ptr{Void}, Base.Process, Base.PipeEndpoint, Base.TTY, Base.Filesystem.File}) precompile(Tuple{typeof(Base.julia_cmd)}) -precompile(Tuple{typeof(Base.load_hook), String, String, String}) -precompile(Tuple{typeof(Base.load_hook), String, String, Void}) precompile(Tuple{typeof(Base.next), Base.Generator{Array{Any, 1}, typeof(Base.FastMath.make_fastmath)}, Int64}) precompile(Tuple{typeof(Base.open), Base.CmdRedirect, String, Base.TTY}) precompile(Tuple{typeof(Base.open), String, String}) @@ -1836,8 +1835,6 @@ precompile(Tuple{typeof(Base.stale_cachefile), String, String}) precompile(Tuple{typeof(Base.start), Tuple{Symbol, UInt64}}) precompile(Tuple{typeof(Base.start), Tuple{Void, Void}}) precompile(Tuple{typeof(Base.:(>)), String, String}) -precompile(Tuple{typeof(Base._str), String}) -precompile(Tuple{typeof(Base._str), Void}) precompile(Tuple{typeof(Base.sync_add), Base.Distributed.Future}) precompile(Tuple{typeof(Base.trunc), Float64, Int64, Int64}) precompile(Tuple{typeof(Base.union!), Base.Set{Tuple{String, Float64}}, Tuple{Tuple{String, Float64}}}) diff --git a/test/loading.jl b/test/loading.jl index f85be6cac24fd..196ef84d6bbe6 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -78,30 +78,3 @@ mktempdir() do dir end end end - -SAVED_LOAD_PATH = copy(LOAD_PATH) -empty!(LOAD_PATH) -dir = abspath(@__DIR__) -push!(LOAD_PATH, dir) - -@test Base.find_in_path("test_sourcepath") == joinpath(dir, "test_sourcepath.jl") -@test Base.find_in_path(GenericString("test_sourcepath")) == joinpath(dir, "test_sourcepath.jl") -LOAD_PATH[end] = GenericString(LOAD_PATH[end]) -@test Base.find_in_path("test_sourcepath") == joinpath(dir, "test_sourcepath.jl") - -struct CustomLoader - path::String -end -push!(LOAD_PATH, CustomLoader("abc")) -let name = randstring(20) - @test_throws ArgumentError Base.find_in_path(name, nothing) - Base.load_hook(prefix::CustomLoader, name::String, found) = joinpath(prefix.path, name) - @test Base.find_in_path(name, nothing) == joinpath("abc", name) -end -@test Base.find_in_path("test_sourcepath", nothing) == joinpath("abc", "test_sourcepath") -Base.load_hook(prefix::CustomLoader, name::String, found::String) = found -@test Base.find_in_path("test_sourcepath", nothing) == joinpath(dir, "test_sourcepath.jl") - -empty!(LOAD_PATH) -append!(LOAD_PATH, SAVED_LOAD_PATH) -@test LOAD_PATH == SAVED_LOAD_PATH