Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gofmt can't be used as a linter #5627

Closed
6 of 7 tasks
mhemeryck opened this issue Mar 25, 2025 · 3 comments
Closed
6 of 7 tasks

gofmt can't be used as a linter #5627

mhemeryck opened this issue Mar 25, 2025 · 3 comments
Labels
area: formatters/fmt question Further information is requested

Comments

@mhemeryck
Copy link

Welcome

  • Yes, I'm using a binary release within 2 latest releases. Only such installations are supported.
  • Yes, I've searched similar issues on GitHub and didn't find any.
  • Yes, I've read the typecheck section of the FAQ.
  • Yes, I've tried with the standalone linter if available (e.g., gocritic, go vet, etc.).
  • I agree to follow this project's Code of Conduct

How did you install golangci-lint?

Docker

Description of the problem

We were using the golangci linter as part of a CI/CD pipeline against a moving docker image tag.

Consequently, when v2 was made available, all of a sudden, our job started failing.

I did try to migrate our current setup over to v2, but I did notice that our current code fails for the case where we configure gofmt as part of the linter.

Full setup; invocation:

golangci-lint run -v

config file:

version: "2"

linters:
  default: none
  enable:
    - gofmt
    - gosec
run:
  timeout: 10m
  issues-exit-code: 0
output:
  formats:
    text:
      print-issued-lines: false
    code-climate:
      path: gl-code-quality-report.json

Error message:

Error: can't load config: gofmt is a formatter
Failed executing command with error: can't load config: gofmt is a formatter

Version info: we used golangci/golangci-lint:latest-alpine the logs mention (redacted):

level=info msg="golangci-lint has version 2.0.1 built with go1.24.1 from e8927ce2 on 2025-03-24T20:34:31Z"
...
Using docker image sha256:80a23913b76757104bb082e05fc7521ec5c4dd548071433410b1ac44511be8ba for golangci/golangci-lint:latest-alpine with digest golangci/golangci-lint@sha256:66854a432087d43cee95e82406b895f93a2cf6448e3edc67f46ce057dab07c7a ...
..

My guess is that the issue lies here:

if slices.Contains(getAllFormatterNames(), n) {

The code checks whether linter isn't a formatter. For gofmt, it's actually both a formatter and a linter, hence the issue.

Version of golangci-lint

$ golangci-lint --version
golangci-lint has version 2.0.1 built with go1.24.1 from e8927ce2 on 2025-03-24T20:34:31Z

Configuration

version: "2"

linters:
  default: none
  enable:
    - gofmt
    - gosec
run:
  timeout: 10m
  issues-exit-code: 0
output:
  formats:
    text:
      print-issued-lines: false
    code-climate:
      path: gl-code-quality-report.json

Go environment

$ go version && go env
# paste output here

Verbose output of running

level=info msg="[config_reader] Used config file .golangci.yml"
Error: can't load config: gofmt is a formatter
Failed executing command with error: can't load config: gofmt is a formatter

A minimal reproducible example or link to a public repository

// add your code here

Validation

  • Yes, I've included all information above (version, config, etc.).

Supporter

@mhemeryck mhemeryck added the bug Something isn't working label Mar 25, 2025
Copy link

boring-cyborg bot commented Mar 25, 2025

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

@ldez ldez closed this as completed Mar 25, 2025
@ldez ldez added area: formatters/fmt question Further information is requested and removed bug Something isn't working labels Mar 25, 2025
@ldez
Copy link
Member

ldez commented Mar 25, 2025

Hello,

gofmt cannot be defined in the linters section.

There is a dedicated formatters section.

I recommend using the migration command.

I also recommend reading my article.

@ldez ldez changed the title gofmt can't be used as alinter gofmt can't be used as a linter Mar 25, 2025
@mhemeryck
Copy link
Author

mhemeryck commented Mar 25, 2025

Thank you for taking the time to explain this; it indeed now works!

I did try at first to use the migration tool, but I only had access to the tool in the CI and no easy way to run it locally.

btw, another thing I missed in the docs was the requirement for a version field in the new config file format, wasn't obvious to me from https://golangci-lint.run/usage/configuration/

Anyways, the issue is resolved for me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: formatters/fmt question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants