Skip to content

x/tools/cmd/goimports: incorrectly adds imports for variables that match package names #13023

@cep21

Description

@cep21

Please see the following console output as a test case. log is a varaible defined in another file and therefor does not need a package import. The expected behavior is for goimports to detect no issues.

11:19:35 jack@Jacks-MacBook-Pro.local:/tmp/b
< go get -u golang.org/x/tools/cmd/goimports
11:20:02 jack@Jacks-MacBook-Pro.local:/tmp/b
< ls -la
total 16
drwxr-xr-x   4 jack  wheel   136B Oct 22 11:18 .
drwxrwxrwt  32 root  wheel   1.1K Oct 22 11:17 ..
-rw-r--r--   1 jack  staff   111B Oct 22 11:18 a.go
-rw-r--r--   1 jack  staff    82B Oct 22 11:18 a_test.go
11:20:05 jack@Jacks-MacBook-Pro.local:/tmp/b
< cat a.go 
package main

import "github.com/Sirupsen/logrus"

var log *logrus.Logger

func init() {
    log = logrus.New()
}
11:20:07 jack@Jacks-MacBook-Pro.local:/tmp/b
< cat a_test.go 
package main

import "testing"

func TestB(t *testing.T) {
    log.Printf("Hello")
}
11:20:09 jack@Jacks-MacBook-Pro.local:/tmp/b
< goimports -d .
diff a_test.go gofmt/a_test.go
--- /var/folders/5r/x8pdxnqs0sdg7w_262278m800000gn/T/gofmt282742701 2015-10-22 11:20:12.000000000 -0700
+++ /var/folders/5r/x8pdxnqs0sdg7w_262278m800000gn/T/gofmt424960296 2015-10-22 11:20:12.000000000 -0700
@@ -1,6 +1,9 @@
 package main

-import "testing"
+import (
+   "log"
+   "testing"
+)

 func TestB(t *testing.T) {
    log.Printf("Hello")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions