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

Closed
cezarsa opened this issue Apr 14, 2016 · 2 comments
Closed

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

cezarsa opened this issue Apr 14, 2016 · 2 comments
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@cezarsa
Copy link
Contributor

cezarsa 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
Copy link
Contributor

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
Note quite sure, but may be related to this ticket:
golang/go#15299
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 12, 2019
@heschi
Copy link
Contributor

heschi commented Nov 7, 2019

I think this is essentially a dupe of #20818. Please comment if you disagree.

@heschi heschi closed this as completed Nov 7, 2019
@golang golang locked and limited conversation to collaborators Nov 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants