Skip to content

Lint/Format On Save is broken and violates POLA #1231

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

Closed
indrora opened this issue Feb 16, 2021 · 4 comments
Closed

Lint/Format On Save is broken and violates POLA #1231

indrora opened this issue Feb 16, 2021 · 4 comments
Labels
FrozenDueToAge Question This is a question, rather than an issue report.

Comments

@indrora
Copy link

indrora commented Feb 16, 2021

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

  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.15.8 windows/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
----------
golang.org/x/tools/gopls v0.6.5
    golang.org/x/tools/[email protected] h1:kLt9rD/dWtVdvc8LmdcxagDFih6zxYXREpKSYYZu5KE=
    github.com/BurntSushi/[email protected] h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    github.com/google/[email protected] h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
    github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/[email protected] h1:8pl+sMODzuvGJkmj2W4kZihvVb5mKm8pB/X44PIQHv8=
    golang.org/x/[email protected] h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
    golang.org/x/[email protected] h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
    golang.org/x/[email protected] h1:/wdPW261t381NDQd8TBo63/FyvACfLICwtH8wMRoHJQ=
    golang.org/x/[email protected] h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/[email protected] h1:W18jzjh8mfPez+AwGLxmOImucz/IFjpNlrKVnaj2YVc=
    mvdan.cc/[email protected] h1:hsVv+Y9UsZ/mFZTxJZuHVI6shSQCtzZ11h1JEFPAZLw=
    mvdan.cc/xurls/[email protected] h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=```
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
1.52.1
ea3859d4ba2f3e577a159bc91e3074c5d85c0523
x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.22.1
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
Checking configured tools....
GOBIN: undefined
toolsGopath: 
gopath: C:\Users\indrora\go
GOROOT: C:\Program Files\Go
PATH: C:\bin\Python38\Scripts\;C:\bin\Python38\;C:\Program Files\Amazon Corretto\jdk1.8.0_252\bin;C:\Program Files\Amazon Corretto\jdk11.0.6_10\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\PuTTY\;C:\Program Files\dotnet\;C:\Program Files\Git\cmd;C:\Program Files (x86)\Universal Extractor;C:\Program Files (x86)\Universal Extractor\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Go\bin;C:\Users\indrora\AppData\Local\Microsoft\WindowsApps;C:\Users\indrora\AppData\Local\Programs\Microsoft VS Code\bin;c:\users\indrora\bin\libav\bin\;C:\Users\indrora\AppData\Roaming\Python\Python38\Scripts;C:\Users\indrora\go\bin

   gopkgs: C:\Users\indrora\go\bin\gopkgs.exe installed
   go-outline: C:\Users\indrora\go\bin\go-outline.exe installed
   gotests: gotests not installed
   gomodifytags: gomodifytags not installed
   impl: impl not installed
   goplay: goplay not installed
   dlv: C:\Users\indrora\go\bin\dlv.exe installed
   golint: C:\Users\indrora\go\bin\golint.exe installed
   gopls: C:\Users\indrora\go\bin\gopls.exe installed

go env
Workspace Folder (photo-tool): c:\Users\indrora\gocode\photo-tool
	set GO111MODULE=
	set GOARCH=amd64
	set GOBIN=
	set GOCACHE=C:\Users\indrora\AppData\Local\go-build
	set GOENV=C:\Users\indrora\AppData\Roaming\go\env
	set GOEXE=.exe
	set GOFLAGS=
	set GOHOSTARCH=amd64
	set GOHOSTOS=windows
	set GOINSECURE=
	set GOMODCACHE=C:\Users\indrora\go\pkg\mod
	set GONOPROXY=
	set GONOSUMDB=
	set GOOS=windows
	set GOPATH=C:\Users\indrora\go
	set GOPRIVATE=
	set GOPROXY=https://proxy.golang.org,direct
	set GOROOT=C:\Program Files\Go
	set GOSUMDB=sum.golang.org
	set GOTMPDIR=
	set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
	set GCCGO=gccgo
	set AR=ar
	set CC=gcc
	set CXX=g++
	set CGO_ENABLED=1
	set GOMOD=c:\Users\indrora\gocode\photo-tool\go.mod
	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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\indrora\AppData\Local\Temp\go-build759872926=/tmp/go-build -gno-record-gcc-switches
	

Share the Go related settings you have added/edited

{
    "go.buildOnSave": "off",
    "go.lintOnSave": "off"
}

Describe the bug

A clear and concise description of what the bug.

The document changes.

A clear and concise description of what you expected to happen.

I expect the document to not change.

Steps to reproduce the behavior:

  1. write some code, for instance an import statement
  2. habitually press ^s
  3. wonder what just happened to your import statement, as it is now gone because the linter went "you don't use that you don't need that, right?"

Sometimes the following steps follow naturally:

  1. ANGRILY REPEAT STEPS 1-2
  2. Complain on GitHub

I have Lint On Save turned off, and yet it still does it:

2021-02-16_004057.mp4

As someone learning Go, this behavior

  • Violates POLA
  • Makes me not want to learn Go and in fact pushes me from suggesting others learn Go

How do I stop this from happening? Who knows! I've restarted VS Code, I've uninstalled and reinstalled the extension, and have come to the conclusion that this is a bug.

@indrora
Copy link
Author

indrora commented Feb 16, 2021

Digging through some searching, it appears that this is a common issue when people inadvertently turn this on:

microsoft/vscode#53988
microsoft/vscode#29335

This is a common enough issue that it's been answered a few times on StackOverflow:
https://stackoverflow.com/questions/48124565/why-does-vscode-delete-golang-source-on-save

(it should be noted that the top answer is incomplete and that the following is required to actually achieve a "Don't mess with my code" result:

"[go]": {

        "editor.formatOnSave": false,
        "editor.codeActionsOnSave": {
            "source.organizeImports": false
        },
    },
    "go.formatTool": "gofmt",

This is non-obvious)

This is mentioned as a distinct thing in a Chinese-language overview of the VS Code extension:
https://simpleowen.github.io/post/go_lps_4_vscode/

@hyangah
Copy link
Contributor

hyangah commented Feb 16, 2021

Glad to hear that you figured it out how to disable them from the editor. They can be customized with the general editor settings.

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

Organizing imports and format on save by default is working as intended.
About the unused import/variable, please see https://golang.org/doc/faq#unused_variables_and_imports.

FYI you will not need "go.formatTool": "gofmt" any more because we transitioned to use the language server for formatting recently.

@hyangah hyangah closed this as completed Feb 16, 2021
@hyangah hyangah added the Question This is a question, rather than an issue report. label Feb 16, 2021
@indrora
Copy link
Author

indrora commented Feb 16, 2021

Organizing imports and format on save by default is working as intended.

As a learner, this makes no sense.
As a developer, this makes no sense.

When I save, I expect the content of the buffer to be written to disk. I do not expect save to change the content of the buffer.

This default is broken.

@190n
Copy link

190n commented Sep 26, 2021

Update on this? While I get the appeal of format on save, it's very confusing default behavior when you (like me) don't usually use it, and it would've been even more so for someone who didn't know that formatting on save was a possibility. And I had to dig through quite a few search results before finding this issue. It should at least be easier to turn off.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Question This is a question, rather than an issue report.
Projects
None yet
Development

No branches or pull requests

4 participants