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

vscode golang formatter, I can't modify its config #2556

Closed
iabdo9 opened this issue Dec 4, 2022 · 4 comments
Closed

vscode golang formatter, I can't modify its config #2556

iabdo9 opened this issue Dec 4, 2022 · 4 comments

Comments

@iabdo9
Copy link

iabdo9 commented Dec 4, 2022

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

Go version: go1.19.3
gopls: v0.10.1
vscode: 1.73.1

Share the Go related settings you have added/edited

I didn't edit any settings in Preferences: Open Settings (JSON) after install golang extension
just enable format on save

Describe the bug

when i type code like this in golang and save it (Ctrl + S)

package main
import (
    "fmt"
    "net/http"
)
func main() {
    fmt.Println("Hello World!")
}

this code will be formatted thus:

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}
  • It has added blank lines between the function and import
  • It removed net/http from the code because I didn't use this package
    • This is really annoying, my hand is used to pressing Ctrl + S every moment, so imports that I haven't used yet are always deleted

as a nodejs (js) developer, is used vscode for long time for js development
when i type code like this:

const express = require("express");
fucntion send(text) {
console.log(text)
   }
send(         "Hello World!");

this code will be formatted thus:

const express = require("express");
fucntion send(text) {
    console.log(text)
}
send("Hello World!");

As you can see here, just formatting the spaces,

  1. it didn't remove the import even though I didn't use it
  2. It does not create blank lines between the function and the import

Screenshots or recordings

A gif showing what happens when I save a file

I want to know where can I get the config file for golang formatter, and what's the best way to solve those above problems?

I know this might be counterintuitive to some, but I'm used to a certain way of formatting

@gopherbot gopherbot added this to the Untriaged milestone Dec 4, 2022
@iabdo9
Copy link
Author

iabdo9 commented Dec 4, 2022

As for the imports, I found out that it is golang-based, I can't write a specific package that I haven't used in the code

I wonder if golang can ignore imports that are not used, in any way

It remains to solve the problem of blank lines (For me it is a problem :)

@pjweinb
Copy link

pjweinb commented Dec 4, 2022 via email

@findleyr
Copy link
Member

See discussion about this feature in https://go.dev/issue/54362. The fundamental problem with implementing this is that unused imports are a compile error, and so not cleaning them up leaves code in a broken state. The example there is compelling: if I remove the last use of "net/http" from the file and save, it is burdensome to then have to delete imports.

I think the common workflow with goimports is what @pjweinb describes: never write an import statement: just let goimports manage them for you. With that said, this takes some getting used to, and may not be ideal as unimported completion lacks hover information, and goimports sometimes chooses the wrong import. There is a lot of room for improvement here.

@findleyr
Copy link
Member

With respect to not putting a line between import and function: part of the gofmt philosophy is that there should be one way to format code. It may not be everyone's favorite way (or even anyone's favorite way), but the value of having one way outweighs any individual formatting decision: https://www.youtube.com/watch?v=PAAkCSZUG1c&t=523s

I'm going to close this as a dupe of golang/go#54362, because that issue tracks improving the UX of managing imports.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2022
@golang golang locked and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants