Skip to content

🌟 Let's talk about "formatters" #5296

@ldez

Description

@ldez

Important

This is a proposal: I don't know if it is possible and what the impact could be inside the code.
The proposal may evolve.


Inside golangci-lint there are 4 "linters" that are not real linters/analyzers but "formatters": gofmt, goimports, gofumpt, and gci.

Their standard outputs are []byte and not Diagnostic, internally golangci-lint produces diff patches based on []byte and converts them into Diagnostic.

Also goimports and gci are not "import formatters": they format all the code and not only imports.

tool format imports order fix imports rewrite-rules simplify extra
gofmt x x (alphabetically) x x
goimports x x (multiple imports) x
gofumpt x x (one import) x x
gci x x (customizable)

⛑️ The Problems

Types

The formatters don't require types, but when they are run with other linters that require types, the possibilities of formaters to apply format and fix the code are limited.

Today, for example, you can use the following command to apply the format without being constraint by the other linters:

golangci-lint run --enable-only="gofumpt,goimports" --fix

It works but this is limited.

💭 The Proposal

The idea is to create a dedicated section formatters like linters (internally they will be used as the other linters).

And adds a specific command fmt that runs only the formatters defined inside the configuration, like a variant of the run command but limited to formatters and with "autofix" always enabled, so no reports.

It allows us to clarify the difference between the formatters and the linters.

Also, we can set up a dedicated default for this section.
I suggest goimports as default because this is an official Go tool, more powerful than gofmt, and fits the needs of many projects.

formatters:
  enable:
    - gofumpt
    - gofmt
    - goimports
    - gci
  settings:
    gofumpt:
      opt1: true
    # ...

Enabling all the formatters make no sense as they provide the same base functionality (format) with some variants but their changes could be incompatible (mainly on imports) unless they are run in a specific order and without incompatible settings. So no need for enable-all and/or a disable section.

Metadata

Metadata

Assignees

Labels

area: configRelated to .golangci.yml and/or cli optionsproposal

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions