From: Bryan C. Mills Date: Mon, 18 Apr 2022 17:12:43 +0000 (-0400) Subject: go/internal/srcimporter: add context to cgo errors X-Git-Tag: go1.19beta1~622 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=17f8d98a4adf8386e63a0d2902ff42ca5e80996e;p=gostls13.git go/internal/srcimporter: add context to cgo errors An error message like "could not import os/user (exit status 1)" (observed in https://go.dev/issue/52407) is fairly inscrutable. On the other hand, srcimporter doesn't report errors with quite enough structure to dump the entire stderr output from 'go tool cgo' without potentially overwhelming the caller. Here, we split the difference by describing which command failed but not printing the output of that command. For #52407, that would at least provide a stronger clue connecting to #52408. Change-Id: Iabdc95b17ba20a0f6ff38e5c7084e5081e1ef5e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/400817 Run-TryBot: Bryan Mills Reviewed-by: Matthew Dempsky Auto-Submit: Bryan Mills TryBot-Result: Gopher Robot --- diff --git a/src/go/internal/srcimporter/srcimporter.go b/src/go/internal/srcimporter/srcimporter.go index d7ec6691bc..ea6f01280a 100644 --- a/src/go/internal/srcimporter/srcimporter.go +++ b/src/go/internal/srcimporter/srcimporter.go @@ -136,7 +136,7 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type setUsesCgo(&conf) file, err := p.cgo(bp) if err != nil { - return nil, err + return nil, fmt.Errorf("error processing cgo for package %q: %w", bp.ImportPath, err) } files = append(files, file) } @@ -223,9 +223,9 @@ func (p *Importer) cgo(bp *build.Package) (*ast.File, error) { args = append(args, bp.CgoCPPFLAGS...) if len(bp.CgoPkgConfig) > 0 { cmd := exec.Command("pkg-config", append([]string{"--cflags"}, bp.CgoPkgConfig...)...) - out, err := cmd.CombinedOutput() + out, err := cmd.Output() if err != nil { - return nil, err + return nil, fmt.Errorf("pkg-config --cflags: %w", err) } args = append(args, strings.Fields(string(out))...) } @@ -237,7 +237,7 @@ func (p *Importer) cgo(bp *build.Package) (*ast.File, error) { cmd := exec.Command(args[0], args[1:]...) cmd.Dir = bp.Dir if err := cmd.Run(); err != nil { - return nil, err + return nil, fmt.Errorf("go tool cgo: %w", err) } return parser.ParseFile(p.fset, filepath.Join(tmpdir, "_cgo_gotypes.go"), nil, 0)