Skip to content

eliminate errors trying to run a command in a directory that does not exist #1527

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

Merged
merged 2 commits into from
Nov 17, 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
12 changes: 12 additions & 0 deletions autoload/go/tool.vim
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ function! go#tool#FilterValids(items) abort
endfunction

function! go#tool#ExecuteInDir(cmd) abort
" Verify that the directory actually exists. If the directory does not
" exist, then assume that the a:cmd should not be executed. Callers expect
" to check v:shell_error (via go#util#ShellError()), so execute a command
" that will return an error as if a:cmd was run and exited with an error.
" This helps avoid errors when working with plugins that use virtual files
" that don't actually exist on the file system (e.g. vim-fugitive's
" GitDiff).
if !isdirectory(expand("%:p:h"))
let [out, err] = go#util#Exec(["false"])
return ''
endif

let old_gopath = $GOPATH
let old_goroot = $GOROOT
let $GOPATH = go#path#Detect()
Expand Down
23 changes: 23 additions & 0 deletions autoload/go/tool_test.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func! Test_ExecuteInDir() abort
let l:tmp = gotest#write_file('a/a.go', ['package a'])
try
let l:out = go#tool#ExecuteInDir("pwd")
call assert_equal(l:tmp . "/src/a\n", l:out)
finally
call delete(l:tmp, 'rf')
endtry
endfunc

func! Test_ExecuteInDir_nodir() abort
let l:tmp = go#util#tempdir("executeindir")
exe ':e ' . l:tmp . '/new-dir/a'

try
let l:out = go#tool#ExecuteInDir("pwd")
call assert_equal('', l:out)
finally
call delete(l:tmp, 'rf')
endtry
endfunc

" vim: sw=2 ts=2 et