Skip to content

Commit 2880123

Browse files
authored
Merge pull request #2835 from bhcleek/lint/golangci-lint-is-inconsistent-and-undocumented
lint: try to parse golangci-lint errors better
2 parents 3c68cba + 38ebb28 commit 2880123

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

autoload/go/lint.vim

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,40 @@ endfunction
442442

443443
function! s:errorformat(metalinter) abort
444444
if a:metalinter == 'golangci-lint'
445-
" Golangci-lint can output the following:
446-
" <file>:<line>:<column>: <message> (<linter>)
447-
" This can be defined by the following errorformat:
448-
return 'level=%tarning\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%tarning\ msg="%m",level=%trror\ msg="%m:\ [%f:%l:%c:\ %.%#]",level=%trror\ msg="%m",%f:%l:%c:\ %m,%f:%l\ %m'
445+
let l:efm = ''
446+
447+
" Golangci-lint can several formats, all of which seem to be undocumented.
448+
" Based on trial and error, these error format strings seem to catch all
449+
" the relevant combinations.
450+
451+
" When the actual locations and error message is wrapped in parentheses
452+
" within brackets (there is usually duplicated location information in
453+
" this form). This usually happens when the linter can't do its job
454+
" because of compiler or AST problems.
455+
let l:efm .= 'level=%tarning\ msg="%.%#(%f:%l:%c:\ %m)\ %.%#]"'
456+
let l:efm .= ',level=%trror\ msg="%.%#(%f:%l:%c:\ %m)\ %.%#]"'
457+
458+
" When the actual locations and error message are not wrapped in
459+
" in bracked without the inner parenthetical. This usually happens when
460+
" the linter can't do its job because of compiler or AST problems.
461+
let l:efm .= ',level=%tarning\ msg="%.%#:\ [%f:%l:%c:\ %m]"'
462+
let l:efm .= ',level=%trror\ msg="%.%#:\ [%f:%l:%c:\ %m]"'
463+
464+
" When the file location is not provided. The usually happens when the
465+
" linter can't do its job because of some other problem.
466+
let l:efm .= ',level=%tarning\ msg="%m"'
467+
let l:efm .= ',level=%trror\ msg="%m"'
468+
469+
" when the linter was able to compile and/or create the AST, and the
470+
" linter found some problems.
471+
let l:efm .= ',%f:%l:%c:\ %m'
472+
let l:efm .= ',%f:%l\ %m'
449473
elseif a:metalinter == 'gopls'
450-
return '%f:%l:%c:%t:\ %m,%f:%l:%c::\ %m'
474+
let l:efm = '%f:%l:%c:%t:\ %m'
475+
let l:efm .= ',%f:%l:%c::\ %m'
451476
endif
452477

478+
return l:efm
453479
endfunction
454480

455481
function! s:preserveerrors(autosave, listtype) abort

autoload/go/lint_test.vim

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,9 @@ func! s:gometa_problems(metalinter) abort
5353
try
5454
let g:go_metalinter_command = a:metalinter
5555
let expected = [
56-
\ {'lnum': 3, 'bufnr': bufnr('%'), 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'w', 'module': '', 'text': '[runner] Can''t run linter golint: golint: analysis skipped: errors in package'},
57-
\ {'lnum': 3, 'bufnr': bufnr('%'), 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'e', 'module': '', 'text': 'Running error: golint: analysis skipped: errors in package'}
56+
\ {'lnum': 3, 'bufnr': bufnr('%'), 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'w', 'module': '', 'text': 'import \"/quux\": cannot import absolute path'},
57+
\ {'lnum': 3, 'bufnr': bufnr('%'), 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'e', 'module': '', 'text': 'import \"/quux\": cannot import absolute path'},
5858
\ ]
59-
6059
" clear the quickfix lists
6160
call setqflist([], 'r')
6261

@@ -141,8 +140,8 @@ func! s:gometaautosave_problems(metalinter) abort
141140
try
142141
let g:go_metalinter_command = a:metalinter
143142
let expected = [
144-
\ {'lnum': 3, 'bufnr': bufnr('%')+1, 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'w', 'module': '', 'text': '[runner] Can''t run linter golint: golint: analysis skipped: errors in package'},
145-
\ {'lnum': 3, 'bufnr': bufnr('%')+1, 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'e', 'module': '', 'text': 'Running error: golint: analysis skipped: errors in package'}
143+
\ {'lnum': 3, 'bufnr': bufnr('%')+1, 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'w', 'module': '', 'text': 'import \"/quux\": cannot import absolute path'},
144+
\ {'lnum': 3, 'bufnr': bufnr('%')+1, 'col': 8, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'e', 'module': '', 'text': 'import \"/quux\": cannot import absolute path'},
146145
\ ]
147146

148147
" clear the location lists

0 commit comments

Comments
 (0)