Skip to content

ci(lint): add hermetic gocognit cognitive-complexity aspect - #555

Merged
bpalermo merged 1 commit into
mainfrom
ci/gocognit-lint-aspect
Jul 19, 2026
Merged

bpalermo merged 1 commit into
mainfrom
ci/gocognit-lint-aspect

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

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=ci alongside the existing buf/buildifier/shellcheck aspects.

Added / changed

  • go.mod: gocognit pulled into the module graph via a tool github.com/uudashr/gocognit/cmd/gocognit directive (survives bazel mod tidy — no tools.go fallback needed). Repo @com_github_uudashr_gocognit added to MODULE.bazel use_repo. Binary target: @com_github_uudashr_gocognit//cmd/gocognit.
  • bazel/lint/gocognit.bzl (new): lint_gocognit_aspect factory visiting go_library/go_binary/go_test, running gocognit -over <threshold>. Skips generated sources (*.pb.go, *_pb.go, zz_generated*, mocks, testdata).
  • bazel/lint/linters.bzl: registers the gocognit aspect + gocognit_test = lint_test(...).
  • .bazelrc: appends //bazel/lint:linters.bzl%gocognit to the build:lint aspects line.

Contract & upgrade safety

Reproduces aspect_rules_lint's output contract (rules_lint_human/rules_lint_machine/_validation output groups; .out/.report reports always produced; .exit_code sidecars when fail_on_violation is off, action exit code when on). The small amount of helper logic is vendored rather than loaded from lint:private/... (upgrade safety); fail_on_violation is read via the public @aspect_rules_lint//lint:fail_on_violation bool_flag's BuildSettingInfo.

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 N already 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 in gocognit.bzl.

Baseline findings (functions over 20, the ratchet backlog)

  • 109 — gatewayapi (*Reconciler).buildVirtualHost (./agent/internal/edge/gatewayapi/reconciler.go:600:1)
  • 98 — cache (*SnapshotCache).LoadClustersFromRegistry (./agent/internal/xds/cache/cluster.go:152:1)
  • 75 — server (*CNIServer).sweepGhostEndpoints (./agent/internal/cni/server/ghostsweep.go:111:1)
  • 68 — hotrestart (*Supervisor).Run (./agent/internal/proxy/hotrestart/supervisor.go:216:1)
  • 68 — extensionfilter ValidateSpec (./common/extensionfilter/extensionfilter.go:93:1)
  • 62 — gatewayapi (*Reconciler).Reconcile (./agent/internal/edge/gatewayapi/reconciler.go:170:1)
  • 59 — gamma (*Reconciler).Reconcile (./agent/internal/gamma/reconciler.go:142:1)
  • 58 — cmd runRegistrar (./registrar/internal/cmd/root.go:90:1)
  • 51 — cmd runAgent (./agent/internal/cmd/root.go:196:1)
  • 44 — configexport (*Controller).Reconcile (./registrar/internal/configexport/controller.go:78:1)
  • 42 — l4route (*Reconciler).Reconcile (./agent/internal/l4route/reconciler.go:136:1)
  • 42 — e2e TestAgentDaemonSet (./test/e2e/agent_test.go:18:1)
  • 41 — cache (*SnapshotCache).rebuildEdgeDependencies (./agent/internal/xds/cache/edge.go:695:1)
  • 39 — server (*CNIServer).reconcileLiveness (./agent/internal/cni/server/liveness.go:82:1)
  • 39 — gatewayapi (*Reconciler).writeGatewayStatuses (./agent/internal/edge/gatewayapi/status.go:71:1)
  • 39 — cache (*SnapshotCache).buildEdgeVhostsLocked (./agent/internal/xds/cache/edge.go:427:1)
  • 37 — gammaproject ProjectGRPCRule (./common/gammaproject/project.go:104:1)
  • 36 — hotrestart (*Supervisor).watchLiveness (./agent/internal/proxy/hotrestart/coordination.go:159:1)
  • 35 — server (*RegistrarServer).WatchEndpoints (./registrar/internal/server/server.go:189:1)
  • 35 — conformance_test prewarmNamespaces (./test/conformance/conformance_test.go:205:1)
  • 34 — spire (*Bridge).SubscribePod (./agent/internal/spire/bridge.go:294:1)
  • 34 — spire (*Bridge).Start (./agent/internal/spire/bridge.go:160:1)
  • 33 — cache (*SnapshotCache).dependencySetLocked (./agent/internal/xds/cache/dependencies.go:176:1)
  • 32 — file TestWriteFileAtomic (./common/file/file_test.go:9:1)
  • 32 — cmd runEdge (./agent/internal/cmd/edge.go:69:1)
  • 32 — cache (*SnapshotCache).edgeTCPClusters (./agent/internal/xds/cache/capture.go:289:1)
  • 32 — cache (*SnapshotCache).edgeK8sBackendClusters (./agent/internal/xds/cache/edge.go:806:1)
  • 29 — registrar (*RegistrarRegistry).processStream (./registry/internal/registrar/registrar.go:486:1)
  • 28 — e2e TestPodDeletionCleanup (./test/e2e/cni_test.go:220:1)
  • 28 — attachment GatewayParentPorts (./agent/internal/edge/gatewayapi/attachment/refs.go:94:1)
  • 28 — attachment EffectiveHostnames (./agent/internal/edge/gatewayapi/attachment/hostnames.go:96:1)
  • 26 — attachment (*Resolver).httpRouteAttachment (./agent/internal/edge/gatewayapi/attachment/attachment.go:253:1)
  • 25 — proxy resolveCatchAll (./agent/internal/xds/proxy/route_test.go:18:1)
  • 25 — gatewayapi buildHTTPRedirect (./agent/internal/edge/gatewayapi/reconciler.go:1000:1)
  • 24 — configimport (*Importer).materialize (./agent/internal/configimport/importer.go:105:1)
  • 24 — cache equalGammaHeaderMutation (./agent/internal/xds/cache/gamma.go:265:1)
  • 23 — server (*WriteBehindQueue).flushDue (./registrar/internal/server/writebehind.go:135:1)
  • 23 — server (*RegistryRefresher).Start (./agent/internal/xds/server/refresh.go:109:1)
  • 23 — gatewayapi (*Reconciler).resolveListenerTLS (./agent/internal/edge/gatewayapi/reconciler.go:518:1)
  • 23 — gatewayapi (*Reconciler).reconcileGatewayServices (./agent/internal/edge/gatewayapi/service.go:201:1)
  • 23 — gatewayapi buildEdgeHTTPHeaderMutation (./agent/internal/edge/gatewayapi/reconciler.go:1073:1)
  • 23 — e2e TestManagedPodRegistration (./test/e2e/cni_test.go:21:1)
  • 22 — hotrestart metricValue (./agent/internal/proxy/hotrestart/metrics_test.go:25:1)
  • 22 — gammaproject ProjectHTTPRule (./common/gammaproject/project.go:65:1)
  • 22 — e2e TestUnmanagedPodIgnored (./test/e2e/cni_test.go:103:1)
  • 22 — cache equalRoutes (./agent/internal/xds/cache/edge.go:905:1)
  • 21 — services (*Generator).reconcile (./registrar/internal/services/generator.go:74:1)
  • 21 — gatewayapi allocateGatewayListenerPorts (./agent/internal/edge/gatewayapi/service.go:111:1)
  • 21 — 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.
  • Negative test: temporarily lowered threshold to -over 5:
    • without fail_on_violation: build succeeds, report lists violations (e.g. file: AtomicWriteReader 16, WriteFileAtomic 14) and .out.exit_code = 1.
    • with fail_on_violation: build FAILS (Linting //common/file:file with gocognit failed: (Exit 1)).
    • Threshold restored to 109 afterward.
  • bazel run //:format — clean (no changes to the new Starlark).

🤖 Generated with Claude Code

https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR

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
@bpalermo
bpalermo merged commit a789264 into main Jul 19, 2026
10 checks passed
@bpalermo
bpalermo deleted the ci/gocognit-lint-aspect branch July 19, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant