Skip to content

how to make "go fmt, go vet, go test" work from my project' vendor dicectory #29255

@orange-jacky

Description

@orange-jacky

Please answer these questions before submitting your issue. Thanks!

What did you do?

after adding "export GO111MODULE=on" to $HOME/.bash_profile
i create a project directory, and run
go mod init xxx
go build

i write a makefile to easy compile the project

the makefile content
#go env
GOARCH = amd64
export GO111MODULE=on
VET_REPORT = vet.report
TEST_REPORT = tests.xml

#current dir name
CURRENT_DIR=$(shell pwd)
BUILD_DIR=${CURRENT_DIR}
BINARY=$(shell basename ${CURRENT_DIR})

#git info
GITURL=$(shell git remote -v | grep '^origin\s.(fetch)$$' | awk '{print $$2}' | sed 's/.git//' | sed 's/http[s:/]//')
COMMIT=$(shell git rev-parse HEAD)
BRANCH=$(shell git rev-parse --abbrev-ref HEAD)

#VERSION
VERSION?=?

Setup the -ldflags option for go build here, interpolate the variable values

LDFLAGS = -ldflags "-X main.VERSION=${VERSION} -X main.COMMIT=${COMMIT} -X main.BRANCH=${BRANCH}"

prepare: clean test vet fmt

linux: prepare
cd ${BUILD_DIR};
GOOS=linux GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BINARY} . ;
@echo "<== compile linux done"

darwin: prepare
cd ${BUILD_DIR};
GOOS=darwin GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BINARY}-darwin-${GOARCH} . ;
@echo "<== compile darwin done"

windows: prepare
cd ${BUILD_DIR};
GOOS=windows GOARCH=${GOARCH} go build ${LDFLAGS} -o ${BINARY}-windows-${GOARCH}.exe . ;
@echo "<== compile windows done"

test:
if ! hash go2xunit 2>/dev/null; then go install github.com/tebeka/go2xunit; fi
cd ${BUILD_DIR};
go test -v ./... 2>&1 | go2xunit -output ${TEST_REPORT} ;
cd - >/dev/null
@echo "<== go test done"

vet:
cd ${BUILD_DIR};
go vet ./... > ${VET_REPORT} 2>&1 ;
@echo "<== go vet done"

fmt:
cd ${BUILD_DIR};
go fmt $$(go list ./... | grep -v /vendor/) ;
@echo "<== go fmt done"

clean:
@rm -f ${TEST_REPORT}
@rm -f ${VET_REPORT}
@rm -f ${BINARY}-* ${BINARY}
@echo "<== clean done"

when i can connect internet, make linux will success, and when i can't connect internet, make linux will fail.
after comparing the tips, i found that go build, go install ,go fmt, go vet, go test check from $GOPATH/pkg/mod.
when i use "go mod vendor" make all dependency to project's vendor directory
i set up env export GOFLAGS=-mod=vendor in termial
i found go build, go install fetch dependency from project's vendor directory, and go fmt, go vet, go test still fetch dependency from $GOPATH/pkg/mod

What did you expect to see?

before i set up env export GOFLAGS=-mod=vendor in termial, go build, go install , go vet , go fmt, go test fetch dependency from $GOPATH/pkg/mod

after i set up env export GOFLAGS=-mod=vendor in termial, go build, go install , go vet , go fmt, go test fetch dependency from project's vendor directory

Does this issue reproduce with the latest release (go1.11.2)?

System details

macbookpro:~ fredlee$ go version
go version go1.11 darwin/amd64
macbookpro:~ fredlee$ go env
GOARCH="amd64"
GOBIN="/Users/fredlee/Documents/develop/go/workspace/bin"
GOCACHE="/Users/fredlee/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/fredlee/Documents/develop/go/workspace"
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=""
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/34/z2z_vp1573g5014m8k7wys100000gn/T/go-build979020965=/tmp/go-build -gno-record-gcc-switches -fno-common"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions