From 7f87a7ae93076f62697d263eb94781be995e53dd Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Wed, 12 Apr 2023 10:11:32 -0400 Subject: [PATCH] ci: lint all modules even if one module fails The previous implementation stopped linting when it encountered its first module that failed the linter. Now, use a more idiomatic find|xargs pattern, which will run every module and exit non-zero if any module fails to lint. This will speed up the feedback cycle on CI in the event that multiple modules fail linting. Also change the script extension to .bash to make it clear to consumers that this script requires bash, not sh; and make the script executable so that CI can execute it directly instead of calling sh $script. --- Makefile | 4 ++-- scripts/go-lint-all.bash | 18 ++++++++++++++++++ scripts/go-lint-all.sh | 11 ----------- 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100755 scripts/go-lint-all.bash delete mode 100644 scripts/go-lint-all.sh diff --git a/Makefile b/Makefile index b680f3d06114..17c23d1158c6 100644 --- a/Makefile +++ b/Makefile @@ -379,12 +379,12 @@ golangci_version=v1.51.2 lint: @echo "--> Running linter" @go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version) - @sh ./scripts/go-lint-all.sh --timeout=15m + @./scripts/go-lint-all.bash --timeout=15m lint-fix: @echo "--> Running linter" @go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version) - @sh ./scripts/go-lint-all.sh --fix + @./scripts/go-lint-all.bash --fix .PHONY: lint lint-fix diff --git a/scripts/go-lint-all.bash b/scripts/go-lint-all.bash new file mode 100755 index 000000000000..7448807bcbb5 --- /dev/null +++ b/scripts/go-lint-all.bash @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +set -eu -o pipefail + +REPO_ROOT="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." &> /dev/null && pwd )" +export REPO_ROOT + +lint_module() { + local root="$1" + shift + cd "$(dirname "$root")" && + echo "linting $(grep "^module" go.mod)" && + golangci-lint run ./... -c "${REPO_ROOT}/.golangci.yml" "$@" +} +export -f lint_module + +find "${REPO_ROOT}" -type f -name go.mod -print0 | + xargs -0 -I{} bash -c 'lint_module "$@"' _ {} "$@" # Prepend go.mod file before command-line args. diff --git a/scripts/go-lint-all.sh b/scripts/go-lint-all.sh deleted file mode 100644 index 4518046191ed..000000000000 --- a/scripts/go-lint-all.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash - -set -eu - -export pwd=$(pwd) - -for modfile in $(find . -name go.mod); do - echo "linting $(dirname $modfile)" - DIR=$(dirname $modfile) - (cd $DIR; golangci-lint run ./... -c $pwd/.golangci.yml $@) -done