Skip to content

Commit 36c10c0

Browse files
Skarlsomatloob
authored andcommitted
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 <[email protected]>
1 parent b6b7807 commit 36c10c0

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

go/packages/golist.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,16 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
770770
return nil, goTooOldError{fmt.Errorf("unsupported version of go: %s: %s", exitErr, stderr)}
771771
}
772772

773+
// This error only appears in stderr. See golang.org/cl/166398 for a fix in go list to show
774+
// the error in the Err section of stdout in case -e option is provided.
775+
// This fix is provided for backwards compatibility.
776+
if len(stderr.String()) > 0 && strings.Contains(stderr.String(), "named files must be .go files") {
777+
output := fmt.Sprintf(`{"ImportPath": "","Incomplete": true,"Error": {"Pos": "","Err": %s}}`,
778+
strconv.Quote(strings.Trim(stderr.String(), "\n")))
779+
fmt.Println(output)
780+
return bytes.NewBufferString(output), nil
781+
}
782+
773783
// Export mode entails a build.
774784
// If that build fails, errors appear on stderr
775785
// (despite the -e flag) and the Export field is blank.

go/packages/packages_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,29 @@ func TestLoadAbsolutePath(t *testing.T) {
317317
}
318318
}
319319

320+
func TestReturnErrorWhenUsingNoneGoFiles(t *testing.T) {
321+
exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
322+
Name: "golang.org/gopatha",
323+
Files: map[string]interface{}{
324+
"a/a.go": `package a`,
325+
}}, {
326+
Name: "golang.org/gopathb",
327+
Files: map[string]interface{}{
328+
"b/b.c": `package b`,
329+
}}})
330+
defer exported.Cleanup()
331+
config := packages.Config{}
332+
_, err := packages.Load(&config, "a/a.go", "b/b.c")
333+
if err == nil {
334+
t.Fatalf("should have failed with an error")
335+
}
336+
got := err.Error()
337+
want := "named files must be .go files"
338+
if !strings.Contains(got, want) {
339+
t.Fatalf("want error message: %s, got: %s", want, got)
340+
}
341+
}
342+
320343
func TestVendorImports(t *testing.T) {
321344
exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{{
322345
Name: "golang.org/fake",

0 commit comments

Comments
 (0)