Skip to content

Commit

Permalink
lsp: set working directory based on module directory
Browse files Browse the repository at this point in the history
Set the working directory of gopls based on the value of GOMOD. When in
GOPATH mode, the value of Vim's working directory will be used.

Fixes fatih#2263
  • Loading branch information
bhcleek committed May 4, 2019
1 parent e1ef9d5 commit 6c6a8e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
15 changes: 12 additions & 3 deletions autoload/go/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ scriptencoding utf-8
let s:lspfactory = {}

function! s:lspfactory.get() dict abort
if !has_key(self, 'current')
" TODO(bc): check that the lsp is still running.
if !has_key(self, 'current') || empty(self.current)
let self.current = s:newlsp()
endif

Expand Down Expand Up @@ -150,7 +149,17 @@ function! s:newlsp() abort
if !self.last_request_id
" TODO(bc): run a server per module and one per GOPATH? (may need to
" keep track of servers by rootUri).
let l:msg = self.newMessage(go#lsp#message#Initialize(getcwd()))
let l:wd = go#util#ModuleRoot()
if l:wd == -1
call go#util#EchoError('could not determine appropriate working directory for gopls')
return
endif

if l:wd == ''
let l:wd = getcwd()
endif

let l:msg = self.newMessage(go#lsp#message#Initialize(l:wd))

let l:state = s:newHandlerState('')
let l:state.handleResult = funcref('self.handleInitializeResult', [], l:self)
Expand Down
24 changes: 23 additions & 1 deletion autoload/go/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,33 @@ function! go#util#gomod() abort
return substitute(s:exec(['go', 'env', 'GOMOD'])[0], '\n', '', 'g')
endfunction


function! go#util#osarch() abort
return go#util#env("goos") . '_' . go#util#env("goarch")
endfunction

" go#util#ModuleRoot returns the root directory of the module of the current
" buffer.
function! go#util#ModuleRoot() abort
let [l:out, l:err] = go#util#ExecInDir(['go', 'env', 'GOMOD'])
if l:err != 0
return -1
endif

let l:module = split(l:out, '\n', 1)[0]

" When run with `GO111MODULE=on and not in a module directory, the module will be reported as /dev/null.
let l:fakeModule = '/dev/null'
if go#util#IsWin()
let l:fakeModule = 'NUL'
endif

if l:fakeModule == l:module
return expand('%:p:h')
endif

return fnamemodify(l:module, ':p:h')
endfunction

" Run a shell command.
"
" It will temporary set the shell to /bin/sh for Unix-like systems if possible,
Expand Down

0 comments on commit 6c6a8e7

Please sign in to comment.