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

Go extension formats files without imports even if formatOnSave is false #2268

Open
simchev opened this issue May 28, 2022 · 11 comments
Open
Labels
gopls gopls related issues upstream-tools Issues that are caused by problems in the tools that the extension depends on.
Milestone

Comments

@simchev
Copy link

simchev commented May 28, 2022

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
@gopherbot gopherbot added this to the Untriaged milestone May 28, 2022
@findleyr
Copy link
Contributor

findleyr commented Jun 1, 2022

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 findleyr added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jun 1, 2022
hyangah added a commit to hyangah/vscode-go that referenced this issue Jun 13, 2022
And add back the title that was dropped by accident.

Updates golang#2268
Updates golang#1815
Updates golang#1805

Change-Id: Iea464b1e54f0eb5d421d28c7bea40a86e49dc517
hyangah added a commit to hyangah/vscode-go that referenced this issue Jun 14, 2022
And add back the title that was dropped by accident.

Updates golang#2268
Updates golang#1815
Updates golang#1805

Change-Id: Iea464b1e54f0eb5d421d28c7bea40a86e49dc517
@gopherbot
Copy link
Collaborator

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

gopherbot pushed a commit that referenced this issue Jun 14, 2022
And add back the title that was dropped by accident.

Updates #2268
Updates #1815
Updates #1805

Change-Id: Iea464b1e54f0eb5d421d28c7bea40a86e49dc517
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/412054
Reviewed-by: Jamal Carvalho <jamal@golang.org>
@simchev
Copy link
Author

simchev commented Jun 15, 2022

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?

@hyangah
Copy link
Contributor

hyangah commented Jun 15, 2022

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.

@simchev
Copy link
Author

simchev commented Jun 15, 2022

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?

@hyangah
Copy link
Contributor

hyangah commented Jun 15, 2022

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

@simchev
Copy link
Author

simchev commented Jun 15, 2022

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.

@hyangah
Copy link
Contributor

hyangah commented Jun 15, 2022

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

@gopherbot
Copy link
Collaborator

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

@bebimoj

This comment was marked as off-topic.

@hyangah

This comment was marked as off-topic.

@hyangah hyangah added gopls gopls related issues upstream-tools Issues that are caused by problems in the tools that the extension depends on. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls gopls related issues upstream-tools Issues that are caused by problems in the tools that the extension depends on.
Projects
None yet
Development

No branches or pull requests

5 participants