Vim-go: unable to make gopls autocompletion work

Created on 26 Mar 2019  路  6Comments  路  Source: fatih/vim-go

What did you do? (required. The issue will be closed when not provided.)

To reproduce this issue, created test user, upgraded to latest vim-go and created vimrc from scratch.

Created sample code, imported net package, typed net. in code and tried to see autocompletion alternatives with C-x C-o.

What did you expect to happen?

To see net package's autocompletion selection subwindow.

What happened instead?

Nothing. At first it printed vim-go: [gopls] SUCCESS and no autocompletion results.
vim's log shows these lines among others:

finished sourcing /home/test/.vim/plugged/vim-go/autoload/go/statusline.vim
continuing in function go#complete#Complete[10]..go#lsp#Completion[1]..go#lsp#DidChange[9]..6[10]..<SNR>42_start
vim-go: [gopls] SUCCESS
vim-go: [completion] FAIL
vim-go: no file information for file:///home/test/go/test/test.go
vim-go: no matches-- INSERT --

Configuration (MUST fill this out):

  • vim-go version:
    40fa2df

  • vimrc you used to reproduce (use a minimal vimrc with other plugins disabled; do not link to a 2,000 line vimrc):

call plug#begin('~/.vim/plugged')                                              
Plug 'fatih/vim-go'                                                            
let g:go_def_mode = "gopls"                                                    
call plug#end()     
  • Vim version (first three lines from :version):
    VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Apr 10 2018 21:31:58)
    Included patches: 1-1453
    Modified by [email protected]

  • Go version (go version):
    go version go1.12.1 linux/amd64

  • Go environment (go env):

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/test/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/test/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build348581429=/tmp/go-build -gno-record-gcc-switches"

Most helpful comment

I'm also having this problem on a fresh dev server I just setup.

  • Debian 9 Testing
  • Vim 8.1.948
  • vim-go (latest master)

I tried the trick above from u/Lekensteyn to force gocode but that didn't help. Still no autocomplete :(

Switching vim-go to use v1.19 tag release via below command

~/.vim/pack/plugins/start/vim-go] git checkout v1.19

Has things working again using gocode for now. Of course, I'd love to use gopls instead.

All 6 comments

You've encountered a bug in gopls. If you'll move home/test/go/test to $GOPATH/src/test/ and rename test.go to something else, you'll find that autocompletion works as expected.

I recommend that you open an issue with gopls.

Hi, after recently upgrading vim-go and other stuff, I was no longer able to edit gain completion for stuff like ~/scripts/some.go and /tmp/foo.go. Not sure what the best way is to debug this, but strace -e read -f -s9999 -p $(pidof vim) revealed the following error:

send textDocument/completion#2 no file information for file:///tmp/foo.go

If I edit ~/go/src/github.com/google/fscrypt/cmd/fscrypt/fscrypt.go instead, completion works again. Could this issue be reopened? For a self-contained file using stdlib only, there is no need to put it in GOPATH.

I don't mind whether gopls is used or something else, as long as completion works I would be happy.

Environment:
Go 2:1.12-1 (Arch Linux)
vim from gvim 8.1.0996-1
vim-go 17d4c087f0ad1dec22df17048f05629b75dee600

+1. Is there an option to switch from gopls to gocode and vice versa?

A fallback to gocode is possible with this vimrc snippet:

au FileType go setlocal omnifunc=go#complete#GocodeComplete

Verify with:

:verbose setlocal omnifunc?
  omnifunc=go#complete#GocodeComplete
        Last set from ~/.vimrc line 149

I just upgraded to gvim 8.1.1073-1 and go 2:1.12.1-1, but regardless of using gopls/gocode, the same problem exists. Perhaps it is the result of going from Go 1.11 to 1.12.

The error comes from internal/lsp/format.go which occurs due to an error in internal/lsp/cache/file.go (parse fails with "no packages found for /tmp/foo.go"). The problematic call trace from in internal/lsp/cache/check.go:

  • parse

    • checkMetaData



      • packages.Load(cfg, "file=/tmp/foo.go") is called to retrieve the package(s) that contained the file. However since /tmp/foo.go is not part of a package (it was loaded as a single file), no packages will be returned.



Unfortunately this will be hard to fully fix. In case of a single .go source file, this patch would help by creating a fake command-line-arguments package:

diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go
index f49d8719..cd1610dc 100644
--- a/internal/lsp/cache/check.go
+++ b/internal/lsp/cache/check.go
@@ -94,6 +94,10 @@ func (v *View) checkMetadata(ctx context.Context, f *File) ([]packages.Error, er
        cfg := v.Config
        cfg.Mode = packages.LoadImports | packages.NeedTypesSizes
        pkgs, err := packages.Load(&cfg, fmt.Sprintf("file=%s", f.filename))
+       if len(pkgs) == 0 && err == nil {
+           // Try to parse as single file outside a package.
+           pkgs, err = packages.Load(&cfg, fmt.Sprintf("pattern=%s", f.filename))
+       }
        if len(pkgs) == 0 {
            if err == nil {
                err = fmt.Errorf("no packages found for %s", f.filename)

If you have a more complete project though with multiple packages, then this patch would not be sufficient since all files are collected in a single package. Aside from that, you would have to manually add all files and could thus end up missing symbols otherwise (compare go build my/package versus go build my/package/foo.go my/package/bar.go).

The only way to get this reliably to work in all cases is by adding the project directory in $GOPATH/src.

I'm also having this problem on a fresh dev server I just setup.

  • Debian 9 Testing
  • Vim 8.1.948
  • vim-go (latest master)

I tried the trick above from u/Lekensteyn to force gocode but that didn't help. Still no autocomplete :(

Switching vim-go to use v1.19 tag release via below command

~/.vim/pack/plugins/start/vim-go] git checkout v1.19

Has things working again using gocode for now. Of course, I'd love to use gopls instead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wpaulino picture wpaulino  路  4Comments

cassiobotaro picture cassiobotaro  路  3Comments

danielmanesku picture danielmanesku  路  4Comments

derekchiang picture derekchiang  路  3Comments

joeblubaugh picture joeblubaugh  路  3Comments