dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.16k stars 4.71k forks source link

InMemoryDirectoryInfo does not work with absolute paths on Windows #88596

Open keyboardDrummer opened 1 year ago

keyboardDrummer commented 1 year ago

Description

var attempt1 = new InMemoryDirectoryInfo(@"", new[] { @"C:\my\happy\path" }); // crashes because the root is empty
var attempt2 = new InMemoryDirectoryInfo(@"C:\", new[] { @"C:\my\happy\path" });
var l = attempt2.EnumerateFileSystemInfos().ToList(); // returns "my\" instead of "C:\", so any matcher that starts with C:\ cannot be used with this
var matcher = new Matcher();
matcher.AddInclude(@"C:\my\happy\path");
Debug.Assert(matcher.Execute(attempt2).HasMatches) // fails

Reproduction Steps

See description

Expected behavior

I expect there to be a way to use absolute paths on Windows in combination with Matcher and InMemoryDirectoryInfo

Actual behavior

See description

Regression?

No response

Known Workarounds

I have to use paths relative to the root of the filesystem

var attempt3 = new InMemoryDirectoryInfo(@"C:\", new[] { @"my\happy\path" });
var matcher = new Matcher();
matcher.AddInclude(@"my\happy\path");
Debug.Assert(matcher.Execute(attempt3).HasMatches) // passes

Configuration

No response

Other information

I expect that allowing InMemoryDirectoryInfo to take an empty root is required to resolve this issue.

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

Issue Details
### Description ```c# var attempt1 = new InMemoryDirectoryInfo(@"", new[] { @"C:\my\happy\path" }); // crashes because the root is empty ``` ``` var attempt2 = new InMemoryDirectoryInfo(@"C:\", new[] { @"C:\my\happy\path" }); var l = attempt2.EnumerateFileSystemInfos().ToList(); // returns "my\" instead of "C:\", so any matcher that starts with C:\ cannot be used with this var matcher = new Matcher(); matcher.AddInclude(@"C:\my\happy\path"); Debug.Assert(matcher.Execute(attempt2).HasMatches) // fails ``` ### Reproduction Steps See description ### Expected behavior I expect there to be a way to use absolute paths on Windows in combination with `Matcher` and `InMemoryDirectoryInfo` ### Actual behavior See description ### Regression? _No response_ ### Known Workarounds I have to use paths relative to the root of the filesystem ```c# var attempt2 = new InMemoryDirectoryInfo(@"C:\", new[] { @"my\happy\path" }); var matcher = new Matcher(); matcher.AddInclude(@"my\happy\path"); Debug.Assert(matcher.Execute(attempt2).HasMatches) // passes ``` ### Configuration _No response_ ### Other information _No response_
Author: keyboardDrummer
Assignees: -
Labels: `area-System.IO`
Milestone: -
ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-extensions-filesystem See info in area-owners.md if you want to be subscribed.

Issue Details
### Description ```c# var attempt1 = new InMemoryDirectoryInfo(@"", new[] { @"C:\my\happy\path" }); // crashes because the root is empty ``` ```c# var attempt2 = new InMemoryDirectoryInfo(@"C:\", new[] { @"C:\my\happy\path" }); var l = attempt2.EnumerateFileSystemInfos().ToList(); // returns "my\" instead of "C:\", so any matcher that starts with C:\ cannot be used with this var matcher = new Matcher(); matcher.AddInclude(@"C:\my\happy\path"); Debug.Assert(matcher.Execute(attempt2).HasMatches) // fails ``` ### Reproduction Steps See description ### Expected behavior I expect there to be a way to use absolute paths on Windows in combination with `Matcher` and `InMemoryDirectoryInfo` ### Actual behavior See description ### Regression? _No response_ ### Known Workarounds I have to use paths relative to the root of the filesystem ```c# var attempt3 = new InMemoryDirectoryInfo(@"C:\", new[] { @"my\happy\path" }); var matcher = new Matcher(); matcher.AddInclude(@"my\happy\path"); Debug.Assert(matcher.Execute(attempt3).HasMatches) // passes ``` ### Configuration _No response_ ### Other information I expect that allowing `InMemoryDirectoryInfo` to take an empty root is required to resolve this issue.
Author: keyboardDrummer
Assignees: -
Labels: `untriaged`, `area-Extensions-FileSystem`
Milestone: -
jozkee commented 1 year ago

This looks like a duplicate of https://github.com/dotnet/runtime/issues/50648.

keyboardDrummer commented 1 year ago

This looks like a duplicate of #50648.

I don't think it is. Another way to phrase this issue is that InMemoryDirectoryInfo does not support having multiple drives in it. My problem isn't that, but that it is unable to include the drive letter in the paths it returns, and my matchers do include the drive letter.

My perspective is that it would be simplest to consider the drive letter part of a Windows path as another directory, but InMemoryDirectoryInfo doesn't allow that.