Skip to content

Commit 94bf33e

Browse files
belamensomusmvtjnash
authored andcommitted
Fix #38491: fix an abspath() edge case on Windows (#38981)
* Fix #38491: fix an abspath() edge case on Windows * Update base/path.jl Co-authored-by: Jameson Nash <vtjnash@gmail.com> * Update base/path.jl * Update test/path.jl Co-authored-by: Jameson Nash <vtjnash@gmail.com> Co-authored-by: Mustafa M <mus-m@outlook.com> Co-authored-by: Jameson Nash <vtjnash@gmail.com> (cherry picked from commit f023677)
1 parent 9d30535 commit 94bf33e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

base/path.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,19 @@ normpath(a::AbstractString, b::AbstractString...) = normpath(joinpath(a,b...))
387387
Convert a path to an absolute path by adding the current directory if necessary.
388388
Also normalizes the path as in [`normpath`](@ref).
389389
"""
390-
abspath(a::String) = normpath(isabspath(a) ? a : joinpath(pwd(),a))
390+
function abspath(a::String)::String
391+
if !isabspath(a)
392+
cwd = pwd()
393+
a_drive, a_nodrive = splitdrive(a)
394+
if a_drive != "" && lowercase(splitdrive(cwd)[1]) != lowercase(a_drive)
395+
cwd = a_drive * path_separator
396+
a = joinpath(cwd, a_nodrive)
397+
else
398+
a = joinpath(cwd, a)
399+
end
400+
end
401+
return normpath(a)
402+
end
391403

392404
"""
393405
abspath(path::AbstractString, paths::AbstractString...) -> String

test/path.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@
99
@test isabspath(S(homedir()))
1010
@test !isabspath(S("foo"))
1111
end
12+
if Sys.iswindows()
13+
@testset "issue #38491" begin
14+
pwd_drive = uppercase(splitdrive(pwd())[1])
15+
drive = (pwd_drive == "X:") ? "Y:" : "X:"
16+
@test abspath("$(lowercase(drive))a\\b\\c") == "$(lowercase(drive))\\a\\b\\c"
17+
@test abspath("$(uppercase(drive))a\\b\\c") == "$(uppercase(drive))\\a\\b\\c"
18+
@test abspath("$(lowercase(drive))a") == "$(lowercase(drive))\\a"
19+
@test abspath("$(uppercase(drive))a") == "$(uppercase(drive))\\a"
20+
@test abspath(lowercase(drive)) == "$(lowercase(drive))\\"
21+
@test abspath(uppercase(drive)) == "$(uppercase(drive))\\"
22+
23+
@test lowercase(abspath("$(pwd_drive)a\\b\\c")) == lowercase(joinpath(pwd(), "a\\b\\c"))
24+
@test lowercase(abspath("$(pwd_drive)a")) == lowercase(joinpath(pwd(), "a"))
25+
@test lowercase(abspath(lowercase(pwd_drive))) == lowercase("$(pwd())\\")
26+
@test lowercase(abspath(uppercase(pwd_drive))) == lowercase("$(pwd())\\")
27+
end
28+
end
1229
@test basename(S("foo$(sep)bar")) == "bar"
1330
@test dirname(S("foo$(sep)bar")) == "foo"
1431

0 commit comments

Comments
 (0)