Skip to content

Inaccurate/Unhelpful MockFileSystem UNC path error message on Unix-like systems #1350

Closed

Description

@HarrisonTCodes

Issue

When creating a MockFileSystem with initial files using UNC paths (Windows format) on Unix-like systems, the error handling can be contradictory to the code and inaccurate, as the error message is fixed/hardcoded to Windows paths. For example, when running the below on a Unix-like system

var filesystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
  {@"\\server\share\file", "content"}
});

The error reads System.ArgumentException: The UNC path should be of the form \\server\share. (Parameter 'path'), despite the path being in that exact form, and actually requiring the form //server/share on Unix-like systems.

Cause

It seems there is handling of UNC paths with forward-slash separation following this standard in the MockFileSystem class, from this line:

var isUnc =
            StringOperations.StartsWith(fixedPath, @"\\") ||
            StringOperations.StartsWith(fixedPath, @"//");

The next line uses the platform-specific separator:

lastIndex = StringOperations.IndexOf(fixedPath, separator, 2)

Which will then lead to errors if trying to use backslash-separated UNC paths on Unix-like systems, or presumably forwardslash-separated UNC paths on Windows systems (untested). The error message however, as above, is hardcoded/fixed to the Windows paths:

// In MockFileSystem.cs
if (lastIndex < 0)
{
            throw CommonExceptions.InvalidUncPath(nameof(path));
}

// In CommonExceptions.cs
public static ArgumentException InvalidUncPath(string paramName) =>
        new ArgumentException(@"The UNC path should be of the form \\server\share.", paramName);

Activity

  1. added
    type: bugIssues that describe misbehaving functionality
    flag: good-first-issueIssues that are good for first time contributors
    on Oct 9, 2025
  2. vbreuss commented on Oct 9, 2025

    @vbreuss
    Member

    Thanks for reporting. Should I assign this issue to you, @HarrisonTCodes?

  3. HarrisonTCodes commented on Oct 9, 2025

    @HarrisonTCodes
    ContributorAuthor

    More than happy to take a look at implementing a solution/fix to this, please go ahead :)

    Wondering what the best approach may be. Two potential solutions initially come to mind:

    1. The error message is still fixed, but expand it to mention both platforms, such as: The UNC path should be of the form \\server\share on Windows systems and //server/share on Unix-like systems

    2. The error message is dynamic and will be platform-specific, producing either the current message on Windows systems, or a similar one with the corrected path on Unix-like systems

    Would be interested on your take here, or if there is another preferred way to go about this I may be overlooking

  4. vbreuss commented on Oct 9, 2025

    @vbreuss
    Member

    Wondering what the best approach may be. Two potential solutions initially come to mind:

    1. The error message is still fixed, but expand it to mention both platforms, such as: The UNC path should be of the form \\server\share on Windows systems and //server/share on Unix-like systems
    2. The error message is dynamic and will be platform-specific, producing either the current message on Windows systems, or a similar one with the corrected path on Unix-like systems

    Would be interested on your take here, or if there is another preferred way to go about this I may be overlooking

    I would go with your second suggestion. System.IO.Abstractions will always behave as the host operating system, so just having the correct message for the current operating system seems sufficient.

  5. HarrisonTCodes commented on Oct 9, 2025

    @HarrisonTCodes
    ContributorAuthor

    Great, thanks - I'll raise a PR with a fix!

  6. github-actions commented on Nov 22, 2025

    @github-actions

    This is addressed in release v22.1.0.

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

Metadata

Metadata

Labels

flag: good-first-issueIssues that are good for first time contributorsstate: releasedIssues that are releasedtype: bugIssues that describe misbehaving functionality

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions