Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
71 changes: 58 additions & 13 deletions src/Shared/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,27 +1031,72 @@ internal static string MakeRelative(string basePath, string path)
ErrorUtilities.VerifyThrowArgumentNull(basePath, nameof(basePath));
ErrorUtilities.VerifyThrowArgumentLength(path, nameof(path));

if (basePath.Length == 0)
{
return path;
}
string fullBase = Path.GetFullPath(basePath);
string fullPath = Path.GetFullPath(path);

Uri baseUri = new Uri(EnsureTrailingSlash(basePath), UriKind.Absolute); // May throw UriFormatException
string[] splitBase = fullBase.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The char array is worth caching. And looks like we already do it in Constants.DirectorySeparatorChar!

string[] splitPath = fullPath.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);

Uri pathUri = CreateUriFromPath(path);
ErrorUtilities.VerifyThrow(splitPath.Length > 0, "Cannot call MakeRelative on a path of only slashes.");

if (!pathUri.IsAbsoluteUri)
// On a mac, the path could start with any number of slashes and still be valid. We have to check them all.
int indexOfFirstNonSlashChar = 0;
while (path[indexOfFirstNonSlashChar] == Path.DirectorySeparatorChar)
{
// the path is already a relative url, we will just normalize it...
pathUri = new Uri(baseUri, pathUri);
indexOfFirstNonSlashChar++;
}
if (path.IndexOf(splitPath[0]) != indexOfFirstNonSlashChar)
{
// path was already relative so just return it
return FixFilePath(path);
}

Uri relativeUri = baseUri.MakeRelativeUri(pathUri);
string relativePath = Uri.UnescapeDataString(relativeUri.IsAbsoluteUri ? relativeUri.LocalPath : relativeUri.ToString());
int baseI = 0;
int pathI = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit (last one, promise!): Since the two integers are always incremented together, consider having only one and changing the while (baseI < ... and while (pathI < ... below to for loops. Totally optional, no perf impact, just a matter of subjective taste.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep! This PR came out unusually janky, so I appreciate all the refactoring suggestions.

while (true)
{
if (baseI == splitBase.Length)
{
if (pathI == splitPath.Length)
{
return ".";
Comment thread
ladipro marked this conversation as resolved.
Outdated
}
break;
}
else if (pathI == splitPath.Length)
{
break;
}
else if (splitBase[baseI].Equals(splitPath[pathI], PathComparison))
{
baseI++;
pathI++;
}
else
{
break;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would find the following much easier to read. As long as it's equivalent, please double-check:

Suggested change
while (true)
{
if (baseI == splitBase.Length)
{
if (pathI == splitPath.Length)
{
return ".";
}
break;
}
else if (pathI == splitPath.Length)
{
break;
}
else if (splitBase[baseI].Equals(splitPath[pathI], PathComparison))
{
baseI++;
pathI++;
}
else
{
break;
}
}
while (baseI < splitBase.Length && pathI < splitPath.Length && splitBase[baseI].Equals(splitPath[pathI], PathComparison))
{
baseI++;
pathI++;
}
if (baseI == splitBase.Length && pathI == splitPath.Length)
{
return ".";
}
}


string result = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
StringBuilder sb = StringBuilderCache.Acquire();

return result;
// If the paths have no component in common, the only valid relative path is the full path.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

hyper-nit-9000: newline here.

Comment thread
Forgind marked this conversation as resolved.
if (baseI == 0)
Comment thread
benvillalobos marked this conversation as resolved.
Outdated
{
return fullPath;
}
while (baseI < splitBase.Length)
{
sb.Append("..").Append(Path.DirectorySeparatorChar);
baseI++;
}
while (pathI < splitPath.Length)
{
sb.Append(splitPath[pathI]).Append(Path.DirectorySeparatorChar);
pathI++;
}
sb.Remove(sb.Length - 1, 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I would expect

Suggested change
sb.Remove(sb.Length - 1, 1);
sb.Length = sb.Length - 1;

to be slightly faster and also easier to read.

return StringBuilderCache.GetStringAndRelease(sb);
}

/// <summary>
Expand Down
12 changes: 3 additions & 9 deletions src/Shared/UnitTests/FileUtilities_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ private static void TestGetItemSpecModifier(string currentDirectory)
}

[Fact]
[Trait("Category", "mono-osx-failing")]
[Trait("Category", "netcore-osx-failing")]
[Trait("Category", "netcore-linux-failing")]
public void MakeRelativeTests()
{
if (NativeMethodsShared.IsWindows)
Expand All @@ -97,20 +94,17 @@ public void MakeRelativeTests()
Assert.Equal(@"e:\abc\def\foo.cpp", FileUtilities.MakeRelative(@"c:\abc\def", @"e:\abc\def\foo.cpp"));
Assert.Equal(@"foo.cpp", FileUtilities.MakeRelative(@"\\aaa\abc\def", @"\\aaa\abc\def\foo.cpp"));
Assert.Equal(@"foo.cpp", FileUtilities.MakeRelative(@"c:\abc\def", @"foo.cpp"));
Assert.Equal(@"foo.cpp", FileUtilities.MakeRelative(@"c:\abc\def", @"..\def\foo.cpp"));
Assert.Equal(@"\\host\path\file", FileUtilities.MakeRelative(@"c:\abc\def", @"\\host\path\file"));
Assert.Equal(@"\\host\d$\file", FileUtilities.MakeRelative(@"c:\abc\def", @"\\host\d$\file"));
Assert.Equal(@"..\fff\ggg.hh", FileUtilities.MakeRelative(@"c:\foo\bar\..\abc\cde", @"c:\foo\bar\..\abc\fff\ggg.hh"));
}
else
{
Assert.Equal(@"foo.cpp", FileUtilities.MakeRelative(@"/abc/def", @"/abc/def/foo.cpp"));
Assert.Equal(@"bar.cpp", FileUtilities.MakeRelative(@"/abc/def", @"/abc/def/bar.cpp"));
Assert.Equal(@"def/foo.cpp", FileUtilities.MakeRelative(@"/abc/", @"/abc/def/foo.cpp"));
Assert.Equal(@"..\foo.cpp", FileUtilities.MakeRelative(@"/abc/def/xyz", @"/abc/def/foo.cpp"));
Assert.Equal(@"..\ttt\foo.cpp", FileUtilities.MakeRelative(@"/abc/def/xyz/", @"/abc/def/ttt/foo.cpp"));
Assert.Equal(@"/abc/def/foo.cpp", FileUtilities.MakeRelative(@"/abc/def", @"/abc/def/foo.cpp"));
Assert.Equal(@"../foo.cpp", FileUtilities.MakeRelative(@"/abc/def/xyz", @"/abc/def/foo.cpp"));
Assert.Equal(@"../ttt/foo.cpp", FileUtilities.MakeRelative(@"/abc/def/xyz/", @"/abc/def/ttt/foo.cpp"));
Assert.Equal(@"foo.cpp", FileUtilities.MakeRelative(@"/abc/def", @"foo.cpp"));
Assert.Equal(@"foo.cpp", FileUtilities.MakeRelative(@"/abc/def", @"../def/foo.cpp"));
Assert.Equal(@"../fff/ggg.hh", FileUtilities.MakeRelative(@"/foo/bar/../abc/cde", @"/foo/bar/../abc/fff/ggg.hh"));
}
}
Expand Down