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 602dc5a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 3 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ function source_path(default::Union{AbstractString,Void}="")
end

function source_dir()
isinteractive() && return pwd()
p = source_path(nothing)
p === nothing ? p : dirname(p)
end
Expand All @@ -534,8 +535,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
3 changes: 3 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ 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`
@test readchomp(`$exename -E "@__DIR__" -i`) == string('"', pwd(), '"')
end

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

0 comments on commit 602dc5a

Please sign in to comment.