| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 1 | load("@bazel_skylib//rules:write_file.bzl", "write_file") |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 2 | load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers") |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 3 | load("@io_bazel_rules_go//go:def.bzl", "nogo") |
| 4 | |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 5 | # NOGO_PASSES contains all enabled analyzers that nogo should execute. |
| 6 | NOGO_PASSES = [] |
| 7 | |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 8 | # These deps enable the analyses equivalent to running `go vet`. |
| 9 | # Passing vet = True enables only a tiny subset of these (the ones |
| 10 | # that are always correct). |
| 11 | # You can see the what `go vet` does by running `go doc cmd/vet`. |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 12 | NOGO_PASSES += [ |
| 13 | "@org_golang_x_tools//go/analysis/passes/appends", |
| 14 | "@org_golang_x_tools//go/analysis/passes/asmdecl", |
| 15 | "@org_golang_x_tools//go/analysis/passes/assign", |
| 16 | "@org_golang_x_tools//go/analysis/passes/atomic", |
| 17 | "@org_golang_x_tools//go/analysis/passes/atomicalign", |
| 18 | "@org_golang_x_tools//go/analysis/passes/bools", |
| 19 | "@org_golang_x_tools//go/analysis/passes/buildssa", |
| 20 | "@org_golang_x_tools//go/analysis/passes/buildtag", |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 21 | # Disable cgocall because it fails processing com_github_mattn_go_sqlite3 before exclusions are applied |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 22 | #"@org_golang_x_tools//go/analysis/passes/cgocall", |
| 23 | "@org_golang_x_tools//go/analysis/passes/composite", |
| 24 | "@org_golang_x_tools//go/analysis/passes/copylock", |
| 25 | "@org_golang_x_tools//go/analysis/passes/deepequalerrors", |
| 26 | "@org_golang_x_tools//go/analysis/passes/defers", |
| 27 | "@org_golang_x_tools//go/analysis/passes/directive", |
| 28 | "@org_golang_x_tools//go/analysis/passes/errorsas", |
| 29 | # Disabled as there is no real benefit from it. |
| 30 | #"@org_golang_x_tools//go/analysis/passes/fieldalignment", |
| 31 | "@org_golang_x_tools//go/analysis/passes/framepointer", |
| 32 | "@org_golang_x_tools//go/analysis/passes/httpmux", |
| 33 | "@org_golang_x_tools//go/analysis/passes/httpresponse", |
| 34 | "@org_golang_x_tools//go/analysis/passes/ifaceassert", |
| 35 | # Disabled because we are using Go 1.22 |
| 36 | # https://go.dev/blog/loopvar-preview |
| 37 | #"@org_golang_x_tools//go/analysis/passes/loopclosure", |
| 38 | "@org_golang_x_tools//go/analysis/passes/lostcancel", |
| 39 | "@org_golang_x_tools//go/analysis/passes/nilfunc", |
| 40 | "@org_golang_x_tools//go/analysis/passes/nilness", |
| 41 | "@org_golang_x_tools//go/analysis/passes/printf", |
| 42 | "@org_golang_x_tools//go/analysis/passes/reflectvaluecompare", |
| 43 | # Disabled because of too many false positives |
| 44 | # "@org_golang_x_tools//go/analysis/passes/shadow", |
| 45 | "@org_golang_x_tools//go/analysis/passes/shift", |
| 46 | "@org_golang_x_tools//go/analysis/passes/sigchanyzer", |
| 47 | "@org_golang_x_tools//go/analysis/passes/slog", |
| 48 | "@org_golang_x_tools//go/analysis/passes/sortslice", |
| 49 | "@org_golang_x_tools//go/analysis/passes/stdmethods", |
| 50 | "@org_golang_x_tools//go/analysis/passes/stringintconv", |
| 51 | "@org_golang_x_tools//go/analysis/passes/structtag", |
| 52 | "@org_golang_x_tools//go/analysis/passes/testinggoroutine", |
| 53 | "@org_golang_x_tools//go/analysis/passes/tests", |
| 54 | "@org_golang_x_tools//go/analysis/passes/timeformat", |
| 55 | "@org_golang_x_tools//go/analysis/passes/unmarshal", |
| 56 | "@org_golang_x_tools//go/analysis/passes/unreachable", |
| 57 | "@org_golang_x_tools//go/analysis/passes/unsafeptr", |
| 58 | "@org_golang_x_tools//go/analysis/passes/unusedresult", |
| 59 | "@org_golang_x_tools//go/analysis/passes/unusedwrite", |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 60 | ] |
| 61 | |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 62 | # Append some passes provided by CockroachDB. |
| 63 | NOGO_PASSES += [ |
| 64 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errcmp", |
| Tim Windelschmidt | 5f1a7de | 2024-09-19 02:00:14 +0200 | [diff] [blame] | 65 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errwrap", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 66 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/hash", |
| 67 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nilness", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 68 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 69 | "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/unconvert", |
| 70 | ] |
| 71 | |
| 72 | # Combine all staticcheck analyzers with a list |
| 73 | # of all globally disabled staticcheck analyzers |
| 74 | # and append them to the nogo passes. |
| 75 | NOGO_PASSES += format_staticcheck_analyzers(ALL_STATICCHECK_ANALYZERS + [ |
| 76 | "-ST1000", # at least one file in a package should have a package comment |
| 77 | "-ST1003", # should not use ALL_CAPS in Go names; use CamelCase instead |
| 78 | "-QF1006", # could lift into loop condition |
| 79 | "-QF1003", # could use tagged switch |
| 80 | "-QF1008", # Omit embedded fields from selector expression |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 81 | ]) |
| 82 | |
| 83 | NOGO_PASSES += [ |
| 84 | # This analyzer ensures that all comment lines are <= 80 characters long |
| 85 | # in Go source. This is in line with general practices around the Go |
| 86 | # community, where code lines can be as long as needed (and is expected |
| 87 | # to be soft-reflowable by text editors), but comments are kept at a |
| 88 | # 'standard' 80 characters long, as prose within comment blocks does not |
| 89 | # soft-reflow well. |
| 90 | "@com_github_corverroos_commentwrap//:go_default_library", |
| 91 | "//build/analysis/checkcompilerdirectives", |
| 92 | "//build/analysis/noioutil", |
| 93 | "//build/analysis/importsort", |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 94 | "//build/analysis/gofmt", |
| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 95 | "//build/analysis/haslicense", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 96 | ] |
| 97 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 98 | # NOGO_CONFIG_OVERRIDES contains the overrides for nogo to exempt specific files |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 99 | # from being analyzed. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 100 | NOGO_CONFIG_OVERRIDES = { |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 101 | "shift": { |
| 102 | "exclude_files": { |
| 103 | "external/dev_gvisor_gvisor": "third_party", |
| 104 | }, |
| 105 | }, |
| 106 | "stringintconv": { |
| 107 | "exclude_files": { |
| 108 | "external/com_github_masterminds_goutils": "third_party", |
| 109 | }, |
| 110 | }, |
| 111 | "noioutil": { |
| 112 | "exclude_files": { |
| 113 | "external/": "TODO(tim): break me up and filter out unmaintained dependencies", |
| 114 | }, |
| 115 | }, |
| 116 | "nilness": { |
| 117 | "exclude_files": { |
| 118 | "external/org_golang_x_tools": "third_party", |
| 119 | "external/in_gopkg_yaml_v2": "third_party", |
| 120 | "external/com_github_google_cadvisor": "third_party", |
| 121 | "external/com_github_pkg_sftp": "third_party", |
| 122 | "external/com_github_vishvananda_netlink": "third_party", |
| 123 | "external/com_github_go_sql_driver_mysql": "third_party", |
| 124 | "external/com_github_google_go_tpm": "third_party", |
| 125 | "external/com_github_json_iterator_go": "third_party", |
| 126 | "external/com_github_gregjones_httpcache": "third_party", |
| 127 | "external/com_github_cilium_ebpf": "third_party", |
| 128 | "external/com_github_urfave_cli": "third_party", |
| 129 | "external/in_gopkg_square_go_jose_v2": "third_party", |
| 130 | "external/com_github_alecthomas_kingpin_v2": "third_party", |
| 131 | "external/io_k8s_mount_utils": "third_party", |
| 132 | "external/com_github_stefanberger_go_pkcs11uri": "third_party", |
| 133 | "external/com_github_go_delve_delve": "third_party", |
| 134 | "external/io_opencensus_go": "third_party", |
| 135 | "external/io_k8s_apimachinery": "third_party", |
| 136 | "external/io_k8s_kubernetes": "third_party", |
| 137 | "external/io_k8s_kube_openapi": "third_party", |
| 138 | "external/io_k8s_apiextensions_apiserver": "third_party", |
| 139 | "external/io_etcd_go_etcd_client_v3": "third_party", |
| 140 | "external/com_github_coredns_coredns": "third_party", |
| 141 | "external/io_etcd_go_etcd_server_v3": "third_party", |
| Lorenz Brun | 0ec0c53 | 2024-08-29 12:39:47 +0000 | [diff] [blame] | 142 | "external/com_github_containerd_containerd_v2": "third_party", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 143 | "external/io_k8s_client_go": "third_party", |
| 144 | "external/io_k8s_apiserver": "third_party", |
| 145 | "external/io_k8s_kubectl": "third_party", |
| 146 | "external/com_github_spf13_pflag": "third_party", |
| 147 | "external/com_github_burntsushi_toml": "third_party", |
| 148 | }, |
| 149 | }, |
| 150 | "unsafeptr": { |
| 151 | "exclude_files": { |
| 152 | "external/com_github_modern_go_reflect2/": "third_party", |
| 153 | "sqlite3.*go": "third_party", |
| 154 | "external/dev_gvisor_gvisor/": "third_party", |
| 155 | "external/io_k8s_sigs_structured_merge_diff/": "third_party", |
| 156 | "external/com_github_go_delve_delve/": "third_party", |
| 157 | "external/com_github_mailru_easyjson/jlexer/": "third_party", |
| 158 | "external/com_github_cilium_ebpf/": "third_party", |
| 159 | "external/org_golang_x_sys": "third_party", |
| 160 | "external/net_starlark_go": "third_party", |
| 161 | "external/com_github_pingcap_tidb_parser": "third_party", |
| 162 | "external/com_github_dennwc_btrfs": "third_party", |
| 163 | }, |
| 164 | }, |
| 165 | "lostcancel": { |
| 166 | "exclude_files": { |
| 167 | "external/org_golang_x_tools": "third_party", |
| 168 | "external/com_github_grpc_ecosystem_grpc_gateway": "third_party", |
| 169 | }, |
| 170 | }, |
| 171 | "deepequalerrors": { |
| 172 | "exclude_files": { |
| 173 | "external/com_github_u_root_uio": "third_party", |
| 174 | }, |
| 175 | }, |
| 176 | "copylocks": { |
| 177 | "exclude_files": { |
| 178 | "external/org_golang_google_protobuf": "third_party", |
| 179 | "external/com_github_derekparker_trie": "third_party", |
| 180 | "external/com_github_hodgesds_perf_utils": "third_party", |
| 181 | "external/com_github_google_gnostic": "third_party", |
| 182 | "external/com_github_coredns_coredns": "third_party", |
| 183 | "external/com_github_pseudomuto_protoc_gen_doc": "third_party", |
| 184 | "external/io_k8s_apiserver": "third_party", |
| 185 | }, |
| 186 | }, |
| 187 | "defers": { |
| 188 | "exclude_files": { |
| 189 | "external/com_github_sbezverk_nfproxy": "third_party", |
| 190 | }, |
| 191 | }, |
| 192 | "unparam": { |
| 193 | "exclude_files": { |
| 194 | "external/": "third_party", |
| 195 | "bazel-out/": "generated_output", |
| 196 | "cgo/": "cgo", |
| 197 | }, |
| 198 | }, |
| Lorenz Brun | e8beaed | 2025-02-05 22:03:50 +0100 | [diff] [blame] | 199 | "haslicense": { |
| 200 | "exclude_files": { |
| 201 | "metropolis/node/kubernetes/metricsprovider": "Multi-Party copyright statement (#376)", |
| 202 | "external/": "third_party", |
| 203 | "bazel-out/": "generated_output", |
| 204 | "cgo/": "cgo", |
| 205 | }, |
| 206 | }, |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | # All analyzers that should be disabled for external, generated or cgo code. |
| 210 | DISABLED_FOR_EXTERNAL_CODE = [ |
| 211 | "exclude_files", |
| 212 | "commentwrap", |
| 213 | "importsort", |
| 214 | "unreachable", |
| 215 | "unusedwrite", |
| 216 | "composites", |
| 217 | "stdmethods", |
| 218 | "reflectvaluecompare", |
| 219 | "unconvert", |
| 220 | "errwrap", |
| 221 | "ruleguard", |
| 222 | "returnerrcheck", |
| 223 | "hash", |
| 224 | "errcmp", |
| Tim Windelschmidt | 99e1511 | 2025-02-05 17:38:16 +0100 | [diff] [blame] | 225 | "gofmt", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 226 | ] + ALL_STATICCHECK_ANALYZERS |
| 227 | |
| 228 | # We override the variable with itself unioned with the other |
| 229 | # config part, as the Intellij integration doesn't understand |
| 230 | # the |= expression which makes editing this file kinda annoying. |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 231 | NOGO_CONFIG_EXTERNAL = { |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 232 | analyzer: { |
| 233 | "exclude_files": { |
| 234 | # Don't run linters on external dependencies |
| 235 | "external/": "third_party", |
| 236 | "bazel-out/": "generated_output", |
| 237 | "cgo/": "cgo", |
| 238 | }, |
| 239 | } |
| 240 | for analyzer in DISABLED_FOR_EXTERNAL_CODE |
| 241 | } |
| 242 | |
| Tim Windelschmidt | 156248b | 2025-01-10 00:27:45 +0100 | [diff] [blame] | 243 | NOGO_CONFIG = NOGO_CONFIG_OVERRIDES | NOGO_CONFIG_EXTERNAL |
| 244 | |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 245 | write_file( |
| 246 | name = "nogo_config", |
| 247 | out = "nogo_config.json", |
| 248 | content = [json.encode_indent(NOGO_CONFIG)], |
| 249 | ) |
| 250 | |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 251 | nogo( |
| 252 | name = "nogo", |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 253 | config = ":nogo_config", |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 254 | visibility = ["//visibility:public"], |
| Tim Windelschmidt | e2b2865 | 2024-04-12 16:49:16 +0200 | [diff] [blame] | 255 | deps = NOGO_PASSES, |
| Serge Bazanski | 0ef9629 | 2021-05-21 15:41:32 +0200 | [diff] [blame] | 256 | ) |