Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

import formatting clash #1056

Closed
rothskeller opened this issue Jun 23, 2017 · 2 comments
Closed

import formatting clash #1056

rothskeller opened this issue Jun 23, 2017 · 2 comments

Comments

@rothskeller
Copy link

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:

  1. Create and save a file that references a standard library package.
package problem

import (
	"fmt"
)

func test() {
	fmt.Print("foo")
}
  1. 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)
}
  1. 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"
)
@ramya-rao-a
Copy link
Contributor

Adding the import to the start of the import list enables goimports/goreturns to group/sort the imports properly.

Fixed with 21ef733

Thanks for reporting!

@ramya-rao-a
Copy link
Contributor

The fix is now out in the latest update (0.6.63) to the Go extension

@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants