Skip to content

Commit 194289a

Browse files
committed
update Go mod support
It turned out that changes like kubernetes-csi/csi-lib-utils#33 should better have been committed after `go mod tidy` because that adds some indirect dependencies in that example. The revised `test-vendor` checks for that and (just in case that this ever becomes desired) allows projects to not have a vendor directory when using `go mod`. How to use `go mod` properly gets documented in the README.md, because there are such pitfalls.
1 parent 0399988 commit 194289a

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,37 @@ Kubernetes releases:
106106

107107
CSI_PROW_KUBERNETES_VERSION=1.13.3 ./.prow.sh
108108
CSI_PROW_KUBERNETES_VERSION=latest ./.prow.sh
109+
110+
Dependencies and vendoring
111+
--------------------------
112+
113+
Most projects will (eventually) use `go mod` to manage
114+
dependencies. `dep` is also still supported by `csi-release-tools`,
115+
but not documented here because it's not recommended anymore.
116+
117+
The usual instructions for using [go
118+
modules](https://github.com/golang/go/wiki/Modules) apply. Here's a cheat sheet
119+
for some of the relevant commands:
120+
- list available updates: `GO111MODULE=on go list -u -m all`
121+
- update or add a single dependency: `GO111MODULE=on go get <package>`
122+
- update all dependencies to their next minor or patch release:
123+
`GO111MODULE=on go get ./...` (add `-u=patch` to limit to patch
124+
releases)
125+
- lock onto a specific version: `GO111MODULE=on go get <package>@<version>`
126+
- clean up `go.mod`: `GO111MODULE=on go mod tidy`
127+
- update vendor directory: `GO111MODULE=on go mod vendor`
128+
129+
`GO111MODULE=on` can be left out when using Go >= 1.13 or when the
130+
source is checked out outside of `$GOPATH`.
131+
132+
`go mod tidy` must be used to ensure that the listed dependencies are
133+
really still needed. Changing import statements or a tentative `go
134+
get` can result in stale dependencies.
135+
136+
The `test-vendor` verifies that it was used when run locally or in a
137+
pre-merge CI job. If a `vendor` directory is present, it will also
138+
verify that it's content is up-to-date.
139+
140+
The `vendor` directory is optional. It is still present in projects
141+
because it avoids downloading sources during CI builds. If this is no
142+
longer deemed necessary, then a project can also remove the directory.

build.make

+45-10
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ test-fmt:
125125
# - the fabricated merge commit leaves go.mod, go.sum and vendor dir unchanged
126126
# - release-tools also didn't change (changing rules or Go version might lead to
127127
# a different result and thus must be tested)
128+
# - import statements not changed (because if they change, go.mod might have to be updated)
129+
#
130+
# "git diff" is intelligent enough to annotate changes inside the "import" block in
131+
# the start of the diff hunk:
132+
#
133+
# diff --git a/rpc/common.go b/rpc/common.go
134+
# index bb4a5c4..5fa4271 100644
135+
# --- a/rpc/common.go
136+
# +++ b/rpc/common.go
137+
# @@ -21,7 +21,6 @@ import (
138+
# "fmt"
139+
# "time"
140+
#
141+
# - "google.golang.org/grpc"
142+
# "google.golang.org/grpc/codes"
143+
# "google.golang.org/grpc/status"
144+
#
145+
# We rely on that to find such changes.
146+
#
147+
# Vendoring is optional when using go.mod.
128148
.PHONY: test-vendor
129149
test: test-vendor
130150
test-vendor:
@@ -135,22 +155,37 @@ test-vendor:
135155
*v0.[56789]*) dep check && echo "vendor up-to-date" || false;; \
136156
*) echo "skipping check, dep >= 0.5 required";; \
137157
esac; \
138-
else \
139-
echo "Repo uses 'go mod' for vendoring."; \
158+
elif [ -f go.mod ]; then \
159+
echo "Repo uses 'go mod'."; \
140160
if [ "$${JOB_NAME}" ] && \
141161
( [ "$${JOB_TYPE}" != "presubmit" ] || \
142-
[ $$(git diff "${PULL_BASE_SHA}..HEAD" -- go.mod go.sum vendor release-tools | wc -l) -eq 0 ] ); then \
143-
echo "Skipping vendor check because the Prow pre-submit job does not change vendoring."; \
144-
elif ! GO111MODULE=on go mod vendor; then \
162+
[ $$( (git diff "${PULL_BASE_SHA}..HEAD" -- go.mod go.sum vendor release-tools; \
163+
git diff "${PULL_BASE_SHA}..HEAD" | grep -e '^@@.*@@ import (' -e '^[+-]import') | \
164+
wc -l) -eq 0 ] ); then \
165+
echo "Skipping vendor check because the Prow pre-submit job does not affect dependencies."; \
166+
elif ! GO111MODULE=on go mod tidy; then \
145167
echo "ERROR: vendor check failed."; \
146168
false; \
147-
elif [ $$(git status --porcelain -- vendor | wc -l) -gt 0 ]; then \
148-
echo "ERROR: vendor directory *not* up-to-date, it did get modified by 'GO111MODULE=on go mod vendor':"; \
149-
git status -- vendor; \
150-
git diff -- vendor; \
169+
elif [ $$(git status --porcelain -- go.mod go.sum | wc -l) -gt 0 ]; then \
170+
echo "ERROR: go module files *not* up-to-date, they did get modified by 'GO111MODULE=on go mod tidy':"; \
171+
git diff -- go.mod go.sum; \
151172
false; \
173+
elif [ -d vendor ]; then \
174+
if ! GO111MODULE=on go mod vendor; then \
175+
echo "ERROR: vendor check failed."; \
176+
false; \
177+
elif [ $$(git status --porcelain -- vendor | wc -l) -gt 0 ]; then \
178+
echo "ERROR: vendor directory *not* up-to-date, it did get modified by 'GO111MODULE=on go mod vendor':"; \
179+
git status -- vendor; \
180+
git diff -- vendor; \
181+
false; \
182+
else \
183+
echo "Go dependencies and vendor directory up-to-date."; \
184+
fi; \
185+
else \
186+
echo "Go dependencies up-to-date."; \
152187
fi; \
153-
fi;
188+
fi
154189

155190
.PHONY: test-subtree
156191
test: test-subtree

0 commit comments

Comments
 (0)