-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/gopls: references does not guarantee ordered result #32572
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
Comments
The LSP spec does not state anywhere that the result has to be ordered - therefore, I'd say that clients should not rely on this behavior (https://microsoft.github.io/language-server-protocol/specification#textDocument_references). Is there a reason that the client cannot apply this ordering? VSCode definitely orders results, so it seems redundant to me for us to guarantee ordering, when more likely than not, the client will redo the sort. |
No reason, other than perhaps there being a sensible default that the server might want to offer to clients so that they don't have to do the work themselves. But just to note: the current state means that the results sent from server to client will not be stable, which is a problem if you're looking to assert in tests against the behaviour of |
Thanks for bringing this to our attention! We do not want to commit to sorting the returned results at this time since it is not in the spec. |
Thanks @suzmue @stamblerre. Quite agree that this isn't specified in the LSP spec, which I think means that one can in fact argue that returning sorted results is just as correct/valid as returning them in a random order. In addition to the points about it being a sensible default from the client's perspective and making testing easier, I think it's also more intuitive that for a list response type the order is defined, whether in the spec or the implementation. All that said, very happy to defer 😄 Use of the references method has just landed in |
Re-opening this issue to continue discussion on the following specific point. The
From To my mind I think it's most intuitive to have the declaration as the first location, hence we should define that this is what What do you think? |
Yes, that makes sense. Thanks for pointing that out. Until find references is supported at a project level (#32869), this would mean that find references would return all of the references in the current package + the declaration from the other package (if includeDeclaration=true). |
Change https://golang.org/cl/184723 mentions this issue: |
A client can specify "IncludeDeclaration" in its ReferenceParams. When they do so, we want to include the declaration, even if it was not in the scope we searched for references. Additionally, we also return the location of the declaration first in the result array when it is included in the results. Updates golang/go#32572 Change-Id: I12837cd98102ee8d531f0f4bac2fb7bded2564c0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/184723 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Looks like the CL above fixed the problem. Please re-open if there's anything else we should do here. |
A client can specify "IncludeDeclaration" in its ReferenceParams. When they do so, we want to include the declaration, even if it was not in the scope we searched for references. Additionally, we also return the location of the declaration first in the result array when it is included in the results. Updates golang/go#32572 Change-Id: I12837cd98102ee8d531f0f4bac2fb7bded2564c0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/184723 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
The new references implementation is great; thanks @suzmue!
However I'm seeing that list of locations returned is not guaranteed to be ordered. Given that multiple URIs could possibly be returned, I think the most sensible default is to order by URI then start offset.
If the client then wants to apply some other initial ordering (i.e. they take the filename and map it to some state in their editor) then fine, we at least have a stable result.
I don't see this 100% of the time, hence I suspect this is the result of a map to slice conversion, where the resulting slice is then not sorted?
What did you expect to see?
Guaranteed order of results from a call to the
references
method.What did you see instead?
Random ordered results.
cc @stamblerre @ianthehat
The text was updated successfully, but these errors were encountered: