golang/go

cmd/go: build constraint of 'ignore' is special for modules?

myitcv opened this issue · 3 comments

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

> go version
[stdout]
go version go1.12beta1 linux/amd64

Does this issue reproduce with the latest release?

Yes; testing with the beta ahead of the 1.12 release.

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

go env Output
> go env
[stdout]
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="$WORK/gopath"
GOPROXY="http://127.0.0.1:40043/mod"
GORACE=""
GOROOT="/home/myitcv/gos"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="$WORK/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=$WORK/tmp/go-build168896432=/tmp/go-build -gno-record-gcc-switches"

What did you do?

# no build constraint
go list
go mod tidy
grep 'fruit.com v1.0.0' go.mod

# build constraint of other
cp mod.go.other mod.go
go mod tidy
grep 'fruit.com v1.0.0' go.mod

# build constraint of ignore
cp mod.go.ignore mod.go
go mod tidy
grep 'fruit.com v1.0.0' go.mod


-- go.mod --
module mod

-- mod.go --
package mod

import _ "fruit.com/fruit"

-- mod.go.ignore --
// +build ignore

package mod

import _ "fruit.com/fruit"

-- mod.go.other --
// +build other

package mod

import _ "fruit.com/fruit"

using the following module definition:

# mod/fruit.com_v1.0.0.txt 
-- .mod --
module fruit.com

-- .info --
{"Version":"v1.0.0","Time":"2018-10-22T18:45:39Z"}

-- go.mod --
module fruit.com

-- fruit/fruit.go --
package fruit

What did you expect to see?

A passing test.

What did you see instead?

--- FAIL: TestScripts (0.04s)
    --- FAIL: TestScripts/script (0.23s)
        testscript.go:193:
            # no build constraint (0.107s)
            # build constraint of other (0.021s)
            # build constraint of ignore (0.032s)
            > cp mod.go.ignore mod.go
            > go mod tidy
            > grep 'fruit.com v1.0.0' go.mod
            [go.mod]
            module mod

            go 1.12

            FAIL: testdata/script.txt:17: no match for `fruit.com v1.0.0` found in go.mod

I wasn't aware that the ignore build constraint was special but judging from the above it appears to be.

ignore is described as being conventional, but not that it carries any special semantics.

The cmd/go docs also don't appear to make mention of this special case.

Assuming this is the intended behaviour, I think it would be worth updating the docs to call this out. I just spent a good few mins trying to track down a go mod tidy bug that in fact turned out to be an instance of a file with a // +build ignore constraint.

cc @bcmills

FYI @mvdan @rogpeppe

Change https://golang.org/cl/167159 mentions this issue: all: add go.mod

https://golang.org/ref/mod#go-mod-tidy now says this:

There is one exception: the ignore build tag is not enabled, so a file with the build constraint // +build ignore will not be considered.

Since this is now documented in https://golang.org/ref/mod#go-mod-tidy, let's close this.

We'll need to be careful to document this for other commands that load the full import graph the way that go mod tidy does. For now, that's only https://golang.org/ref/mod#go-mod-vendor, which does mention this.