Skip to content

pattern: wrong results for /**foo #1149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
paulhammond opened this issue Apr 7, 2025 · 3 comments
Closed

pattern: wrong results for /**foo #1149

paulhammond opened this issue Apr 7, 2025 · 3 comments

Comments

@paulhammond
Copy link
Contributor

While working on #1147 I think I noticed that the regexp generated for /**foo is incorrect.

https://go.dev/play/p/XXoRQ4EdBwV is some go code that matches /**foo against a number of paths; as you can see all three match.

Here's a bash shell in a container testing the same three paths:

$ echo $BASH_VERSION
5.2.15(1)-release
$ touch /foo
$ mkdir -p /a/b/c
$ touch /a/b/cfoo
$ touch /a/b/c/foo
$ ls /**foo
/foo

Bash only matches /foo, the pattern package matches all three paths. Is this a bug or am I missing something?

@mvdan
Copy link
Owner

mvdan commented Apr 8, 2025

We aim to emulate Bash behavior as closely as possible within reason, so this is likely a bug. The recursive globbing logic is surprisingly difficult to get exactly right. Happy to review a patch, otherwise I'll look at this sometime this week.

mvdan added a commit that referenced this issue Apr 13, 2025
Bash documents globstar as follows:

    When the globstar shell option is enabled, and * is used in a
    pathname expansion context, two adjacent *s used as a single pattern
    will match all files and zero or more directories and subdirectories

That is, `/**/foo` or `bar/**` both result in globstar globbing
where we can match files and nested directories,
but `/**foo` or `bar**/` do not place the two star characters
in a single pattern, so they behave like a single glob star instead.

The interp and expand packages already deals with this correctly,
because they first split the glob pattern into path elements,
and they deal with one element being exactly "**" in a special way.

The pattern package still aims to support "**" by itself,
but it does not handle this edge case properly;
it treats any occurrence of "**" as a globstar wildcard no matter
whether or not it's on its own as a path element.

Fix coming in the next commit.

For #1149.
@mvdan mvdan closed this as completed in 8a36c1e Apr 13, 2025
@mvdan
Copy link
Owner

mvdan commented Apr 13, 2025

This should be fixed now. See the commit adding test cases; this wasn't a bug in the interp or expand packages. It was only a bug when using the pattern package directly.

@paulhammond
Copy link
Contributor Author

Thanks for working on this so quickly! This looks right to me...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants