From 76dcedc920c6712fc302b053f125e0d7d6db69f0 Mon Sep 17 00:00:00 2001 From: Nodir Turakulov Date: Tue, 20 Oct 2015 21:33:18 -0700 Subject: [PATCH] cmd/go: dedup packages in packagesAndErrors packagesAndErrors function doesn't dedup packages. As a result, `go list io ./io` prints io package twice. Same applies to `go build` and `go test`. * dedup packages. * add a test for go list Change-Id: I54d4063979b1c9359e5416e12327cb85c4823a0f Reviewed-on: https://go-review.googlesource.com/16136 Run-TryBot: Andrew Gerrand Reviewed-by: Andrew Gerrand TryBot-Result: Gobot Gobot --- src/cmd/go/go_test.go | 12 ++++++++++++ src/cmd/go/pkg.go | 21 +++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index a4c91c960a..9f4828b341 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -1372,6 +1372,18 @@ func TestGoListCmdOnlyShowsCommands(t *testing.T) { } } +func TestGoListDedupsPackages(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + tg.run("list", "xtestonly", "./testdata/src/xtestonly/...") + got := strings.TrimSpace(tg.getStdout()) + const want = "xtestonly" + if got != want { + t.Errorf("got %q; want %q", got, want) + } +} + // Issue 4096. Validate the output of unsuccessful go install foo/quxx. func TestUnsuccessfulGoInstallShouldMentionMissingPackage(t *testing.T) { tg := testgo(t) diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go index 539e8b92e1..f7b18743de 100644 --- a/src/cmd/go/pkg.go +++ b/src/cmd/go/pkg.go @@ -1604,15 +1604,24 @@ func packagesAndErrors(args []string) []*Package { } args = importPaths(args) - var pkgs []*Package - var stk importStack - var set = make(map[string]bool) + var ( + pkgs []*Package + stk importStack + seenArg = make(map[string]bool) + seenPkg = make(map[*Package]bool) + ) for _, arg := range args { - if !set[arg] { - pkgs = append(pkgs, loadPackage(arg, &stk)) - set[arg] = true + if seenArg[arg] { + continue } + seenArg[arg] = true + pkg := loadPackage(arg, &stk) + if seenPkg[pkg] { + continue + } + seenPkg[pkg] = true + pkgs = append(pkgs, pkg) } computeStale(pkgs...) -- 2.48.1