Skip to content

Commit 014ce4b

Browse files
Copilotjkotas
andcommitted
Skip trimming for extended path syntax (\\?\ and \\.\)
Don't trim trailing spaces/periods from paths using extended syntax (\\?\ or \\.\) because these prefixes explicitly request no path normalization. This fixes EnumerateDirectories_TrailingDot test which creates directories with trailing dots using \\?\ prefix. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
1 parent 89b835a commit 014ce4b

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,26 @@ internal static bool NormalizeInputs(ref string directory, ref string expression
4747
// "/tmp/test/. " → "/tmp/test/" (preserve separator, remove period and space)
4848
// "/tmp/test// " → "/tmp/test//" (preserve separators, remove space)
4949
//
50-
// Special cases we don't trim (would result in empty or meaningless path):
50+
// Special cases we don't trim:
5151
// "." → "." (relative path reference)
5252
// ".." → ".." (parent directory reference)
53-
// " " → " " (only spaces)
53+
// " " → " " (only spaces - would result in empty)
54+
// "\\?\C:\test." → "\\?\C:\test." (extended path syntax - no normalization)
5455
//
55-
// Algorithm: Trim trailing spaces/periods, but only if we end up with a non-empty result
56+
// Algorithm: Trim trailing spaces/periods, but only if:
57+
// 1. Result is non-empty
58+
// 2. Path does not use extended syntax (\\?\ or \\.\)
5659

57-
string trimmed = directory.TrimEnd(' ', '.');
58-
59-
// Only apply the trim if it results in a non-empty string
60-
if (trimmed.Length > 0)
60+
// Don't trim paths using extended syntax (\\?\ or \\.\) as they explicitly disable normalization
61+
if (!PathInternal.IsExtended(directory.AsSpan()))
6162
{
62-
directory = trimmed;
63+
string trimmed = directory.TrimEnd(' ', '.');
64+
65+
// Only apply the trim if it results in a non-empty string
66+
if (trimmed.Length > 0)
67+
{
68+
directory = trimmed;
69+
}
6370
}
6471

6572
// We always allowed breaking the passed ref directory and filter to be separated

0 commit comments

Comments
 (0)