golang/go

go mod vendor should add go generate dependencies

coolaj86 opened this issue · 3 comments

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

$ go version
go version go1.11 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
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/aj/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/aj/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/aj/github.com/xxx/xxx/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0g/01vpdjf507z8qk3lvgfnh1hw0000gn/T/go-build639981897=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I have a module which is not a code dependency, but rather a go generate dependency:

//go:generate go run github.com/UnnoTed/fileb0x b0x.toml

I'm trying to add it to my vendor directory and have it stay there.

What did you expect to see?

I'd expect to be able to run go mod vendor and have the generate dependencies copied over as well.

What did you see instead?

Even though I've manually added the go generate dependency into go.mod it's not it's not being copied to vendor when I run go mod vendor. In fact, it's sometimes pruned from vendor when I run that.

mvdan commented

See #25922 and https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module. go mod generally removes (prunes) unused dependencies, so right now you need to keem them as imports somewhere.

@mvdan Thanks for the quick reply. It's a binary, not a library, so it's not possible even to use _ to import and ignore. Any other ideas?

For the benefit of others:

tools/tools.go:

// +build tools

// tools is a dummy package that will be ignored for builds, but included for dependencies
package tools

import (
  _ "example.com/foo/thing"
)
go mod tidy

This will cause the utilities to be included in go.mod and with go mod vendor (after a go mod tidy or other command that causes deps to re-evaluate), but they will not be checked (i.e. a binary can be included as a dependency, which is not allowed for a build).

For more details see https://stackoverflow.com/a/54028731/151312

Update: Using +build tools rather than +build ignore, which has a distinct meaning (and is ignored by go mod tidy).

Also, you should append -mod vendor to all go commands:

go generate -mod vendor ./...
//go:generate go run -mod vendor example.com/thing
go build -mod vendor