Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make @__DIR__ return pwd() when being run without a file #21759

Merged
merged 2 commits into from
May 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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