Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Formatting a Go document mangles and breaks code. #2969

Closed
danielniccoli opened this issue Jan 6, 2020 · 13 comments
Closed

Formatting a Go document mangles and breaks code. #2969

danielniccoli opened this issue Jan 6, 2020 · 13 comments
Labels
upstream-gopls Issue for gopls

Comments

@danielniccoli
Copy link

danielniccoli commented Jan 6, 2020

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

  • Run go version to get version of Go
    • go version go1.13.5 windows/amd64
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
    • 1.41.1 26076a4de974ead31f97692a0d32f90d735645c0 x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • Go 0.12.0
  • Run go env GOOS GOARCH to get the operating system and processor arhcitecture details
    • windows amd64

Share the Go related settings you have added/edited

{
    "python.pythonPath": "/usr/bin/python3",
    "go.useLanguageServer": true
}

Describe the bug

When formatting the document, code gets mangled and broken.
Happens also when saving.

Steps to reproduce the behavior:

Happens when formatting the document (ALT-SHIFT-F).
Sometimes also when saving the document.
I can't always reproduce this.

Screenshots or recordings

CEoqaCxUcw

It also breaks the import statement

@stamblerre
Copy link
Contributor

What is the output of gopls version? Can you share your gopls logs for when this happens? The instructions for capturing gopls logs are here: https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#capturing-logs.

@ramya-rao-a
Copy link
Contributor

@Borkason Can you please provide the details @stamblerre is asking above?

@darjun
Copy link

darjun commented Jan 15, 2020

I encountered the same problem. gopls version:

$ gopls version
golang.org/x/tools/gopls 0.2.2
    golang.org/x/tools/gopls@v0.2.2 h1:ujGisyytgY1VGcmd66wIJ9+wVAfmodXj6daHM43HRXk=

but by "View: Debug Console" -> "Output" -> "Tasks" -> "gopls", I cann't find the gopls log.

It's just a black windows there at Debug Console.

this is my user settings.json:

{
    "git.ignoreMissingGitWarning": true,
    "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
    "explorer.confirmDragAndDrop": false,
    "go.autocompleteUnimportedPackages": true,
    "editor.insertSpaces": false,
	"editor.detectIndentation": false,
	"go.formatTool": "gofmt",
	"explorer.confirmDelete": false,
	"go.gotoSymbol.includeGoroot": true,
	"go.gotoSymbol.includeImports": true,
	"go.useLanguageServer": true
}

@stamblerre
Copy link
Contributor

@darjun: You can also try Ctrl + Shift + P -> "View: Toggle Output", and then there should be a drop-down menu that says "Tasks" in the right-hand top corner. When you click on it, "gopls" should be in this list. Click on that, and then the log should be there. You will also need to add the following to your settings to get more detailed logging:

"go.languageServerFlags": [
    "-rpc.trace"
]

@stamblerre
Copy link
Contributor

There is also more information on the Go issue tracker (golang/go#34955), so please follow-up there, if possible.

@darjun
Copy link

darjun commented Jan 15, 2020

I put gopls log at gist, this is the link https://gist.github.com/darjun/447159f76166f2d45684da406e646f1c

@stamblerre
Copy link
Contributor

@darjun: Would you be able to provide a smaller log with a repro and the other details requested on golang/go#34955 (feel free to just add a comment to that issue)? The log you've provided seems to be truncated and for a larger project, so it's difficult to parse. Thanks!

@LordNeznay
Copy link

I have the same problem.
I put example of problim into repository: https://github.com/LordNeznay/vscode_go_issue_2969_example
gopls log: https://github.com/LordNeznay/vscode_go_issue_2969_example/blob/master/gopls.log

For reproduce clone repo, open folder in visual studio code, open main.go and save it

@stamblerre
Copy link
Contributor

@LordNeznay: Do you have the same issue with gopls at master (you can download it by running GO111MODULE=on go get golang.org/x/tools/gopls@master golang.org/x/tools@master)?

@LordNeznay
Copy link

@stamblerre, after GO111MODULE=on go get golang.org/x/tools/gopls@master golang.org/x/tools@master the problem has stopped reproducing. So much thank!

@stamblerre
Copy link
Contributor

Glad to hear it! Thanks for following up.

@stamblerre
Copy link
Contributor

@Borkason, @darjun: If you get a chance, please try out gopls at master and let me know if the problem persists. Thank you!

@ramya-rao-a
Copy link
Contributor

Thanks @stamblerre

Since the next version gopls/v0.3.0 will have the fix, closing this issue.
For folks who want to try the fix before the next version is out, please try out gopls from the master branch. You can download it by running

GO111MODULE=on 
go get golang.org/x/tools/gopls@master golang.org/x/tools@master

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
upstream-gopls Issue for gopls
Projects
None yet
Development

No branches or pull requests

5 participants