Skip to content

Commit

Permalink
Do not Path.Join user directory if it's "C:" (#94587)
Browse files Browse the repository at this point in the history
Fixes #68503

On netfx we were concatenating user directories ending with volume separator `:` instead of joining with a path separator `\`. That is not the case in .NET [core]. This change brings that back as it is incorrect to join paths like "C:" with a separator in between the enumerated entries as the meaning of "relative to drive's CWD" gets lost.
  • Loading branch information
jozkee authored Nov 27, 2023
1 parent a56efef commit 1af3963
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,11 @@ public FileSystemInfo ToFileSystemInfo()
/// </summary>
public string ToFullPath() =>
new string(FullPath);

private static string Join(
ReadOnlySpan<char> originalRootDirectory,
ReadOnlySpan<char> relativePath,
ReadOnlySpan<char> fileName) =>
Path.Join(originalRootDirectory, relativePath, fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,18 @@ public FileSystemInfo ToFileSystemInfo()
/// <returns>A string representing the full path.</returns>
public string ToFullPath() =>
Path.Join(Directory, FileName);

private static string Join(
ReadOnlySpan<char> originalRootDirectory,
ReadOnlySpan<char> relativePath,
ReadOnlySpan<char> fileName)
{
if (originalRootDirectory.Length == 2 && originalRootDirectory[1] == Path.VolumeSeparatorChar)
{
return string.Concat(originalRootDirectory, Path.Join(relativePath, fileName));
}

return Path.Join(originalRootDirectory, relativePath, fileName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public string ToSpecifiedFullPath()
if (Path.EndsInDirectorySeparator(OriginalRootDirectory) && PathInternal.StartsWithDirectorySeparator(relativePath))
relativePath = relativePath.Slice(1);

return Path.Join(OriginalRootDirectory, relativePath, FileName);
return Join(OriginalRootDirectory, relativePath, FileName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,27 @@ public void WindowsInvalidCharsPath_Core(string invalid)
Assert.Throws<IOException>(() => GetEntries(invalid));
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[PlatformSpecific(TestPlatforms.Windows)]
public void WindowsRelativeToCurrentDrivePath()
{
RemoteExecutor.Invoke(() =>
{
string testRootDir = GetTestFilePath();
Directory.CreateDirectory(testRootDir);
Directory.SetCurrentDirectory(testRootDir);
const string TestSubdir = "foo";
Directory.CreateDirectory(Path.Combine(testRootDir, TestSubdir));
string currentDrive = testRootDir.Substring(0, 2);
string[] results = GetEntries(currentDrive);
string result = Assert.Single(results);
Assert.Equal(currentDrive + TestSubdir, result);
}).Dispose();
}

[Theory,
InlineData(" "),
InlineData(" "),
Expand Down

0 comments on commit 1af3963

Please sign in to comment.