golangci/golangci-lint-action

Support `golangci-lint fmt --diff`

Closed this issue · 5 comments

Welcome

  • Yes, I understand that the GitHub action repository is not the repository of golangci-lint itself.
  • Yes, I've searched similar issues on GitHub and didn't find any.

Your feature request related to a problem? Please describe.

Support golangci-lint fmt --diff

In #1245, the claim is that golangci-lint run will run the same formatters that golangci-lint fmt would but I don't think that is true based on my experience.

Testing strategy:

  1. (using the default/standard config)
  2. First auto-fixing my codebase with go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 run ./... --fix
  3. go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 run ./... now passes
  4. But go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 fmt ./... changes even more things. (--diff also shows the changes)

Describe the solution you'd like.

Add an option to check that there are no additional changes from auto-formatters (golangci-lint fmt --diff)

Describe alternatives you've considered.

jobs:
  lint-go:
    name: Lint Go
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

      - name: Setup go
        uses: actions/setup-go@be3c94b385c4f180051c996d336f57a34c397495 # v3.6.1
        with:
          go-version: "stable"

      - name: Run golangci-lint (lint)
        run: go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.1 run ./... --max-issues-per-linter=0

      - name: Run golangci-lint (format)
        run: go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.1 fmt ./... --diff

Additional context.

No response

ldez commented

I don't think that is true based on my experience.

But this is true: golangci-lint run runs formatters explicitly defined inside the configuration files.

@ldez Here is a perfect minimal reproduction:

  1. Create a new go project: go mod init example/hello
  2. Create a new file in the project: hello.go
    package hello
    
    import "time"
    
    const (
    	DAY_DURATION = 24 * time.Hour
    	YEAR_DURATION = 365 * DAY_DURATION
    )
  3. Running go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 run ./... passes without changing anything
    go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 run ./...
    0 issues.
    
  4. Even go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 run ./... --fix passes without changing anything
    go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 run ./... --fix
    0 issues.
    
  5. But go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.6.0 fmt ./... does make some changes
  6. git diff
    diff --git a/hello.go b/hello.go
    index 0ad6464..2975b0a 100644
    --- a/hello.go
    +++ b/hello.go
    @@ -3,6 +3,6 @@ package hello
     import "time"
    
     const (
    -       DAY_DURATION = 24 * time.Hour
    +       DAY_DURATION  = 24 * time.Hour
            YEAR_DURATION = 365 * DAY_DURATION
     )
ldez commented

golangci-lint run runs formatters explicitly defined inside the configuration files.

The default formatter is merely a convenience and should not be considered a full-fledged formatter.

If you want to keep the same behavior as the default, but run it when using golangci-lint run, you must explicitly define gofmt inside the formatters section.

https://golangci-lint.run/docs/configuration/file/#formatters-configuration

Thanks for the details @ldez!

With more hindsight, looking at my real-life case (where I did have a .golangci.yml defined with formatters), perhaps I was just confused as I first ran the lint script that had golangci-lint fmt ./... and golangci-lint run ./... which made one set of changes for the formatting, and then later adjusted the run command to actually fix things golangci-lint run ./... --fix which made another set of changes.

But if I just started with golangci-lint run ./... --fix it would do everything all in one ✅

And my minimum reproduction example is just conflating the ideas and mistakenly thought I reproduced what I experienced earlier.

Sorry for the noise and confusion 🙇


I'll probably submit a PR to update golangci-lint --help to point this out more clearly (including the docs) -> golangci/golangci-lint#6174

ldez commented

No problem, this issue may help other users.

About the doc, there is already a section about that: https://golangci-lint.run/docs/configuration/cli/#run