aboutsummaryrefslogtreecommitdiff
path: root/imports
diff options
context:
space:
mode:
authorkazyshr <kazyshr0301@gmail.com>2019-03-31 21:03:24 +0900
committerBrad Fitzpatrick <bradfitz@golang.org>2019-04-07 03:08:57 +0000
commit0fdf0c73855bae8482c5d3907a9e06f33ff70a10 (patch)
tree3aabb735e74b1a65529c9154edb809ae847c404e /imports
parenta81264a82310305db9c186558cb6466c4057c97d (diff)
downloadgolang-x-tools-0fdf0c73855bae8482c5d3907a9e06f33ff70a10.tar.gz
imports: fix circular imports
goimports will add an import for the package of target source file accidentally, so check if package path is different from target source file when finding import candidates. Fixes golang/go#30663 Change-Id: I77c29bc74bef6c888e63ccb501b013a5fbc30b5c Reviewed-on: https://go-review.googlesource.com/c/tools/+/170238 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'imports')
-rw-r--r--imports/fix.go2
-rw-r--r--imports/fix_test.go22
2 files changed, 23 insertions, 1 deletions
diff --git a/imports/fix.go b/imports/fix.go
index 4c0339305..777d28ccd 100644
--- a/imports/fix.go
+++ b/imports/fix.go
@@ -1043,7 +1043,7 @@ func findImport(ctx context.Context, env *fixEnv, dirScan []*pkg, pkgName string
// Find candidate packages, looking only at their directory names first.
var candidates []pkgDistance
for _, pkg := range dirScan {
- if pkgIsCandidate(filename, pkgName, pkg) {
+ if pkg.dir != pkgDir && pkgIsCandidate(filename, pkgName, pkg) {
candidates = append(candidates, pkgDistance{
pkg: pkg,
distance: distance(pkgDir, pkg.dir),
diff --git a/imports/fix_test.go b/imports/fix_test.go
index d34fef66e..101978c1a 100644
--- a/imports/fix_test.go
+++ b/imports/fix_test.go
@@ -2066,6 +2066,28 @@ var _ = fmt.Printf
}
+// Tests that an input file's own package is ignored.
+func TestIgnoreOwnPackage(t *testing.T) {
+ const input = `package pkg
+
+const _ = pkg.X
+`
+ const want = `package pkg
+
+const _ = pkg.X
+`
+
+ testConfig{
+ module: packagestest.Module{
+ Name: "foo.com",
+ Files: fm{
+ "pkg/a.go": "package pkg\nconst X = 1",
+ "pkg/b.go": input,
+ },
+ },
+ }.processTest(t, "foo.com", "pkg/b.go", nil, nil, want)
+}
+
func TestPkgIsCandidate(t *testing.T) {
tests := []struct {
name string