Skip to content

Different output in Unix between Path.GetPathRoot overloads for redundant segments #35260

@carlossanlop

Description

@carlossanlop

While working on this issue (PR), I found a bug in the string overload for Path.GetPathRoot (Unix):

using System;
using System.IO;
namespace ConsoleAppCore
{
    class Program
    {
        static void Main()
        {
            string original = "/home/../folder/../..";
            string expected = "/";
            ReadOnlySpan<char> actualSpan = Path.GetPathRoot(original.AsSpan());
            string actualString = Path.GetPathRoot(original);
            Console.WriteLine($"Expected:      '{expected}'");
            Console.WriteLine($"Actual string: '{actualString}'");          // This returns '/'
            Console.WriteLine($"Actual span:   '{actualSpan.ToString()}'"); // This returns ''
        }
    }
}

I also found that running the same code in Windows will yield the behavior below, which is why I originally opened this issue, but @jkotas kindly pointed out it's expected behavior - the string method overload normalizes, while the span overload does not:

            Console.WriteLine($"Actual span:   '{actualSpan.ToString()}'"); // This returns '/'
            Console.WriteLine($"Actual string: '{actualString}'");          // This returns '\'

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions