]> Cypherpunks repositories - gostls13.git/commitdiff
go/internal/srcimporter: add context to cgo errors
authorBryan C. Mills <bcmills@google.com>
Mon, 18 Apr 2022 17:12:43 +0000 (13:12 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 20 Apr 2022 15:44:57 +0000 (15:44 +0000)
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>

src/go/internal/srcimporter/srcimporter.go

index d7ec6691bc79cb37913a47c097727ff1b1e973ce..ea6f01280a73b58f9f2447ac88916902e29501ac 100644 (file)
@@ -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)