aboutsummaryrefslogtreecommitdiff
path: root/imports
diff options
context:
space:
mode:
authorHeschi Kreinick <heschi@google.com>2018-12-12 13:11:39 -0500
committerHeschi Kreinick <heschi@google.com>2018-12-12 18:25:43 +0000
commit5f4a60f04f23ac48e0665f257413ae3eacf339be (patch)
tree69fb4ee9c2694dfcf638edc7a84689ceed5583de /imports
parent837e80568c097270a86b7410b4b6d2f8de427d2e (diff)
downloadgolang-x-tools-5f4a60f04f23ac48e0665f257413ae3eacf339be.tar.gz
imports: fix renamed sibling imports more
This is the fix I should have made in CL 153440. What I get for being in a rush. Fixes golang/go#29180 Change-Id: I7ee3e26173b86c70574b9710f84094e46db27a37 Reviewed-on: https://go-review.googlesource.com/c/153859 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Cottrell <iancottrell@google.com>
Diffstat (limited to 'imports')
-rw-r--r--imports/fix.go7
-rw-r--r--imports/fix_test.go28
2 files changed, 34 insertions, 1 deletions
diff --git a/imports/fix.go b/imports/fix.go
index d009cd50f..a8ac4ac14 100644
--- a/imports/fix.go
+++ b/imports/fix.go
@@ -200,7 +200,12 @@ func (p *pass) findMissingImport(pkg string, syms map[string]bool) *importInfo {
if !ok {
continue
}
- if candidate.name != pkg && pkgInfo.name != pkg {
+ // If the candidate import has a name, it must match pkg.
+ if candidate.name != "" && candidate.name != pkg {
+ continue
+ }
+ // Otherwise, the real name of the package must match.
+ if candidate.name == "" && pkgInfo.name != pkg {
continue
}
diff --git a/imports/fix_test.go b/imports/fix_test.go
index ec22c60bc..f83239f3a 100644
--- a/imports/fix_test.go
+++ b/imports/fix_test.go
@@ -1966,6 +1966,34 @@ func LogSomethingElse() {
}.processTest(t, "foo.com", "p/needs_import.go", nil, nil, want)
}
+// Tests #29180: a sibling import of the right package with the wrong name is used.
+func TestSiblingImport_Misnamed(t *testing.T) {
+ const sibling = `package main
+import renamed "fmt"
+var _ = renamed.Printf
+`
+ const input = `package pkg
+var _ = fmt.Printf
+`
+ const want = `package pkg
+
+import "fmt"
+
+var _ = fmt.Printf
+`
+
+ testConfig{
+ module: packagestest.Module{
+ Name: "foo.com",
+ Files: fm{
+ "pkg/main.go": sibling,
+ "pkg/uses.go": input,
+ },
+ },
+ }.processTest(t, "foo.com", "pkg/uses.go", nil, nil, want)
+
+}
+
func TestPkgIsCandidate(t *testing.T) {
tests := []struct {
name string