Skip to content

Commit

Permalink
debug: allow debugging outside GOPATH without a go.mod
Browse files Browse the repository at this point in the history
Do not try to determine the package name being debugged. Delve supports
debugging and testing without specifying a package name; vim-go should,
too. One of the things that this makes easier is debugging code that is
neither in GOPATH nor in a module.

Remove unnecessary is_test key from the state dictionary.

Remove code that set the current directory when debugging tests. Because
delve is started as an async job, its cwd will be set to the directory
of the buffer from which debugging is started, anyway. Using `lcd` to
set the buffer local directory is just another thing to keep track of
and it doesn't seem to have any real benefit.

Update documentation to explain how relative paths work for `:GoDebug`
and `:GoDebugTest`.

Refactor go#package#FromPath so that it returns -2 when the package is
neither in GOPATH nor in the current module.
  • Loading branch information
bhcleek committed May 5, 2019
1 parent 64178ea commit 9865e44
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
47 changes: 17 additions & 30 deletions autoload/go/debug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ if !exists('s:state')
\ 'localVars': {},
\ 'functionArgs': {},
\ 'message': [],
\ 'is_test': 0,
\}

if go#util#HasDebug('debugger-state')
Expand Down Expand Up @@ -507,7 +506,7 @@ function! s:out_cb(ch, msg) abort
if has('nvim')
let s:state['data'] = []
let l:state = {'databuf': ''}

" explicitly bind callback to state so that within it, self will
" always refer to state. See :help Partial for more information.
let l:state.on_data = function('s:on_data', [], l:state)
Expand Down Expand Up @@ -589,38 +588,23 @@ function! go#debug#Start(is_test, ...) abort
endif

try
let l:cmd = [
\ dlv,
\ (a:is_test ? 'test' : 'debug'),
\]

" append the package when it's given.
if len(a:000) > 0
let l:pkgname = a:1
if l:pkgname[0] == '.'
let l:pkgname = go#package#FromPath(l:pkgname)
let l:pkgname = go#package#FromPath(a:1)
if l:pkgname is -1
call go#util#EchoError('could not determine package name')
return
endif
else
let l:pkgname = go#package#FromPath(getcwd())
endif

if l:pkgname is -1
call go#util#EchoError('could not determine package name')
return
endif

" cd in to test directory; this is also what running "go test" does.
if a:is_test
" TODO(bc): Either remove this if it's ok to do so or else record it and
" reset cwd after the job completes.
lcd %:p:h
let l:cmd += [l:pkgname]
endif

let s:state.is_test = a:is_test

let l:args = []
if len(a:000) > 1
let l:args = ['--'] + a:000[1:]
endif

let l:cmd = [
\ dlv,
\ (a:is_test ? 'test' : 'debug'),
\ l:pkgname,
let l:cmd += [
\ '--output', tempname(),
\ '--headless',
\ '--api-version', '2',
Expand All @@ -635,7 +619,10 @@ function! go#debug#Start(is_test, ...) abort
if buildtags isnot ''
let l:cmd += ['--build-flags', '--tags=' . buildtags]
endif
let l:cmd += l:args

if len(a:000) > 1
let l:cmd += ['--'] + a:000[1:]
endif

let s:state['message'] = []
let l:opts = {
Expand Down
10 changes: 6 additions & 4 deletions autoload/go/package.vim
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ function! go#package#ImportPath() abort
endfunction


" FromPath returns the import path of arg.
" go#package#FromPath returns the import path of arg. -1 is returned when arg
" does not specify a package. -2 is returned when arg is a relative path
" outside of GOPATH and not in a module.
function! go#package#FromPath(arg) abort
let l:cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd'
let l:dir = getcwd()
Expand All @@ -156,10 +158,10 @@ function! go#package#FromPath(arg) abort

let l:importpath = split(l:out, '\n')[0]

" go list returns '_CURRENTDIRECTORY' if the directory is not inside GOPATH.
" Check it and retun an error if that is the case
" go list returns '_CURRENTDIRECTORY' if the directory is neither in GOPATH
" nor in a module. Check it and retun an error if that is the case
if l:importpath[0] ==# '_'
return -1
return -2
endif

return l:importpath
Expand Down
6 changes: 3 additions & 3 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2026,8 +2026,9 @@ rest of the commands and mappings become available after starting debug mode.
* Setup the debug windows according to |'g:go_debug_windows'|.
* Make the `:GoDebug*` commands and `(go-debug-*)` mappings available.

The current directory is used if [pkg] is empty. Any other arguments will
be passed to the program.
The directory of the current buffer is used if [pkg] is empty. Any other
arguments will be passed to the program. When [pkg] is relative, it will
be interpreted relative to the directory of the current buffer.

Use |:GoDebugStop| to stop `dlv` and exit debugging mode.

Expand All @@ -2040,7 +2041,6 @@ rest of the commands and mappings become available after starting debug mode.
Use `-test.flag` to pass flags to `go test` when debugging a test; for
example `-test.v` or `-test.run TestFoo`


*:GoDebugRestart*
:GoDebugRestart

Expand Down

0 comments on commit 9865e44

Please sign in to comment.