Skip to content

[glob] Fix StateError on patterns with an empty option branch - #2579

Open
hkarmoush wants to merge 1 commit into
dart-lang:mainfrom
hkarmoush:glob-fix-empty-option-canmatchabsolute-2004
Open

hkarmoush wants to merge 1 commit into
dart-lang:mainfrom
hkarmoush:glob-fix-empty-option-canmatchabsolute-2004

Conversation

@hkarmoush

Copy link
Copy Markdown
Contributor

Summary

  • SequenceNode.canMatchAbsolute/canMatchRelative in pkgs/glob/lib/src/ast.dart called nodes.first without checking for an empty node list, which throws Bad state: No element for any pattern with an empty option branch (e.g. {**/,}foo.dart).
  • Guard both getters against an empty nodes list, falling back to the base-class defaults (canMatchAbsolute = false, canMatchRelative = true), which correctly describe an empty sequence: it can only match an empty (relative) segment, never an absolute path.

Test plan

  • Added a regression test in pkgs/glob/test/match_test.dart reproducing the reported pattern ({**/,}foo.dart) and confirming it matches instead of throwing.
  • dart test — all 145 tests pass
  • dart analyze — no issues
  • dart format --output=none --set-exit-if-changed . — clean

Fixes #2004

@hkarmoush
hkarmoush requested a review from a team as a code owner September 1, 2026 10:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a StateError ("Bad state: No element") that occurred when calling matches() or matchAsPrefix() on glob patterns containing an empty option branch, such as {**/,}foo.dart. The fix ensures that SequenceNode.canMatchAbsolute and SequenceNode.canMatchRelative in pkgs/glob/lib/src/ast.dart safely handle empty node sequences. Additionally, a unit test has been added to verify this behavior, and the changelog has been updated. There are no review comments, so I have no feedback to provide.

@hkarmoush

Copy link
Copy Markdown
Contributor Author

Friendly ping on this one. It's a small fix for the StateError on patterns with an empty option branch, and CI is green. Happy to adjust anything if someone has a moment to take a look.

SequenceNode.canMatchAbsolute and canMatchRelative called nodes.first
without checking whether nodes was empty. An empty SequenceNode arises
from the empty branch of an options group, e.g. the pattern
'{**/,}foo.dart', whose second option has no nodes. Calling matches()
or matchAsPrefix() on such a pattern threw "Bad state: No element"
instead of matching normally.

Fixes dart-lang#2004
@hkarmoush
hkarmoush force-pushed the glob-fix-empty-option-canmatchabsolute-2004 branch from f1c06ed to a0b580a Compare November 13, 2026 07:38

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Bad state: no element" for "{**/,}foo.dart"

1 participant