The interactions between vscode-go's auto-import feature, goimports formatting, and formatOnSave seem often to result in a poorly formatted import block. I'm not sure whether this is a misconfiguration on my part, or a true bug, but it's something I hit frequently and I would love to know of a solution for it.
Steps to reproduce:
- Create and save a file that references a standard library package.
package problem
import (
"fmt"
)
func test() {
fmt.Print("foo")
}
- Add to the function a reference to some package outside the standard library, using auto-complete import for the package. Save the result.
package problem
import (
"fmt"
"github.com/garyburd/redigo/redis"
)
func test() {
fmt.Print("foo", redis.ErrPoolExhausted)
}
- Add to the function a reference to a different standard library package, again using auto-complete import. Save the result.
package problem
import (
"fmt"
"bytes"
"github.com/garyburd/redigo/redis"
)
func test() {
fmt.Print("foo", redis.ErrPoolExhausted, bytes.ErrTooLarge)
}
The auto-complete import added the third package (bytes in this case) to the bottom of the import list, and that caused goimports to leave a newline between it and fmt. The result is that the standard library imports are not grouped together and not sorted correctly. Over time, during active development, this problem magnifies and the import block can have a half-dozen newline-separate groups, alternating between standard and external libraries, rather than having things grouped and sorted correctly.
What can be done to avoid this and get the expected grouping and sorting?
package problem
import (
"bytes"
"fmt"
"github.com/garyburd/redigo/redis"
)
The interactions between vscode-go's auto-import feature,
goimportsformatting, and formatOnSave seem often to result in a poorly formatted import block. I'm not sure whether this is a misconfiguration on my part, or a true bug, but it's something I hit frequently and I would love to know of a solution for it.Steps to reproduce:
The auto-complete import added the third package (
bytesin this case) to the bottom of the import list, and that causedgoimportsto leave a newline between it andfmt. The result is that the standard library imports are not grouped together and not sorted correctly. Over time, during active development, this problem magnifies and the import block can have a half-dozen newline-separate groups, alternating between standard and external libraries, rather than having things grouped and sorted correctly.What can be done to avoid this and get the expected grouping and sorting?