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: no folding ranges for invalid code #41281

Open
ghost opened this issue Sep 7, 2020 · 20 comments
Open

x/tools/gopls: no folding ranges for invalid code #41281

ghost opened this issue Sep 7, 2020 · 20 comments
Labels
gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@ghost
Copy link

ghost commented Sep 7, 2020

Not sure why this has happened however out of the blue Visual Studio Code is now unfolding all code every time the file is saved. I have tried uninstalling/reinstalling and removing extensions though nothing has worked.

All code is unfolded every single time the file is saved and results in a lot of lost time. I reported the issue in the Visual Studio Issue for Bugs and was told that the issue is with the Go Extension itself. Basically it appears that the Go Extension for VSCode is causing the code to unfold every time the code is saved. See what was said in the VSCode issues report below

I can reproduce.

From debugging I see that the go extension now also registers a folding range provider.
However that provider doesn't work well with incomplete code. It then returns no folding ranges, what results in all folded ranges to go way.
E.g. no ranges returned for that code:

package main
w
import (
"fmt"
"io"
"net/http"

"github.com/microsoft/vscode-remote-try-go/hello"
)

func handle(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, hello.Hello())
}
Can you file the issue against the go extension? https://github.com/golang/vscode-go
Thanks a lot!

Please direct general questions to:

Please review the documentation before filing an issue.
Helpful pages include:

Please answer these questions before submitting your issue. Thanks!

What version of Go, VS Code & VS Code Go extension are you using?

  • Run go version to get version of Go
    go version go1.15.1 windows/amd64
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
    1.48.2
    a0479759d6e9ea56afa657e454193f72aef85bd0
    x64
  • Check your installed extensions to get the version of the VS Code Go extension
    Go 0.16.2
  • Run go env to get the go development environment details
    set GO111MODULE=
    set GOARCH=amd64
    set GOBIN=
    set GOCACHE=C:\Users\nicholasimon\AppData\Local\go-build
    set GOENV=C:\Users\nicholasimon\AppData\Roaming\go\env
    set GOEXE=.exe
    set GOFLAGS=
    set GOHOSTARCH=amd64
    set GOHOSTOS=windows
    set GOINSECURE=
    set GOMODCACHE=C:\Users\nicholasimon\go\pkg\mod
    set GONOPROXY=
    set GONOSUMDB=
    set GOOS=windows
    set GOPATH=C:\Users\nicholasimon\go
    set GOPRIVATE=
    set GOPROXY=https://proxy.golang.org,direct
    set GOROOT=c:\go
    set GOSUMDB=sum.golang.org
    set GOTMPDIR=
    set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
    set GCCGO=gccgo
    set AR=ar
    set CC=gcc
    set CXX=g++
    set CGO_ENABLED=1
    set GOMOD=
    set CGO_CFLAGS=-g -O2
    set CGO_CPPFLAGS=
    set CGO_CXXFLAGS=-g -O2
    set CGO_FFLAGS=-g -O2
    set CGO_LDFLAGS=-g -O2
    set PKG_CONFIG=pkg-config
    set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\NICHOL~1\AppData\Local\Temp\go-build025678214=/tmp/go-build -gno-record-gcc-switches

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file.
None

Describe the bug

Not sure why this has happened however out of the blue Visual Studio Code is now unfolding all code every time the file is saved. I have tried uninstalling/reinstalling and removing extensions though nothing has worked.

All code is unfolded every single time the file is saved and results in a lot of lost time. I reported the issue in the Visual Studio Issue for Bugs and was told that the issue is with the Go Extensions itself. Basically it appears that the Go Extension for VSCode is causing the code to unfold every time the code is saved. See what was said in the VSCode issues report below

Steps to reproduce the behavior:

Save a folded file, all fold unfold

@hyangah
Copy link
Contributor

hyangah commented Sep 8, 2020

@nicholasimon thanks for the issue report. Analyzing incomplete code is tricky. For now, you may try to turn off the autosave option. (e.g "files.autoSave": "off").

@stamblerre I could easily reproduce bothwith the old provider and with the new LSP based provider. Since we are moving towards the LSP-based solution, I want us to focus on the new way ("go.useLanguageServer": true). The LSP spec does not explicitly specify the behavior around incomplete code handling.

  • Is it possible for gopls to preserve some folding ranges when folding range requests came in as results of edit activities with auto save on?
  • Strangely "files.autoSave": "off" trick does not fix this issue when the language server is enabled "go.useLanguageServer":true. Do you know any other way to prevent folding range requests?

EDIT: Just realized we never had any folding range provider without LSP. (see golang/vscode-go#134)
So, this is the gopls (or the LSP client) issue.
I confirmed the problem does not exist with the default folding range provider implemented inside vscode

@ghost
Copy link
Author

ghost commented Sep 8, 2020

Thanks for letting me know however turning off autosave does nothing. What happens is when I save manually, it unfolds everything below the area of code that I am working on (see image). So everything above remains folded and then everything below is unfolded.

Anyway, just wanted to bring it to your attention, it would be great if it could be fixed in future as it is very, very annoying though I do understand these things take time

Annotation 2020-09-08 162524

@hyangah
Copy link
Contributor

hyangah commented Sep 8, 2020

@nicholasimon Thanks! Do me a favor - can you please double check if you have the language server running? When you run "View: Toggle Output", if you see "gopls (server)" channel from the drop down menu, the language server is running.

@ghost
Copy link
Author

ghost commented Sep 8, 2020

Not at my desk will be back just now and check for you

@ghost
Copy link
Author

ghost commented Sep 8, 2020

I am not understanding exactly what I need to do.

I ran View: Toggle Output which I did then I navigated to VIEW > OPEN VIEW and you can see the drop down there in the image. Don't think this is what you meant? Sorry if you can just explain what drop down menu when you say "channel from the drop down menu, "

Let me know if this is correct or I should do something else

Annotation 2020-09-08 175941

@stamblerre
Copy link
Contributor

You need to do View -> Output, and then you'll see a drop-down that looks like this on the right-hand side. You're looking for an item that says "gopls (server)".

Screen Shot 2020-09-08 at 12 09 20 PM

Screen Shot 2020-09-08 at 12 08 37 PM

@ghost
Copy link
Author

ghost commented Sep 8, 2020

Are you on Windows? It is not the same I think, you need to Click VIEW in top menu then OPEN VIEW I found a very similar list and it does not appear that gopls is present in the list

Annotation 2020-09-08 185523
2

@ghost
Copy link
Author

ghost commented Sep 8, 2020

What exactly is gopls? Am I supposed to have installed this as well? I just installed the Go Extension from VSCode marketplace nothing else. Would it be fixed by installing gopls?

@hyangah
Copy link
Contributor

hyangah commented Sep 8, 2020

@nicholasimon `View: Toggle Output" will open "OUTPUT" section at the bottom part of the window, like the following. Then, use the drop down on the pane to find gopls (server). This screenshot was taken from my windows machine.

Screen Shot 2020-09-08 at 1 37 11 PM

If gopls doesn't appear in the list, sorry that I cannot reproduce your issue.
And, our extension does not seem to implement the folding range provider (the implementation is inside the vscode
https://github.com/microsoft/vscode/blob/master/extensions/go/language-configuration.json) at all.

Would it be fixed by installing gopls?

No - see my comment (https://github.com/golang/vscode-go/issues/617#issuecomment-688890175). I could reproduce the exact behavior only when gopls is enabled.

Gopls is the language server this extension launches to provide the intellisense and all the language analysis services. See doc for more info. It is currently opt-in and you don't need to enable it now if you are currently happy without it.

@ghost
Copy link
Author

ghost commented Sep 8, 2020

Okay there is a screenshot of my Output, I do not see gopls on the list anyway I have read the doc and am going to install it as well as switch from Go Extension to Go Nightly and see if the issue persists.

Annotation 2020-09-08 200739

@ghost
Copy link
Author

ghost commented Sep 8, 2020

For some reason after installing gopls it is now working perfectly, as it should. Have tested with Go Extension as well as Go Nightly and it works fine. Retains my folding on save as it should, the error therefore must relate something to not having gopls installed or not.

It is now much better, thanks for the help it is appreciated.

@hyangah
Copy link
Contributor

hyangah commented Sep 8, 2020

@nicholasimon thanks for trying gopls. Honestly I am now more confused because this problem happens to me only if I use gopls. :-(

@stamblerre if it works with gopls, please feel free to close this issue - maybe some misconfiguration on my side?.
If you observed what I saw, feel free to transfer to the gopls tracker.

@stamblerre stamblerre changed the title Go Extension causes Unfold All on Save x/tools/gopls: no folding ranges for invalid code Sep 9, 2020
@stamblerre stamblerre transferred this issue from golang/vscode-go Sep 9, 2020
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Sep 9, 2020
@gopherbot gopherbot added this to the Unreleased milestone Sep 9, 2020
@stamblerre stamblerre removed this from the Unreleased milestone Sep 9, 2020
@ghost
Copy link
Author

ghost commented Sep 9, 2020

That doesn't make sense at all as I had never heard of gopls before you told me so it can't be because of gopls as I have never used or it installed it before. Anyway it is up to you is you want to close the issue. Once again thanks for the help.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/291569 mentions this issue: internal/lsp/source: error for foldingRange in case of parse error

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/299569 mentions this issue: src/goLanguageServer: replace [] foldingRange response with undefined

gopherbot pushed a commit to golang/vscode-go that referenced this issue Mar 8, 2021
In the presence of parse errors, gopls may fail to compute
foldingRanges correctly and return only partial results.
Partial results can confuse editors and cause to drop still
valid, previously known folding ranges information.

Currently, LSP does not allow a way for the server to tell
clients the server is not ready for answering to the request.
VSCode foldingRange API allows registered foldingRange
provider to opt out of the folding range computation by
providing undefined.

https://code.visualstudio.com/api/references/vscode-api#FoldingRangeProvider
"Returns a list of folding ranges or null and undefined
if the provider does not want to participate or was cancelled."

But, LSP currently does not disginguish undefined/null and
empty results. Raising an error may be an option, but some
LSP clients may not handle errors in user-friendly ways. Thus
gopls devs want to avoid raising errors here.

With https://go-review.googlesource.com/c/tools/+/291569, gopls
will return an empty foldingRange response when encountering
parse errors instead of returning incomplete results.
This CL adds provideFoldingRanges in the middleware, that
converts an empty foldingRange response with 'undefined'
if the document is not empty.

Manually tested.
Testing it in the integration test setup is currently tricky
until gopls with the change is released.

Updates #1224
Updates golang/go#41281

Change-Id: I32cfb30d7e6a9874b9d1cb6c7e5309f19ee081e5
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/299569
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
gopherbot pushed a commit to golang/tools that referenced this issue Mar 8, 2021
When parse errors occur, go's parse package cannot recover nicely.
gopls tried to compute folding ranges based on the partial info
in this case, but returning partial folding range info confuses
editors (vscode) and results in dropping previous folding range
info from the region after the parse error location.

This CL makes gopls not to return anything - so the editor can
tell the result is not believable and ignore it.

The ideal solution is to return a response explicitly surfacing
this case, but currently LSP (3.16, as of today) does not have
a way to describe this condition. See the discussion in
microsoft/language-server-protocol#1200.

We also tried to make gopls return an error. While it worked
nicely in VSCode, we are not sure about how other editors handle
errors from foldingRange. So, instead, we just let gopls return
an empty result - since foldingRange is already broken in this
case, we hope it doesn't add a lot of noise to existing users.

VSCode Go will check the response from the middleware. If the
response is empty but the file is not empty, VSCode Go will
ignore the response.
(https://go-review.googlesource.com/c/vscode-go/+/299569)

Updates golang/vscode-go#1224
Updates golang/go#41281

Change-Id: I917d6667508aabbca1906137eb5e21a97a6cfdaf
Reviewed-on: https://go-review.googlesource.com/c/tools/+/291569
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/300290 mentions this issue: [release] src/goLanguageServer: replace [] foldingRange response with undefined

@findleyr
Copy link
Member

@hyangah what's left to do here? Should we prioritize this? Thanks.

@ghost
Copy link
Author

ghost commented Aug 14, 2022

Just to let you know this is not resolved. Folding works fine in VS Code with Go extension up to a few thousand lines (not sure of the exact point) however at a certain point it no longer works.

I have worked out (since my original post) that this relates to SETTINGS > FORMAT ON SAVE > FILE (default). Something about having thousands of lines of Go then it breaks formatting of the entire file when saving and it unfolds everything. Currently the workaround I use is to set FORMAT ON SAVE to MODIFICATIONS then it does not try and format the entire file. Then you can change it to FILE (default) occasionally and it will format the file correctly.

image

@findleyr findleyr removed this from the gopls/unplanned milestone Aug 22, 2022
@findleyr findleyr added this to the gopls/later milestone Aug 22, 2022
@findleyr
Copy link
Member

Thanks @nicholasimon -- I've put this back in our backlog.

@atilkan
Copy link

atilkan commented Dec 24, 2023

I have the same problem. It does it both on save and any syntax error while typing.

Go extension version: v0.40.1
VSCode version: 1.85
macOS Venture

Before:
Screenshot 2023-12-24 at 19 57 17

While typing: (I am yet typing, notice last line)
Screenshot 2023-12-24 at 19 57 34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. help wanted Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants