diff --git a/base/loading.jl b/base/loading.jl index 5491b120d95e7..46a0ccb0550aa 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -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 """ @@ -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 `. +containing the macro. Returns the current working directory if run from a REPL or an empty +string if evaluated by `julia -e `. """ macro __DIR__() source_dir() end diff --git a/test/loading.jl b/test/loading.jl index 618700d228411..034fadc225f44 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -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__"`) == "nothing" # non-interactive +end # Issue #5789 and PR #13542: mktempdir() do dir