Skip to content

Commit

Permalink
Merge pull request #3 from Carpetsmoker/debug-cd
Browse files Browse the repository at this point in the history
Run dlv from a temporary directory
  • Loading branch information
mattn authored Sep 12, 2017
2 parents 685be01 + 6aa7a2f commit 951b3be
Show file tree
Hide file tree
Showing 2 changed files with 51 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 @@ -395,20 +395,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 pkgname = go#package#FromPath(bufname(''))
let tmp = go#util#tempdir('vim-go-debug-')
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
32 changes: 32 additions & 0 deletions autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,36 @@ function! go#util#GetLines()
return buf
endfunction

" Make a named temporary directory which starts with "prefix".
"
" Unfortunately Vim's tempname() is not portable enough across various systems;
" see: https://github.com/mattn/vim-go/pull/3#discussion_r138084911
function! go#util#tempdir(prefix) abort
" See :help tempfile
if go#util#IsWin()
let l:dirs = [$TMP, $TEMP, 'c:\tmp', 'c:\temp']
else
let l:dirs = [$TMPDIR, '/tmp', './', $HOME]
endif

let l:dir = ''
for l:d in dirs
if !empty(l:d) && filewritable(l:d) == 2
let l:dir = l:d
break
endif
endfor

if l:dir == ''
echoerr 'Unable to find directory to store temporary directory in'
return
endif

" Not great randomness, but "good enough" for our purpose here.
let l:rnd = sha256(printf('%s%s', localtime(), fnamemodify(bufname(''), ":p")))
let l:tmp = printf("%s/%s%s", l:dir, a:prefix, l:rnd)
call mkdir(l:tmp, 'p', 0700)
return l:tmp
endfunction

" vim: sw=2 ts=2 et

0 comments on commit 951b3be

Please sign in to comment.