diff --git a/go/packages/golist.go b/go/packages/golist.go index 067b008d..c464f698 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 0f584b14..82aeb0e5 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",