Skip to content

Commit 189a043

Browse files
committed
fix #9079
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.
1 parent 7b18780 commit 189a043

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

base/loading.jl

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

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

5-
function find_in_path(name::AbstractString)
5+
# `wd` is a working directory to search. from the top level (e.g the prompt)
6+
# it's just the cwd, otherwise it will be set to source_dir().
7+
function find_in_path(name::AbstractString, wd = pwd())
68
isabspath(name) && return name
7-
isfile(name) && return abspath(name)
9+
if wd === nothing; wd = pwd(); end
10+
isfile(joinpath(wd,name)) && return joinpath(wd,name)
811
base = name
912
if endswith(name,".jl")
1013
base = name[1:end-3]
1114
else
1215
name = string(base,".jl")
13-
isfile(name) && return abspath(name)
16+
isfile(joinpath(wd,name)) && return joinpath(wd,name)
1417
end
1518
for prefix in [Pkg.dir(); LOAD_PATH]
1619
path = joinpath(prefix, name)
@@ -23,8 +26,13 @@ function find_in_path(name::AbstractString)
2326
return nothing
2427
end
2528

26-
find_in_node_path(name, node::Int=1) = myid() == node ?
27-
find_in_path(name) : remotecall_fetch(node, find_in_path, name)
29+
function find_in_node_path(name, srcpath, node::Int=1)
30+
if myid() == node
31+
find_in_path(name, srcpath)
32+
else
33+
remotecall_fetch(node, find_in_path, name, srcpath)
34+
end
35+
end
2836

2937
function find_source_file(file)
3038
(isabspath(file) || isfile(file)) && return file
@@ -127,7 +135,7 @@ function require(mod::Symbol)
127135
end
128136

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

180+
function source_dir()
181+
p = source_path(nothing)
182+
p === nothing ? p : dirname(p)
183+
end
184+
172185
macro __FILE__() source_path() end
173186

174187
function include_from_node1(path::AbstractString)

0 commit comments

Comments
 (0)