Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrimTrailingSeparators on GetRelativePath #52912

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,21 @@ public void CombinePaths_DifferentFromPathCombine(string expected, string path1,
{
Assert.Equal(expected, PathUtilities.CombinePaths(path1, path2));
}

[ConditionalFact(typeof(WindowsOnly)), WorkItem(51602, @"https://github.com/dotnet/roslyn/issues/51602")]
jmarolf marked this conversation as resolved.
Show resolved Hide resolved
public void GetRelativePath_EnsureNo_IndexOutOfRangeException_Windows()
{
var expected = "";
var result = PathUtilities.GetRelativePath(@"C:\A\B\", @"C:\A\B");
Assert.Equal(expected, result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of making this windows only, can we instead just build the string using the platform sepaarator? or, barring that, have some tests for non-windows?

}

[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);
}
}
}
3 changes: 3 additions & 0 deletions src/Compilers/Core/Portable/FileSystem/PathUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,9 @@ public static string GetRelativePath(string directory, string fullPath)
{
string relativePath = string.Empty;

directory = TrimTrailingSeparators(directory);
fullPath = TrimTrailingSeparators(fullPath);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is always terrifying to me. Is there a reason we have our own GetRelativePath? I thought this was something the platform provided. If we write our own, won't we always have the risk of just deviating and needing to continually patch like this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because Path.GetRelativePath doesn't exist in .NET Framework of .NET Standard I assume? @dotnet/roslyn-compiler

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct. I don't know if it would be better to use the platform implementation on TFMs where it's available, and fall back to our own implementation otherwise, since if there are bugs with this it may make it harder to reproduce them.

As far as copying the platform implementation, that may help, if the implementation doesn't depend on too much stuff within dotnet/runtime. If it's practical to copy the tests for this method from dotnet/runtime repo that might be helpful too. I consider both to be up to the author's discretion here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that its would be much better if we called the underlying platform apis on .NET Core and newer. The question is whether we want #51602 to make 16.10. we basically have today and tomorrow to get this in if we do.

if (IsChildPath(directory, fullPath))
{
return GetRelativeChildPath(directory, fullPath);
Expand Down