Skip to content
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

run: capture error messages from panics #2861

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
35 changes: 33 additions & 2 deletions autoload/go/cmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,39 @@ function! go#cmd#Generate(bang, ...) abort
endfunction

function! s:runerrorformat()
let l:panicaddress = "%\\t%#%f:%l +0x%[0-9A-Fa-f]%\\+"
let l:errorformat = '%A' . l:panicaddress . "," . &errorformat
let goroot = go#util#goroot()

let stdlibaddress = "%\\t" . goroot . "%\\f%\\+:%\\d%\\+ +0x%[0-9A-Fa-f]%\\+"

" panicaddress and readyaddress are identical except for
" panicaddress sets the filename and line number.
let l:panicaddress = '%\\t%f:%l%\\%( +0x%[0-9A-Fa-f]%\\+%\\)%\\='
let l:readyaddress = '%\\t%\\f%\\+:%\\d%\\+%\\%( +0x%[0-9A-Fa-f]%\\+%\\)%\\='
" stdlib address is identical to readyaddress, except it matches files
" inside GOROOT.
let l:stdlibaddress = '%\\t' . goroot . '%\\f%\\+:%\\d%\\+%\\%( +0x%[0-9A-Fa-f]%\\+%\\)%\\='

let l:errorformat = ''
let l:errorformat .= '%-Cexit status 2'
let l:errorformat .= ",%-C" . stdlibaddress
" Match address lines in the first matching goroutine. This means the panic
" message will only be shown as the error message in the first address of
" the running goroutine's stack.
let l:errorformat .= ',%Z' . l:panicaddress

" Match and ignore panic address without being part of a multi-line message.
" This is to catch those lines that come after the top most non-standard
" library line in stack traces.
let l:errorformat .= ",%-G" . l:panicaddress

" Match and discard empty lines in a multi-line message.
let l:errorformat .= ',%-C'
" Match and ignore the goroutine lines.
let l:errorformat .= ',%-Cgoroutine %\\d%\\+%.%#:'
" Match and discard lines containing function names in multi-line messages.
let l:errorformat .= ',%-C%.%#(%\\%(%\\.%\\.%\\.%\\)%\\=)'
let l:errorformat .= ',%+Epanic: %m'
let l:errorformat .= "," . &errorformat
return l:errorformat
endfunction

Expand Down
16 changes: 16 additions & 0 deletions autoload/go/cmd_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ func! Test_GoBuildErrors()
endtry
endfunc

func! Test_GoRunPanic()
let l:filename = 'cmd/run/panic/panic.go'
let l:tmp = gotest#load_fixture(l:filename)

let expected = [
\ {'lnum': 9, 'bufnr': bufnr('%'), 'col': 0, 'valid': 1, 'vcol': 0, 'nr': -1, 'type': 'E', 'pattern': '', 'text': 'panic: quux'}
\ ]
try
call go#cmd#Run(0)
let l:actual = getqflist()
call gotest#assert_quickfix(l:actual, l:expected)
finally
call delete(l:tmp, 'rf')
endtry
endfunc

" restore Vi compatibility settings
let &cpo = s:cpo_save
unlet s:cpo_save
Expand Down
20 changes: 10 additions & 10 deletions autoload/go/complete_test.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ func! Test_GetInfo_gopls()
endfunction

func! s:getinfo()
let l:filename = 'complete/complete.go'
let l:tmp = gotest#load_fixture(l:filename)
try
call cursor(8, 3)
let l:filename = 'complete/complete.go'
let l:tmp = gotest#load_fixture(l:filename)
try
call cursor(8, 3)

let expected = 'func Example(s string)'
let actual = go#complete#GetInfo()
call assert_equal(expected, actual)
finally
call delete(l:tmp, 'rf')
endtry
let expected = 'func Example(s string)'
let actual = go#complete#GetInfo()
call assert_equal(expected, actual)
finally
call delete(l:tmp, 'rf')
endtry
endfunction

" restore Vi compatibility settings
Expand Down
15 changes: 15 additions & 0 deletions autoload/go/test-fixtures/cmd/run/panic/panic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"errors"
"fmt"
)

func quux() {
panic(errors.New("quux"))
}

func main() {
quux()
fmt.Println("vim-go")
}