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

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

Open
cezarsa opened this issue Apr 14, 2016 · 1 comment

Comments

@cezarsa
Copy link
Contributor

commented Apr 14, 2016

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.

@bradfitz

This comment has been minimized.

Copy link
Member

commented Apr 14, 2016

Have at it. Manipulating the imports is a bit of a black art. Have fun!

/cc @griesemer

@bradfitz bradfitz added this to the Unplanned milestone Apr 14, 2016

jandubois added a commit to cloudfoundry-incubator/fissile that referenced this issue Apr 19, 2016
HCF-590 Hack around a bug in goimports
Note quite sure, but may be related to this ticket:
golang/go#15299

@gopherbot gopherbot added the Tools label Sep 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.