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

import formatting clash #1056

@rothskeller

Description

@rothskeller

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"
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions