From 79559ac20e19eabc4a9c17d620f23c4e71f7448c Mon Sep 17 00:00:00 2001 From: Mustafa M Date: Sun, 4 Apr 2021 05:54:36 -0400 Subject: [PATCH] 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 89fff18d590db9be84598e0045003b903d48d2df) --- base/path.jl | 14 +++++++++----- test/path.jl | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/base/path.jl b/base/path.jl index d6566f209dd0c..91386cb562d56 100644 --- a/base/path.jl +++ b/base/path.jl @@ -514,12 +514,16 @@ function relpath(path::String, startpath::String = ".") curdir = "." pardir = ".." path == startpath && return curdir - path_drive, path_without_drive = splitdrive(path) - startpath_drive, startpath_without_drive = splitdrive(startpath) - path_arr = split(abspath(path_without_drive), path_separator_re) - start_arr = split(abspath(startpath_without_drive), path_separator_re) if Sys.iswindows() - lowercase(path_drive) != lowercase(startpath_drive) && return abspath(path) + path_drive, path_without_drive = splitdrive(path) + startpath_drive, startpath_without_drive = splitdrive(startpath) + isempty(startpath_drive) && (startpath_drive = path_drive) # by default assume same as path drive + uppercase(path_drive) == uppercase(startpath_drive) || return abspath(path) # if drives differ return first path + path_arr = split(abspath(path_drive * path_without_drive), path_separator_re) + start_arr = split(abspath(path_drive * startpath_without_drive), path_separator_re) + else + path_arr = split(abspath(path), path_separator_re) + start_arr = split(abspath(startpath), path_separator_re) end i = 0 while i < min(length(path_arr), length(start_arr)) diff --git a/test/path.jl b/test/path.jl index ca772e24d41de..e263b7c7db917 100644 --- a/test/path.jl +++ b/test/path.jl @@ -290,6 +290,10 @@ # Additional cases @test_throws ArgumentError relpath(S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)"), "") @test_throws ArgumentError relpath(S(""), S("$(sep)home$(sep)user$(sep)dir_withendsep$(sep)")) + + # issue 40237 + path = "..$(sep)a$(sep)b$(sep)c" + @test relpath(abspath(path)) == path end test_relpath() end