golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.68k stars 17.49k forks source link

path/filepath: Match does not use Glob patterns #41394

Closed vatine closed 3 years ago

vatine commented 3 years ago

What version of Go are you using (go version)?

$ go version
go version go1.14 linux/amd64

Does this issue reproduce with the latest release?

Based on docuemntation and the Go playground, yes. https://play.golang.org/p/WHq5MYYn_yf

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ingvar/.cache/go-build"
GOENV="/home/ingvar/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ingvar/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.14"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.14/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ingvar/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build667987227=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I passed the pattern "ga*[!k]" to filepath.Match expecting it to not match the string "gazonk" (as it clearly states it must match the entire pattern, and the glob-like syntax for a negated character class is [!...]). It turns out that filepath.Match actually uses regexp-like syntx for character classes.

https://play.golang.org/p/WHq5MYYn_yf

What did you expect to see?

I expected a function that says "shell file name pattern" to understand glob patterns. Especially since filepath.Glob says "uses the same pattern as Match".

What did you see instead?

I saw a function that partially uses glob patterns and partially (well, specifically for character classes) uses regexp patterns.

toothrot commented 3 years ago

I believe the negation is documented as ^: https://play.golang.org/p/9zwuCeWNtXS

filepath.Glob does call Match: https://github.com/golang/go/blob/go1.15.2/src/path/filepath/match.go#L328

I'm not sure if these patterns are changeable without breaking any existing Go programs. See also the long discussion at #11862.

/cc @robpike @rsc

ianlancetaylor commented 3 years ago

filepath.Match documents the exact syntax that it supports: https://golang.org/pkg/path/filepath/#Match .

I agree that in the POSIX shell definition, ! negates a character class and ^ does not. I guess we got that wrong. (In the bash shell, both ! and ^ negate a character class).

Unfortunately, we can't change it now. Sorry.

vatine commented 3 years ago

Would it be worth keeping "allow both ! and ^ for character class negation" in mind for Go2?

ianlancetaylor commented 3 years ago

Well, path/filepath/v2, but, sure.