Skip to content

Commit bcd555d

Browse files
authored
Merge pull request #1527 from bhcleek/fix-fugitive-errors
eliminate errors trying to run a command in a directory that does not exist
2 parents ebbc5b3 + a50c831 commit bcd555d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

autoload/go/tool.vim

+12
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ function! go#tool#FilterValids(items) abort
162162
endfunction
163163

164164
function! go#tool#ExecuteInDir(cmd) abort
165+
" Verify that the directory actually exists. If the directory does not
166+
" exist, then assume that the a:cmd should not be executed. Callers expect
167+
" to check v:shell_error (via go#util#ShellError()), so execute a command
168+
" that will return an error as if a:cmd was run and exited with an error.
169+
" This helps avoid errors when working with plugins that use virtual files
170+
" that don't actually exist on the file system (e.g. vim-fugitive's
171+
" GitDiff).
172+
if !isdirectory(expand("%:p:h"))
173+
let [out, err] = go#util#Exec(["false"])
174+
return ''
175+
endif
176+
165177
let old_gopath = $GOPATH
166178
let old_goroot = $GOROOT
167179
let $GOPATH = go#path#Detect()

autoload/go/tool_test.vim

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func! Test_ExecuteInDir() abort
2+
let l:tmp = gotest#write_file('a/a.go', ['package a'])
3+
try
4+
let l:out = go#tool#ExecuteInDir("pwd")
5+
call assert_equal(l:tmp . "/src/a\n", l:out)
6+
finally
7+
call delete(l:tmp, 'rf')
8+
endtry
9+
endfunc
10+
11+
func! Test_ExecuteInDir_nodir() abort
12+
let l:tmp = go#util#tempdir("executeindir")
13+
exe ':e ' . l:tmp . '/new-dir/a'
14+
15+
try
16+
let l:out = go#tool#ExecuteInDir("pwd")
17+
call assert_equal('', l:out)
18+
finally
19+
call delete(l:tmp, 'rf')
20+
endtry
21+
endfunc
22+
23+
" vim: sw=2 ts=2 et

0 commit comments

Comments
 (0)