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 dlv from a temporary directory #3

Merged
merged 2 commits into from
Sep 12, 2017
Merged
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
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 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']
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add $TMPDIR too.

Copy link
Owner

@mattn mattn Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry. my mistake. Ignore this.

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