Skip to content

feat: golangci-lint v2 support #1198

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

Merged
merged 3 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: golangci-lint v2 support
  • Loading branch information
ldez committed Mar 24, 2025
commit d638311f5b66d7935baaa072861d11ff7305a1ed
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
version:
- ""
- "latest"
- "v1.63" # TODO(ldez): it should be updated for v2.
- "v1.63.4" # TODO(ldez): it should be updated for v2.
- "v2.0"
- "v2.0.0"
runs-on: ${{ matrix.os }}
permissions:
contents: read
Expand Down Expand Up @@ -83,8 +83,8 @@ jobs:
version:
- ""
- "latest"
- "v1.63.4" # TODO(ldez): it should be updated for v2.
- "95c39ac1fbaf66475705c06c16259ffd9d6bf9a2" # TODO(ldez): it should be updated for v2.
- "v2.0.0"
- "95c39ac1fbaf66475705c06c16259ffd9d6bf9a2" # FIXME(ldez): it should be updated for v2.
runs-on: ${{ matrix.os }}
permissions:
contents: read
Expand Down
5 changes: 3 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: "2"

output:
show-stats: true
sort-results: true
sort-order:
- file
- linter
- file
53 changes: 27 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ jobs:
with:
go-version: stable
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: v1.64
version: v2.0
```

</details>
Expand Down Expand Up @@ -92,9 +92,9 @@ jobs:
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: v1.64
version: v2.0
```

You will also likely need to add the following `.gitattributes` file to ensure that line endings for Windows builds are properly formatted:
Expand All @@ -120,7 +120,7 @@ on:

env:
GO_VERSION: stable
GOLANGCI_LINT_VERSION: v1.64
GOLANGCI_LINT_VERSION: v2.0

jobs:
detect-modules:
Expand All @@ -147,7 +147,7 @@ jobs:
with:
go-version: ${{ env.GO_VERSION }}
- name: golangci-lint ${{ matrix.modules }}
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: ${{ env.GOLANGCI_LINT_VERSION }}
working-directory: ${{ matrix.modules }}
Expand Down Expand Up @@ -179,7 +179,7 @@ jobs:
with:
os: ${{ matrix.os }}
go-version: ${{ matrix.go-version }}
golangci-lint-version: v1.64
golangci-lint-version: v2.0
```

```yaml
Expand All @@ -201,7 +201,7 @@ on:
golangci-lint-version:
description: 'Golangci-lint version'
type: string
default: 'v1.64'
default: 'v2.0'

jobs:
detect-modules:
Expand Down Expand Up @@ -229,7 +229,7 @@ jobs:
with:
go-version: ${{ inputs.go-version }}
- name: golangci-lint ${{ matrix.modules }}
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: ${{ inputs.golangci-lint-version }}
working-directory: ${{ matrix.modules }}
Expand All @@ -245,6 +245,7 @@ You will also likely need to add the following `.gitattributes` file to ensure t

## Compatibility

* `v7.0.0` supports golangci-lint v2 only.
* `v6.0.0+` removes `annotations` option, removes the default output format (`github-actions`).
* `v5.0.0+` removes `skip-pkg-cache` and `skip-build-cache` because the cache related to Go itself is already handled by `actions/setup-go`.
* `v4.0.0+` requires an explicit `actions/setup-go` installation step before using this action: `uses: actions/setup-go@v5`.
Expand All @@ -262,17 +263,17 @@ You will also likely need to add the following `.gitattributes` file to ensure t
The version of golangci-lint to use.

When `install-mode` is:
* `binary` (default): the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
* `goinstall`: the value can be v1.2.3, `latest`, or the hash of a commit.
* `binary` (default): the value can be v2.3 or v2.3.4 or `latest` to use the latest version.
* `goinstall`: the value can be v2.3.4, `latest`, or the hash of a commit.
* `none`: the value is ignored.

<details>
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
version: v1.58
version: v2.0
# ...
```

Expand All @@ -290,7 +291,7 @@ The default value is `binary`.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
install-mode: "goinstall"
# ...
Expand All @@ -310,7 +311,7 @@ By default, it uses the `github.token` from the action.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
github-token: xxx
# ...
Expand All @@ -333,7 +334,7 @@ The JSONSchema used to validate the configuration depends on the version of gola
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
verify: false
# ...
Expand All @@ -358,7 +359,7 @@ The default value is `false`.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
only-new-issues: true
# ...
Expand All @@ -376,7 +377,7 @@ Working directory, useful for monorepos.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
working-directory: somedir
# ...
Expand All @@ -397,9 +398,9 @@ The location of the configuration file can be changed by using `--config=`
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0
args: --config=/my/path/.golangci.yml --issues-exit-code=0
# ...
```

Expand All @@ -411,7 +412,7 @@ with:

Force the usage of the embedded problem matchers.

By default, the [problem matcher of Go (`actions/setup-go`)](https://github.com/actions/setup-go/blob/main/matchers.json) already handles the golangci-lint output (`colored-line-number`).
By default, the [problem matcher of Go (`actions/setup-go`)](https://github.com/actions/setup-go/blob/main/matchers.json) already handles the default golangci-lint output (`text`).

Works only with `colored-line-number` (the golangci-lint default).

Expand All @@ -423,7 +424,7 @@ The default value is `false`.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
problem-matchers: true
# ...
Expand All @@ -444,7 +445,7 @@ The default value is `false`.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
skip-cache: true
# ...
Expand All @@ -464,7 +465,7 @@ The default value is `false`.
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
skip-save-cache: true
# ...
Expand All @@ -486,7 +487,7 @@ If set the number is `<= 0`, the cache will be always invalidate (Not recommende
<summary>Example</summary>

```yml
uses: golangci/golangci-lint-action@v6
uses: golangci/golangci-lint-action@v7
with:
cache-invalidation-interval: 15
# ...
Expand Down Expand Up @@ -515,7 +516,7 @@ permissions:
pull-requests: read
```

For annotations to work use the default `colored-line-number` output and either use `actions/setup-go` in the job or enable the internal [problem matchers](#problem-matchers).
For annotations to work use the default format output (`text`) and either use `actions/setup-go` in the job or enable the internal [problem matchers](#problem-matchers).

## Performance

Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ inputs:
description: |
The version of golangci-lint to use.
When `install-mode` is:
- `binary` (default): the value can be v1.2 or v1.2.3 or `latest` to use the latest version.
- `goinstall`: the value can be v1.2.3, `latest`, or the hash of a commit.
- `binary` (default): the value can be v2.3 or v2.3.4 or `latest` to use the latest version.
- `goinstall`: the value can be v2.3.4, `latest`, or the hash of a commit.
- `none`: the value is ignored.
required: false
install-mode:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/golangci/golangci-lint-action

go 1.14
go 1.23
5 changes: 3 additions & 2 deletions sample-go-mod/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: "2"

output:
show-stats: true
sort-results: true
sort-order:
- file
- linter
- file
2 changes: 1 addition & 1 deletion sample-go-mod/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module sample

go 1.22.1

require github.com/golangci/golangci-lint v1.60.1
require github.com/golangci/golangci-lint/v2 v2.0.0

require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
Expand Down
2 changes: 1 addition & 1 deletion sample-go-mod/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
package main

import (
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
_ "github.com/golangci/golangci-lint/v2/cmd/golangci-lint"
)
5 changes: 3 additions & 2 deletions sample-go-tool/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: "2"

output:
show-stats: true
sort-results: true
sort-order:
- file
- linter
- file
4 changes: 2 additions & 2 deletions sample-go-tool/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.24

toolchain go1.24.0

tool github.com/golangci/golangci-lint/cmd/golangci-lint
tool github.com/golangci/golangci-lint/v2/cmd/golangci-lint

require (
4d63.com/gocheckcompilerdirectives v1.2.1 // indirect
Expand Down Expand Up @@ -68,7 +68,7 @@ require (
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-printf-func-name v0.1.0 // indirect
github.com/golangci/gofmt v0.0.0-20250106114630-d62b90e6713d // indirect
github.com/golangci/golangci-lint v1.64.4 // indirect
github.com/golangci/golangci-lint/v2 v2.0.0 // indirect
github.com/golangci/misspell v0.6.0 // indirect
github.com/golangci/plugin-module-register v0.1.1 // indirect
github.com/golangci/revgrep v0.8.0 // indirect
Expand Down
6 changes: 2 additions & 4 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,14 @@ async function goInstall(versionInfo: VersionInfo): Promise<string> {

const options: ExecOptions = { env: { ...process.env, CGO_ENABLED: "1" } }

// TODO(ldez): it should be updated for v2.
const exres = await execShellCommand(
`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionInfo.TargetVersion}`,
`go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@${versionInfo.TargetVersion}`,
options
)
printOutput(exres)

// TODO(ldez): it should be updated for v2.
const res = await execShellCommand(
`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionInfo.TargetVersion}`,
`go install -n github.com/golangci/golangci-lint/v2/cmd/golangci-lint@${versionInfo.TargetVersion}`,
options
)
printOutput(res)
Expand Down
27 changes: 9 additions & 18 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function runLint(binPath: string, patchPath: string): Promise<void> {
printOutput(res)
}

let userArgs = core.getInput(`args`)
const userArgs = core.getInput(`args`)
const addedArgs: string[] = []

const userArgsList = userArgs
Expand All @@ -76,23 +76,13 @@ async function runLint(binPath: string, patchPath: string): Promise<void> {
}
}

const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`)) // Removes `github-actions` format.
.join(",")

if (formats) {
// Adds formats but without `github-actions` format.
addedArgs.push(`--out-format=${formats}`)
}

// Removes `--out-format` from the user flags because it's already inside `addedArgs`.
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim()

if (isOnlyNewIssues()) {
if (userArgNames.has(`new`) || userArgNames.has(`new-from-rev`) || userArgNames.has(`new-from-patch`)) {
if (
userArgNames.has(`new`) ||
userArgNames.has(`new-from-rev`) ||
userArgNames.has(`new-from-patch`) ||
userArgNames.has(`new-from-merge-base`)
) {
throw new Error(`please, don't specify manually --new* args when requesting only new issues`)
}

Expand All @@ -110,6 +100,7 @@ async function runLint(binPath: string, patchPath: string): Promise<void> {
// Override config values.
addedArgs.push(`--new=false`)
addedArgs.push(`--new-from-rev=`)
addedArgs.push(`--new-from-merge-base=`)
}
break
case `merge_group`:
Expand All @@ -118,6 +109,7 @@ async function runLint(binPath: string, patchPath: string): Promise<void> {
// Override config values.
addedArgs.push(`--new=false`)
addedArgs.push(`--new-from-patch=`)
addedArgs.push(`--new-from-merge-base=`)
break
default:
break
Expand Down Expand Up @@ -150,7 +142,6 @@ async function runLint(binPath: string, patchPath: string): Promise<void> {
core.info(`golangci-lint found no issues`)
} catch (exc) {
// This logging passes issues to GitHub annotations but comments can be more convenient for some users.
// TODO: support reviewdog or leaving comments by GitHub API.
printOutput(exc)

if (exc.code === 1) {
Expand Down
Loading