Skip to content

Commit

Permalink
Run dlv from a temporary directory
Browse files Browse the repository at this point in the history
This fixes two things:

- My comments here:
  fatih@8d617ec#r138018608

  It's true that `:lcd` is only for the current window, but me
  personally I still wouldn't expect any user-visible changes in the
  current directory when using `:GoDebugStart`.

- It prevents `dlv` from creating a binary in the directory. At best
  this is an annoying side-effect, but at worst it could cause people to
  lose files: consider what would happen if someone has a package named
  `foo` but also had a file named `foo` in that directory?
  If you always use `go install` (like I do) then this is fine, and
  `:GoDebugStart` creating this file can be rather surprising.

Note: this PR requires this patch to work correctly:
fatih#1435
  • Loading branch information
arp242 committed Nov 23, 2017
1 parent e463532 commit e6da3c5
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions autoload/go/debug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -391,20 +391,33 @@ function! go#debug#StartWith(...) abort
endif

try
" Run dlv from a temporary directory so it won't put the binary in the
" current dir. We pass --wd= so the binary is still run from the current
" dir.
let original_dir = getcwd()
let tmp = fnamemodify(tempname(), ':h')
let pkgname = go#package#FromPath(bufname(''))
exe 'lcd ' . tmp

echohl SpecialKey | echomsg 'Starting GoDebug...' | echohl None
let s:state['message'] = []
exe 'lcd' fnamemodify(bufname(''), ':p:h')
let job = job_start(dlv . ' debug --headless --api-version=2 --log --listen=' . g:go_debug_address . ' --accept-multiclient ' . join(a:000, ' '), {
\ 'out_cb': function('s:starting'),
\ 'err_cb': function('s:starting'),
\ 'exit_cb': function('s:exit'),
\ 'stoponexit': 'kill',

let l:cmd = printf('%s debug --headless --api-version=2 --log --listen=%s --wd=%s --accept-multiclient %s %s',
\ dlv, g:go_debug_address, original_dir, pkgname, join(a:000, ' '))

let job = job_start(l:cmd, {
\ 'out_cb': function('s:starting'),
\ 'err_cb': function('s:starting'),
\ 'exit_cb': function('s:exit'),
\ 'stoponexit': 'kill',
\})
let ch = job_getchannel(job)
let s:state['job'] = job
catch
echohl Error | echomsg v:exception | echohl None
return
finally
exe 'lcd ' . original_dir
endtry
endfunction

Expand Down

0 comments on commit e6da3c5

Please sign in to comment.