Repository navigation
ci(lint): add hermetic gocognit cognitive-complexity aspect - #555
Merged
Merged
Conversation
Add a hermetic Bazel lint aspect that runs gocognit (github.com/uudashr/gocognit) over first-party Go sources, reporting functions whose cognitive complexity exceeds a threshold. - Pull gocognit into the module graph via a go.mod `tool` directive (repo @com_github_uudashr_gocognit); the binary target is @com_github_uudashr_gocognit//cmd/gocognit. - New bazel/lint/gocognit.bzl: lint_gocognit_aspect factory that mirrors aspect_rules_lint's output-file / exit-code / output-group contract (rules_lint_human, rules_lint_machine, _validation) so fail_on_violation and lint_test work. The small amount of helper logic is vendored rather than loaded from lint:private (upgrade safety); fail_on_violation is read via the public bool_flag's BuildSettingInfo. - Skips generated sources (*.pb.go, *_pb.go, zz_generated*, mocks, testdata). - Registers the aspect + gocognit_test in bazel/lint/linters.bzl and appends it to the build:lint aspects in .bazelrc. Threshold is a ratchet at 109 (smallest value >= 20 with zero current violations); the ideal target is 20, blocked by an existing backlog of complex functions (refactor is a separate decision). Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR
This was referenced Jul 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a hermetic Bazel lint aspect that runs gocognit over first-party Go sources, reporting functions whose cognitive complexity exceeds a threshold. Wired into
--config=lint/--config=cialongside the existing buf/buildifier/shellcheck aspects.Added / changed
tool github.com/uudashr/gocognit/cmd/gocognitdirective (survivesbazel mod tidy— no tools.go fallback needed). Repo@com_github_uudashr_gocognitadded toMODULE.bazeluse_repo. Binary target:@com_github_uudashr_gocognit//cmd/gocognit.lint_gocognit_aspectfactory visitinggo_library/go_binary/go_test, runninggocognit -over <threshold>. Skips generated sources (*.pb.go,*_pb.go,zz_generated*, mocks,testdata).gocognitaspect +gocognit_test = lint_test(...).//bazel/lint:linters.bzl%gocognitto thebuild:lintaspects line.Contract & upgrade safety
Reproduces aspect_rules_lint's output contract (
rules_lint_human/rules_lint_machine/_validationoutput groups;.out/.reportreports always produced;.exit_codesidecars whenfail_on_violationis off, action exit code when on). The small amount of helper logic is vendored rather than loaded fromlint:private/...(upgrade safety);fail_on_violationis read via the public@aspect_rules_lint//lint:fail_on_violationbool_flag'sBuildSettingInfo.Deviation from plan
The plan assumed "gocognit itself exits 0 on findings" and that the runner must invert the exit code. In v1.2.1,
gocognit -over Nalready exits 1 iff its output is non-empty and 0 otherwise. So no inversion is needed — the same success/exit-code-capture shell wrapper the upstream shellcheck/buf aspects use works verbatim.Threshold + ratchet rationale
The ideal target is 20 (gocognit's documented recommendation), but the repo currently has 49 functions over 20 (worst case 109) — far more than the 5-function refactor budget. Per policy, the threshold is set to the smallest value ≥ 20 that yields zero current violations:
-over 109. Refactoring the backlog is a separate decision; lower the threshold as complexity is paid down (the goal is-over 20). This is recorded in a comment ingocognit.bzl.Baseline findings (functions over 20, the ratchet backlog)
gatewayapi (*Reconciler).buildVirtualHost(./agent/internal/edge/gatewayapi/reconciler.go:600:1)cache (*SnapshotCache).LoadClustersFromRegistry(./agent/internal/xds/cache/cluster.go:152:1)server (*CNIServer).sweepGhostEndpoints(./agent/internal/cni/server/ghostsweep.go:111:1)hotrestart (*Supervisor).Run(./agent/internal/proxy/hotrestart/supervisor.go:216:1)extensionfilter ValidateSpec(./common/extensionfilter/extensionfilter.go:93:1)gatewayapi (*Reconciler).Reconcile(./agent/internal/edge/gatewayapi/reconciler.go:170:1)gamma (*Reconciler).Reconcile(./agent/internal/gamma/reconciler.go:142:1)cmd runRegistrar(./registrar/internal/cmd/root.go:90:1)cmd runAgent(./agent/internal/cmd/root.go:196:1)configexport (*Controller).Reconcile(./registrar/internal/configexport/controller.go:78:1)l4route (*Reconciler).Reconcile(./agent/internal/l4route/reconciler.go:136:1)e2e TestAgentDaemonSet(./test/e2e/agent_test.go:18:1)cache (*SnapshotCache).rebuildEdgeDependencies(./agent/internal/xds/cache/edge.go:695:1)server (*CNIServer).reconcileLiveness(./agent/internal/cni/server/liveness.go:82:1)gatewayapi (*Reconciler).writeGatewayStatuses(./agent/internal/edge/gatewayapi/status.go:71:1)cache (*SnapshotCache).buildEdgeVhostsLocked(./agent/internal/xds/cache/edge.go:427:1)gammaproject ProjectGRPCRule(./common/gammaproject/project.go:104:1)hotrestart (*Supervisor).watchLiveness(./agent/internal/proxy/hotrestart/coordination.go:159:1)server (*RegistrarServer).WatchEndpoints(./registrar/internal/server/server.go:189:1)conformance_test prewarmNamespaces(./test/conformance/conformance_test.go:205:1)spire (*Bridge).SubscribePod(./agent/internal/spire/bridge.go:294:1)spire (*Bridge).Start(./agent/internal/spire/bridge.go:160:1)cache (*SnapshotCache).dependencySetLocked(./agent/internal/xds/cache/dependencies.go:176:1)file TestWriteFileAtomic(./common/file/file_test.go:9:1)cmd runEdge(./agent/internal/cmd/edge.go:69:1)cache (*SnapshotCache).edgeTCPClusters(./agent/internal/xds/cache/capture.go:289:1)cache (*SnapshotCache).edgeK8sBackendClusters(./agent/internal/xds/cache/edge.go:806:1)registrar (*RegistrarRegistry).processStream(./registry/internal/registrar/registrar.go:486:1)e2e TestPodDeletionCleanup(./test/e2e/cni_test.go:220:1)attachment GatewayParentPorts(./agent/internal/edge/gatewayapi/attachment/refs.go:94:1)attachment EffectiveHostnames(./agent/internal/edge/gatewayapi/attachment/hostnames.go:96:1)attachment (*Resolver).httpRouteAttachment(./agent/internal/edge/gatewayapi/attachment/attachment.go:253:1)proxy resolveCatchAll(./agent/internal/xds/proxy/route_test.go:18:1)gatewayapi buildHTTPRedirect(./agent/internal/edge/gatewayapi/reconciler.go:1000:1)configimport (*Importer).materialize(./agent/internal/configimport/importer.go:105:1)cache equalGammaHeaderMutation(./agent/internal/xds/cache/gamma.go:265:1)server (*WriteBehindQueue).flushDue(./registrar/internal/server/writebehind.go:135:1)server (*RegistryRefresher).Start(./agent/internal/xds/server/refresh.go:109:1)gatewayapi (*Reconciler).resolveListenerTLS(./agent/internal/edge/gatewayapi/reconciler.go:518:1)gatewayapi (*Reconciler).reconcileGatewayServices(./agent/internal/edge/gatewayapi/service.go:201:1)gatewayapi buildEdgeHTTPHeaderMutation(./agent/internal/edge/gatewayapi/reconciler.go:1073:1)e2e TestManagedPodRegistration(./test/e2e/cni_test.go:21:1)hotrestart metricValue(./agent/internal/proxy/hotrestart/metrics_test.go:25:1)gammaproject ProjectHTTPRule(./common/gammaproject/project.go:65:1)e2e TestUnmanagedPodIgnored(./test/e2e/cni_test.go:103:1)cache equalRoutes(./agent/internal/xds/cache/edge.go:905:1)services (*Generator).reconcile(./registrar/internal/services/generator.go:74:1)gatewayapi allocateGatewayListenerPorts(./agent/internal/edge/gatewayapi/service.go:111:1)cache equalEdgeTLSRoutes(./agent/internal/xds/cache/edge.go:1078:1)Validation
bazel build --config=lint //...— green at threshold 109.bazel build --config=lint --@aspect_rules_lint//lint:fail_on_violation //...(CI lint semantics) — green at threshold 109.-over 5:fail_on_violation: build succeeds, report lists violations (e.g.file:AtomicWriteReader16,WriteFileAtomic14) and.out.exit_code=1.fail_on_violation: build FAILS (Linting //common/file:file with gocognit failed: (Exit 1)).bazel run //:format— clean (no changes to the new Starlark).🤖 Generated with Claude Code
https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR