You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PathVerifier fails on whitespace filename under Linux and .NET Core #1070
Describe the bug
When using FileSystem and MockFileSystem I noticed there is different behavior when creating a new IFileInfo using only whitespaces.
The "error" trails down to the PathVerifier that the MockFileSystem uses
To Reproduce
// Both throw in Linux and windowsvarmfs=newMockFileSystem();mfs.FileInfo.New(" ");// ASCII spacemfs.FileInfo.New("\u00A0");// Unicode char that's treated as whitespace// Both pass in Linuxmfs.FileInfo.New("./ ")// Explicit relative path
mfs.FileInfo.New("/ ")// Explicit absolute path // Passes in Linux varfs=newFileSystem();fs.FileInfo.New(" ");// ASCII space// Passes in Linux and Windows (.NET Core); throws on NET4.8fs.FileInfo.New("\u00A0");// Unicode char that's treated as whitespace
Expected behavior
Both calls throw an exception when they should not.
For Linux (whitespace) is a valid file name
For Windows and Linux \u00A0 is a valid file name. However under .NET Framework it also fails
Additional context
In PathInternal.OS.cs .NET has a bool IsEffectivelyEmpty(string? path) method which behaves differently to linux and windows, just to cover those cases.
Currently PathVerifier uses the string.Trim() method, which causes the two explained scenarios
valid unicode chars get trimmed (\u00A0)
a whitespace-only relative path is considered to be illegal when it actually is not under linux.
Maybe just remove .Trim() == string.Empty check? I do not see anything similar in dotnet/runtime. Unix seems to interpret spaces literlly as provided. Windows with NTFS seems to trim ending spaces.
Hi, this still happens on main (7f444c9). On Linux (mcr.microsoft.com/dotnet/sdk:10.0), the real FileInfo.New(" "), File.WriteAllText(" ", ...) and Path.GetFullPath(" ") all work, while MockFileSystem throws ArgumentException: The path is not of a legal form. (Parameter 'path') for each of them, and the same for other whitespace-only names. vbreuss suggested in July 2025 running this check only on Windows, and as far as I can see no PR came out of that, so I'd be glad to pick it up. My plan is one helper in PathVerifier, like .NET's IsEffectivelyEmpty: an empty path is always illegal, a whitespace-only one only on Windows, where today's behaviour stays as it is. It would replace the same check in PathVerifier.IsLegalAbsoluteOrRelative, MockPath.GetFullPath and GetRelativePath, MockFile.Exists and the MockDirectoryInfo constructor, because FileInfo.New also goes through GetFullPath. About 30 existing tests expect the Windows rule on every platform, so they'd get [WindowsOnly(WindowsSpecifics.StrictPathRules)], and I'd add a Unix-only test in MockFileInfoTests that creates, writes and finds a " " file, plus a Windows test that it still throws. Would a PR along those lines be welcome?
Activity
fix: handling of whitespace-only filenames (#439)
fgreinacher commentedon Jan 9, 2024
Thanks for reporting! Happy to accept a PR to fix this 🙇
bearpro commentedon Jul 13, 2025
Maybe just remove
.Trim() == string.Emptycheck? I do not see anything similar in dotnet/runtime. Unix seems to interpret spaces literlly as provided. Windows with NTFS seems to trim ending spaces.The line with
.Trim()was added in bce1401#diff-de56bbf9f4705d511bb1e432ef634a4656b9dd4cd46dff4338c085cd6ceca72cR27, and seem like just an assumption.UPD:
All follodwing commands create valid file on windows:
But actually none of them can be deleted via windows exporer.
vbreuss commentedon Jul 19, 2025
No, the check already existed previously (see here and was only moved in this commit.
Originally this code was introduced with #cdbf565:
Maybe this check should only run under Windows?
I would be open for a pull request, @bearpro...
mohsinaliwork686-rgb commentedon Nov 23, 2026
Hi, this still happens on
main(7f444c9). On Linux (mcr.microsoft.com/dotnet/sdk:10.0), the realFileInfo.New(" "),File.WriteAllText(" ", ...)andPath.GetFullPath(" ")all work, whileMockFileSystemthrowsArgumentException: The path is not of a legal form. (Parameter 'path')for each of them, and the same for other whitespace-only names. vbreuss suggested in July 2025 running this check only on Windows, and as far as I can see no PR came out of that, so I'd be glad to pick it up. My plan is one helper inPathVerifier, like .NET'sIsEffectivelyEmpty: an empty path is always illegal, a whitespace-only one only on Windows, where today's behaviour stays as it is. It would replace the same check inPathVerifier.IsLegalAbsoluteOrRelative,MockPath.GetFullPathandGetRelativePath,MockFile.Existsand theMockDirectoryInfoconstructor, becauseFileInfo.Newalso goes throughGetFullPath. About 30 existing tests expect the Windows rule on every platform, so they'd get[WindowsOnly(WindowsSpecifics.StrictPathRules)], and I'd add a Unix-only test inMockFileInfoTeststhat creates, writes and finds a" "file, plus a Windows test that it still throws. Would a PR along those lines be welcome?vbreuss commentedon Nov 24, 2026
I assigned the issue to you, @mohsinaliwork686-rgb