blob: 62285521800b42752c677d3e22cebd1238ce3965 [file] [log] [blame]
Philipp Schrader773577f2021-12-17 23:45:39 -08001#!/bin/bash
2
3# Perform runfile initialization here so that child processes have a proper
4# RUNFILES_DIR variable set.
5
6# --- begin runfiles.bash initialization v2 ---
7# Copy-pasted from the Bazel Bash runfiles library v2.
8set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
9source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
10 source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
11 source "$0.runfiles/$f" 2>/dev/null || \
12 source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
13 source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
14 { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
15# --- end runfiles.bash initialization v2 ---
16
Philipp Schrader37fdbb62021-12-18 00:30:37 -080017set -o nounset
18
19# Redirect the Go cache on buildkite. Otherwise we run into errors like:
20# "failed to initialize build cache at /var/lib/buildkite-agent/.cache/go-build"
21# due to permission errors.
22if ((RUNNING_IN_CI == 1)); then
23 export GOCACHE=/tmp/lint_go_cache
24fi
25
Philipp Schrader773577f2021-12-17 23:45:39 -080026gofmt() {
27 ./tools/lint/gofmt
28}
29
Philipp Schrader37fdbb62021-12-18 00:30:37 -080030gomod() {
31 local -r go="$(readlink -f external/go_sdk/bin/go)"
32 cd "${BUILD_WORKSPACE_DIRECTORY}"
33 "${go}" mod tidy -e
34}
35
36update_repos() {
37 ./gazelle-runner.bash update-repos \
38 -from_file=go.mod \
39 -to_macro=go_deps.bzl%go_dependencies \
40 -prune
41}
42
43gazelle() {
44 ./gazelle-runner.bash
45}
46
47tweak_gazelle_go_deps() {
48 local -r tweaker="$(readlink -f tools/go/tweak_gazelle_go_deps)"
49 cd "${BUILD_WORKSPACE_DIRECTORY}"
50 "${tweaker}" ./go_deps.bzl
51}
52
Philipp Schradercc016b32021-12-30 08:59:58 -080053buildifier() {
54 ./tools/lint/buildifier
55}
56
Philipp Schrader773577f2021-12-17 23:45:39 -080057git_status_is_clean() {
58 cd "${BUILD_WORKSPACE_DIRECTORY}"
59 if ! git diff --quiet; then
60 echo "One or more linters appear to have made changes to your code!" >&2
61 return 1
62 fi
63}
64
65# All the linters that we are going to run.
66readonly -a LINTERS=(
67 gofmt
Philipp Schrader37fdbb62021-12-18 00:30:37 -080068 gomod
69 update_repos
70 gazelle
71 tweak_gazelle_go_deps
Philipp Schradercc016b32021-12-30 08:59:58 -080072 buildifier
Philipp Schrader773577f2021-12-17 23:45:39 -080073 git_status_is_clean # This must the last linter.
74)
75
76failure=0
77for linter in "${LINTERS[@]}"; do
78 if ! (eval "${linter}"); then
79 failure=1
80 fi
81done
82
83if ((failure != 0)); then
84 echo "One or more linters failed." >&2
85 cd "${BUILD_WORKSPACE_DIRECTORY}"
86 git --no-pager diff || :
87 exit "${failure}"
88fi
89
90exit 0