Skip to content

go/build: file name build constraints incorrect when name contains dots #46662

@knusbaum

Description

@knusbaum

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

$ go version
go version go1.16.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/kyle.nusbaum/Library/Caches/go-build"
GOENV="/Users/kyle.nusbaum/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/kyle.nusbaum/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/kyle.nusbaum/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/kyle.nusbaum/sdk/go1.16.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/kyle.nusbaum/sdk/go1.16.4/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/kyle.nusbaum/Documents/CodeBase/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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8x/bn6z9tk109n_hvbhl8fwdmpw0000gn/T/go-build558940160=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I created a .go file which contained dots in the name along with a _GOOS extension (e.g. file.name_darwin.go)

What did you expect to see?

I expected this file to be compiled only when GOOS=darwin

Please see this example, which I expect to compile but does not:
https://github.com/knusbaum/goissue/

What did you see instead?

The error that appears:

% go build
# github.com/knusbaum/goissue
./my.name.result_linux.go:3:6: Result redeclared in this block
	previous declaration at ./my.name.result_darwin.go:3:15

Additional information

The build constraint documentation says this:

 If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

*_GOOS
*_GOARCH
*_GOOS_GOARCH

Wherever a file's "extension" is mentioned in the Go documentation, it means whatever comes after the final . in the file name. This is also what path.Ext documents and computes.

However, the os/arch-matching code takes the extension from the first instance of . rather than the last, as the documentation would lead me to expect.

This places requirements for using the os/arch constraints in file names that are not documented anywhere and are inconsistent with the way Go code and documentation usually deals with file extensions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions