go/buildutil: ExpandPatterns: ignore a trailing slash in a pattern

Previously, a trailing slash would cause "foo/" to be treated as the
name of a package, with comical results.

Fixed golang/go#14584

Change-Id: I660f8a079bbd63d3645812e516a9264c8e080e61
Reviewed-on: https://go-review.googlesource.com/30452
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Alan Donovan 2016-10-05 14:42:45 -04:00
parent 27f88d9b7a
commit 03663480fa
2 changed files with 6 additions and 1 deletions

View File

@ -137,6 +137,9 @@ func allPackages(ctxt *build.Context, root string, ch chan<- item) {
// //
// encoding/... -encoding/xml // encoding/... -encoding/xml
// //
// A trailing slash in a pattern is ignored. (Path components of Go
// package names are separated by slash, not the platform's path separator.)
//
func ExpandPatterns(ctxt *build.Context, patterns []string) map[string]bool { func ExpandPatterns(ctxt *build.Context, patterns []string) map[string]bool {
// TODO(adonovan): support other features of 'go list': // TODO(adonovan): support other features of 'go list':
// - "std"/"cmd"/"all" meta-packages // - "std"/"cmd"/"all" meta-packages
@ -187,7 +190,7 @@ func ExpandPatterns(ctxt *build.Context, patterns []string) map[string]bool {
} }
} else { } else {
// single package // single package
doPkg(arg, neg) doPkg(strings.TrimSuffix(arg, "/"), neg)
} }
} }

View File

@ -61,6 +61,8 @@ func TestExpandPatterns(t *testing.T) {
{"...", "encoding encoding/hex encoding/json encoding/xml fmt"}, {"...", "encoding encoding/hex encoding/json encoding/xml fmt"},
{"encoding/... -encoding/xml", "encoding encoding/hex encoding/json"}, {"encoding/... -encoding/xml", "encoding encoding/hex encoding/json"},
{"... -encoding/...", "fmt"}, {"... -encoding/...", "fmt"},
{"encoding", "encoding"},
{"encoding/", "encoding"},
} { } {
var pkgs []string var pkgs []string
for pkg := range buildutil.ExpandPatterns(ctxt, strings.Fields(test.patterns)) { for pkg := range buildutil.ExpandPatterns(ctxt, strings.Fields(test.patterns)) {