-
Notifications
You must be signed in to change notification settings - Fork 89
textDocument/xdefinition: do not return "nil" AND no error. #103
Conversation
@sqs I've tagged you in this PR because you wrote that code and it's very unclear from the docs why it's doing that. Please let me know what you think? |
The return type signature of `handleDefinition` is `([]lsp.Location, error)`. If `nil, nil` is returned then ` (*LangHandler).Handle` also returns `result interface{}` as `nil`. When the result of a jsonrpc2 handler is nil, `jsonrpc2.HandlerWithError` [converts it into a struct](https://sourcegraph.com/github.com/sourcegraph/jsonrpc2/-/blob/handler_with_error.go#L25-28) which is then encoded as a JSON object instead of an array. When decoding, this results in: ``` json: cannot unmarshal object into Go value of type []lspext.SymbolLocationInformation ``` Which is (obviously) bad because that looks like an error instead of 'no results'. According to that jsonrpc2 implementation (linked above): ``` // isNilValue tests if an interface is empty, because an empty interface does // not encode any information, we can't encode it in JSON so that the proxy // knows it's a response, not a request. func isNilValue(resp interface{}) bool { ``` This makes it unclear to me whether or not the conversion into a struct is truly correct or not. I'm open to other ideas on how to resolve this issue.
Thanks for digging into this. A response of I think that if
It is not. That is code I wrote and I see it is incorrect. An JSON-RPC 2.0 response's result can be any JSON type (http://www.jsonrpc.org/specification#response_object, note |
@@ -51,7 +51,7 @@ func (h *LangHandler) handleXDefinition(ctx context.Context, conn JSONRPC2Conn, | |||
// | |||
// TODO(sqs): find a way to actually emit builtin locations | |||
// (pointing to builtin/builtin.go). | |||
return nil, nil | |||
return []symbolLocationInformation{}, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes are both correct.
Nevermind about this paragraph. I see that your change accomplishes the same result with less code (I had forgotten |
|
@felixfbecker FYI in go by default an empty slice (ie empty list) is nil.
|
The return type signature of
handleDefinition
is([]lsp.Location, error)
. Ifnil, nil
is returned then
(*LangHandler).Handle
also returnsresult interface{}
asnil
. Whenthe result of a jsonrpc2 handler is nil,
jsonrpc2.HandlerWithError
converts it into a structwhich is then encoded as a JSON object instead of an array. When decoding, this results in:
Which is (obviously) bad because that looks like an error instead of 'no results'.
According to that jsonrpc2 implementation (linked above):
This makes it unclear to me whether or not the conversion into a struct is
truly correct or not. I'm open to other ideas on how to resolve this issue.