From 68c5ac90f574c3cf0e181d3cdde7cc60cb38fa9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Fri, 28 Dec 2018 19:44:36 +0100 Subject: [PATCH] go/packages: Load with no patterns should load "." MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit At least on the go list driver, which is the default fallback. Note that we need to call 'go list' on Load("foo") and Load(), but not Load("name=bar"). This is what the current logic was trying to accomplish. But it didn't take into account the case where there are zero initial patterns, in which case we should still call 'go list'. Fix that, and add a test. Fixes #28767. Change-Id: I40af9eb7f2407449c5683df1403928e2c57c86a4 Reviewed-on: https://go-review.googlesource.com/c/155898 Run-TryBot: Daniel Martí Reviewed-by: Michael Matloob TryBot-Result: Gobot Gobot --- go/packages/golist.go | 7 ++++--- go/packages/packages_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/go/packages/golist.go b/go/packages/golist.go index ffe36d2d..a5717cc0 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -120,7 +120,6 @@ extractQueries: } } } - patterns = restPatterns // TODO(matloob): Remove the definition of listfunc and just use golistPackages once go1.12 is released. var listfunc driver @@ -139,8 +138,10 @@ extractQueries: response := &responseDeduper{} var err error - // see if we have any patterns to pass through to go list. - if len(restPatterns) > 0 { + // See if we have any patterns to pass through to go list. Zero initial + // patterns also requires a go list call, since it's the equivalent of + // ".". + if len(restPatterns) > 0 || len(patterns) == 0 { dr, err := listfunc(cfg, restPatterns...) if err != nil { return nil, err diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index b5d451fd..3ef87ffa 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -1277,6 +1277,30 @@ func testRedundantQueries(t *testing.T, exporter packagestest.Exporter) { } } +// Test that Load with no patterns is equivalent to loading "." via the golist +// driver. +func TestNoPatterns(t *testing.T) { packagestest.TestAll(t, testNoPatterns) } +func testNoPatterns(t *testing.T, exporter packagestest.Exporter) { + exported := packagestest.Export(t, exporter, []packagestest.Module{{ + Name: "golang.org/fake", + Files: map[string]interface{}{ + "a/a.go": `package a;`, + "a/b/b.go": `package b;`, + }}}) + defer exported.Cleanup() + + aDir := filepath.Dir(exported.File("golang.org/fake", "a/a.go")) + exported.Config.Dir = aDir + + initial, err := packages.Load(exported.Config) + if err != nil { + t.Fatal(err) + } + if len(initial) != 1 || initial[0].Name != "a" { + t.Fatalf(`Load() = %v, wanted just the package in the current directory`, initial) + } +} + func TestJSON(t *testing.T) { packagestest.TestAll(t, testJSON) } func testJSON(t *testing.T, exporter packagestest.Exporter) { //TODO: add in some errors