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: organize imports formats the package statement #63153

Open
70sh1 opened this issue Sep 17, 2023 · 8 comments
Open

x/tools/gopls: organize imports formats the package statement #63153

70sh1 opened this issue Sep 17, 2023 · 8 comments
Labels
gopls/imports gopls Issues related to the Go language server, gopls.
Milestone

Comments

@70sh1
Copy link

70sh1 commented Sep 17, 2023

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

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.21.1 windows/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • gopls v0.13.2
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.82.1 x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.39.1
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
GOBIN: undefined
toolsGopath: 
gopath: C:\Users\redacted\go
GOROOT: C:\Users\redacted\scoop\apps\go\current
PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\WireGuard\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PowerShell\7\;C:\Program Files\dotnet\;C:\Users\redacted\go\bin;C:\Users\redacted\scoop\apps\python\current\Scripts;C:\Users\redacted\scoop\apps\python\current;C:\Users\redacted\scoop\apps\vscode\current\bin;C:\Users\redacted\scoop\apps\gcc\current\bin;C:\Users\redacted\scoop\apps\rustup\current\.cargo\bin;C:\Users\redacted\scoop\shims;C:\Users\redacted\AppData\Local\Microsoft\WindowsApps;C:\Users\redacted\Apps\poetry\bin;C:\Users\redacted\dev\Inno Setup 6;c:\users\redacted\.local\bin

    go: C:\Users\redacted\scoop\shims\go.exe: go version go1.21.1 windows/amd64

    gotests:    not installed
    gomodifytags:   not installed
    impl:   not installed
    goplay: not installed
    dlv:    C:\Users\redacted\go\bin\dlv.exe   (version: v1.21.0 built with go: go1.21.1)
    staticcheck:    C:\Users\redacted\go\bin\staticcheck.exe   (version: v0.4.6 built with go: go1.21.1)
    gopls:  C:\Users\redacted\go\bin\gopls.exe (version: v0.13.2 built with go: go1.21.1)

go env
Workspace Folder (playground): c:\Users\redacted\dev\go_projects\playground
    set GO111MODULE=
    set GOARCH=amd64
    set GOBIN=
    set GOCACHE=C:\Users\redacted\AppData\Local\go-build
    set GOENV=C:\Users\redacted\AppData\Roaming\go\env
    set GOEXE=.exe
    set GOEXPERIMENT=
    set GOFLAGS=
    set GOHOSTARCH=amd64
    set GOHOSTOS=windows
    set GOINSECURE=
    set GOMODCACHE=C:\Users\redacted\go\pkg\mod
    set GONOPROXY=
    set GONOSUMDB=
    set GOOS=windows
    set GOPATH=C:\Users\redacted\go
    set GOPRIVATE=
    set GOPROXY=https://proxy.golang.org,direct
    set GOROOT=C:\Users\redacted\scoop\apps\go\current
    set GOSUMDB=sum.golang.org
    set GOTMPDIR=
    set GOTOOLCHAIN=auto
    set GOTOOLDIR=C:\Users\redacted\scoop\apps\go\current\pkg\tool\windows_amd64
    set GOVCS=
    set GOVERSION=go1.21.1
    set GCCGO=gccgo
    set GOAMD64=v1
    set AR=ar
    set CC=gcc
    set CXX=g++
    set CGO_ENABLED=1
    set GOMOD=NUL
    set GOWORK=
    set CGO_CFLAGS=-O2 -g
    set CGO_CPPFLAGS=
    set CGO_CXXFLAGS=-O2 -g
    set CGO_FFLAGS=-O2 -g
    set CGO_LDFLAGS=-O2 -g
    set PKG_CONFIG=pkg-config
    set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\redacted\AppData\Local\Temp\go-build1818320928=/tmp/go-build -gno-record-gcc-switches

Share the Go related settings you have added/edited

"[go]": {
   "editor.formatOnSave": false,
   "editor.defaultFormatter": "golang.go"
 },

Describe the bug

Disabling format on save is not possible without disabling gopls altogether.

Steps to reproduce the behavior:

  1. Install Go extension
  2. Create new Go file
  3. Type anything with the wrong formatting e. g. package main
  4. Save the file
@70sh1
Copy link
Author

70sh1 commented Sep 17, 2023

Upon further investigation, it turns out that the bulk of the formatting is indeed turned off, but it is the extra spaces between words that are still removed for some reason (as in the package main example).

@70sh1 70sh1 changed the title Cannot disable format on save Not all formatting is disabled with "editor.formatOnSave": false Sep 17, 2023
@findleyr
Copy link
Contributor

This looks like a dupe of golang/vscode-go#2268, which is still not resolved. Interestingly, I apparently couldn't reproduce that issue, but do reproduce it now.

@hyangah this looks like a VS Code issue: the extension is setting a formatting request to gopls.

@suzmue
Copy link
Contributor

suzmue commented Sep 21, 2023

I think this is actually the result of a formatting from Organize Imports (https://github.com/golang/vscode-go/blob/d9015c19ed5be58bb51f3c53b651fe2468540086/docs/faq.md#how-can-i-stop-the-extension-from-formatting-files-on-save).

Can you try turning off organize imports on save to confirm this is where the bug is?

    "[go]": {
        "editor.formatOnSave": false,
        "editor.defaultFormatter": "golang.go",
        "editor.codeActionsOnSave": {"source.organizeImports": false}
      },     

@suzmue suzmue added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 21, 2023
@suzmue suzmue assigned suzmue and unassigned suzmue Sep 21, 2023
@70sh1
Copy link
Author

70sh1 commented Sep 21, 2023

Confirmed. editor.codeActionsOnSave": {"source.organizeImports": false} indeed solves the issue.

@suzmue suzmue changed the title Not all formatting is disabled with "editor.formatOnSave": false x/tools/gopls: organize imports formats the package statement Sep 21, 2023
@suzmue suzmue removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 21, 2023
@suzmue suzmue transferred this issue from golang/vscode-go Sep 21, 2023
@suzmue suzmue added this to the gopls/backlog milestone Sep 21, 2023
@suzmue
Copy link
Contributor

suzmue commented Sep 21, 2023

Transferred to the gopls issue tracker for the organize imports bug.

@suzmue suzmue added the gopls Issues related to the Go language server, gopls. label Sep 22, 2023
@suzmue suzmue removed their assignment Sep 29, 2023
@abel1502
Copy link

abel1502 commented Feb 8, 2024

Any progress? The proposed fixes don't work for me. The only thing that does is disabling the language server altogether

@findleyr
Copy link
Contributor

@abel1502 you mean the the fixes in #63153 (comment) does not turn off auto-formatting for you? Can you share the full contents of your settings.json?

@abel1502
Copy link

@findleyr Sorry, after fruitlessly fiddling with it for a while I eventually gave up and removed all of my attempted modifications. By now I think I'm used to this behaviour... Neat brainwashing setup you've got, it turns out)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/imports gopls Issues related to the Go language server, gopls.
Projects
None yet
Development

No branches or pull requests

4 participants