Skip to content

x/tools/cmd/goimports: import blocks consistency #15299

@cezarsa

Description

@cezarsa

Having the following files inside $GOPATH/src:

$ tree
.
├── code.google.com
│   └── p
│       └── x
│           └── b
│               └── b.go
└── github.com
    ├── foo
    │   └── bar
    │       └── bar.go
    └── x
        └── a
            └── a.go

And the bar.go file containing:

package bar

import (
    "fmt"
)

var (
    _ = fmt.Println
    _ = a.Foo
    _ = b.Foo
)

Running goimports -d github.com/foo/bar results in:

@@ -2,6 +2,9 @@

 import (
    "fmt"
+
+   "code.google.com/p/x/b"
+   "github.com/x/a"
 )

 var (

This seems like the correct thing to do according to https://github.com/golang/go/wiki/CodeReviewComments#imports, adding a single new import block for external code.

However if we change the bar.go file to:

package bar

import (
    "fmt"

    "github.com/x/a"
)

var (
    _ = fmt.Println
    _ = a.Foo
    _ = b.Foo
)

And execute goimports -d github.com/foo/bar again, the result is quite different:

@@ -3,6 +3,8 @@
 import (
    "fmt"

+   "code.google.com/p/x/b"
+
    "github.com/x/a"
 )

Instead of adding the new import path to the existing block of external code, a new block is created. This only seems to happen if the added import path is from a domain not yet present in the existing external block. This seems weird as goimports itself adds them together if they weren't present.

So my question, is this the expected behavior for goimports or is this a bug? If it's confirmed a bug I may take a shot at creating a CL to fix it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions