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

feat: new fmt command with dedicated formatter configuration #5357

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

ldez
Copy link
Member

@ldez ldez commented Jan 28, 2025

Important

This PR deliberately omits documentation about the new options and section (but the JSONSchema is updated).
This is because I don't want to deprecate the previous elements for now: I think it is better to handle that when all the v2 proposals are managed to avoid multiple migrations.


The command golangci-lint fmt always formats the code (no diff, no preview, no reports).

The current default is the same format used by go fmt (!= gofmt).

There is no support for stdin.

The formatters defined in the formatters.enable are added automatically to linters.enable.

If a formatter is enabled inside formatters and linters, the configuration of formatters.settings will override linters-settings even if the configuration is omitted or empty inside formatters.settings section.
This means the formatter activation and configurations should be defined only on the formatters or linters+linters-settings section and not mixed.

The exclusions from formatters.exclusions[].paths are converted to linters.exclusions[].rules automatically.

There are only 2(3) flags:

Flags:
  -c, --config PATH      Read config from file path PATH
      --no-config        Don't read config file
  -E, --enable strings   Enable specific formatter

Note, the -E, --enable flags works like --enable-only for linters: it overrides the formatters defined inside the configuration file.
It is the opposite of the behavior the -E, --enable flags of the run command where this flag always adds linters to those defined inside the configuration file.

In v2, the configuration of formatters will not be allowed inside the linters-settings section.

formatters:
  enable:
    - gofumpt
    - gofmt
    - goimports
    - gci
  settings:
    gofumpt:
      # ...
    gofmt:
      # ...
    goimports:
      # ...
    gci:
      # ...
  exclusions:
    generated: strict
    paths:
      - foo
      - bar
      # ...

Fixes #5296

@ldez ldez added enhancement New feature or improvement area: fmt labels Jan 28, 2025
@ldez ldez added this to the v2 milestone Jan 28, 2025
@ldez ldez requested review from bombsimon and alexandear January 28, 2025 22:53
@ldez ldez modified the milestones: v2, next Jan 28, 2025
pkg/config/formatters_settings.go Show resolved Hide resolved
pkg/config/loader.go Show resolved Hide resolved
pkg/config/loader.go Show resolved Hide resolved
cmd/golangci-lint/main.go Show resolved Hide resolved
pkg/commands/fmt.go Outdated Show resolved Hide resolved
pkg/goformat/runner.go Show resolved Hide resolved
pkg/goformat/runner.go Outdated Show resolved Hide resolved
pkg/goformat/runner.go Outdated Show resolved Hide resolved
pkg/goformat/runner.go Outdated Show resolved Hide resolved
pkg/goformat/runner.go Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: fmt enhancement New feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🌟 Let's talk about "formatters"
2 participants