blob: b97a58e0c6d3552247d9d3b6ce1e3d34f1ca13a0 [file] [log] [blame]
Tim Windelschmidte2b28652024-04-12 16:49:16 +02001load("@bazel_skylib//rules:write_file.bzl", "write_file")
Tim Windelschmidt156248b2025-01-10 00:27:45 +01002load("@com_github_sluongng_nogo_analyzer//staticcheck:def.bzl", ALL_STATICCHECK_ANALYZERS = "ANALYZERS", format_staticcheck_analyzers = "staticcheck_analyzers")
Serge Bazanski0ef96292021-05-21 15:41:32 +02003load("@io_bazel_rules_go//go:def.bzl", "nogo")
4
Tim Windelschmidte2b28652024-04-12 16:49:16 +02005# NOGO_PASSES contains all enabled analyzers that nogo should execute.
6NOGO_PASSES = []
7
Serge Bazanski0ef96292021-05-21 15:41:32 +02008# 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 Windelschmidte2b28652024-04-12 16:49:16 +020012NOGO_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 Bazanski0ef96292021-05-21 15:41:32 +020021 # Disable cgocall because it fails processing com_github_mattn_go_sqlite3 before exclusions are applied
Tim Windelschmidte2b28652024-04-12 16:49:16 +020022 #"@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 Bazanski0ef96292021-05-21 15:41:32 +020060]
61
Tim Windelschmidte2b28652024-04-12 16:49:16 +020062# Append some passes provided by CockroachDB.
63NOGO_PASSES += [
64 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errcmp",
Tim Windelschmidt5f1a7de2024-09-19 02:00:14 +020065 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/errwrap",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020066 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/hash",
67 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/nilness",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020068 "@com_github_cockroachdb_cockroach//pkg/testutils/lint/passes/returnerrcheck",
Tim Windelschmidte2b28652024-04-12 16:49:16 +020069 "@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.
75NOGO_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 Windelschmidte2b28652024-04-12 16:49:16 +020081])
82
83NOGO_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 Windelschmidt7c0bd0b2025-01-10 04:15:37 +010094 # "//build/analysis/gofmt", # TODO(tim): Enable when formatted
Tim Windelschmidte2b28652024-04-12 16:49:16 +020095]
96
Tim Windelschmidt156248b2025-01-10 00:27:45 +010097# NOGO_CONFIG_OVERRIDES contains the overrides for nogo to exempt specific files
Tim Windelschmidte2b28652024-04-12 16:49:16 +020098# from being analyzed.
Tim Windelschmidt156248b2025-01-10 00:27:45 +010099NOGO_CONFIG_OVERRIDES = {
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200100 "shift": {
101 "exclude_files": {
102 "external/dev_gvisor_gvisor": "third_party",
103 },
104 },
105 "stringintconv": {
106 "exclude_files": {
107 "external/com_github_masterminds_goutils": "third_party",
108 },
109 },
110 "noioutil": {
111 "exclude_files": {
112 "external/": "TODO(tim): break me up and filter out unmaintained dependencies",
113 },
114 },
115 "nilness": {
116 "exclude_files": {
117 "external/org_golang_x_tools": "third_party",
118 "external/in_gopkg_yaml_v2": "third_party",
119 "external/com_github_google_cadvisor": "third_party",
120 "external/com_github_pkg_sftp": "third_party",
121 "external/com_github_vishvananda_netlink": "third_party",
122 "external/com_github_go_sql_driver_mysql": "third_party",
123 "external/com_github_google_go_tpm": "third_party",
124 "external/com_github_json_iterator_go": "third_party",
125 "external/com_github_gregjones_httpcache": "third_party",
126 "external/com_github_cilium_ebpf": "third_party",
127 "external/com_github_urfave_cli": "third_party",
128 "external/in_gopkg_square_go_jose_v2": "third_party",
129 "external/com_github_alecthomas_kingpin_v2": "third_party",
130 "external/io_k8s_mount_utils": "third_party",
131 "external/com_github_stefanberger_go_pkcs11uri": "third_party",
132 "external/com_github_go_delve_delve": "third_party",
133 "external/io_opencensus_go": "third_party",
134 "external/io_k8s_apimachinery": "third_party",
135 "external/io_k8s_kubernetes": "third_party",
136 "external/io_k8s_kube_openapi": "third_party",
137 "external/io_k8s_apiextensions_apiserver": "third_party",
138 "external/io_etcd_go_etcd_client_v3": "third_party",
139 "external/com_github_coredns_coredns": "third_party",
140 "external/io_etcd_go_etcd_server_v3": "third_party",
Lorenz Brun0ec0c532024-08-29 12:39:47 +0000141 "external/com_github_containerd_containerd_v2": "third_party",
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200142 "external/io_k8s_client_go": "third_party",
143 "external/io_k8s_apiserver": "third_party",
144 "external/io_k8s_kubectl": "third_party",
145 "external/com_github_spf13_pflag": "third_party",
146 "external/com_github_burntsushi_toml": "third_party",
147 },
148 },
149 "unsafeptr": {
150 "exclude_files": {
151 "external/com_github_modern_go_reflect2/": "third_party",
152 "sqlite3.*go": "third_party",
153 "external/dev_gvisor_gvisor/": "third_party",
154 "external/io_k8s_sigs_structured_merge_diff/": "third_party",
155 "external/com_github_go_delve_delve/": "third_party",
156 "external/com_github_mailru_easyjson/jlexer/": "third_party",
157 "external/com_github_cilium_ebpf/": "third_party",
158 "external/org_golang_x_sys": "third_party",
159 "external/net_starlark_go": "third_party",
160 "external/com_github_pingcap_tidb_parser": "third_party",
161 "external/com_github_dennwc_btrfs": "third_party",
162 },
163 },
164 "lostcancel": {
165 "exclude_files": {
166 "external/org_golang_x_tools": "third_party",
167 "external/com_github_grpc_ecosystem_grpc_gateway": "third_party",
168 },
169 },
170 "deepequalerrors": {
171 "exclude_files": {
172 "external/com_github_u_root_uio": "third_party",
173 },
174 },
175 "copylocks": {
176 "exclude_files": {
177 "external/org_golang_google_protobuf": "third_party",
178 "external/com_github_derekparker_trie": "third_party",
179 "external/com_github_hodgesds_perf_utils": "third_party",
180 "external/com_github_google_gnostic": "third_party",
181 "external/com_github_coredns_coredns": "third_party",
182 "external/com_github_pseudomuto_protoc_gen_doc": "third_party",
183 "external/io_k8s_apiserver": "third_party",
184 },
185 },
186 "defers": {
187 "exclude_files": {
188 "external/com_github_sbezverk_nfproxy": "third_party",
189 },
190 },
191 "unparam": {
192 "exclude_files": {
193 "external/": "third_party",
194 "bazel-out/": "generated_output",
195 "cgo/": "cgo",
196 },
197 },
198}
199
200# All analyzers that should be disabled for external, generated or cgo code.
201DISABLED_FOR_EXTERNAL_CODE = [
202 "exclude_files",
203 "commentwrap",
204 "importsort",
205 "unreachable",
206 "unusedwrite",
207 "composites",
208 "stdmethods",
209 "reflectvaluecompare",
210 "unconvert",
211 "errwrap",
212 "ruleguard",
213 "returnerrcheck",
214 "hash",
215 "errcmp",
216] + ALL_STATICCHECK_ANALYZERS
217
218# We override the variable with itself unioned with the other
219# config part, as the Intellij integration doesn't understand
220# the |= expression which makes editing this file kinda annoying.
Tim Windelschmidt156248b2025-01-10 00:27:45 +0100221NOGO_CONFIG_EXTERNAL = {
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200222 analyzer: {
223 "exclude_files": {
224 # Don't run linters on external dependencies
225 "external/": "third_party",
226 "bazel-out/": "generated_output",
227 "cgo/": "cgo",
228 },
229 }
230 for analyzer in DISABLED_FOR_EXTERNAL_CODE
231}
232
Tim Windelschmidt156248b2025-01-10 00:27:45 +0100233NOGO_CONFIG = NOGO_CONFIG_OVERRIDES | NOGO_CONFIG_EXTERNAL
234
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200235write_file(
236 name = "nogo_config",
237 out = "nogo_config.json",
238 content = [json.encode_indent(NOGO_CONFIG)],
239)
240
Serge Bazanski0ef96292021-05-21 15:41:32 +0200241nogo(
242 name = "nogo",
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200243 config = ":nogo_config",
Serge Bazanski0ef96292021-05-21 15:41:32 +0200244 visibility = ["//visibility:public"],
Tim Windelschmidte2b28652024-04-12 16:49:16 +0200245 deps = NOGO_PASSES,
Serge Bazanski0ef96292021-05-21 15:41:32 +0200246)