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: formatting corrupts /**/ comments with CRLF line endings #40355

Closed
Radranic opened this issue Jul 22, 2020 · 5 comments
Closed

x/tools/gopls: formatting corrupts /**/ comments with CRLF line endings #40355

Radranic opened this issue Jul 22, 2020 · 5 comments
Labels
Milestone

Comments

@Radranic
Copy link

@Radranic Radranic commented Jul 22, 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 windows/amd64
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
    • 1.47.1
    • 485c41f9460bdb830c4da12c102daff275415b53
    • x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.15.1
  • Run go env to get the go development environment details
set GO111MODULE=
set GOARCH=amd64
set GOBIN=F:\GoProj\bin
set GOCACHE=C:\Users\XXXXXX\AppData\Local\go-build
set GOENV=C:\Users\XXXXXX\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=F:\GoProj
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\XXXXXX\AppData\Local\Temp\go-build023548266=/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.
Share all the settings with the go. or ["go"] or gopls prefixes.

    "go.buildOnSave": "workspace",
    "go.lintOnSave": "workspace",
    "go.coverageOptions": "showUncoveredCodeOnly",
    "go.lintTool": "golangci-lint",
    "go.formatTool": "goimports",
    "go.useLanguageServer": true,

Describe the bug

When working on some files with the CRLF line endings, if the auto formatter is going to make changes to the import lines (and maybe others?) it can get into a state where it seems to not account for the length of the line endings.

I indicate "seems" as the more lines I give it to remove, the more extra characters it adds back into the file in the wrong place that are splices of nearby lines.

It also behaves if I switch the line ending to LF which is when I started counting the number of characters of garbage I was getting.

Steps to reproduce the behavior:

  • Open the attached file:minimal.txt
  • Rename to minimal.go
  • Open minimal.go in vscode
  • Ensure line endings is set to CRLF in the bottom right of the editor
  • Save the file
  • The import section will be removed correctly, but at the end of the comment (which is now line 5) will have an extra /

Screenshots or recordings

Before and after here:
FormatBug

@Radranic
Copy link
Author

@Radranic Radranic commented Jul 22, 2020

Just changed my format tool to goreturns and the behavior changed.

It went away in the attached example file, but just caused different things in my actual code file.

@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Jul 22, 2020

gopls doesn't respect the go.formatTool setting, so I don't think it's possible that that setting affected this.
I've actually seen this before, so I think it's a gopls issue. In general, I would stick to using LF line endings, as all of the Go formatters will apply them to your file no matter what.

@stamblerre stamblerre transferred this issue from golang/vscode-go Jul 22, 2020
@stamblerre stamblerre changed the title On save autoformat corrupts file based on number of lines removed x/tools/gopls: formatting corrupts /**/ comments with CRLF line endings Jul 22, 2020
@gopherbot gopherbot added this to the Unreleased milestone Jul 22, 2020
@Radranic
Copy link
Author

@Radranic Radranic commented Jul 23, 2020

As a note, it's not just comment blocks.

I ran into this before by accident but couldn't find/figure out a reproduction file then.

At that time it was duplicating a large portion of the end of one of my functions about 3/4 of the way down the file.

@gopherbot
Copy link

@gopherbot gopherbot commented Jul 27, 2020

Change https://golang.org/cl/244897 mentions this issue: internal/lsp: reproduce bad formatting with CRLF line endings

@Radranic
Copy link
Author

@Radranic Radranic commented Jul 28, 2020

Thanks for the fast fix!

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.