imports: support match without trailing slash for -local flag
Make it so that import prefixes specified using the -local flag are considered
a match for an import path if the prefix ends with a '/' and the import path
matches exactly the prefix without a slash. For example, specifying
"golang.org/x/tools/" as a prefix would match the import for the package
"golang.org/x/tools".
Fixes golang/go#24368
Change-Id: I0302db72fda63ad24d7b964aa73f78aa0ebccb37
Reviewed-on: https://go-review.googlesource.com/100460
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/imports/fix.go b/imports/fix.go
index d792a78..68961ba 100644
--- a/imports/fix.go
+++ b/imports/fix.go
@@ -49,7 +49,7 @@
var importToGroup = []func(importPath string) (num int, ok bool){
func(importPath string) (num int, ok bool) {
for _, p := range localPrefixes() {
- if strings.HasPrefix(importPath, p) {
+ if strings.HasPrefix(importPath, p) || strings.TrimSuffix(p, "/") == importPath {
return 3, true
}
}
diff --git a/imports/fix_test.go b/imports/fix_test.go
index 9d93b65..10e397d 100644
--- a/imports/fix_test.go
+++ b/imports/fix_test.go
@@ -1449,6 +1449,29 @@
{
config: testConfig{
gopathFiles: map[string]string{
+ "foo/foo.go": "package foo \n const X = 1",
+ "foo/bar/bar.go": "package bar \n const X = 1",
+ },
+ },
+ localPrefix: "foo/",
+ src: "package main \n const Y = bar.X \n const Z = foo.X \n const _ = runtime.GOOS",
+ want: `package main
+
+import (
+ "runtime"
+
+ "foo"
+ "foo/bar"
+)
+
+const Y = bar.X
+const Z = foo.X
+const _ = runtime.GOOS
+`,
+ },
+ {
+ config: testConfig{
+ gopathFiles: map[string]string{
"example.org/pkg/pkg.go": "package pkg \n const A = 1",
"foo/bar/bar.go": "package bar \n const B = 1",
"code.org/r/p/expproj/expproj.go": "package expproj \n const C = 1",