Skip to content

Commit

Permalink
Make @__DIR__ return pwd() when being run without a file (#21759)
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan authored May 14, 2017
1 parent f55543a commit 9b428dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ This section lists changes that do not have deprecation warnings.
* `ntuple(f, n::Integer)` throws `ArgumentError` if `n` is negative.
Previously an empty tuple was returned ([#21697]).

* `@__DIR__` returns the current working directory rather than `nothing` when not run
from a file ([#21759]).


Library improvements
--------------------

Expand Down
4 changes: 2 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ end

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

"""
Expand All @@ -545,7 +545,7 @@ 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
containing the macro. Returns the current working directory if run from a REPL or 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 = sprint(show, pwd())
@test readchomp(`$exename -E "@__DIR__" -i`) == wd
@test readchomp(`$exename -E "cd(()->eval(:(@__DIR__)), tempdir())" -i`) != wd
@test readchomp(`$exename -E "@__DIR__"`) == wd # non-interactive
end

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

0 comments on commit 9b428dc

Please sign in to comment.