Skip to content

path/filepath: filepath.Match returns different output depending on OS #59188

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
simar7 opened this issue Mar 23, 2023 · 2 comments
Closed

path/filepath: filepath.Match returns different output depending on OS #59188

simar7 opened this issue Mar 23, 2023 · 2 comments

Comments

@simar7
Copy link

simar7 commented Mar 23, 2023

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

Windows

$ go version
go version go1.20.2 windows/amd64

macOS

$ go version
go version go1.20.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

Windows:

go env Output
go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\simar\AppData\Local\go-build
set GOENV=C:\Users\simar\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\simar\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\simar\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=\\vmware-host\Shared Folders\simar On My Mac\repos\filepath-match-test\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\simar\AppData\Local\Temp\go-build550900517=/tmp/go-build -gno-record-gcc-switches

macOS:

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/simar/Library/Caches/go-build"
GOENV="/Users/simar/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/simar/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/simar/go"
GOPRIVATE=""
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.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/simar/repos/filepath-match-test/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/46/5653r61560j_p5gx3b46l01w0000gn/T/go-build3059257642=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here's a small test program.

package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	match, err := filepath.Match(`/etc/*`, "/etc/foo/bar")
	fmt.Println(match, err)
}

What did you expect to see?

I expect to see the following output on both macOS/Linux and Windows:

false <nil>

What did you see instead?

On Linux/macOS

false <nil>

On Windows

true <nil>

Additional info

Apologies if this is related to user error. I have read the filepath.Match documentation here which does briefly talk about Windows and escaping as:

On Windows, escaping is disabled. Instead, '\\' is treated as path separator.

But to me it's not entirely clear what this implies and how it is different than any other OS.

Signed-off-by: Simar simar@linux.com

@mvdan
Copy link
Member

mvdan commented Mar 23, 2023

You want https://pkg.go.dev/path#Match to always use forward slashes. The filepath docs are very clear at the top:

The filepath package uses either forward slashes or backslashes, depending on the operating system. To process paths such as URLs that always use forward slashes regardless of the operating system, see the path package.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2023
@simar7
Copy link
Author

simar7 commented Mar 23, 2023

You want https://pkg.go.dev/path#Match to always use forward slashes. The filepath docs are very clear at the top:

The filepath package uses either forward slashes or backslashes, depending on the operating system. To process paths such as URLs that always use forward slashes regardless of the operating system, see the path package.

Sorry I missed that piece at the very top. Thanks again.

@golang golang locked and limited conversation to collaborators Mar 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants