Skip to content

Globstar doesn't match zero directories #88

@mmxmb

Description

@mmxmb

Issue description

I expect a/**/ to match a directory a but doublestar.Match returns false:

import (
	"fmt"

	"github.com/bmatcuk/doublestar/v4"
)

func main() {
	match1, err := doublestar.Match("a/**/", "a")
	if err != nil {
		panic(err)
	}
	match2, err := doublestar.Match("a/**/", "a/")
	if err != nil {
		panic(err)
	}
	fmt.Printf("a/**/ matches a: %t\n", match1)
	fmt.Printf("a/**/ matches a/: %t\n", match2)
}

Running this returns:

› go run main.go
a/**/ matches a: false
a/**/ matches a/: false

I understand that it's ambiguous whether a by itself is a regular file or a directory, therefore I am also trying to match a/.

I assume that this project is attempting to replicate Bash globstar functionality, so here's my attempt at doing the same in Bash 5.2:

bash-5.2$ bash --version
GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin23.0.0)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
bash-5.2$ mkdir a
bash-5.2$ shopt -s globstar
bash-5.2$ ls -d a/**/
a/

Bash shopt docs confirm that this is the intended behavior:

globstar
If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.

In this example, I am trying to match zero directories, or subdirectories.

Question

Can you please confirm whether this is an oversight in doublestar.Match implementation and check whether it's possible to correct this behavior?

Versions

I am using go1.21.3 and bmatcuk/doublestar/v4 v4.6.0.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions