From 8eba94f271ba180a86886dea6e4f8f392f862051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Tue, 5 Nov 2019 14:50:28 +0100 Subject: [PATCH] Use -mod=vendor in (go {list,test,vet}) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows using the vendored dependencies instead of searching for them in $GOPATH and elsewhere. This does not necessarily matter for skopeo itself, but the test-skopeo Makefile target in containers/image uses (go mod edit -replace) to replace the vendored c/image with a locally-edited copy; skopeo's (make check) then runs tests in a container which does not have access to this locally-edited copy, and since Go 1.13 this causes (go {list,test,vet}) to fail if -mod=vendor is not used. Signed-off-by: Miloslav Trmač --- Makefile | 13 +++++++------ hack/make.sh | 12 ++++++++++-- hack/make/validate-vet | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 86b3c724..57c85908 100644 --- a/Makefile +++ b/Makefile @@ -26,10 +26,11 @@ GO ?= go CONTAINER_RUNTIME := $(shell command -v podman 2> /dev/null || echo docker) GOMD2MAN ?= $(shell command -v go-md2man || echo '$(GOBIN)/go-md2man') -GO_BUILD=$(GO) build -# Go module support: set `-mod=vendor` to use the vendored sources +# Go module support: set `-mod=vendor` to use the vendored sources. +# See also hack/make.sh. ifeq ($(shell go help mod >/dev/null 2>&1 && echo true), true) - GO_BUILD=GO111MODULE=on $(GO) build -mod=vendor + GO:=GO111MODULE=on $(GO) + MOD_VENDOR=-mod=vendor endif ifeq ($(DEBUG), 1) @@ -100,10 +101,10 @@ binary-static: cmd/skopeo # Build w/o using containers binary-local: - $(GPGME_ENV) $(GO_BUILD) ${GO_DYN_FLAGS} -ldflags "-X main.gitCommit=${GIT_COMMIT}" -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o skopeo ./cmd/skopeo + $(GPGME_ENV) $(GO) build $(MOD_VENDOR) ${GO_DYN_FLAGS} -ldflags "-X main.gitCommit=${GIT_COMMIT}" -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o skopeo ./cmd/skopeo binary-local-static: - $(GPGME_ENV) $(GO_BUILD) -ldflags "-extldflags \"-static\" -X main.gitCommit=${GIT_COMMIT}" -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o skopeo ./cmd/skopeo + $(GPGME_ENV) $(GO) build $(MOD_VENDOR) -ldflags "-extldflags \"-static\" -X main.gitCommit=${GIT_COMMIT}" -gcflags "$(GOGCFLAGS)" -tags "$(BUILDTAGS)" -o skopeo ./cmd/skopeo build-container: ${CONTAINER_RUNTIME} build ${BUILD_ARGS} -t "$(IMAGE)" . @@ -174,7 +175,7 @@ validate-local: hack/make.sh validate-git-marks validate-gofmt validate-lint validate-vet test-unit-local: - $(GPGME_ENV) $(GO) test -tags "$(BUILDTAGS)" $$($(GO) list -tags "$(BUILDTAGS)" -e ./... | grep -v '^github\.com/containers/skopeo/\(integration\|vendor/.*\)$$') + $(GPGME_ENV) $(GO) test $(MOD_VENDOR) -tags "$(BUILDTAGS)" $$($(GO) list $(MOD_VENDOR) -tags "$(BUILDTAGS)" -e ./... | grep -v '^github\.com/containers/skopeo/\(integration\|vendor/.*\)$$') vendor: export GO111MODULE=on \ diff --git a/hack/make.sh b/hack/make.sh index 622dbf2e..ef820690 100755 --- a/hack/make.sh +++ b/hack/make.sh @@ -59,6 +59,14 @@ DEFAULT_BUNDLES=( TESTFLAGS+=" -test.timeout=10m" +# Go module support: set `-mod=vendor` to use the vendored sources +# See also the top-level Makefile. +mod_vendor= +if go help mod >/dev/null 2>&1; then + export GO111MODULE=on + mod_vendor='-mod=vendor' +fi + # If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'. # You can use this to select certain tests to run, eg. # @@ -72,10 +80,10 @@ TESTFLAGS+=" -test.timeout=10m" go_test_dir() { dir=$1 ( - echo '+ go test' $TESTFLAGS ${BUILDTAGS:+-tags "$BUILDTAGS"} "${SKOPEO_PKG}${dir#.}" + echo '+ go test' $mod_vendor $TESTFLAGS ${BUILDTAGS:+-tags "$BUILDTAGS"} "${SKOPEO_PKG}${dir#.}" cd "$dir" export DEST="$ABS_DEST" # we're in a subshell, so this is safe -- our integration-cli tests need DEST, and "cd" screws it up - go test $TESTFLAGS ${BUILDTAGS:+-tags "$BUILDTAGS"} + go test $mod_vendor $TESTFLAGS ${BUILDTAGS:+-tags "$BUILDTAGS"} ) } diff --git a/hack/make/validate-vet b/hack/make/validate-vet index f95d0fc8..f3c30fb1 100755 --- a/hack/make/validate-vet +++ b/hack/make/validate-vet @@ -1,6 +1,6 @@ #!/bin/bash -errors=$(go vet $(go list -e ./... | grep -v "$SKOPEO_PKG"/vendor)) +errors=$(go vet $mod_vendor $(go list $mod_vendor -e ./... | grep -v "$SKOPEO_PKG"/vendor)) if [ -z "$errors" ]; then echo 'Congratulations! All Go source files have been vetted.'