Skip to content

Glob class with a trailing dash like file[a-] is treated as unparseable: as a filter it shows every item, as an exclusion it hides nothing #130

Description

@matt-edmondson

What's wrong

file[a-] is a complete, valid glob. In glob syntax a - right before ] is a literal dash, so the class matches a or -. DotNet.Glob's tokeniser still throws IndexOutOfRangeException on it. ResolveGlob (TextFilter.cs:417-443) catches that and caches the token as unparseable (null). By the rule documented at lines 305-309, an unparseable token matches everything as a plain or required token and excludes nothing as an excluded token.

var items = new[] { "filea", "file-", "fileZ", "report" };
TextFilter.Filter(items, "file[a-]");   // -> filea, file-, fileZ, report   (expected: filea, file-)
TextFilter.Filter(items, "-file[a-]");  // -> filea, file-, fileZ, report   (expected: fileZ, report)

DotNet.Globbing.Glob.Parse("file[a-]");                // throws IndexOutOfRangeException
DotNet.Globbing.Glob.Parse("file[-a]").IsMatch("file-"); // true: the leading-dash form works

The results above were observed by running against HEAD 769c9b1.

Why it matters

The "ignore unparseable tokens" fallback from #106/#110 was meant for half-typed ranges such as file[0-. It also swallows this finished, legal pattern, and nothing tells the user. The results are the opposite of what was asked for:

  • The include filter shows report, which doesn't even start with file.
  • The exclusion removes nothing.

Suggested fix

In ResolveGlob, before calling Glob.Parse, rewrite each character class so that a - immediately before the closing ] moves to the front of the class, after any !:

  • [a-] becomes [-a]
  • [!a-] becomes [!-a]

DotNet.Glob parses that form correctly. Keep the existing catch for genuinely half-typed input such as file[0-.

Acceptance criteria: tests show that file[a-] includes filea and file- only, -file[a-] excludes exactly those, and file[0- still behaves as it does today.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions