diff --git a/base/path.jl b/base/path.jl index 1a7c2dc55018d..449085c00f26d 100644 --- a/base/path.jl +++ b/base/path.jl @@ -390,7 +390,19 @@ normpath(a::AbstractString, b::AbstractString...) = normpath(joinpath(a,b...)) Convert a path to an absolute path by adding the current directory if necessary. Also normalizes the path as in [`normpath`](@ref). """ -abspath(a::String) = normpath(isabspath(a) ? a : joinpath(pwd(),a)) +function abspath(a::String)::String + if !isabspath(a) + cwd = pwd() + a_drive, a_nodrive = splitdrive(a) + if a_drive != "" && lowercase(splitdrive(cwd)[1]) != lowercase(a_drive) + cwd = a_drive * path_separator + a = joinpath(cwd, a_nodrive) + else + a = joinpath(cwd, a) + end + end + return normpath(a) +end """ abspath(path::AbstractString, paths::AbstractString...) -> String diff --git a/test/path.jl b/test/path.jl index bbd9159c59295..ca772e24d41de 100644 --- a/test/path.jl +++ b/test/path.jl @@ -9,6 +9,23 @@ @test isabspath(S(homedir())) @test !isabspath(S("foo")) end + if Sys.iswindows() + @testset "issue #38491" begin + pwd_drive = uppercase(splitdrive(pwd())[1]) + drive = (pwd_drive == "X:") ? "Y:" : "X:" + @test abspath("$(lowercase(drive))a\\b\\c") == "$(lowercase(drive))\\a\\b\\c" + @test abspath("$(uppercase(drive))a\\b\\c") == "$(uppercase(drive))\\a\\b\\c" + @test abspath("$(lowercase(drive))a") == "$(lowercase(drive))\\a" + @test abspath("$(uppercase(drive))a") == "$(uppercase(drive))\\a" + @test abspath(lowercase(drive)) == "$(lowercase(drive))\\" + @test abspath(uppercase(drive)) == "$(uppercase(drive))\\" + + @test lowercase(abspath("$(pwd_drive)a\\b\\c")) == lowercase(joinpath(pwd(), "a\\b\\c")) + @test lowercase(abspath("$(pwd_drive)a")) == lowercase(joinpath(pwd(), "a")) + @test lowercase(abspath(lowercase(pwd_drive))) == lowercase("$(pwd())\\") + @test lowercase(abspath(uppercase(pwd_drive))) == lowercase("$(pwd())\\") + end + end @test basename(S("foo$(sep)bar")) == "bar" @test dirname(S("foo$(sep)bar")) == "foo"