blob: c8d65013366da8c95983357ba20b12445ab264f7 [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 Schradere3a69d92023-07-05 20:54:16 -070026clang_format() {
27 ./tools/lint/clang_format
28}
29
Philipp Schrader773577f2021-12-17 23:45:39 -080030gofmt() {
31 ./tools/lint/gofmt
32}
33
Philipp Schrader37fdbb62021-12-18 00:30:37 -080034gomod() {
35 local -r go="$(readlink -f external/go_sdk/bin/go)"
36 cd "${BUILD_WORKSPACE_DIRECTORY}"
37 "${go}" mod tidy -e
38}
39
Brian Silverman4e278082022-05-19 22:47:41 -070040update_go_repos() {
Philipp Schraderd96d4cb2022-02-06 15:37:29 -080041 # Clear out the go_deps.bzl file so that gazelle won't hesitate to update
42 # it. Without this step gazelle would never try to remove a dependency.
43 cat > "${BUILD_WORKSPACE_DIRECTORY}"/go_deps.bzl <<EOF
44def go_dependencies():
45 pass
46EOF
Philipp Schrader37fdbb62021-12-18 00:30:37 -080047 ./gazelle-runner.bash update-repos \
48 -from_file=go.mod \
49 -to_macro=go_deps.bzl%go_dependencies \
50 -prune
51}
52
53gazelle() {
54 ./gazelle-runner.bash
55}
56
57tweak_gazelle_go_deps() {
58 local -r tweaker="$(readlink -f tools/go/tweak_gazelle_go_deps)"
59 cd "${BUILD_WORKSPACE_DIRECTORY}"
60 "${tweaker}" ./go_deps.bzl
61}
62
Philipp Schraderd96d4cb2022-02-06 15:37:29 -080063clean_up_go_mirrors() {
64 ./tools/go/mirror_go_repos --prune
65}
66
Brian Silverman4e278082022-05-19 22:47:41 -070067rustfmt() {
68 ./tools/lint/rustfmt
69}
70
Brian Silvermana7ba2512022-12-19 19:13:52 -080071cargo_lockfile() {
72 cd "${BUILD_WORKSPACE_DIRECTORY}"
73 cp third_party/cargo/Cargo.raze.lock Cargo.lock
74 external/rust__x86_64-unknown-linux-gnu_tools/bin/cargo generate-lockfile --locked --manifest-path=Cargo.toml
75 rm Cargo.lock
76}
77
Brian Silverman4e278082022-05-19 22:47:41 -070078cargo_raze() {
79 local -r cargo_raze="$(readlink -f external/cargo_raze/impl/cargo_raze_bin)"
Brian Silvermane84b0cf2022-08-13 19:36:21 -070080 export CARGO="$(readlink -f external/rust__x86_64-unknown-linux-gnu_tools/bin/cargo)"
81 export RUSTC="$(readlink -f external/rust__x86_64-unknown-linux-gnu_tools/bin/rustc)"
Brian Silverman4e278082022-05-19 22:47:41 -070082 cd "${BUILD_WORKSPACE_DIRECTORY}"
83 # Note we don't run with --generate-lockfile here. If there's a new
84 # dependency, we don't want to download it, just failing with an error
85 # is sufficient.
86 "${cargo_raze}" --manifest-path=Cargo.toml
87}
88
89tweak_cargo_raze() {
90 local -r tweaker="$(readlink -f tools/rust/tweak_cargo_raze_output)"
91 cd "${BUILD_WORKSPACE_DIRECTORY}"
92 "${tweaker}" .
93}
94
Philipp Schradercc016b32021-12-30 08:59:58 -080095buildifier() {
96 ./tools/lint/buildifier
97}
98
Philipp Schraderace08842022-03-26 14:52:55 -070099prettier() {
Philipp Schrader817cce32022-03-26 15:00:00 -0700100 ./tools/lint/prettier
Philipp Schraderace08842022-03-26 14:52:55 -0700101}
102
Ravago Jones23dac942022-07-31 16:18:54 -0700103yapf() {
104 ./tools/lint/yapf
105}
106
Philipp Schrader773577f2021-12-17 23:45:39 -0800107git_status_is_clean() {
108 cd "${BUILD_WORKSPACE_DIRECTORY}"
109 if ! git diff --quiet; then
110 echo "One or more linters appear to have made changes to your code!" >&2
111 return 1
112 fi
113}
114
115# All the linters that we are going to run.
116readonly -a LINTERS=(
Philipp Schrader790cb542023-07-05 21:06:52 -0700117 clang_format
Philipp Schrader773577f2021-12-17 23:45:39 -0800118 gofmt
Philipp Schraderbfa88db2023-01-05 13:34:12 -0800119 gomod
120 update_go_repos
121 gazelle
122 tweak_gazelle_go_deps
123 clean_up_go_mirrors
Brian Silverman4e278082022-05-19 22:47:41 -0700124 rustfmt
Brian Silvermana7ba2512022-12-19 19:13:52 -0800125 cargo_lockfile
Brian Silverman4e278082022-05-19 22:47:41 -0700126 cargo_raze
127 tweak_cargo_raze
Philipp Schradercc016b32021-12-30 08:59:58 -0800128 buildifier
Philipp Schraderace08842022-03-26 14:52:55 -0700129 prettier
Ravago Jones23dac942022-07-31 16:18:54 -0700130 yapf
Philipp Schrader773577f2021-12-17 23:45:39 -0800131 git_status_is_clean # This must the last linter.
132)
133
134failure=0
135for linter in "${LINTERS[@]}"; do
Brian Silverman4e278082022-05-19 22:47:41 -0700136 echo "Running ${linter}..." >&2
Philipp Schrader773577f2021-12-17 23:45:39 -0800137 if ! (eval "${linter}"); then
Brian Silverman4e278082022-05-19 22:47:41 -0700138 echo "LINTER FAILURE: ${linter}" >&2
Philipp Schrader773577f2021-12-17 23:45:39 -0800139 failure=1
Brian Silverman4e278082022-05-19 22:47:41 -0700140 else
141 echo "${linter} succeeded" >&2
Philipp Schrader773577f2021-12-17 23:45:39 -0800142 fi
143done
144
145if ((failure != 0)); then
146 echo "One or more linters failed." >&2
147 cd "${BUILD_WORKSPACE_DIRECTORY}"
148 git --no-pager diff || :
149 exit "${failure}"
150fi
151
152exit 0