From 36c10c0a621fb2fede4686e8ae7bb1d900735d53 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam Date: Sat, 2 Mar 2019 21:03:14 +0100 Subject: [PATCH] x/tools/go/packages: seems to do nothing when given non-Go files This CL fixes packages ignoring errors regarding files that have non .go extensions. Packages can be called with just file names or path which includes files. These aren't checked at all by packages if they are go files or not, but it fails silently because of it. In more detail, go list fails with named files error in STDERR. However, that is ignored, because go list notoriously abused STDERR for non-error messages. Fixes golang/go#29899 Change-Id: Ie4dc39da0b87200ebd23e6c607396557685e2807 Reviewed-on: https://go-review.googlesource.com/c/tools/+/164663 Reviewed-by: Michael Matloob --- go/packages/golist.go | 10 ++++++++++ go/packages/packages_test.go | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/go/packages/golist.go b/go/packages/golist.go index 067b008d3c2..c464f69865c 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -770,6 +770,16 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) { return nil, goTooOldError{fmt.Errorf("unsupported version of go: %s: %s", exitErr, stderr)} } + // This error only appears in stderr. See golang.org/cl/166398 for a fix in go list to show + // the error in the Err section of stdout in case -e option is provided. + // This fix is provided for backwards compatibility. + if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "named files must be .go files") { + output := fmt.Sprintf(`{"ImportPath": "","Incomplete": true,"Error": {"Pos": "","Err": %s}}`, + strconv.Quote(strings.Trim(stderr.String(), "\n"))) + fmt.Println(output) + return bytes.NewBufferString(output), nil + } + // Export mode entails a build. // If that build fails, errors appear on stderr // (despite the -e flag) and the Export field is blank. diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index 0f584b14f5f..82aeb0e51aa 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -317,6 +317,29 @@ func TestLoadAbsolutePath(t *testing.T) { } } +func TestReturnErrorWhenUsingNoneGoFiles(t *testing.T) { + exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ + Name: "golang.org/gopatha", + Files: map[string]interface{}{ + "a/a.go": `package a`, + }}, { + Name: "golang.org/gopathb", + Files: map[string]interface{}{ + "b/b.c": `package b`, + }}}) + defer exported.Cleanup() + config := packages.Config{} + _, err := packages.Load(&config, "a/a.go", "b/b.c") + if err == nil { + t.Fatalf("should have failed with an error") + } + got := err.Error() + want := "named files must be .go files" + if !strings.Contains(got, want) { + t.Fatalf("want error message: %s, got: %s", want, got) + } +} + func TestVendorImports(t *testing.T) { exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{ Name: "golang.org/fake",