aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2022-04-18 13:12:43 -0400
committerGopher Robot <gobot@golang.org>2022-04-20 15:44:57 +0000
commit17f8d98a4adf8386e63a0d2902ff42ca5e80996e (patch)
treefcc5f878c26c4d20a03c680d14986b59be5ed414
parentcbc9e589053e7339c1e3c4d6e88c6c015792efce (diff)
downloadgo-17f8d98a4adf8386e63a0d2902ff42ca5e80996e.tar.gz
go-17f8d98a4adf8386e63a0d2902ff42ca5e80996e.zip
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 <bcmills@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Auto-Submit: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
-rw-r--r--src/go/internal/srcimporter/srcimporter.go8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/go/internal/srcimporter/srcimporter.go b/src/go/internal/srcimporter/srcimporter.go
index d7ec6691bc7..ea6f01280a7 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)