1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

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 <matloob@golang.org>
This commit is contained in:
Gergely Brautigam 2019-03-02 21:03:14 +01:00 committed by Michael Matloob
parent b6b7807791
commit 36c10c0a62
2 changed files with 33 additions and 0 deletions

View File

@ -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.

View File

@ -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",