Skip to content
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

io/fs: fs.Glob does not match patterns with the backslash magic character #44171

Closed
gazerro opened this issue Feb 8, 2021 · 2 comments
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@gazerro
Copy link
Contributor

gazerro commented Feb 8, 2021

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

$ go version
go version go1.16rc1 darwin/amd64

Does this issue reproduce with the latest release?

No.

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/marco/Library/Caches/go-build"
GOENV="/Users/marco/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/marco/go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/marco/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16rc1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/z9/xrln_qks56jbzxjbhs04fpm80000gn/T/go-build899111098=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run the code:

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

source

package main

import (
	"io/fs"
	"os"
	"path/filepath"
	"runtime"
	"testing"
)

func TestGlobBackslash(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip(`Skip test on Windows.`)
	}

	dir := t.TempDir()
	err := os.WriteFile(filepath.Join(dir, `abc`), []byte{}, 0644)
	if err != nil {
		t.Fatal(err)
	}

	patters := []string{`abc`, `a\bc`, `a\b?`}

	fsys := os.DirFS(dir)

	for _, pattern := range patters {
		files, err := fs.Glob(fsys, pattern)
		if err != nil {
			t.Fatal(err)
		}
		if files == nil {
			t.Logf("%q no match", pattern)
		} else {
			t.Logf("%q match", pattern)
		}
	}
}

What did you expect to see?

"abc" match
"a\\bc" match
"a\\b?" match

What did you see instead?

"abc" match
"a\\bc" no match
"a\\b?" match

Resolution

The hasMeta function in file src/io/fs/glob.go is a rewrite of the hasMeta function in file src/path/filepath/match.go but with the condition on runtime.GOOS inverted.

This line

if c == '*' || c == '?' || c == '[' || runtime.GOOS == "windows" && c == '\\' {

should be written as

if c == '*' || c == '?' || c == '[' || runtime.GOOS != "windows" && c == '\\' {

Question

Even if this function is fixed, why the fs.Glob function depends on the operating system?

@dmitshur
Copy link
Contributor

dmitshur commented Feb 8, 2021

CC @rsc, @ianlancetaylor.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 8, 2021
@gopherbot
Copy link

Change https://golang.org/cl/290512 mentions this issue: io/fs: backslash is always a glob meta character

@dmitshur dmitshur added this to the Go1.16 milestone Feb 9, 2021
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Feb 9, 2021
@golang golang locked and limited conversation to collaborators Feb 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants