From fb3fe32ae96579d0b682880cb6b0c0595e985181 Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Wed, 19 Jun 2024 23:21:29 +0200 Subject: [PATCH] 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 Signed-off-by: Chun-Hung Tseng --- Makefile | 6 +++++- test.sh | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8cfd790e0..928f9ac09 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/test.sh b/test.sh index a9c883e04..e14bb4249 100755 --- a/test.sh +++ b/test.sh @@ -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"