Skip to content

lint: replace gocyclo with gocognit - #14055

Closed
ndeloof wants to merge 3 commits into
docker:mainfrom
ndeloof:gocognit-eval
Closed

lint: replace gocyclo with gocognit#14055
ndeloof wants to merge 3 commits into
docker:mainfrom
ndeloof:gocognit-eval

Conversation

@ndeloof

@ndeloof ndeloof commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

What I did

Replaced the gocyclo linter with gocognit (cognitive complexity, threshold 30 — the linter default). Cognitive complexity barely charges guard clauses and early returns, and penalizes nesting instead, which matches how this codebase is written: 8 of the 21 //nolint:gocyclo suppressions become unnecessary, while deeply nested functions gocyclo never flagged are now caught. The 19 functions still above the threshold keep a suppression, each marked FIXME so they can be restructured over time to complete the migration.

includes refactoring of doBuildBake (hot spot after migration to gocognit, never identified by gocyclo)

Related issue

n/a

🤖 Generated with Claude Code

Cognitive complexity (gocognit, threshold 30 — the linter default) fits
this codebase better than raw cyclomatic complexity: it barely charges
guard clauses and early returns, and penalizes nesting instead. As a
result 8 of the 21 //nolint:gocyclo suppressions become unnecessary,
while deeply nested functions gocyclo never flagged are now caught.

The 19 functions still above the threshold keep a suppression, each
marked FIXME to complete the migration by restructuring them.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
@ndeloof
ndeloof requested review from a team as code owners August 17, 2026 06:23
@ndeloof
ndeloof requested a review from glours August 17, 2026 06:23

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assessment: 🟢 APPROVE

This PR is a clean mechanical replacement of gocyclo with gocognit in the linter configuration. The changes are well-structured:

  • .golangci.yml: gocognit correctly enabled with min-complexity: 30 (the linter default); gocyclo fully removed.
  • 8 suppressions removed from functions whose gocognit score is now below 30 — consistent with gocognit's guard-clause-friendly scoring that penalizes nesting over linear branching.
  • 19 suppressions retained (converted from //nolint:gocyclo to //nolint:gocognit) each with a FIXME for future refactoring — count matches the PR description.
  • Nolint directive placement: All //nolint:gocognit directives appear immediately before their respective func declarations with no intervening blank lines, satisfying golangci-lint's directive-recognition rules.

No bugs introduced by this PR.

thaJeztah
thaJeztah previously approved these changes Aug 17, 2026

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice!

Does golangci have an explicit "disable" section as well? This may be a case where putting gocyclo as explicitly disabled with a short comment could prevent someone adding it back in future.

doBuildBake mixed five concerns in a 300-line body (cognitive
complexity 88): progress display setup, translation of the project
into a bake file definition, temp metadata file allocation, bake
command construction, stderr rawjson streaming, and result
collection. Each now lives in its own function; the driver reads as
the sequence of those stages (cognitive complexity 17), and the
FIXME suppression is gone.

The bake variable was named to avoid shadowing the docker/cli
'build' package. No behavior change: prepareBakeBuild emits the same
config, and the stderr loop keeps the decoder-per-line semantics.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
gocognit replaced gocyclo on purpose: raw cyclomatic complexity
charges a flat guard clause the same as a deeply nested branch, so our
fail-fast style accumulated //nolint suppressions on functions that
are long but flat. Cognitive complexity penalizes nesting and barely
charges early returns, which keeps the linter's signal on genuinely
tangled code instead.

With 'default: none' the disable entry is functionally redundant, but
it records that decision exactly where someone would re-add the
linter, with a hard stop: golangci-lint rejects a config listing the
same linter in both enable and disable.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
@ndeloof

ndeloof commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #14057, recreated from an in-repo branch so the follow-up fix could be opened as a stacked PR (#14058). No content change.

@ndeloof ndeloof closed this Aug 17, 2026
@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.23129% with 32 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/compose/build_bake.go 76.81% 21 Missing and 11 partials ⚠️

📢 Thoughts on this report? Let us know!

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.

3 participants