From 7a64e1989a688db5e3cd0f6cd8d15777b4989da3 Mon Sep 17 00:00:00 2001 From: Anna Song Date: Tue, 24 Jan 2023 14:14:13 -0800 Subject: [PATCH] Add script that runs go mod tidy with replace statements --- Makefile | 13 ++----------- hack/test-go-mod.sh | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 11 deletions(-) create mode 100755 hack/test-go-mod.sh diff --git a/Makefile b/Makefile index 78be684b0fe..5d704dcb417 100644 --- a/Makefile +++ b/Makefile @@ -108,19 +108,10 @@ verify-kustomize-repo: \ prow-presubmit-check: \ install-tools \ test-unit-kustomize-plugins \ + test-go-mod \ build-non-plugin-all \ test-examples-kustomize-against-HEAD \ test-examples-kustomize-against-v4-release - # TODO(annasong): Find permanent solution. - # We have temporarily commented out this job because it fails for public - # cross-module changes, given that we've removed replace directives with go - # workspace and the job runs on the latest released modules. This - # complicates releases. - # This job is also less effective than intended because it only detects - # go mod tidy errors, instead of any changes to the original code that - # go mod tidy makes. - - # test-go-mod .PHONY: license license: $(MYGOBIN)/addlicense @@ -161,7 +152,7 @@ functions-examples-all: done test-go-mod: - ./hack/for-each-module.sh "go list -m -json all > /dev/null && go mod tidy -v" + ./hack/for-each-module.sh $$(PWD)/hack/test-go-mod.sh .PHONY: verify-kustomize-e2e: $(MYGOBIN)/mdrip $(MYGOBIN)/kind diff --git a/hack/test-go-mod.sh b/hack/test-go-mod.sh new file mode 100755 index 00000000000..512f3c52d4c --- /dev/null +++ b/hack/test-go-mod.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# Copyright 2023 The Kubernetes Authors. +# SPDX-License-Identifier: Apache-2.0 + +read -a modules <<< $(go list -m) +read -a module_paths <<< $(go list -m -f {{.Dir}}) + +for i in ${!modules[@]}; do + replace_path=$(realpath --relative-to=$(PWD) ${module_paths[i]}) + if [ $replace_path == . ]; then + continue + fi + go mod edit -replace=${modules[i]}=$replace_path +done + +go mod tidy -v + +for i in ${!modules[@]}; do + go mod edit -dropreplace=${modules[i]} +done \ No newline at end of file