Skip to content

Commit 1b08791

Browse files
committed
Make staticcheck configurable with GOPATH detection
1 parent a02a4f2 commit 1b08791

File tree

6 files changed

+63
-25
lines changed

6 files changed

+63
-25
lines changed

ale_linters/go/gopls.vim

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ call ale#Set('go_gopls_options', '--mode stdio')
77
call ale#Set('go_gopls_init_options', {})
88
call ale#Set('go_gopls_use_global', get(g:, 'ale_use_global_executables', 0))
99

10-
function! s:GetGoPathExecutable(suffix) abort
11-
let l:prefix = $GOPATH
12-
13-
if !empty($GOPATH)
14-
let l:prefix = $GOPATH
15-
elseif has('win32')
16-
let l:prefix = $USERPROFILE . '/go'
17-
else
18-
let l:prefix = $HOME . '/go'
19-
endif
20-
21-
return ale#path#Simplify(l:prefix . '/' . a:suffix)
22-
endfunction
23-
2410
function! ale_linters#go#gopls#GetCommand(buffer) abort
2511
return ale#go#EnvString(a:buffer)
2612
\ . '%e'
@@ -45,7 +31,7 @@ call ale#linter#Define('go', {
4531
\ 'name': 'gopls',
4632
\ 'lsp': 'stdio',
4733
\ 'executable': {b -> ale#path#FindExecutable(b, 'go_gopls', [
48-
\ s:GetGoPathExecutable('bin/gopls'),
34+
\ ale#go#GetGoPathExecutable('bin/gopls'),
4935
\ ])},
5036
\ 'command': function('ale_linters#go#gopls#GetCommand'),
5137
\ 'project_root': function('ale_linters#go#gopls#FindProjectRoot'),

ale_linters/go/staticcheck.vim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
" Author: Ben Reedy <https://github.com/breed808>
22
" Description: staticcheck for Go files
33

4+
call ale#Set('go_staticcheck_executable', 'staticcheck')
45
call ale#Set('go_staticcheck_options', '')
56
call ale#Set('go_staticcheck_lint_package', 0)
7+
call ale#Set('go_staticcheck_use_global', get(g:, 'ale_use_global_executables', 0))
68

79
function! ale_linters#go#staticcheck#GetCommand(buffer) abort
810
let l:options = ale#Var(a:buffer, 'go_staticcheck_options')
911
let l:lint_package = ale#Var(a:buffer, 'go_staticcheck_lint_package')
1012
let l:env = ale#go#EnvString(a:buffer)
1113

1214
if l:lint_package
13-
return l:env . 'staticcheck'
15+
return l:env . '%e'
1416
\ . (!empty(l:options) ? ' ' . l:options : '') . ' .'
1517
endif
1618

17-
return l:env . 'staticcheck'
19+
return l:env . '%e'
1820
\ . (!empty(l:options) ? ' ' . l:options : '')
1921
\ . ' %s:t'
2022
endfunction
2123

2224
call ale#linter#Define('go', {
2325
\ 'name': 'staticcheck',
24-
\ 'executable': 'staticcheck',
26+
\ 'executable': {b -> ale#path#FindExecutable(b, 'go_staticcheck', [
27+
\ ale#go#GetGoPathExecutable('bin/staticcheck'),
28+
\ ])},
2529
\ 'cwd': '%s:h',
2630
\ 'command': function('ale_linters#go#staticcheck#GetCommand'),
2731
\ 'callback': 'ale#handlers#go#Handler',

autoload/ale/go.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,17 @@ function! ale#go#EnvString(buffer) abort
4242

4343
return l:env
4444
endfunction
45+
46+
function! ale#go#GetGoPathExecutable(suffix) abort
47+
let l:prefix = $GOPATH
48+
49+
if !empty($GOPATH)
50+
let l:prefix = $GOPATH
51+
elseif has('win32')
52+
let l:prefix = $USERPROFILE . '/go'
53+
else
54+
let l:prefix = $HOME . '/go'
55+
endif
56+
57+
return ale#path#Simplify(l:prefix . '/' . a:suffix)
58+
endfunction

doc/ale-go.txt

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ the benefit of running a number of linters, more than ALE would by default,
2020
while ensuring it doesn't run any linters known to be slow or resource
2121
intensive.
2222

23-
g:ale_go_go_executable *g:ale_go_go_options*
24-
*b:ale_go_go_options*
23+
g:ale_go_go_executable *g:ale_go_go_executable*
24+
*b:ale_go_go_executable*
2525

2626
Type: |String|
2727
Default: `'go'`
@@ -289,6 +289,18 @@ g:ale_go_revive_options *g:ale_go_revive_options*
289289
===============================================================================
290290
staticcheck *ale-go-staticcheck*
291291

292+
g:ale_go_staticcheck_executable *g:ale_go_staticcheck_executable*
293+
*b:ale_go_staticcheck_executable*
294+
Type: |String|
295+
Default: `'staticcheck'`
296+
297+
See |ale-integrations-local-executables|
298+
299+
ALE will search for `staticcheck` in locally installed directories first by
300+
default, and fall back on a globally installed `staticcheck` if it can't be
301+
found otherwise.
302+
303+
292304
g:ale_go_staticcheck_options *g:ale_go_staticcheck_options*
293305
*b:ale_go_staticcheck_options*
294306
Type: |String|
@@ -307,5 +319,13 @@ g:ale_go_staticcheck_lint_package *g:ale_go_staticcheck_lint_package*
307319
current file.
308320

309321

322+
g:ale_go_staticcheck_use_global *g:ale_go_staticcheck_use_global*
323+
*b:ale_go_staticcheck_use_global*
324+
Type: |String|
325+
Default: `get(g:, 'ale_use_global_executables', 0)`
326+
327+
See |ale-integrations-local-executables|
328+
329+
310330
===============================================================================
311331
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:

test/linter/test_staticcheck.vader

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Before:
22
Save g:ale_go_go111module
3+
Save $GOPATH
4+
5+
let $GOPATH = '/non/existent/directory'
36

47
call ale#assert#SetUpLinterTest('go', 'staticcheck')
58
call ale#test#SetFilename('test.go')
@@ -11,25 +14,36 @@ After:
1114

1215
Execute(The staticcheck callback should return the right defaults):
1316
AssertLinterCwd '%s:h'
14-
AssertLinter 'staticcheck', 'staticcheck %s:t'
17+
AssertLinter 'staticcheck', ale#Escape('staticcheck') . ' %s:t'
18+
19+
Execute(staticcheck should be found in GOPATH):
20+
" This is a directory with a fake executable
21+
let $GOPATH = ale#test#GetFilename('../test-files/go/gopath')
22+
23+
AssertLinter
24+
\ ale#test#GetFilename('../test-files/go/gopath/bin/staticcheck'),
25+
\ ale#Escape(ale#test#GetFilename('../test-files/go/gopath/bin/staticcheck'))
26+
\ . ' %s:t'
1527

1628
Execute(The staticcheck callback should use configured options):
1729
let b:ale_go_staticcheck_options = '-test'
1830

19-
AssertLinter 'staticcheck', 'staticcheck -test %s:t'
31+
AssertLinter 'staticcheck', ale#Escape('staticcheck') . ' -test %s:t'
2032

2133
Execute(The staticcheck `lint_package` option should use the correct command):
2234
let b:ale_go_staticcheck_lint_package = 1
2335

2436
AssertLinterCwd '%s:h'
25-
AssertLinter 'staticcheck', 'staticcheck .'
37+
AssertLinter 'staticcheck', ale#Escape('staticcheck') . ' .'
2638

2739
Execute(The staticcheck callback should use the `GO111MODULE` option if set):
2840
let b:ale_go_go111module = 'off'
2941

30-
AssertLinter 'staticcheck', ale#Env('GO111MODULE', 'off') . 'staticcheck %s:t'
42+
AssertLinter 'staticcheck',
43+
\ ale#Env('GO111MODULE', 'off') . ale#Escape('staticcheck') . ' %s:t'
3144

3245
" Test with lint_package option set
3346
let b:ale_go_staticcheck_lint_package = 1
3447

35-
AssertLinter 'staticcheck', ale#Env('GO111MODULE', 'off') . 'staticcheck .'
48+
AssertLinter 'staticcheck',
49+
\ ale#Env('GO111MODULE', 'off') . ale#Escape('staticcheck') . ' .'

test/test-files/go/gopath/bin/staticcheck

Whitespace-only changes.

0 commit comments

Comments
 (0)