Skip to content

Commit

Permalink
fix #9079
Browse files Browse the repository at this point in the history
in `using` and `import`, we used to search cwd followed by the
load path. now, search (source_path or cwd) followed by load path,
where cwd is only used if we're outside any file.
  • Loading branch information
JeffBezanson committed Jul 20, 2015
1 parent 7b18780 commit 189a043
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

# Base.require is the implementation for the `import` statement

function find_in_path(name::AbstractString)
# `wd` is a working directory to search. from the top level (e.g the prompt)
# it's just the cwd, otherwise it will be set to source_dir().
function find_in_path(name::AbstractString, wd = pwd())
isabspath(name) && return name
isfile(name) && return abspath(name)
if wd === nothing; wd = pwd(); end
isfile(joinpath(wd,name)) && return joinpath(wd,name)
base = name
if endswith(name,".jl")
base = name[1:end-3]
else
name = string(base,".jl")
isfile(name) && return abspath(name)
isfile(joinpath(wd,name)) && return joinpath(wd,name)
end
for prefix in [Pkg.dir(); LOAD_PATH]
path = joinpath(prefix, name)
Expand All @@ -23,8 +26,13 @@ function find_in_path(name::AbstractString)
return nothing
end

find_in_node_path(name, node::Int=1) = myid() == node ?
find_in_path(name) : remotecall_fetch(node, find_in_path, name)
function find_in_node_path(name, srcpath, node::Int=1)
if myid() == node
find_in_path(name, srcpath)
else
remotecall_fetch(node, find_in_path, name, srcpath)
end
end

function find_source_file(file)
(isabspath(file) || isfile(file)) && return file
Expand Down Expand Up @@ -127,7 +135,7 @@ function require(mod::Symbol)
end

name = string(mod)
path = find_in_node_path(name, 1)
path = find_in_node_path(name, source_dir(), 1)
path === nothing && throw(ArgumentError("$name not found in path"))
if last && myid() == 1 && nprocs() > 1
# broadcast top-level import/using from node 1 (only)
Expand Down Expand Up @@ -169,6 +177,11 @@ function source_path(default::Union{AbstractString,Void}="")
end
end

function source_dir()
p = source_path(nothing)
p === nothing ? p : dirname(p)
end

macro __FILE__() source_path() end

function include_from_node1(path::AbstractString)
Expand Down

0 comments on commit 189a043

Please sign in to comment.