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

x/tools/internal/lsp: support files outside $GOPATH and outside of a module #31168

Closed
Lekensteyn opened this issue Mar 31, 2019 · 9 comments
Closed
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Lekensteyn
Copy link
Contributor

Lekensteyn commented Mar 31, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.1 linux/amd64

Originally encountered with 1.12. Reproduced on Arch Linux (x86_64).

What did you do?

Install vim-go (fatih/vim-go@17d4c08) for use with vim (8.1.1073-1). This will use gopls by default for autocompletion.

Create a file outside $GOPATH, e.g. /tmp/foo.vim with this sample content:

package main

import "fmt"

func main() {
	fmt.Println("vim-go")
	fmt.
}

After fmt., press Ctrl-X, Ctrl-O to trigger auto completion.

Alternatively, try this reproducer which does not require vim:

repro.py
#!/usr/bin/env python3
import json
import os
import subprocess
import sys

uri = os.path.expanduser('~/go/src/github.com/google/fscrypt/cmd/fscrypt/foo.go')
# does not work
uri = '/tmp/foo.go'
if len(sys.argv) >= 2:
    uri = sys.argv[1]

# rootUri value does not seem to matter
rootUri = "file:///"
uri = "file://%s" % uri

cmd_a = {
  "method": "initialize",
  "jsonrpc": "2.0",
  "id": 1,
  "params": {
    "rootUri": rootUri,
    "capabilities": {
      "workspace": {},
      "textDocument": {}
    },
    "processId": 13448
  }
}

cmd_b = {
  "method": "textDocument/didChange",
  "jsonrpc": "2.0",
  "params": {
    "contentChanges": [
      {
        "text": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"vim-go\")\n\tfmt.\n}\n"
      }
    ],
    "textDocument": {
      "uri": uri
    }
  }
}

cmd_c = {
  "method": "textDocument/completion",
  "jsonrpc": "2.0",
  "id": 2,
  "params": {
    "textDocument": {
      "uri": uri
    },
    "position": {
      "character": 5,
      "line": 6
    }
  }
}

def send_receive(proc, cmd, receive=False):
    print('\n# Sending: %r\n' % cmd)
    s = json.dumps(cmd)
    proc.stdin.write('Content-Length: %d\r\n\r\n%s' % (len(s), s))
    proc.stdin.flush()
    # Read response
    if receive:
        size = int(proc.stdout.readline().split('Content-Length: ')[1])
        print('\n# Response (%d bytes)' % size)
        while proc.stdout.readline().strip():
            pass
        print(json.dumps(json.loads(proc.stdout.read(size)), indent=4))

with subprocess.Popen(os.path.expanduser('~/go/bin/gopls'),
        stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf8') as p:
    send_receive(p, cmd_a, receive=True)
    send_receive(p, cmd_b)
    send_receive(p, cmd_c, receive=True)
    p.terminate()
    print('')

Run python3 repro.py which will send some commands to gopls and print the responses.

What did you expect to see?

Expected to see completion:
vim-go

With the Python reproducer, I would expect something like:

[Trace - 12:52:45 AM] Received response 'textDocument/completion - (2)' in 1010ms.
Params: {"isIncomplete":false,"items":[{"label":"Errorf(format string, a ...interface{})", ...

What did you see instead?

No autocompletion.

With the Python reproducer, I see:

[Trace - 12:57:34 AM] Sending request 'textDocument/completion - (2)'.
Params: {"textDocument": {"uri": "file:///tmp/foo.go"}, "position": {"character": 5, "line": 6}}

[Error - 12:57:34 AM] send textDocument/completion#2 no file information for file:///tmp/foo.go

Additional information

I posted an analysis at fatih/vim-go#2193 (comment), the following patch would permit gopls to be used with individual files, e.g. for use with go run script.go. When used with projects with multiple files, there may be issues like missing identifiers (unless the client loads all files) or duplicate identifiers when multiple packages are combined.

Regardless of those issues, it does address the single script use case though.

[Patch redacted by @bcmills. Please send a PR or CL so that we can verify CLA compliance.]

Other similar issues:

@gopherbot gopherbot added this to the Unreleased milestone Mar 31, 2019
@stamblerre stamblerre added the gopls Issues related to the Go language server, gopls. label Mar 31, 2019
@NamPNQ
Copy link

NamPNQ commented Apr 1, 2019

Workaround

mv /tmp/foo.go $GOPATH/src/
ln -s $GOPATH/src/foo.go /tmp/foo.go

@bcmills
Copy link
Contributor

bcmills commented Apr 12, 2019

CC @stamblerre @ianthehat

@bcmills
Copy link
Contributor

bcmills commented Apr 12, 2019

@Lekensteyn We can't accept patches posted to issues, but if you have a fix please send a PR or CL per https://golang.org/doc/contribute.html.

@bcmills bcmills added FeatureRequest NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 12, 2019
@newtorn

This comment has been minimized.

@stamblerre stamblerre changed the title x/tools/internal/lsp: process files outside $GOPATH for autocompletion x/tools/internal/lsp: support files outside $GOPATH and outside of a module Jun 5, 2019
@stamblerre
Copy link
Contributor

This should work correctly as of https://golang.org/cl/183620. Closing.

@bstaletic
Copy link

bstaletic commented Aug 11, 2019

This isn't resolved. Editing a file outside of $GOPATH still produces the same error with gopls from last night (09. Aug 2019.)

EDIT: Apologies. It works after there's package main in the file.

@relunctance
Copy link

  • cd YouCompleteMe/third_party/ycmd
  • cd third_party/go/src/golang.org/x/tools
  • git pull origin master
  • cd cmd/gopls
  • go build && mv main gopls

Thanks Guy , it can resolve my problem
my env :
Vim version 8.1.1722
Go version go1.11
vimrc :

" YouCompleteMe 
let g:ycm_server_python_interpreter='/usr/local/bin/python3'
" Note that you can install YCM with both libclang and clangd enabled. In that case clangd will be preferred unless you have the following in your vimrc:
"let g:ycm_use_clangd = 0
let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py"
let g:ycm_language_server =
  \ [
  \   {
  \     'name': 'gopls',
  \     'cmdline': [ '~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x/tools/cmd/gopls/gopls' , "-rpc.trace" ],
  \     'filetypes': [ 'go' ],
  \     "project_root_files": [ "go.mod" ]
  \   }
  \ ]

let g:go_def_mode='~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x/tools/cmd/gopls/gopls'
let g:go_info_mode='~/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x/tools/cmd/gopls/gopls'
" let g:ycm_log_level = 'debug'
set completeopt=longest,menu

@bstaletic
Copy link

@relunctance Don't do that. YCM migrated to gopls a while ago. Just use ./install.py --go-completer - that's why gopls is in third_party.

@relunctance
Copy link

@bstaletic Thanks , I have remove ycm_language_server

@golang golang locked and limited conversation to collaborators Aug 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

8 participants