Skip to content

Commit 1a6a0b5

Browse files
musmKristofferC
authored andcommitted
Fix relpath when path and startpath are in the same drive (#40323)
* Fix relpath when path and startpath are in the same drive When startpath == ".", assume the startpath is in the same drive. This subsequently required tweaking the existing logic to then canonicalize the drive casing in instances where the drive casing differs. (cherry picked from commit 89fff18)
1 parent 31e405f commit 1a6a0b5

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

base/path.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,16 @@ function relpath(path::String, startpath::String = ".")
514514
curdir = "."
515515
pardir = ".."
516516
path == startpath && return curdir
517-
path_drive, path_without_drive = splitdrive(path)
518-
startpath_drive, startpath_without_drive = splitdrive(startpath)
519-
path_arr = split(abspath(path_without_drive), path_separator_re)
520-
start_arr = split(abspath(startpath_without_drive), path_separator_re)
521517
if Sys.iswindows()
522-
lowercase(path_drive) != lowercase(startpath_drive) && return abspath(path)
518+
path_drive, path_without_drive = splitdrive(path)
519+
startpath_drive, startpath_without_drive = splitdrive(startpath)
520+
isempty(startpath_drive) && (startpath_drive = path_drive) # by default assume same as path drive
521+
uppercase(path_drive) == uppercase(startpath_drive) || return abspath(path) # if drives differ return first path
522+
path_arr = split(abspath(path_drive * path_without_drive), path_separator_re)
523+
start_arr = split(abspath(path_drive * startpath_without_drive), path_separator_re)
524+
else
525+
path_arr = split(abspath(path), path_separator_re)
526+
start_arr = split(abspath(startpath), path_separator_re)
523527
end
524528
i = 0
525529
while i < min(length(path_arr), length(start_arr))

test/path.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@
290290
# Additional cases
291291
@test_throws ArgumentError relpath(S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"), "")
292292
@test_throws ArgumentError relpath(S(""), S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"))
293+
294+
# issue 40237
295+
path = "..$(sep)a$(sep)b$(sep)c"
296+
@test relpath(abspath(path)) == path
293297
end
294298
test_relpath()
295299
end

0 commit comments

Comments
 (0)