Skip to content

Commit 067e74f

Browse files
authored
Add support for glnagci-lint v2 (#4936)
Version 2 of golangci-lint introduces several breaking changes to the command line arguments and the configuration file. Thi PR updates ale integration to support both version 1.6x.x and 2.x.x of golangci-lint.
1 parent ff8fe94 commit 067e74f

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

ale_linters/go/golangci_lint.vim

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,38 @@ call ale#Set('go_golangci_lint_options', '')
55
call ale#Set('go_golangci_lint_executable', 'golangci-lint')
66
call ale#Set('go_golangci_lint_package', 1)
77

8-
function! ale_linters#go#golangci_lint#GetCommand(buffer) abort
8+
function! ale_linters#go#golangci_lint#GetExecutable(buffer) abort
9+
let l:executable = ale#Var(a:buffer, 'go_golangci_lint_executable')
10+
11+
return l:executable
12+
endfunction
13+
14+
function! ale_linters#go#golangci_lint#GetCommand(buffer, version) abort
915
let l:filename = expand('#' . a:buffer . ':t')
1016
let l:options = ale#Var(a:buffer, 'go_golangci_lint_options')
1117
let l:lint_package = ale#Var(a:buffer, 'go_golangci_lint_package')
1218

19+
if ale#semver#GTE(a:version, [2, 0, 0])
20+
let l:options = l:options
21+
\ . ' --output.json.path stdout'
22+
\ . ' --output.text.path stderr'
23+
\ . ' --show-stats=0'
24+
else
25+
let l:options = l:options
26+
\ . ' --out-format=json'
27+
\ . ' --show-stats=0'
28+
endif
1329

1430
if l:lint_package
1531
return ale#go#EnvString(a:buffer)
1632
\ . '%e run '
1733
\ . l:options
18-
\ . ' --out-format=json'
19-
\ . ' --show-stats=0'
2034
endif
2135

2236
return ale#go#EnvString(a:buffer)
2337
\ . '%e run '
2438
\ . ale#Escape(l:filename)
2539
\ . ' ' . l:options
26-
\ . ' --out-format=json'
27-
\ . ' --show-stats=0'
2840
endfunction
2941

3042
function! ale_linters#go#golangci_lint#Handler(buffer, lines) abort
@@ -58,9 +70,14 @@ endfunction
5870

5971
call ale#linter#Define('go', {
6072
\ 'name': 'golangci-lint',
61-
\ 'executable': {b -> ale#Var(b, 'go_golangci_lint_executable')},
73+
\ 'executable': function('ale_linters#go#golangci_lint#GetExecutable'),
6274
\ 'cwd': '%s:h',
63-
\ 'command': function('ale_linters#go#golangci_lint#GetCommand'),
75+
\ 'command': {buffer -> ale#semver#RunWithVersionCheck(
76+
\ buffer,
77+
\ ale_linters#go#golangci_lint#GetExecutable(buffer),
78+
\ '%e --version',
79+
\ function('ale_linters#go#golangci_lint#GetCommand'),
80+
\ )},
6481
\ 'callback': 'ale_linters#go#golangci_lint#Handler',
6582
\ 'lint_file': 1,
6683
\})

test/linter/test_golangci_lint.vader

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ Before:
44
call ale#assert#SetUpLinterTest('go', 'golangci_lint')
55
call ale#test#SetFilename('test.go')
66

7+
" Test with version 1.64.8 by default
8+
GivenCommandOutput ['golangci-lint has version 1.64.8 built with go1.23.0']
9+
710
After:
811
Restore
912

@@ -16,13 +19,33 @@ Execute(The golangci-lint defaults should be correct):
1619
AssertLinter 'golangci-lint',
1720
\ ale#Escape('golangci-lint') . ' run --out-format=json --show-stats=0'
1821

22+
Execute(The golangci-lint defaults should be correct with no version info):
23+
GivenCommandOutput []
24+
AssertLinterCwd '%s:h',
25+
AssertLinter 'golangci-lint',
26+
\ ale#Escape('golangci-lint') . ' run --out-format=json --show-stats=0'
27+
28+
Execute(The golangci-lint defaults should be correct with version 2):
29+
GivenCommandOutput ['golangci-lint has version 2.0.2 built with go1.24.0']
30+
AssertLinterCwd '%s:h',
31+
AssertLinter 'golangci-lint',
32+
\ ale#Escape('golangci-lint') . ' run --output.json.path stdout --output.text.path stderr --show-stats=0'
33+
1934
Execute(The golangci-lint callback should use a configured executable):
2035
let b:ale_go_golangci_lint_executable = 'something else'
2136

2237
AssertLinter 'something else',
2338
\ ale#Escape('something else')
2439
\ . ' run --out-format=json --show-stats=0'
2540

41+
Execute(The golangci-lint callback should use a configured version 2 executable):
42+
GivenCommandOutput ['golangci-lint has version 2.0.0 built with go1.22.0']
43+
let b:ale_go_golangci_lint_executable = 'something else'
44+
45+
AssertLinter 'something else',
46+
\ ale#Escape('something else')
47+
\ . ' run --output.json.path stdout --output.text.path stderr --show-stats=0'
48+
2649
Execute(The golangci-lint callback should use configured options):
2750
let b:ale_go_golangci_lint_options = '--foobar'
2851

0 commit comments

Comments
 (0)