Fix dependency inconsistency detection and add make verify-dep

Makefile's target `verify-dep` current behavior is to use `go list` to
check consistent dependency versions from direct dependencies. Ignoring
indirect dependencies in a multi-module project could lead to version
mismatches. If module A imports module B, module B's dependency will be
an indirect dependency in module A. Which can potentially have a version
mismatch. Therefore, use `go mod edit` with indirect dependencies, too.
So it can work with all dependencies defined in go.mod.

Fix displaying dependencies with mismatches, as the old code was
searching with grep just for the prefix, which would show other
dependencies that shared the same prefix.

Reference:
- https://github.com/etcd-io/etcd/pull/18205

Signed-off-by: Ivan Valdes <ivan@vald.es>
Signed-off-by: Chun-Hung Tseng <henrybear327@gmail.com>
This commit is contained in:
Chun-Hung Tseng
2024-06-19 23:21:29 +02:00
parent 5b25d6b5a5
commit fb3fe32ae9
2 changed files with 7 additions and 3 deletions

View File

@@ -582,12 +582,16 @@ gofail-disable: install-gofail
PASSES="toggle_failpoints" ./test.sh
.PHONY: verify
verify: verify-go-versions
verify: verify-go-versions verify-dep
.PHONY: verify-go-versions
verify-go-versions:
./scripts/verify_go_versions.sh
.PHONY: verify-dep
verify-dep:
PASSES="dep" ./test.sh 2<&1
.PHONY: fix
fix: sync-toolchain-directive
./scripts/fix.sh

View File

@@ -610,7 +610,7 @@ function dump_deps_of_module() {
if ! module=$(run go list -m); then
return 255
fi
run go list -f "{{if not .Indirect}}{{if .Version}}{{.Path}},{{.Version}},${module}{{end}}{{end}}" -m all
run go mod edit -json | jq -r '.Require[] | .Path+","+.Version+","+if .Indirect then " (indirect)" else "" end+",'"${module}"'"'
}
# Checks whether dependencies are consistent across modules
@@ -623,7 +623,7 @@ function dep_pass {
for dup in ${duplicates}; do
log_error "FAIL: inconsistent versions for depencency: ${dup}"
echo "${all_dependencies}" | grep "${dup}" | sed "s|\\([^,]*\\),\\([^,]*\\),\\([^,]*\\)| - \\1@\\2 from: \\3|g"
echo "${all_dependencies}" | grep "${dup}," | sed 's|\([^,]*\),\([^,]*\),\([^,]*\),\([^,]*\)| - \1@\2\3 from: \4|g'
done
if [[ -n "${duplicates}" ]]; then
log_error "FAIL: inconsistent dependencies"