FileSystemGlobbing: Matcher throws ArgumentOutOfRangeException when the rootDir is the same value as the file

Open Beginner friendly
#136,989 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

Start with src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs, especially the EnumerateFileSystemInfos code linked in the issue. Reproduce the failure with the provided file based app where rootDir and file are the same path, then inspect the related FileSystemGlobbing tests. Done means Match() returns HasMatches == false without throwing for this case.

Written by the indexing model from the issue text.

Description

area-Extensions-FileSystem untriaged
Description

See title and file-based app repro below.

Reproduction Steps
#:package [email protected]

using Microsoft.Extensions.FileSystemGlobbing;

var root = Path.GetPathRoot(Environment.CurrentDirectory)!;
var matcher = new Matcher();
matcher.AddInclude("MyFile.txt");

Console.WriteLine($"Match(rootDir: \"{root}\", file: \"{root}\")");
Console.WriteLine(matcher.Match(root, root).HasMatches);
Expected behavior

Match() returns a result with HasMatches == false and doesn't throw an exception.

I'm unsure whether it would/should be possible to setup a matcher which would match the path root.

Actual behavior

Match throws an exception:

Unhandled exception. System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'startIndex')
   at System.String.IndexOfAny(Char[] anyOf, Int32 startIndex, Int32 count)
   at Microsoft.Extensions.FileSystemGlobbing.InMemoryDirectoryInfo.EnumerateFileSystemInfos()+MoveNext()
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at Microsoft.Extensions.FileSystemGlobbing.Internal.MatcherContext.Match(DirectoryInfoBase directory, String parentRelativePath)
   at Microsoft.Extensions.FileSystemGlobbing.Internal.MatcherContext.Execute()
   at Microsoft.Extensions.FileSystemGlobbing.Matcher.Execute(DirectoryInfoBase directoryInfo)
   at Microsoft.Extensions.FileSystemGlobbing.MatcherExtensions.Match(Matcher matcher, String rootDir, IEnumerable`1 files)
   at Microsoft.Extensions.FileSystemGlobbing.MatcherExtensions.Match(Matcher matcher, String rootDir, String file)
   at Program.<Main>$(String[] args)
Regression?

No

Known Workarounds

Check that rootDir != file before calling Match().

Configuration

Using 11.0.100-rc.3.26522.148 SDK to build. Unsure which runtime that means I am running.
Library version given in the sample.
Running on Ubuntu 24.04, x64.
I doubt the problem is configuration-specific, I believe it also repros on Windows.

Other information

Probably here: https://github.com/dotnet/runtime/blob/8f030f80c0dd2722eb2f618984e9db6784765963/src/libraries/Microsoft.Extensions.FileSystemGlobbing/src/InMemoryDirectoryInfo.cs#L94

Dominant language
C#
Stars
18.4k
Forks
5.8k
Avg merge
2d 15h
Merged PRs (30d)
596

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.