From c41d1439521820892798a1ede0322bc890f8dd4c Mon Sep 17 00:00:00 2001 From: Nick Miyake Date: Tue, 13 Mar 2018 14:48:25 -0700 Subject: [PATCH] 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 --- imports/fix.go | 2 +- imports/fix_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/imports/fix.go b/imports/fix.go index d792a78c..68961ba6 100644 --- a/imports/fix.go +++ b/imports/fix.go @@ -49,7 +49,7 @@ func localPrefixes() []string { 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 9d93b659..10e397d4 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -1444,6 +1444,29 @@ import ( const Y = bar.X const _ = runtime.GOOS +`, + }, + { + 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 `, }, {