Fix #4714 - Handle ruff garbage

When ruff outputs errors are invalid JSON text, handle that and stop ALE
from throwing exceptions.
This commit is contained in:
w0rp 2024-07-08 03:22:46 +01:00
parent a35fa4d732
commit 3220b94d20
No known key found for this signature in database
GPG Key ID: 0FC1ECAA8C81CD83
2 changed files with 61 additions and 10 deletions

View File

@ -61,8 +61,15 @@ endfunction
function! ale_linters#python#ruff#Handle(buffer, lines) abort function! ale_linters#python#ruff#Handle(buffer, lines) abort
let l:output = [] let l:output = []
" Read all lines of ruff output and parse use all the valid JSONL lines.
for l:line in a:lines for l:line in a:lines
try
let l:item = json_decode(l:line) let l:item = json_decode(l:line)
catch
let l:item = v:null
endtry
if !empty(l:item)
call add(l:output, { call add(l:output, {
\ 'lnum': l:item.location.row, \ 'lnum': l:item.location.row,
\ 'col': l:item.location.column, \ 'col': l:item.location.column,
@ -72,6 +79,7 @@ function! ale_linters#python#ruff#Handle(buffer, lines) abort
\ 'text': l:item.message, \ 'text': l:item.message,
\ 'type': l:item.code =~? '\vE\d+' ? 'E' : 'W', \ 'type': l:item.code =~? '\vE\d+' ? 'E' : 'W',
\}) \})
endif
endfor endfor
return l:output return l:output

View File

@ -0,0 +1,43 @@
Before:
runtime ale_linters/python/ruff.vim
After:
call ale#linter#Reset()
Execute(We should handle basic output of ruff correctly):
AssertEqual
\ [
\ {
\ 'lnum': 2,
\ 'col': 1,
\ 'code': 'F821',
\ 'type': 'W',
\ 'end_col': 7,
\ 'end_lnum': 2,
\ 'text': 'Undefined name example',
\ },
\ ],
\ ale_linters#python#ruff#Handle(bufnr(''), [
\ '{"cell":null,"code":"F821","end_location":{"column":8,"row":2},"filename":"/home/eduardo/Code/Python/test.py","fix":null,"location":{"column":1,"row":2},"message":"Undefined name example","noqa_row":2,"url":"https://docs.astral.sh/ruff/rules/undefined-name"}',
\ ])
Execute(We should handle totally broken output from ruff):
AssertEqual [], ale_linters#python#ruff#Handle(bufnr(''), ['ERROR: oh noes!'])
Execute(We should handle mixed error lines and JSON output from ruff):
AssertEqual
\ [
\ {
\ 'lnum': 2,
\ 'col': 1,
\ 'code': 'F821',
\ 'type': 'W',
\ 'end_col': 7,
\ 'end_lnum': 2,
\ 'text': 'Undefined name example',
\ },
\ ],
\ ale_linters#python#ruff#Handle(bufnr(''), [
\ 'ERROR: oh noes!',
\ '{"cell":null,"code":"F821","end_location":{"column":8,"row":2},"filename":"/home/eduardo/Code/Python/test.py","fix":null,"location":{"column":1,"row":2},"message":"Undefined name example","noqa_row":2,"url":"https://docs.astral.sh/ruff/rules/undefined-name"}',
\ ])