Skip to content

Commit 63f7b83

Browse files
Copilotjkotas
andcommitted
Revert to original simple fix and update test to match issue scenario
- Reverted FileSystemEnumerator.cs to the simple fix that normalizes _originalRootDirectory - Updated WindowsEnumerateDirectoryWithTrailingSpacePeriod test to match issue #113120: * Create directory WITHOUT trailing spaces * Call GetFiles WITH trailing spaces in the path * Verify returned paths do NOT contain trailing spaces * Verify File.Exists works on returned paths Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
1 parent e56dc76 commit 63f7b83

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerator.cs

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,9 @@ internal FileSystemEnumerator(string directory, bool isNormalized, EnumerationOp
3232
{
3333
ArgumentNullException.ThrowIfNull(directory);
3434

35-
// If not already normalized, we need to normalize the path. We preserve trailing
36-
// directory separators from the original input to maintain backward compatibility,
37-
// but Path.GetFullPath will normalize away trailing spaces (Windows behavior).
38-
string normalizedPath;
39-
if (!isNormalized)
40-
{
41-
// Count trailing separators from input AFTER removing any trailing spaces
42-
// (spaces after separators should not prevent separator preservation)
43-
ReadOnlySpan<char> trimmedInput = directory.AsSpan().TrimEnd(' ');
44-
int originalTrailingSeparators = 0;
45-
for (int i = trimmedInput.Length - 1; i >= 0 && PathInternal.IsDirectorySeparator(trimmedInput[i]); i--)
46-
{
47-
originalTrailingSeparators++;
48-
}
49-
50-
// Normalize the full path (this also removes trailing spaces on Windows)
51-
normalizedPath = Path.GetFullPath(directory);
52-
53-
// If original (after space trimming) had trailing separators, preserve them
54-
if (originalTrailingSeparators > 0)
55-
{
56-
// Remove any trailing separators from normalized path
57-
normalizedPath = Path.TrimEndingDirectorySeparator(normalizedPath);
58-
// Add back the exact number from the original
59-
normalizedPath += new string(Path.DirectorySeparatorChar, originalTrailingSeparators);
60-
}
61-
62-
_originalRootDirectory = normalizedPath;
63-
}
64-
else
65-
{
66-
normalizedPath = directory;
67-
_originalRootDirectory = directory;
68-
}
69-
70-
_rootDirectory = Path.TrimEndingDirectorySeparator(normalizedPath);
35+
string path = isNormalized ? directory : Path.GetFullPath(directory);
36+
_originalRootDirectory = path;
37+
_rootDirectory = Path.TrimEndingDirectorySeparator(path);
7138
_options = options ?? EnumerationOptions.Default;
7239
_remainingRecursionDepth = _options.MaxRecursionDepth;
7340

src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/GetFiles.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,26 @@ public void WindowsEnumerateFilesWithTrailingSpacePeriod(string fileName)
224224
[Theory]
225225
[MemberData(nameof(TestData.WindowsTrailingProblematicFileNames), MemberType = typeof(TestData))]
226226
[PlatformSpecific(TestPlatforms.Windows)]
227-
public void WindowsEnumerateDirectoryWithTrailingSpacePeriod(string dirName)
227+
public void WindowsEnumerateDirectoryWithTrailingSpacePeriod(string trailingChars)
228228
{
229-
DirectoryInfo parentDir = Directory.CreateDirectory(GetTestFilePath());
230-
string problematicDirPath = Path.Combine(parentDir.FullName, dirName);
231-
Directory.CreateDirectory(@"\\?\" + problematicDirPath);
232-
229+
// Test scenario from issue #113120: calling Directory.GetFiles with a path that has
230+
// trailing spaces/periods should return paths without those trailing characters.
231+
// Windows normalizes trailing spaces/periods away, so the returned paths should be normalized too.
232+
233+
DirectoryInfo testDir = Directory.CreateDirectory(GetTestFilePath());
233234
string normalFileName = "normalfile.txt";
234-
string filePath = Path.Combine(problematicDirPath, normalFileName);
235+
string filePath = Path.Combine(testDir.FullName, normalFileName);
235236
File.Create(filePath).Dispose();
236237

237-
string[] files = GetEntries(problematicDirPath);
238+
// Call GetFiles with trailing problematic characters added to the path
239+
string pathWithTrailing = testDir.FullName + trailingChars;
240+
string[] files = GetEntries(pathWithTrailing);
238241
Assert.Single(files);
239242

240243
string returnedPath = files[0];
244+
// The returned path should NOT contain the trailing spaces/periods
245+
Assert.False(returnedPath.Contains(trailingChars),
246+
$"Returned path should not contain trailing characters. Path: '{returnedPath}'");
241247
Assert.True(File.Exists(returnedPath),
242248
$"File.Exists should work on path returned by Directory.GetFiles. Path: '{returnedPath}'");
243249
}

0 commit comments

Comments
 (0)