From 9bd7f49a81c36e768d95ba7232fa7571f6b6841d Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Mon, 26 Apr 2021 05:48:29 -0700 Subject: [PATCH 1/3] TrimTrailingSeparators on GetRelativePath fixes #51602 --- .../CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs | 8 ++++++++ src/Compilers/Core/Portable/FileSystem/PathUtilities.cs | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs b/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs index 16ecf206029d7..260d1093490d3 100644 --- a/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs @@ -383,5 +383,13 @@ public void CombinePaths_DifferentFromPathCombine(string expected, string path1, { Assert.Equal(expected, PathUtilities.CombinePaths(path1, path2)); } + + [Fact, WorkItem(51602, @"https://github.com/dotnet/roslyn/issues/51602")] + public void GetRelativePath_IndexOutOfRangeException() + { + var expected = ""; + var result = PathUtilities.GetRelativePath(@"C:\A\B\", @"C:\A\B"); + Assert.Equal(expected, result); + } } } diff --git a/src/Compilers/Core/Portable/FileSystem/PathUtilities.cs b/src/Compilers/Core/Portable/FileSystem/PathUtilities.cs index 297dec288f528..b3b7cedccfe78 100644 --- a/src/Compilers/Core/Portable/FileSystem/PathUtilities.cs +++ b/src/Compilers/Core/Portable/FileSystem/PathUtilities.cs @@ -534,6 +534,9 @@ public static string GetRelativePath(string directory, string fullPath) { string relativePath = string.Empty; + directory = TrimTrailingSeparators(directory); + fullPath = TrimTrailingSeparators(fullPath); + if (IsChildPath(directory, fullPath)) { return GetRelativeChildPath(directory, fullPath); From 22c6bd233da2d858be14a41e56e9152c2dfeff2b Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Mon, 26 Apr 2021 08:21:01 -0700 Subject: [PATCH 2/3] only run on windows --- .../Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs b/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs index 260d1093490d3..526e2f2b4bd37 100644 --- a/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs @@ -384,7 +384,7 @@ public void CombinePaths_DifferentFromPathCombine(string expected, string path1, Assert.Equal(expected, PathUtilities.CombinePaths(path1, path2)); } - [Fact, WorkItem(51602, @"https://github.com/dotnet/roslyn/issues/51602")] + [ConditionalFact(typeof(WindowsOnly)), WorkItem(51602, @"https://github.com/dotnet/roslyn/issues/51602")] public void GetRelativePath_IndexOutOfRangeException() { var expected = ""; From 4558a7569961f64621c00b840afcb478ac2a4e5f Mon Sep 17 00:00:00 2001 From: Jonathon Marolf Date: Mon, 26 Apr 2021 14:43:38 -0700 Subject: [PATCH 3/3] add unix tests --- .../CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs b/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs index 526e2f2b4bd37..c660ffa62b568 100644 --- a/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs +++ b/src/Compilers/Core/CodeAnalysisTest/FileSystem/PathUtilitiesTests.cs @@ -385,11 +385,19 @@ public void CombinePaths_DifferentFromPathCombine(string expected, string path1, } [ConditionalFact(typeof(WindowsOnly)), WorkItem(51602, @"https://github.com/dotnet/roslyn/issues/51602")] - public void GetRelativePath_IndexOutOfRangeException() + public void GetRelativePath_EnsureNo_IndexOutOfRangeException_Windows() { var expected = ""; var result = PathUtilities.GetRelativePath(@"C:\A\B\", @"C:\A\B"); Assert.Equal(expected, result); } + + [ConditionalFact(typeof(UnixLikeOnly)), WorkItem(51602, @"https://github.com/dotnet/roslyn/issues/51602")] + public void GetRelativePath_EnsureNo_IndexOutOfRangeException_Unix() + { + var expected = ""; + var result = PathUtilities.GetRelativePath(@"/A/B/", @"/A/B"); + Assert.Equal(expected, result); + } } }