Skip to content

Commit

Permalink
Make at-__DIR__ return pwd() when run interactively
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed May 9, 2017
1 parent d214d57 commit a14769a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 7 additions & 3 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,11 @@ end

function source_dir()
p = source_path(nothing)
p === nothing ? p : dirname(p)
if p === nothing
isinteractive() ? pwd() : p
else
dirname(p)
end
end

"""
Expand All @@ -534,8 +538,8 @@ macro __FILE__() source_path() end
@__DIR__ -> AbstractString
`@__DIR__` expands to a string with the directory part of the absolute path of the file
containing the macro. Returns `nothing` if run from a REPL or an empty string if
evaluated by `julia -e <expr>`.
containing the macro. Returns the current working directory if run from a REPL or an empty
string if evaluated by `julia -e <expr>`.
"""
macro __DIR__() source_dir() end

Expand Down
6 changes: 6 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ include_string_test_func = include_string("include_string_test() = @__FILE__", t

@test isdir(@__DIR__)
@test @__DIR__() == dirname(@__FILE__)
let exename = `$(Base.julia_cmd()) --precompiled=yes --startup-file=no`
wd = string('"', pwd(), '"')
@test readchomp(`$exename -E "@__DIR__" -i`) == wd
@test readchomp(`$exename -E "cd(()->@__DIR__, tempdir())" -i`) != wd
@test readchomp(`$exename -E "@__DIR__"`) == "\"nothing\"" # non-interactive
end

# Issue #5789 and PR #13542:
mktempdir() do dir
Expand Down

0 comments on commit a14769a

Please sign in to comment.