moby / moby/patternmatcher

Incorrect suffixMatch optimization of `**`

Open
#12 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
22
Forks
12
PR merge metrics
No merged PRs in 30d

Description

https://github.com/moby/patternmatcher/commit/2d3e8372017c5e8113b281ac3c2d6058101742ed introduced matchType to speed up matching; that changed semantics of **.

https://docs.docker.com/build/concepts/context/#matching says ** … matches any number of directories (including zero)”. (https://git-scm.com/docs/gitignore is even more restrictive).

But consider these added test cases:

diff --git a/patternmatcher_test.go b/patternmatcher_test.go
index 5f02521..81d73fd 100644
--- a/patternmatcher_test.go
+++ b/patternmatcher_test.go
@@ -184,6 +184,14 @@ func TestMatches(t *testing.T) {
                {"a.|)$(}+{bc", "a.|)$(}+{bc", true},
                {"dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", "dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", true},
                {"dist/*.whl", "dist/proxy.py-2.4.0rc3.dev36+g08acad9-py3-none-any.whl", true},
+               {"**.md", ".md", true},
+               {"**.md", "name.md", true},
+               {"**.md", "dir/.md", true},
+               {"**.md", "dir/name.md", true},
+               {"**.m[dD]", ".md", true},
+               {"**.m[dD]", "name.md", false},
+               {"**.m[dD]", "dir/.md", true},
+               {"**.m[dD]", "dir/name.md", false},
        }
        multiPatternTests := []multiPatternTestCase{
                {[]string{"**", "!util/docker/web"}, "util/docker/web/foo", false},

The suffixMatch code path is a simple suffix, so name.md and dir/name.md match; the regexpMatch code path uses (.*/)?, so name.md and dir/name.md don’t match.


(On the other side of **, matching is also not interpreting the value as “any number of directories”, d** matches dir/name.md, but at least it is consistent between prefixMatch and regexpMatch.)

Contributor guide

No contributing guide indexed for this repository

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.

Research direction

Start with patternmatcher_test.go and the added cases for **.md and **.m[dD], then trace the matchType, suffixMatch, and regexpMatch paths. Run the Go tests to reproduce the differing results. Done means ** matches any number of directories, including zero, consistently across these cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.