Browse code

Rework Go mod tidy/vendor checks

This change reworks the Go mod tidy/vendor checks to run for all tracked Go modules by the project and fail for any uncommitted changes.

Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>

Austin Vazquez authored on 2025/10/04 07:38:33
Showing 2 changed files
... ...
@@ -5,38 +5,30 @@ set -e
5 5
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6 6
 source "${SCRIPTDIR}/.validate"
7 7
 
8
-tidy_files=('go.mod' 'go.sum')
8
+modules_files=('api/go.mod' 'client/go.mod' 'man/go.mod' 'go.mod')
9
+tidy_files=("${modules_files[@]}" 'api/go.sum' 'client/go.sum' 'man/go.sum' 'go.sum')
9 10
 vendor_files=("${tidy_files[@]}" 'vendor/')
10 11
 
11
-validate_vendor_files_exist() {
12
-	local file
13
-	for file in "${vendor_files[@]}"; do
14
-		if [ ! -e "$file" ]; then
15
-			echo >&2 "ERROR: required file '$file' does not exist"
12
+validate_tidy_modules() {
13
+	# check that all go.mod files exist in HEAD; go.sum files are generated by 'go mod tidy'
14
+	# so we don't need to check for their existence beforehand
15
+	for f in "${modules_files[@]}"; do
16
+		if [ ! -f "$f" ]; then
17
+			echo >&2 "ERROR: missing $f"
16 18
 			return 1
17 19
 		fi
18 20
 	done
19
-	return 0
20
-}
21
-
22
-validate_vendor_tidy() {
23 21
 	# run mod tidy
24 22
 	./hack/vendor.sh tidy
25 23
 	# check if any files have changed
26
-	git diff --quiet HEAD -- "${tidy_files[@]}"
24
+	git diff --quiet HEAD -- "${tidy_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
27 25
 }
28 26
 
29 27
 validate_vendor_diff() {
30
-	mapfile -t changed_files < <(validate_diff --diff-filter=ACMR --name-only -- "${vendor_files[@]}")
31
-
32
-	if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ "${#changed_files[@]}" -gt 0 ]; then
33
-		# recreate vendor/
34
-		./hack/vendor.sh vendor
35
-		# check if any files have changed
36
-		git diff --quiet HEAD -- "${vendor_files[@]}"
37
-	else
38
-		echo >&2 'INFO: no vendor changes in diff; skipping vendor check.'
39
-	fi
28
+	# recreate vendor/
29
+	./hack/vendor.sh vendor
30
+	# check if any files have changed
31
+	git diff --quiet HEAD -- "${vendor_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
40 32
 }
41 33
 
42 34
 validate_vendor_license() {
... ...
@@ -48,26 +40,22 @@ validate_vendor_license() {
48 48
 	done < <(awk '/^# /{ print $2 }' vendor/modules.txt)
49 49
 }
50 50
 
51
-# First check the required files exist as git diff will not report missing files.
52
-if ! validate_vendor_files_exist; then
53
-	{
54
-		echo 'FAIL: Vendoring was not performed correctly!'
55
-		echo
56
-		echo 'Please revendor with hack/vendor.sh'
57
-	} >&2
58
-	exit 1
59
-fi
60
-
61
-if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then
51
+if validate_tidy_modules && validate_vendor_diff && validate_vendor_license; then
62 52
 	echo >&2 'PASS: Vendoring has been performed correctly!'
63 53
 else
64 54
 	{
65 55
 		echo 'FAIL: Vendoring was not performed correctly!'
66 56
 		echo
67
-		echo 'The following files changed during re-vendor:'
68
-		echo
69
-		git diff --name-status HEAD -- "${vendor_files[@]}"
70
-		echo
57
+		if [ -n "$(git ls-files --others --exclude-standard)" ]; then
58
+			echo 'The following files are missing:'
59
+			git ls-files --others --exclude-standard
60
+			echo
61
+		fi
62
+		if [ -n "$(git diff --name-status HEAD -- "${vendor_files[@]}")" ]; then
63
+			echo 'The following files changed during re-vendor:'
64
+			git diff --name-status HEAD -- "${vendor_files[@]}"
65
+			echo
66
+		fi
71 67
 		echo 'Please revendor with hack/vendor.sh'
72 68
 		echo
73 69
 		git diff --diff-filter=M -- "${vendor_files[@]}"
... ...
@@ -7,15 +7,31 @@
7 7
 set -e
8 8
 
9 9
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10
+PROJECT_DIR="$(cd "$SCRIPTDIR/.." && pwd)"
11
+
12
+# All module paths are relative to PROJECT_DIR
13
+# Currently only a subset of modules are vendored.
14
+vendor_modules_paths=(. man)
15
+modules_paths=(api client "${vendor_modules_paths[@]}")
10 16
 
11 17
 tidy() (
12
-		set -x
13
-		go mod tidy
18
+	for module_path in "${modules_paths[@]}"; do
19
+		(
20
+			set -x
21
+			cd "$PROJECT_DIR/$module_path"
22
+			go mod tidy
23
+		)
24
+	done
14 25
 )
15 26
 
16 27
 vendor() (
17
-		set -x
18
-		go mod vendor
28
+	for module_path in "${vendor_modules_paths[@]}"; do
29
+		(
30
+			set -x
31
+			cd "$PROJECT_DIR/$module_path"
32
+			go mod vendor
33
+		)
34
+	done
19 35
 )
20 36
 
21 37
 replace() (