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/gopls: suggests completion 'nilfunc' instead of 'nil' when module requires golang.org/x/tools #42097

Closed
bcmills opened this issue Oct 20, 2020 · 2 comments

Comments

@bcmills
Copy link
Member

@bcmills bcmills commented Oct 20, 2020

example.com$ go version
go version devel +55b2d479d Tue Oct 20 08:08:15 2020 +0000 linux/amd64

example.com$ ls -d ~/.emacs.d/elpa/eglot*
/usr/local/google/home/bcmills/.emacs.d/elpa/eglot-20200830.1254

example.com$ go version -m $(which gopls)
/usr/local/google/home/bcmills/bin/gopls: devel +186f0220d0 Mon Oct 5 11:12:24 2020 -0400
        path    golang.org/x/tools/gopls
        mod     golang.org/x/tools/gopls        v0.5.1  h1:AF3Uh7HF08SZpKFfgJO6zfF3bbxyDXWqdkK4kMXiQ1o=
        dep     github.com/BurntSushi/toml      v0.3.1  h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
        dep     github.com/google/go-cmp        v0.5.1  h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
        dep     github.com/sergi/go-diff        v1.1.0  h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
        dep     golang.org/x/mod        v0.3.0  h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
        dep     golang.org/x/sync       v0.0.0-20200625203802-6e8e738ad208      h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=
        dep     golang.org/x/tools      v0.0.0-20200930165741-f1523d29dbb9      h1:1R38tQp22dcHpTKJPjgVa16FhlDy/kHEaCM/ndi/FIc=
        dep     golang.org/x/xerrors    v0.0.0-20200804184101-5ec99f83aff1      h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
        dep     honnef.co/go/tools      v0.0.1-2020.1.5 h1:nI5egYTGJakVyOryqLs1cQO5dO0ksin5XXs2pspk75k=
        dep     mvdan.cc/gofumpt        v0.0.0-20200802201014-ab5a8192947d      h1:t8TAw9WgTLghti7RYkpPmqk4JtQ3+wcP5GgZqgWeWLQ=
        dep     mvdan.cc/xurls/v2       v2.2.0  h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=

example.com$

Steps to reproduce

$ go mod init example.com
go: creating new go.mod: module example.com

$ go get -d golang.org/x/tools
go: downloading golang.org/x/tools v0.0.0-20201020141929-90550980fc82
go: golang.org/x/tools upgrade => v0.0.0-20201020141929-90550980fc82

example.com$ cat > example.go
package example

func F() interface{} {
}

$ emacs example.go

Now move the cursor to the function body, type return nil, leave the cursor at the end of that line, and wait for completions.

Expected

nil is the obvious thing to return from a func interface{}, and is also a valid and complete statement, so no completion should be suggested (or the first completion should be nil itself).

Observed

The first completion returned by gopls is the package name nilfunc for package golang.org/x/tools/go/analysis/passes/nilfunc, so any stray press of the Tab key will result in a broken source file. 😞

[Trace - 11:45:39.152 AM] Sending request 'initialize - (1)'.
Params: {"processId":9779,"rootPath":"/tmp/tmp.UF2AKOuMKI/example.com/","rootUri":"file:///tmp/tmp.UF2AKOuMKI/example.com/","initializationOptions":null,"capabilities":{"workspace":{"applyEdit":true,"executeCommand":{"dynamicRegistration":false},"workspaceEdit":{"documentChanges":false},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"dynamicRegistration":false},"configuration":true},"textDocument":{"synchronization":{"dynamicRegistration":false,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":false,"completionItem":{"snippetSupport":true},"contextSupport":true},"hover":{"dynamicRegistration":false,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":false,"signatureInformation":{"parameterInformation":{"labelOffsetSupport":true}}},"references":{"dynamicRegistration":false},"definition":{"dynamicRegistration":false},"declaration":{"dynamicRegistration":false},"implementation":{"dynamicRegistration":false},"typeDefinition":{"dynamicRegistration":false},"documentSymbol":{"dynamicRegistration":false,"hierarchicalDocumentSymbolSupport":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"documentHighlight":{"dynamicRegistration":false},"codeAction":{"dynamicRegistration":false,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"formatting":{"dynamicRegistration":false},"rangeFormatting":{"dynamicRegistration":false},"rename":{"dynamicRegistration":false},"publishDiagnostics":{"relatedInformation":false}},"experimental":null}}


[Trace - 11:45:39.153 AM] Received response 'initialize - (1)' in 1ms.
Result: {"capabilities":{"textDocumentSync":{"openClose":true,"change":2,"save":{}},"completionProvider":{"triggerCharacters":["."]},"hoverProvider":true,"signatureHelpProvider":{"triggerCharacters":["(",","]},"definitionProvider":true,"typeDefinitionProvider":true,"implementationProvider":true,"referencesProvider":true,"documentHighlightProvider":true,"documentSymbolProvider":true,"codeActionProvider":{"codeActionKinds":["quickfix","refactor.extract","refactor.rewrite","source.fixAll","source.organizeImports"]},"codeLensProvider":{},"documentLinkProvider":{},"workspaceSymbolProvider":true,"documentFormattingProvider":true,"documentOnTypeFormattingProvider":{"firstTriggerCharacter":""},"renameProvider":true,"foldingRangeProvider":true,"executeCommandProvider":{"commands":["generate","fill_struct","regenerate_cgo","test","tidy","undeclared_name","upgrade_dependency","vendor","extract_variable","extract_function","gc_details","generate_gopls_mod"]},"callHierarchyProvider":true,"workspace":{"workspaceFolders":{"supported":true,"changeNotifications":"workspace/didChangeWorkspaceFolders"}}},"serverInfo":{"name":"gopls","version":"Build info\n----------\ngolang.org/x/tools/gopls v0.5.1\n    golang.org/x/tools/gopls@v0.5.1 h1:AF3Uh7HF08SZpKFfgJO6zfF3bbxyDXWqdkK4kMXiQ1o=\n    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=\n    github.com/google/go-cmp@v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=\n    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=\n    golang.org/x/mod@v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=\n    golang.org/x/sync@v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA=\n    golang.org/x/tools@v0.0.0-20200930165741-f1523d29dbb9 h1:1R38tQp22dcHpTKJPjgVa16FhlDy/kHEaCM/ndi/FIc=\n    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=\n    honnef.co/go/tools@v0.0.1-2020.1.5 h1:nI5egYTGJakVyOryqLs1cQO5dO0ksin5XXs2pspk75k=\n    mvdan.cc/gofumpt@v0.0.0-20200802201014-ab5a8192947d h1:t8TAw9WgTLghti7RYkpPmqk4JtQ3+wcP5GgZqgWeWLQ=\n    mvdan.cc/xurls/v2@v2.2.0 h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=\n"}}


[Trace - 11:45:39.154 AM] Sending notification 'initialized'.
Params: {}


[Trace - 11:45:39.155 AM] Received notification 'window/showMessage'.
Params: {"type":4,"message":"Loading packages..."}


[Trace - 11:45:39.155 AM] Received request 'workspace/configuration - (1)'.
Params: {"items":[{"scopeUri":"file:///tmp/tmp.UF2AKOuMKI/example.com/","section":"gopls"},{"scopeUri":"file:///tmp/tmp.UF2AKOuMKI/example.com/","section":"gopls-example.com"}]}


[Trace - 11:45:39.155 AM] Sending notification 'textDocument/didOpen'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go","version":0,"languageId":"go","text":"package example\n\nfunc F() interface{} {\n}\n"}}


[Trace - 11:45:39.156 AM] Sending notification 'workspace/didChangeConfiguration'.
Params: {"settings":{"gopls":{"staticcheck":true,"matcher":"CaseSensitive"}}}


[Trace - 11:45:39.156 AM] Sending request 'textDocument/documentSymbol - (2)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"}}


[Trace - 11:45:39.161 AM] Sending response 'workspace/configuration - (1)' in 6ms.
Result: [{"staticcheck":true,"matcher":"CaseSensitive"},null]


[Trace - 11:45:39.197 AM] Received request 'client/registerCapability - (2)'.
Params: {"registrations":[{"id":"workspace/didChangeWatchedFiles-0","method":"workspace/didChangeWatchedFiles","registerOptions":{"watchers":[{"globPattern":"**/*.{go,mod,sum}","kind":7},{"globPattern":"/tmp/tmp.UF2AKOuMKI/example.com/**/*.{go,mod,sum}","kind":7}]}}]}


[Trace - 11:45:39.198 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2020/10/20 11:45:39 go env for /tmp/tmp.UF2AKOuMKI/example.com/\n(root /tmp/tmp.UF2AKOuMKI/example.com)\n(go version go version devel +55b2d479d Tue Oct 20 08:08:15 2020 +0000 linux/amd64\n)\n(valid build configuration = true)\n(build flags: [])\nGO111MODULE=auto\nGOFLAGS=\nGOMOD=/tmp/tmp.UF2AKOuMKI/example.com/go.mod\nGONOPROXY=\nGOPATH=/tmp/tmp.UF2AKOuMKI/_gopath\nGOINSECURE=\nGOSUMDB=sum.golang.org\nGONOSUMDB=\nGOPRIVATE=\nGOCACHE=/usr/local/google/home/bcmills/.cache/go-build\nGOMODCACHE=/tmp/tmp.UF2AKOuMKI/_gopath/pkg/mod\nGOPROXY=https://proxy.golang.org,direct\nGOROOT=/usr/local/google/home/bcmills/sdk/gotip\n\n"}


[Trace - 11:45:39.199 AM] Sending response 'client/registerCapability - (2)' in 1ms.
Result: 


[Trace - 11:45:39.295 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2020/10/20 11:45:39 go/packages.Load\n\tsnapshot=0\n\tdirectory=/tmp/tmp.UF2AKOuMKI/example.com\n\tquery=[./... builtin]\n\tpackages=2\n"}


[Trace - 11:45:39.295 AM] Received notification 'window/showMessage'.
Params: {"type":3,"message":"Finished loading packages."}


[Trace - 11:45:39.296 AM] Received request 'workspace/configuration - (3)'.
Params: {"items":[{"scopeUri":"file:///tmp/tmp.UF2AKOuMKI/example.com/","section":"gopls"},{"scopeUri":"file:///tmp/tmp.UF2AKOuMKI/example.com/","section":"gopls-example.com"}]}


[Trace - 11:45:39.304 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/go.mod","diagnostics":[{"range":{"start":{"line":4,"character":0},"end":{"line":4,"character":61}},"severity":2,"source":"go mod tidy","message":"golang.org/x/tools is not used in this module"}]}


[Trace - 11:45:39.305 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go","diagnostics":[{"range":{"start":{"line":3,"character":0},"end":{"line":3,"character":1}},"severity":1,"source":"compiler","message":"missing return"}]}


[Trace - 11:45:39.319 AM] Sending response 'workspace/configuration - (3)' in 23ms.
Result: [{"staticcheck":true,"matcher":"CaseSensitive"},null]


[Trace - 11:45:39.319 AM] Received response 'textDocument/documentSymbol - (2)' in 162ms.
Result: [{"name":"F","detail":"()","kind":12,"range":{"start":{"line":2,"character":0},"end":{"line":3,"character":1}},"selectionRange":{"start":{"line":2,"character":5},"end":{"line":2,"character":6}}}]


[Trace - 11:45:57.507 AM] Sending request 'textDocument/signatureHelp - (3)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":2,"character":22}}


[Trace - 11:45:57.508 AM] Received response 'textDocument/signatureHelp - (3)' in 0ms.
Result: null


[Trace - 11:45:57.508 AM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2020/10/20 11:45:57 no signature help: cannot find an enclosing function\n\tposition={2 22}\n"}


[Trace - 11:45:57.508 AM] Sending request 'textDocument/hover - (4)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":2,"character":22}}


[Trace - 11:45:57.509 AM] Received response 'textDocument/hover - (4)' in 0ms.
Result: null


[Trace - 11:45:57.509 AM] Sending request 'textDocument/documentHighlight - (5)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":2,"character":22}}


[Trace - 11:45:57.510 AM] Received response 'textDocument/documentHighlight - (5)' in 0ms.
Result: []


[Trace - 11:45:58.058 AM] Sending notification 'textDocument/didChange'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go","version":2},"contentChanges":[{"range":{"start":{"line":2,"character":22},"end":{"line":2,"character":22}},"rangeLength":0,"text":"\n"},{"range":{"start":{"line":3,"character":0},"end":{"line":3,"character":0}},"rangeLength":0,"text":"\t"}]}


[Trace - 11:45:58.059 AM] Sending request 'textDocument/signatureHelp - (6)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":1}}


[Trace - 11:45:58.059 AM] Sending request 'textDocument/hover - (7)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":1}}


[Trace - 11:45:58.059 AM] Received response 'textDocument/signatureHelp - (6)' in 0ms.
Result: null


[Trace - 11:45:58.060 AM] Received response 'textDocument/hover - (7)' in 0ms.
Result: null


[Trace - 11:45:58.060 AM] Sending request 'textDocument/documentHighlight - (8)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":1}}


[Trace - 11:45:58.060 AM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2020/10/20 11:45:58 no signature help: cannot find an enclosing function\n\tposition={3 1}\n"}


[Trace - 11:45:58.060 AM] Received response 'textDocument/documentHighlight - (8)' in 0ms.
Result: []


[Trace - 11:45:58.060 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go","version":2,"diagnostics":[{"range":{"start":{"line":4,"character":0},"end":{"line":4,"character":1}},"severity":1,"source":"compiler","message":"missing return"}]}


[Trace - 11:46:00.130 AM] Sending notification 'textDocument/didChange'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go","version":12},"contentChanges":[{"range":{"start":{"line":3,"character":1},"end":{"line":3,"character":1}},"rangeLength":0,"text":"r"},{"range":{"start":{"line":3,"character":2},"end":{"line":3,"character":2}},"rangeLength":0,"text":"e"},{"range":{"start":{"line":3,"character":3},"end":{"line":3,"character":3}},"rangeLength":0,"text":"t"},{"range":{"start":{"line":3,"character":4},"end":{"line":3,"character":4}},"rangeLength":0,"text":"u"},{"range":{"start":{"line":3,"character":5},"end":{"line":3,"character":5}},"rangeLength":0,"text":"r"},{"range":{"start":{"line":3,"character":6},"end":{"line":3,"character":6}},"rangeLength":0,"text":"n"},{"range":{"start":{"line":3,"character":7},"end":{"line":3,"character":7}},"rangeLength":0,"text":" "},{"range":{"start":{"line":3,"character":8},"end":{"line":3,"character":8}},"rangeLength":0,"text":"n"},{"range":{"start":{"line":3,"character":9},"end":{"line":3,"character":9}},"rangeLength":0,"text":"i"},{"range":{"start":{"line":3,"character":10},"end":{"line":3,"character":10}},"rangeLength":0,"text":"l"}]}


[Trace - 11:46:00.132 AM] Sending request 'textDocument/completion - (9)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":11},"context":{"triggerKind":1}}


[Trace - 11:46:00.133 AM] Sending request 'textDocument/signatureHelp - (10)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":11}}


[Trace - 11:46:00.134 AM] Sending request 'textDocument/hover - (11)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":11}}


[Trace - 11:46:00.135 AM] Sending request 'textDocument/documentHighlight - (12)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go"},"position":{"line":3,"character":11}}


[Trace - 11:46:00.137 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///tmp/tmp.UF2AKOuMKI/example.com/example.go","version":12,"diagnostics":[{"range":{"start":{"line":0,"character":0},"end":{"line":0,"character":15}},"severity":2,"source":"ST1000","message":"at least one file in a package should have a package comment"}]}


[Trace - 11:46:00.168 AM] Received response 'textDocument/completion - (9)' in 36ms.
Result: {"isIncomplete":true,"items":[{"label":"nilfunc","kind":9,"detail":"\"golang.org/x/tools/go/analysis/passes/nilfunc\"","preselect":true,"sortText":"00000","filterText":"nilfunc","insertTextFormat":2,"textEdit":{"range":{"start":{"line":3,"character":8},"end":{"line":3,"character":11}},"newText":"nilfunc"},"additionalTextEdits":[{"range":{"start":{"line":1,"character":0},"end":{"line":1,"character":0}},"newText":"\nimport \"golang.org/x/tools/go/analysis/passes/nilfunc\"\n"}]},{"label":"nilness","kind":9,"detail":"\"golang.org/x/tools/go/analysis/passes/nilness\"","sortText":"00001","filterText":"nilness","insertTextFormat":2,"textEdit":{"range":{"start":{"line":3,"character":8},"end":{"line":3,"character":11}},"newText":"nilness"},"additionalTextEdits":[{"range":{"start":{"line":1,"character":0},"end":{"line":1,"character":0}},"newText":"\nimport \"golang.org/x/tools/go/analysis/passes/nilness\"\n"}]},{"label":"nil","kind":6,"sortText":"00002","filterText":"nil","insertTextFormat":2,"textEdit":{"range":{"start":{"line":3,"character":8},"end":{"line":3,"character":11}},"newText":"nil"}}]}


[Trace - 11:46:00.169 AM] Received response 'textDocument/signatureHelp - (10)' in 35ms.
Result: null


[Trace - 11:46:00.169 AM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2020/10/20 11:46:00 no signature help: cannot find an enclosing function\n\tposition={3 11}\n"}


[Trace - 11:46:00.169 AM] Received response 'textDocument/hover - (11)' in 35ms.
Result: {"contents":{"kind":"markdown","value":"```go\nnil\n```"},"range":{"start":{"line":3,"character":8},"end":{"line":3,"character":11}}}


[Trace - 11:46:00.170 AM] Received response 'textDocument/documentHighlight - (12)' in 34ms.
Result: [{"range":{"start":{"line":2,"character":9},"end":{"line":2,"character":20}},"kind":1},{"range":{"start":{"line":3,"character":8},"end":{"line":3,"character":11}},"kind":1}]


[Trace - 11:46:30.169 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2020/10/20 11:46:30 background imports cache refresh starting\n"}


[Trace - 11:46:30.172 AM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2020/10/20 11:46:30 background refresh finished after 3.385517ms\n"}


CC @stamblerre @heschik

@gopherbot gopherbot added this to the Unreleased milestone Oct 20, 2020
@stamblerre stamblerre modified the milestones: Unreleased, gopls/v1.0.0 Oct 20, 2020
@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Oct 20, 2020

Thanks for the report! This seems like a case of unimported completions getting ranked higher than other completions, which I've actually noticed happening more recently.

@heschik: Maybe we need to reevaluate the relative scoring.

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Nov 13, 2020

#40599 tracks reevaluating scoring--going to close this issue in favor of that one, with a comment to make sure we fix this problem.

@stamblerre stamblerre closed this Nov 13, 2020
@stamblerre stamblerre removed this from the gopls/unplanned milestone Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants