golangci-lint icon indicating copy to clipboard operation
golangci-lint copied to clipboard

Disable a formatter for a path

Open corentinmusard opened this issue 8 months ago • 10 comments

Welcome

How did you install golangci-lint?

Brew

Your feature request related to a problem? Please describe

(using version 2.0.1)

Can you make it possible to disable a specific formatter for a give path?

For linters we have

  exclusions:
    rules:
      # Exclude some linters from running on tests files.
      - linters:
          - containedctx
          - dogsled
        path: _test.go

Use case: I want to enable golines everywhere but not on my _test.go files.

Describe the solution you'd like

formatters:
  enable:
    - golines
  exclusions:
    paths:
      - linters:
          - golines
        path: _test.go

Describe alternatives you've considered

Keeping golines disabled

Additional context

No response

Supporter

corentinmusard avatar Mar 25 '25 14:03 corentinmusard

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

boring-cyborg[bot] avatar Mar 25 '25 14:03 boring-cyborg[bot]

Hello,

Currently, you cannot disable one formatter for a path, but you can exclude a path (for all formatters):

formatters:
  paths:
    - _test.go

Can you explain why do you want to apply formatters except one on a specific file?

ldez avatar Mar 25 '25 14:03 ldez

Thank you for the quick response!

We have a lot of long one-liner in our units tests that I want to keep in a single line. Such as:

createdUser := client.User.Create().SetID("dummy_id").SetName("Dummy User").SetEmail("[email protected]").SaveX(ctx)

Making them multiline makes our tests much longer and less readable, so I'd like to disable this formatter for test files

corentinmusard avatar Mar 25 '25 14:03 corentinmusard

Same here. We have this interface, and I would prefer to keep it single-lined. Something like //noformat:golines would be great for specific pieces of code

Image

thiagodeev avatar May 07 '25 21:05 thiagodeev

The exclusions based on a path are possible, but a directive for formatters will never happen: formatters are not linters, the formatting is applied to raw files, not parsed files.

ldez avatar May 07 '25 22:05 ldez

@ldez got it, thanks

thiagodeev avatar May 08 '25 00:05 thiagodeev

@ldez I thought I'd share another use-case where we want to exclude a formatter.

In a large codebase we want to introduce a new formatter (gci in this case), but apply the changes incrementally so it's not a big bang change; follow up PRs can then remove each exclusion for a smaller diff / blast radius.

rcrowe avatar Jun 04 '25 09:06 rcrowe

@rcrowe I don't get the point of formatting imports incrementally: formatters are not linters, there is no real need to review the formatting so that the changes can be applied in one go.

One PR with only formatting is better than multiple PRs with unrelated changes related to formatting.

IMO, this is not a "valid" use case.

ldez avatar Jun 04 '25 11:06 ldez

@ldez It was with the aim to reduce conflicts across branches, to be incrementally raise PRs and fix things up.

We can just create a standalone config file targeting just these formatters, so not blocked. Thought helpful to give an example in the wild.

rcrowe avatar Jun 04 '25 11:06 rcrowe

It was with the aim to reduce conflicts across branches, to be incrementally raise PRs and fix things up.

This is much more of a "valid" use case explanation. I still think it's better to apply the formatting in a reduced number of PRs, but I understand your context. Thank you for the clarification.

ldez avatar Jun 04 '25 11:06 ldez