Skip to content

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

@gazerro

Description

@gazerro

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions