Merge pull request #51117 from austinvazquez/cherry-pick-fix-go-validation-checks-to-25.0

[25.0] Rework Go mod tidy/vendor checks
diff --git a/hack/validate/vendor b/hack/validate/vendor
index a0b35d3..b01a45c 100755
--- a/hack/validate/vendor
+++ b/hack/validate/vendor
@@ -5,27 +5,30 @@
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 source "${SCRIPTDIR}/.validate"
 
-tidy_files=('vendor.mod' 'vendor.sum')
+modules_files=('man/go.mod' 'vendor.mod')
+tidy_files=("${modules_files[@]}" 'man/go.sum' 'vendor.sum')
 vendor_files=("${tidy_files[@]}" 'vendor/')
 
-validate_vendor_tidy() {
+validate_tidy_modules() {
+	# check that all go.mod files exist in HEAD; go.sum files are generated by 'go mod tidy'
+	# so we don't need to check for their existence beforehand
+	for f in "${modules_files[@]}"; do
+		if [ ! -f "$f" ]; then
+			echo >&2 "ERROR: missing $f"
+			return 1
+		fi
+	done
 	# run mod tidy
 	./hack/vendor.sh tidy
 	# check if any files have changed
-	git diff --quiet HEAD -- "${tidy_files[@]}"
+	git diff --quiet HEAD -- "${tidy_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
 }
 
 validate_vendor_diff() {
-	mapfile -t changed_files < <(validate_diff --diff-filter=ACMR --name-only -- "${vendor_files[@]}")
-
-	if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ "${#changed_files[@]}" -gt 0 ]; then
-		# recreate vendor/
-		./hack/vendor.sh vendor
-		# check if any files have changed
-		git diff --quiet HEAD -- "${vendor_files[@]}"
-	else
-		echo >&2 'INFO: no vendor changes in diff; skipping vendor check.'
-	fi
+	# recreate vendor/
+	./hack/vendor.sh vendor
+	# check if any files have changed
+	git diff --quiet HEAD -- "${vendor_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
 }
 
 validate_vendor_license() {
@@ -37,16 +40,22 @@
 	done < <(awk '/^# /{ print $2 }' vendor/modules.txt)
 }
 
-if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then
+if validate_tidy_modules && validate_vendor_diff && validate_vendor_license; then
 	echo >&2 'PASS: Vendoring has been performed correctly!'
 else
 	{
 		echo 'FAIL: Vendoring was not performed correctly!'
 		echo
-		echo 'The following files changed during re-vendor:'
-		echo
-		git diff --name-status HEAD -- "${vendor_files[@]}"
-		echo
+		if [ -n "$(git ls-files --others --exclude-standard)" ]; then
+			echo 'The following files are missing:'
+			git ls-files --others --exclude-standard
+			echo
+		fi
+		if [ -n "$(git diff --name-status HEAD -- "${vendor_files[@]}")" ]; then
+			echo 'The following files changed during re-vendor:'
+			git diff --name-status HEAD -- "${vendor_files[@]}"
+			echo
+		fi
 		echo 'Please revendor with hack/vendor.sh'
 		echo
 		git diff --diff-filter=M -- "${vendor_files[@]}"
diff --git a/hack/vendor.sh b/hack/vendor.sh
index 32538a3..8d55056 100755
--- a/hack/vendor.sh
+++ b/hack/vendor.sh
@@ -7,15 +7,32 @@
 set -e
 
 SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+PROJECT_DIR="$(cd "$SCRIPTDIR/.." && pwd)"
 
 tidy() (
+	(
 		set -x
 		"${SCRIPTDIR}"/with-go-mod.sh go mod tidy -modfile vendor.mod -compat 1.18
+	)
+
+	(
+		set -x
+		cd man
+		go mod tidy
+	)
 )
 
 vendor() (
+	(
 		set -x
 		"${SCRIPTDIR}"/with-go-mod.sh go mod vendor -modfile vendor.mod
+	)
+
+	(
+		set -x
+		cd man
+		go mod vendor
+	)
 )
 
 help() {