vscode-go
vscode-go copied to clipboard
Go extension formats files without imports even if formatOnSave is false
Versions
go version go1.18.2 windows/amd64 gopls -v version golang.org/x/tools/gopls v0.8.4 code -v 1.67.2 go extension v0.33.1
Settings (user and workspace)
"editor.formatOnSave": false, "[go]": { "editor.formatOnSave": false },
Describe the bug
Despite having my formatOnSave settings to false everywhere, the extension is formatting my go files that doesn't have imports.
Steps to reproduce the behavior:
- Create file bug.go
- Write this code
type TestStruct struct { Test string }
func (t *TestStruct) Testing() { t.Test = "bug" }
- Save and see the formatting
- Now write this code instead that uses fmt
import "fmt"
type TestStruct struct { Test string }
func (t *TestStruct) Testing() { t.Test = "bug"; fmt.Println("Fixed (not really)") }
- Save and it doesn't format
Hi, I can't repro this, though I may be misunderstanding.
Per https://github.com/golang/vscode-go/wiki/advanced, the correct way to disable auto-formatting is as follows:
"[go]": {
"editor.formatOnSave": false
},
(which you have done)
When I do this, I don't get any formatting. Do you have any other settings (e.g. do you have "go.useLanguageServer": false?).
Change https://go.dev/cl/412054 mentions this issue: docs/faq.md: entries on formatting issues
Hi, I can't repro this, though I may be misunderstanding.
Per https://github.com/golang/vscode-go/wiki/advanced, the correct way to disable auto-formatting is as follows:
"[go]": { "editor.formatOnSave": false },(which you have done)
When I do this, I don't get any formatting. Do you have any other settings (e.g. do you have
"go.useLanguageServer": false?).
Sorry I completely missed your reply but I don't have any other settings. The file you're saving must have no imports, meaning that you shouldn't use any modules, otherwise it works as intended. Are you sure you tried to save a go file without any imports?
The file you're saving must have no imports, meaning that you shouldn't use any modules
Do you mean you do not have go.mod file? (no imports does not necessarily mean no go.mod)
@findleyr I could reproduce the issue with GOPATH mode (GO111MODULE=off), or ad-hoc package mode (no go.mod). I am not sure how much of support gopls wants to provide for those modes.
No, I do have a go.mod file. What I meant was no module/package used in the specific file you're saving (basically just no imports for that file). I didn't change any go settings.
You guys did my 5 steps exactly, with formatOnSave false, and couldn't reproduce?
Can you help me find what I did differently? (the first part is when formatOnSave is false, the second part is when formatOnSave is true) https://user-images.githubusercontent.com/4999471/173849368-a34e752f-05fa-4914-8309-83127b27acb0.mp4
It seems like it's working correctly on your end for some reason. Mine is formatting even when false. Are you using a different formatter? My workaround for now is to disable gopls with "go.useLanguageServer": false, save my file and then re-enable it right after. I have to do that everytime I want to save a file without imports.
No I don't use a different formatter. These are all my settings
Preference : Open Settings (JSON)
{
"go.languageServerFlags": [
"-rpc.trace"
],
"gopls": {
"ui.semanticTokens": true
},
}
Preference: Open Workspace Settings (JSON)
{
"editor.formatOnSave": false,
"[go]": {
"editor.formatOnSave": false
},
}
Can you capture the gopls log (from the beginning to the moment you get the formatting edits) with your minimal repro example and share the log? https://github.com/golang/vscode-go/wiki/troubleshooting#collect-gopls-information
Change https://go.dev/cl/412383 mentions this issue: docs/faq.md: entries on formatting issues
In my case new M1 mac nothing works. Can't even install tools. Formater is not working auto import and auto complete is not working and when I run : Go Install/Update Tools this is what i get.
@realnatuki It looks like a different issue to me. Please open a new issue (following the issue template) if you still experience the issue.