vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

Go extension formats files without imports even if formatOnSave is false

Open simchev opened this issue 3 years ago • 11 comments

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:

  1. Create file bug.go
  2. Write this code
type TestStruct struct { Test string }
func (t *TestStruct) Testing() { t.Test = "bug" }
  1. Save and see the formatting
  2. 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)") }
  1. Save and it doesn't format

simchev avatar May 28 '22 01:05 simchev

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?).

findleyr avatar Jun 01 '22 19:06 findleyr

Change https://go.dev/cl/412054 mentions this issue: docs/faq.md: entries on formatting issues

gopherbot avatar Jun 14 '22 04:06 gopherbot

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?

simchev avatar Jun 15 '22 07:06 simchev

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.

hyangah avatar Jun 15 '22 13:06 hyangah

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?

simchev avatar Jun 15 '22 14:06 simchev

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

hyangah avatar Jun 15 '22 14:06 hyangah

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.

simchev avatar Jun 15 '22 15:06 simchev

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

hyangah avatar Jun 15 '22 19:06 hyangah

Change https://go.dev/cl/412383 mentions this issue: docs/faq.md: entries on formatting issues

gopherbot avatar Jun 17 '22 18:06 gopherbot

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.

Screenshot 2022-06-26 at 08 27 56

bebimoj avatar Jun 26 '22 06:06 bebimoj

@realnatuki It looks like a different issue to me. Please open a new issue (following the issue template) if you still experience the issue.

hyangah avatar Jul 28 '22 14:07 hyangah