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: line offset out of range in codeAction response #37702

Open
bstaletic opened this issue Mar 5, 2020 · 2 comments
Open

x/tools/gopls: line offset out of range in codeAction response #37702

bstaletic opened this issue Mar 5, 2020 · 2 comments
Labels
Milestone

Comments

@bstaletic
Copy link

@bstaletic bstaletic commented Mar 5, 2020

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

$ go version
golang.org/x/tools/gopls v0.3.3
    golang.org/x/tools/gopls@(devel)

Does this issue reproduce with the latest release?

Version 0.3.3 is the latest gopls version. I haven't tried the latest master, to be honest.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/bstaletic/.cache/go-build"
GOENV="/home/bstaletic/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/bstaletic/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build561996429=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. mkdir test && cd test
  2. go mod init t
  3. echo -en 'package main\nimport ( "fmt" )' > main.go
  4. Open main.go in your favourite editor and make a textDocument/codeAction request from cursor position (0, 0).

What did you expect to see?

"Organize imports" code action, with valid Range objects.

What did you see instead?

The "range" keys in the response contain "line":2, which, considering 0-indexing of the protocol, doesn't exist for the main.go in question. This can cause a lot of troubles in some clients.

Additional info:

  • Complete log of exchanged messages:
2020-03-05 23:34:43,116 - DEBUG - TX: Sending message: b'Content-Length: 887\r\n\r\n{"id":1,"jsonrpc":"2.0","method":"initialize","params":{"capabilities":{"textDocument":{"codeAction":{"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"completion":{"completionItem":{"documentationFormat":["plaintext","markdown"]},"completionItemKind":{"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]}},"hover":{"contentFormat":["plaintext","markdown"]},"signatureHelp":{"signatureInformation":{"documentationFormat":["plaintext","markdown"],"parameterInformation":{"labelOffsetSupport":false}}}},"workspace":{"applyEdit":true,"documentChanges":true}},"initializationOptions":{"fuzzyMatching":false,"hoverKind":"Structured"},"processId":25006,"rootPath":"/home/bstaletic/work/test","rootUri":"file:///home/bstaletic/work/test"}}'
2020-03-05 23:34:43,141 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","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","source.organizeImports"]},"codeLensProvider":{},"documentLinkProvider":{},"workspaceSymbolProvider":true,"documentFormattingProvider":true,"documentOnTypeFormattingProvider":{"firstTriggerCharacter":""},"renameProvider":true,"foldingRangeProvider":true,"executeCommandProvider":{"commands":["tidy","upgrade.dependency"]},"workspace":{"workspaceFolders":{"supported":true,"changeNotifications":"workspace/didChangeWorkspaceFolders"}}},"serverInfo":{"name":""}},"id":1}'
2020-03-05 23:34:43,142 - DEBUG - TX: Sending notification: b'Content-Length: 52\r\n\r\n{"jsonrpc":"2.0","method":"initialized","params":{}}'
2020-03-05 23:34:43,142 - DEBUG - TX: Sending notification: b'Content-Length: 132\r\n\r\n{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"fuzzyMatching":false,"hoverKind":"Structured"}}}'
2020-03-05 23:34:43,143 - DEBUG - TX: Sending notification: b'Content-Length: 200\r\n\r\n{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"languageId":"go","text":"package main\\nimport ( \\"fmt\\" )\\n","uri":"file:///home/bstaletic/work/test/main.go","version":1}}}'
2020-03-05 23:34:43,215 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"2020/03/05 23:34:43 Build info\\n----------\\ngolang.org/x/tools/gopls v0.3.3\\n    golang.org/x/tools/gopls@(devel)\\n    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=\\n    github.com/sergi/go-diff@v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=\\n    golang.org/x/mod@v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=\\n    golang.org/x/sync@v0.0.0-20190423024810-112230192c58\\n    golang.org/x/tools@v0.0.0-20200227200655-6862ededa516 =\\u003e ../\\n    golang.org/x/xerrors@v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=\\n    honnef.co/go/tools@v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=\\n    mvdan.cc/xurls/v2@v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=\\n\\nGo info\\n-------\\ngo version go1.14 linux/amd64\\n\\nGO111MODULE=\\"\\"\\nGOARCH=\\"amd64\\"\\nGOBIN=\\"\\"\\nGOCACHE=\\"/home/bstaletic/.cache/go-build\\"\\nGOENV=\\"/home/bstaletic/.config/go/env\\"\\nGOEXE=\\"\\"\\nGOFLAGS=\\"\\"\\nGOHOSTARCH=\\"amd64\\"\\nGOHOSTOS=\\"linux\\"\\nGOINSECURE=\\"\\"\\nGONOPROXY=\\"\\"\\nGONOSUMDB=\\"\\"\\nGOOS=\\"linux\\"\\nGOPATH=\\"/home/bstaletic/go\\"\\nGOPRIVATE=\\"\\"\\nGOPROXY=\\"https://proxy.golang.org,direct\\"\\nGOROOT=\\"/usr/lib/go\\"\\nGOSUMDB=\\"sum.golang.org\\"\\nGOTMPDIR=\\"\\"\\nGOTOOLDIR=\\"/usr/lib/go/pkg/tool/linux_amd64\\"\\nGCCGO=\\"gccgo\\"\\nAR=\\"ar\\"\\nCC=\\"gcc\\"\\nCXX=\\"g++\\"\\nCGO_ENABLED=\\"1\\"\\nGOMOD=\\"/home/bstaletic/work/test/go.mod\\"\\nCGO_CFLAGS=\\"-g -O2\\"\\nCGO_CPPFLAGS=\\"\\"\\nCGO_CXXFLAGS=\\"-g -O2\\"\\nCGO_FFLAGS=\\"-g -O2\\"\\nCGO_LDFLAGS=\\"-g -O2\\"\\nPKG_CONFIG=\\"pkg-config\\"\\nGOGCCFLAGS=\\"-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build349430410=/tmp/go-build -gno-record-gcc-switches\\"\\n"}}'
2020-03-05 23:34:43,455 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"2020/03/05 23:34:43 go/packages.Load\\n\\tsnapshot = 0\\n\\tquery = [./... builtin]\\n\\tpackages = 2"}}'
2020-03-05 23:34:43,730 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///home/bstaletic/work/test/main.go","diagnostics":[{"range":{"start":{"line":1,"character":9},"end":{"line":1,"character":14}},"severity":1,"source":"compiler","message":"\\"fmt\\" imported but not used"}]}}'
2020-03-05 23:34:46,225 - DEBUG - TX: Sending message: b'Content-Length: 240\r\n\r\n{"id":2,"jsonrpc":"2.0","method":"textDocument/codeAction","params":{"context":{"diagnostics":[]},"range":{"end":{"character":12,"line":0},"start":{"character":0,"line":0}},"textDocument":{"uri":"file:///home/bstaletic/work/test/main.go"}}}'
2020-03-05 23:34:46,239 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"2020/03/05 23:34:46 12.784925ms for GOROOT= GOPATH=/home/bstaletic/go GO111MODULE= GOPROXY= PWD=/home/bstaletic/work/test go [go env GOMOD]"}}'
2020-03-05 23:34:46,253 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"2020/03/05 23:34:46 13.457413ms for GOROOT= GOPATH=/home/bstaletic/go GO111MODULE= GOPROXY= PWD=/home/bstaletic/work/test go [go list -modfile=/tmp/go.test.907449470.mod -m -f {{.Path}}\\n{{.Dir}}\\n{{.GoMod}}\\n{{.GoVersion}}\\n{{range context.ReleaseTags}}{{if eq . \\"go1.14\\"}}{{.}}{{end}}{{end}}\\n]"}}'
2020-03-05 23:34:46,259 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"window/logMessage","params":{"type":3,"message":"2020/03/05 23:34:46 5.742742ms for GOROOT= GOPATH=/home/bstaletic/go GO111MODULE= GOPROXY= PWD=/home/bstaletic/work/test go [go list -modfile=/tmp/go.test.907449470.mod -m -json ...]"}}'
2020-03-05 23:34:46,259 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","result":[{"title":"Organize Imports","kind":"source.organizeImports","edit":{"documentChanges":[{"textDocument":{"version":1,"uri":"file:///home/bstaletic/work/test/main.go"},"edits":[{"range":{"start":{"line":0,"character":0},"end":{"line":2,"character":0}},"newText":""},{"range":{"start":{"line":2,"character":0},"end":{"line":2,"character":0}},"newText":"package main"}]}]}}],"id":2}'
  • Just the edit:
[
  {
    "range": {
      "start": {
        "line": 0,
        "character": 0
      },
      "end": {
        "line": 2,
        "character": 0
      }
    },
    "newText": ""
  },
  {
    "range": {
      "start": {
        "line": 2,
        "character": 0
      },
      "end": {
        "line": 2,
        "character": 0
      }
    },
    "newText": "package main"
  }
]

In case the 3rd line exists in the file (actually 1st line after import - not necessarily 3rd), the line offset of 2 is valid and doesn't cause issues.

For reference, this was noticed during the review of a ycmd pull request.

@gopherbot gopherbot added this to the Unreleased milestone Mar 5, 2020
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Mar 5, 2020

Thank you for filing a gopls issue! Please take a look at the Troubleshooting guide, and make sure that you have provided all of the relevant information here.

@stamblerre stamblerre modified the milestones: Unreleased, gopls/v0.4.0 Mar 5, 2020
@stamblerre stamblerre changed the title x/tools/gopls: Line offset out of range in codeAction response x/tools/gopls: line offset out of range in codeAction response Mar 5, 2020
@bstaletic

This comment has been minimized.

Copy link
Author

@bstaletic bstaletic commented Mar 6, 2020

I tried to git bisect gopls.

  • Last known good: gopls/v0.3.0-pre1~397
  • First known bad: gopls/v0.3.0-pre1~299

I couldn't check what's going on with the remaining ~100 commits, because, for the minimal repro, they all hit this line and then gopls just dies.

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
You can’t perform that action at this time.