Skip to content

PathVerifier fails on whitespace filename under Linux and .NET Core #1070

Todo

Description

@AnakinRaW

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 windows
var mfs = new MockFileSystem();
mfs.FileInfo.New("   "); // ASCII space
mfs.FileInfo.New("\u00A0"); // Unicode char that's treated as whitespace

// Both pass in Linux
mfs.FileInfo.New("./   ") // Explicit relative path 
mfs.FileInfo.New("/   ") // Explicit absolute path 

// Passes in Linux 
var fs = new FileSystem();
fs.FileInfo.New("   "); // ASCII space

// Passes in Linux and Windows (.NET Core); throws on NET4.8
fs.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.

Activity

  1. added
    state: needs discussion
    type: bug
    on Dec 25, 2023
  2. added
    state: ready to pick
    area: testinghelpers
    and removed
    state: needs discussion
    on Jan 9, 2024
  3. fgreinacher commented on Jan 9, 2024

    @fgreinacher
    Contributor

    Thanks for reporting! Happy to accept a PR to fix this 🙇

  4. bearpro commented on Jul 13, 2025

    @bearpro

    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.

    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:

    > touch "  k   "
    > touch "    .txt   "
    > touch "    .txt"
    > touch "k   "
    > touch "  "

    But actually none of them can be deleted via windows exporer.

  5. vbreuss commented on Jul 19, 2025

    @vbreuss
    Member

    No, the check already existed previously (see here and was only moved in this commit.

    Originally this code was introduced with #cdbf565:

    MockFile.Move handles now invalid filenames (blank, empty, null) with correct exceptions.

    Maybe this check should only run under Windows?

    I would be open for a pull request, @bearpro...

  6. mohsinaliwork686-rgb commented on Nov 23, 2026

    @mohsinaliwork686-rgb
    Contributor

    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?

  7. vbreuss commented on Nov 24, 2026

    @vbreuss
    Member

    I assigned the issue to you, @mohsinaliwork686-rgb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: testinghelpersstate: ready to picktype: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions