blob: 6e026ee78b3c597a4ab0f80abdaa01109c69de2a [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
Brian Silverman4e278082022-05-19 22:47:41 -070036update_go_repos() {
Philipp Schraderd96d4cb2022-02-06 15:37:29 -080037 # Clear out the go_deps.bzl file so that gazelle won't hesitate to update
38 # it. Without this step gazelle would never try to remove a dependency.
39 cat > "${BUILD_WORKSPACE_DIRECTORY}"/go_deps.bzl <<EOF
40def go_dependencies():
41 pass
42EOF
Philipp Schrader37fdbb62021-12-18 00:30:37 -080043 ./gazelle-runner.bash update-repos \
44 -from_file=go.mod \
45 -to_macro=go_deps.bzl%go_dependencies \
46 -prune
47}
48
49gazelle() {
50 ./gazelle-runner.bash
51}
52
53tweak_gazelle_go_deps() {
54 local -r tweaker="$(readlink -f tools/go/tweak_gazelle_go_deps)"
55 cd "${BUILD_WORKSPACE_DIRECTORY}"
56 "${tweaker}" ./go_deps.bzl
57}
58
Philipp Schraderd96d4cb2022-02-06 15:37:29 -080059clean_up_go_mirrors() {
60 ./tools/go/mirror_go_repos --prune
61}
62
Brian Silverman4e278082022-05-19 22:47:41 -070063rustfmt() {
64 ./tools/lint/rustfmt
65}
66
Brian Silvermana7ba2512022-12-19 19:13:52 -080067cargo_lockfile() {
68 cd "${BUILD_WORKSPACE_DIRECTORY}"
69 cp third_party/cargo/Cargo.raze.lock Cargo.lock
70 external/rust__x86_64-unknown-linux-gnu_tools/bin/cargo generate-lockfile --locked --manifest-path=Cargo.toml
71 rm Cargo.lock
72}
73
Brian Silverman4e278082022-05-19 22:47:41 -070074cargo_raze() {
75 local -r cargo_raze="$(readlink -f external/cargo_raze/impl/cargo_raze_bin)"
Brian Silvermane84b0cf2022-08-13 19:36:21 -070076 export CARGO="$(readlink -f external/rust__x86_64-unknown-linux-gnu_tools/bin/cargo)"
77 export RUSTC="$(readlink -f external/rust__x86_64-unknown-linux-gnu_tools/bin/rustc)"
Brian Silverman4e278082022-05-19 22:47:41 -070078 cd "${BUILD_WORKSPACE_DIRECTORY}"
79 # Note we don't run with --generate-lockfile here. If there's a new
80 # dependency, we don't want to download it, just failing with an error
81 # is sufficient.
82 "${cargo_raze}" --manifest-path=Cargo.toml
83}
84
85tweak_cargo_raze() {
86 local -r tweaker="$(readlink -f tools/rust/tweak_cargo_raze_output)"
87 cd "${BUILD_WORKSPACE_DIRECTORY}"
88 "${tweaker}" .
89}
90
Philipp Schradercc016b32021-12-30 08:59:58 -080091buildifier() {
92 ./tools/lint/buildifier
93}
94
Philipp Schraderace08842022-03-26 14:52:55 -070095prettier() {
Philipp Schrader817cce32022-03-26 15:00:00 -070096 ./tools/lint/prettier
Philipp Schraderace08842022-03-26 14:52:55 -070097}
98
Ravago Jones23dac942022-07-31 16:18:54 -070099yapf() {
100 ./tools/lint/yapf
101}
102
Philipp Schrader773577f2021-12-17 23:45:39 -0800103git_status_is_clean() {
104 cd "${BUILD_WORKSPACE_DIRECTORY}"
105 if ! git diff --quiet; then
106 echo "One or more linters appear to have made changes to your code!" >&2
107 return 1
108 fi
109}
110
111# All the linters that we are going to run.
112readonly -a LINTERS=(
113 gofmt
Philipp Schrader37fdbb62021-12-18 00:30:37 -0800114 gomod
Brian Silverman4e278082022-05-19 22:47:41 -0700115 update_go_repos
Philipp Schrader37fdbb62021-12-18 00:30:37 -0800116 gazelle
117 tweak_gazelle_go_deps
Philipp Schraderd96d4cb2022-02-06 15:37:29 -0800118 clean_up_go_mirrors
Brian Silverman4e278082022-05-19 22:47:41 -0700119 rustfmt
Brian Silvermana7ba2512022-12-19 19:13:52 -0800120 cargo_lockfile
Brian Silverman4e278082022-05-19 22:47:41 -0700121 cargo_raze
122 tweak_cargo_raze
Philipp Schradercc016b32021-12-30 08:59:58 -0800123 buildifier
Philipp Schraderace08842022-03-26 14:52:55 -0700124 prettier
Ravago Jones23dac942022-07-31 16:18:54 -0700125 yapf
Philipp Schrader773577f2021-12-17 23:45:39 -0800126 git_status_is_clean # This must the last linter.
127)
128
129failure=0
130for linter in "${LINTERS[@]}"; do
Brian Silverman4e278082022-05-19 22:47:41 -0700131 echo "Running ${linter}..." >&2
Philipp Schrader773577f2021-12-17 23:45:39 -0800132 if ! (eval "${linter}"); then
Brian Silverman4e278082022-05-19 22:47:41 -0700133 echo "LINTER FAILURE: ${linter}" >&2
Philipp Schrader773577f2021-12-17 23:45:39 -0800134 failure=1
Brian Silverman4e278082022-05-19 22:47:41 -0700135 else
136 echo "${linter} succeeded" >&2
Philipp Schrader773577f2021-12-17 23:45:39 -0800137 fi
138done
139
140if ((failure != 0)); then
141 echo "One or more linters failed." >&2
142 cd "${BUILD_WORKSPACE_DIRECTORY}"
143 git --no-pager diff || :
144 exit "${failure}"
145fi
146
147exit 0