-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/gopls: span.FromUTF16Column should handle positions beyond end of line #31883
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
Because of golang/go#31883 (at least) it's annoying having Vim error when it gets a diagnostic from gopls that it can't safely handle. Therefore, follow a best-efforts approach to populating the quickfix window; if we encounter an error, log it and continue. Hope is now the strategy :) but at least the compiler/similar will give the definitive answer. Also use undojoin for squashing the formatting-fixes into the previous change; prevents an undo block being created for, say, the addition of imports when saving a file.
Perhaps related to this is that all diagnostics come back with Version 0; should this not correspond to the version of the file to which the error pertains? |
The LSP specification doesn't contain a version (https://microsoft.github.io/language-server-protocol/specification#textDocument_publishDiagnostics), so I don't believe that would be related. |
Per discussion with @ianthehat on Slack.
Ian's thoughts on this were:
To my mind, a version is required because responses could be sent/handled out-of-order (much like |
@myitcv: I just tried reproducing this on master and was not able to. I got: [Trace - 8:47:02 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {
"uri": "file:///Users/rstambler/bob/bob.go",
"diagnostics": [
{
"range": {
"start": {
"line": 2,
"character": 7
},
"end": {
"line": 2,
"character": 7
}
},
"severity": 1,
"source": "LSP",
"message": "expected ';', found 'EOF'"
}
]
} Do you still see this error on master? |
Yes, I'm still seeing this issue using fb37f6b of both Log file: https://gist.github.com/myitcv/89cafa97b2b24b852eb7532f29fb10a4 I started with a
And then as you can see from the logs made edits, such that the last diagnostic sent to the client is: {
"diagnostics": [
{
"message": "expected ';', found 'EOF'",
"range": {
"end": {
"character": 8,
"line": 2
},
"start": {
"character": 8,
"line": 2
}
},
"severity": 1,
"source": "LSP"
}
],
"uri": "file:///home/myitcv/gostuff/src/github.com/myitcv/playground/main.go"
} |
Just out of curiosity, can you try reproducing this with VSCode or another editor? I'm wondering if I am maybe trying to reproduce it incorrectly or if this is something specific to |
Would it perhaps be easier/more precise to compare the traces? Can you provide a trace from VSCode? |
Sure! It's interesting because I am seeing the 8 as a response from the code action (really from goimports), but not from the diagnostics.
|
@stamblerre thanks very much for that trace. Much appreciated. That's already helped to narrow this down to something in I'm going to close this for now to reduce the noise 😄 |
Actually, I'm now back to my original position: what The file contents following the edits are, in quoted form:
Line 2 (in LSP terms) is:
which is 7 bytes long (not including the newline, because they defines the end of the line) Therefore the last valid position is 7 (in LSP terms). The error is, however reported at position 8. I actually think package main
import (
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
)
const hello = `package main
var _ *
`
func main() {
fset := token.NewFileSet()
fmt.Printf(">> %q\n", hello)
f, err := parser.ParseFile(fset, "main.go", hello, 0)
if err != nil {
log.Fatalf("failed to parse main.go: %v", err)
}
conf := types.Config{Importer: importer.Default()}
_, err = conf.Check("cmd/hello", fset, []*ast.File{f}, nil)
if err != nil {
log.Fatalf("failed to type check main.go: %v", err)
}
fmt.Printf("Successfully type-checked")
} This gives:
Column references here are 1-indexed, but here too the reference is beyond the end of the line. That said, perhaps this is intentional. Because despite there being a newline at the end of the file, it would never make sense to have this error message be reported in terms of line 4 (1-indexed). From the user's perspective, line 4 does not exist). So we conclude that That said, I don't think it would be correct for
Instead, I think we need to fallback to the LSP spec here:
Specifically, in this case, the character value is great than the line length, so we should drop back to the line length. Which would give us a valid position. So, very long story short: I think |
Change https://golang.org/cl/185058 mentions this issue: |
…6Column On the assumption these implementations are designed to support/implement the LSP spec, FromUTF16Column should handle the case where a character value is beyond the end of the line. https://github.com/Microsoft/language-server-protocol/blob/gh-pages/specification.md#position: > * If the character value is greater than the line length it defaults back to the > * line length. Fixes golang/go#31883 Change-Id: I370845b7f2f046d8e84048a26bae5b23e9c27d06 Reviewed-on: https://go-review.googlesource.com/c/tools/+/185058 Run-TryBot: Paul Jolly <paul@myitcv.org.uk> Reviewed-by: Ian Cottrell <iancottrell@google.com>
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?
Edit the following file:
Load and observe the diagnostic that comes back:
Character 8 (0-based) is beyond the end of line 2 (0-based); 7 is that last possible index for line 2.
What did you expect to see?
An error within the bounds of line 2.
What did you see instead?
An error beyond the end of line 2.
cc @stamblerre @ianthehat
The text was updated successfully, but these errors were encountered: