Skip to content

Commit cd57ef8

Browse files
BasavarajBankolligopherbot
authored andcommitted
go/packages: include dependency errors when CompiledGoFiles is missing
When go list fails during cgo processing, go/packages reports a generic "go list failed to return CompiledGoFiles" error. This hides underlying dependency errors returned by go list. Include errors from DepsErrors in the reported message so that tools like gopls can surface the actual compiler failure when available. Fixes golang/go#78083 Change-Id: I4af7baf6424aeaa21df184d9bfb8e0fdf75b4da6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/754340 Auto-Submit: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Peter Weinberger <pjw@google.com> Reviewed-by: Alan Donovan <adonovan@google.com>
1 parent 053fdbc commit cd57ef8

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

go/packages/golist.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,18 @@ func (state *golistState) createDriverResponse(words ...string) (*DriverResponse
562562
} else {
563563
// golang/go#38990: go list silently fails to do cgo processing
564564
pkg.CompiledGoFiles = nil
565+
566+
var msg strings.Builder
567+
fmt.Fprintf(&msg, "go list failed to return CompiledGoFiles for %q.\n", p.Name)
568+
569+
for _, err := range p.DepsErrors {
570+
msg.WriteString(strings.TrimSpace(err.Err))
571+
msg.WriteByte('\n')
572+
}
573+
574+
msg.WriteString("This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990.")
565575
pkg.Errors = append(pkg.Errors, Error{
566-
Msg: "go list failed to return CompiledGoFiles. This may indicate failure to perform cgo processing; try building at the command line. See https://golang.org/issue/38990.",
576+
Msg: msg.String(),
567577
Kind: ListError,
568578
})
569579
}

go/packages/packages_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,6 +3455,57 @@ func main() {
34553455
t.Logf("Packages: %+v", pkgs)
34563456
}
34573457

3458+
// TestCompiledGoFilesIncludesDepsErrors ensures that when CompiledGoFiles
3459+
// is missing, errors reported by go/packages include underlying dependency
3460+
// errors from go list, rather than only a generic message.
3461+
//
3462+
// See golang/go#78083.
3463+
func TestCompiledGoFilesIncludesDepsErrors(t *testing.T) {
3464+
testenv.NeedsGoPackages(t)
3465+
3466+
dir := writeTree(t, `
3467+
-- go.mod --
3468+
module example.com
3469+
go 1.18
3470+
3471+
-- a/a.go --
3472+
package a
3473+
3474+
/*
3475+
#cgo CFLAGS: -I/nonexistent
3476+
#include <missing.h>
3477+
*/
3478+
import "C"
3479+
3480+
func Foo() {}
3481+
`)
3482+
pkgs, err := packages.Load(&packages.Config{
3483+
Dir: dir,
3484+
Mode: packages.LoadAllSyntax,
3485+
Env: append(os.Environ(),
3486+
"CGO_ENABLED=1",
3487+
),
3488+
}, "example.com/a")
3489+
if err != nil {
3490+
t.Fatal(err)
3491+
}
3492+
if len(pkgs) == 0 {
3493+
t.Fatal("no packages loaded")
3494+
}
3495+
found := false
3496+
// Expected error should include something like:
3497+
// fatal error: missing.h: No such file or directory
3498+
// We assert only "missing.h" for portability across systems.
3499+
for _, err := range pkgs[0].Errors {
3500+
if strings.Contains(err.Msg, "missing.h") {
3501+
found = true
3502+
}
3503+
}
3504+
if !found {
3505+
t.Errorf("expected error message to include underlying dependency errors, got: %+v", pkgs[0].Errors)
3506+
}
3507+
}
3508+
34583509
// TestMainPackagePathInModeTypes tests (*types.Package).Path() for
34593510
// main packages in mode NeedTypes, a regression test for #70742, a
34603511
// bug in cmd/compile's export data that caused them to appear as

0 commit comments

Comments
 (0)