Skip to content

Commit 7ee9980

Browse files
committed
cmd/go: replace formatOutput/showOutput with reportCmd
The general pattern is to replace if len(cmdOut) > 0 { output := b.processOutput(cmdOut) if err != nil { err = formatOutput(b.WorkDir, dir, p.ImportPath, desc, output) } else { b.showOutput(a, dir, desc, output) } } if err != nil { return err } with if err := b.reportCmd(a, p, desc, dir, cmdOut, err); err != nil { return err } However, there is a fair amount of variation between call sites. The most common non-trivial variation is sites where errors are an expected outcome. In this case, often we simply pass "nil" for the error to trigger only the printing behavior of reportCmd. For #62067, but also a nice cleanup on its own. Change-Id: Ie5f918017c02d8558f23ad4c38261077c0fa4ea3 Reviewed-on: https://go-review.googlesource.com/c/go/+/529219 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 5ae9724 commit 7ee9980

File tree

5 files changed

+68
-91
lines changed

5 files changed

+68
-91
lines changed

src/cmd/go/internal/work/cover.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ func WriteCoveragePercent(b *Builder, runAct *Action, mf string, w io.Writer) er
7272
dir := filepath.Dir(mf)
7373
output, cerr := b.CovData(runAct, "percent", "-i", dir)
7474
if cerr != nil {
75-
p := runAct.Package
76-
return formatOutput(b.WorkDir, p.Dir, p.ImportPath,
77-
p.Desc(), string(output))
75+
return b.reportCmd(runAct, nil, "", "", output, cerr)
7876
}
7977
_, werr := w.Write(output)
8078
return werr
@@ -89,9 +87,7 @@ func WriteCoverageProfile(b *Builder, runAct *Action, mf, outf string, w io.Writ
8987
dir := filepath.Dir(mf)
9088
output, err := b.CovData(runAct, "textfmt", "-i", dir, "-o", outf)
9189
if err != nil {
92-
p := runAct.Package
93-
return formatOutput(b.WorkDir, p.Dir, p.ImportPath,
94-
p.Desc(), string(output))
90+
return b.reportCmd(runAct, nil, "", "", output, err)
9591
}
9692
_, werr := w.Write(output)
9793
return werr

src/cmd/go/internal/work/exec.go

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
594594
var objects, cgoObjects, pcCFLAGS, pcLDFLAGS []string
595595

596596
if p.UsesCgo() || p.UsesSwig() {
597-
if pcCFLAGS, pcLDFLAGS, err = b.getPkgConfigFlags(p); err != nil {
597+
if pcCFLAGS, pcLDFLAGS, err = b.getPkgConfigFlags(a); err != nil {
598598
return
599599
}
600600
}
@@ -868,15 +868,7 @@ OverlayLoop:
868868
// Compile Go.
869869
objpkg := objdir + "_pkg_.a"
870870
ofile, out, err := BuildToolchain.gc(b, a, objpkg, icfg.Bytes(), embedcfg, symabis, len(sfiles) > 0, gofiles)
871-
if len(out) > 0 {
872-
output := b.processOutput(out)
873-
if err != nil {
874-
return formatOutput(b.WorkDir, p.Dir, p.ImportPath, p.Desc(), output)
875-
} else {
876-
b.showOutput(a, p.Dir, p.Desc(), output)
877-
}
878-
}
879-
if err != nil {
871+
if err := b.reportCmd(a, nil, "", "", out, err); err != nil {
880872
return err
881873
}
882874
if ofile != objpkg {
@@ -1000,8 +992,11 @@ func (b *Builder) checkDirectives(a *Action) error {
1000992
}
1001993
}
1002994
if msg != nil {
1003-
return formatOutput(b.WorkDir, p.Dir, p.ImportPath, p.Desc(), b.processOutput(msg.Bytes()))
1004-
995+
// We pass a non-nil error to reportCmd to trigger the failure reporting
996+
// path, but the content of the error doesn't matter because msg is
997+
// non-empty.
998+
err := errors.New("invalid directive")
999+
return b.reportCmd(a, nil, "", "", msg.Bytes(), err)
10051000
}
10061001
return nil
10071002
}
@@ -1616,8 +1611,9 @@ func splitPkgConfigOutput(out []byte) ([]string, error) {
16161611
return flags, nil
16171612
}
16181613

1619-
// Calls pkg-config if needed and returns the cflags/ldflags needed to build the package.
1620-
func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string, err error) {
1614+
// Calls pkg-config if needed and returns the cflags/ldflags needed to build a's package.
1615+
func (b *Builder) getPkgConfigFlags(a *Action) (cflags, ldflags []string, err error) {
1616+
p := a.Package
16211617
if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
16221618
// pkg-config permits arguments to appear anywhere in
16231619
// the command line. Move them all to the front, before --.
@@ -1640,8 +1636,8 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
16401636
var out []byte
16411637
out, err = b.runOut(nil, p.Dir, nil, b.PkgconfigCmd(), "--cflags", pcflags, "--", pkgs)
16421638
if err != nil {
1643-
err = formatOutput(b.WorkDir, p.Dir, p.ImportPath, b.PkgconfigCmd()+" --cflags "+strings.Join(pcflags, " ")+" -- "+strings.Join(pkgs, " "), string(out)+err.Error())
1644-
return nil, nil, err
1639+
desc := b.PkgconfigCmd() + " --cflags " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
1640+
return nil, nil, b.reportCmd(a, nil, desc, "", out, err)
16451641
}
16461642
if len(out) > 0 {
16471643
cflags, err = splitPkgConfigOutput(bytes.TrimSpace(out))
@@ -1654,8 +1650,8 @@ func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string,
16541650
}
16551651
out, err = b.runOut(nil, p.Dir, nil, b.PkgconfigCmd(), "--libs", pcflags, "--", pkgs)
16561652
if err != nil {
1657-
err = formatOutput(b.WorkDir, p.Dir, p.ImportPath, b.PkgconfigCmd()+" --libs "+strings.Join(pcflags, " ")+" -- "+strings.Join(pkgs, " "), string(out)+err.Error())
1658-
return nil, nil, err
1653+
desc := b.PkgconfigCmd() + " --libs " + strings.Join(pcflags, " ") + " -- " + strings.Join(pkgs, " ")
1654+
return nil, nil, b.reportCmd(a, nil, desc, "", out, err)
16591655
}
16601656
if len(out) > 0 {
16611657
// We need to handle path with spaces so that C:/Program\ Files can pass
@@ -2511,17 +2507,10 @@ var cgoTypeSigRe = lazyregexp.New(`\b_C2?(type|func|var|macro)_\B`)
25112507
// and returns a non-nil error.
25122508
func (b *Builder) run(a *Action, dir string, desc string, env []string, cmdargs ...any) error {
25132509
out, err := b.runOut(a, dir, env, cmdargs...)
2514-
if len(out) > 0 {
2515-
if desc == "" {
2516-
desc = b.fmtcmd(dir, "%s", strings.Join(str.StringList(cmdargs...), " "))
2517-
}
2518-
if err != nil {
2519-
err = formatOutput(b.WorkDir, dir, a.Package.ImportPath, desc, b.processOutput(out))
2520-
} else {
2521-
b.showOutput(a, dir, desc, b.processOutput(out))
2522-
}
2510+
if desc == "" {
2511+
desc = b.fmtcmd(dir, "%s", strings.Join(str.StringList(cmdargs...), " "))
25232512
}
2524-
return err
2513+
return b.reportCmd(a, nil, desc, dir, out, err)
25252514
}
25262515

25272516
// processOutput prepares the output of runOut to be output to the console.
@@ -2870,38 +2859,33 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
28702859
overlayPath = p
28712860
}
28722861
output, err := b.runOut(a, filepath.Dir(overlayPath), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(overlayPath))
2873-
if len(output) > 0 {
2874-
// On FreeBSD 11, when we pass -g to clang 3.8 it
2875-
// invokes its internal assembler with -dwarf-version=2.
2876-
// When it sees .section .note.GNU-stack, it warns
2877-
// "DWARF2 only supports one section per compilation unit".
2878-
// This warning makes no sense, since the section is empty,
2879-
// but it confuses people.
2880-
// We work around the problem by detecting the warning
2881-
// and dropping -g and trying again.
2882-
if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
2883-
newFlags := make([]string, 0, len(flags))
2884-
for _, f := range flags {
2885-
if !strings.HasPrefix(f, "-g") {
2886-
newFlags = append(newFlags, f)
2887-
}
2888-
}
2889-
if len(newFlags) < len(flags) {
2890-
return b.ccompile(a, p, outfile, newFlags, file, compiler)
2891-
}
2892-
}
28932862

2894-
if err == nil && os.Getenv("GO_BUILDER_NAME") != "" {
2895-
output = append(output, "C compiler warning promoted to error on Go builders\n"...)
2896-
err = errors.New("warning promoted to error")
2863+
// On FreeBSD 11, when we pass -g to clang 3.8 it
2864+
// invokes its internal assembler with -dwarf-version=2.
2865+
// When it sees .section .note.GNU-stack, it warns
2866+
// "DWARF2 only supports one section per compilation unit".
2867+
// This warning makes no sense, since the section is empty,
2868+
// but it confuses people.
2869+
// We work around the problem by detecting the warning
2870+
// and dropping -g and trying again.
2871+
if bytes.Contains(output, []byte("DWARF2 only supports one section per compilation unit")) {
2872+
newFlags := make([]string, 0, len(flags))
2873+
for _, f := range flags {
2874+
if !strings.HasPrefix(f, "-g") {
2875+
newFlags = append(newFlags, f)
2876+
}
28972877
}
2898-
if err != nil {
2899-
err = formatOutput(b.WorkDir, p.Dir, p.ImportPath, p.Desc(), b.processOutput(output))
2900-
} else {
2901-
b.showOutput(a, p.Dir, p.Desc(), b.processOutput(output))
2878+
if len(newFlags) < len(flags) {
2879+
return b.ccompile(a, p, outfile, newFlags, file, compiler)
29022880
}
29032881
}
2904-
return err
2882+
2883+
if len(output) > 0 && err == nil && os.Getenv("GO_BUILDER_NAME") != "" {
2884+
output = append(output, "C compiler warning promoted to error on Go builders\n"...)
2885+
err = errors.New("warning promoted to error")
2886+
}
2887+
2888+
return b.reportCmd(a, p, "", "", output, err)
29052889
}
29062890

29072891
// gccld runs the gcc linker to create an executable from a set of object files.
@@ -2915,7 +2899,6 @@ func (b *Builder) gccld(a *Action, p *load.Package, objdir, outfile string, flag
29152899
}
29162900

29172901
cmdargs := []any{cmd, "-o", outfile, objs, flags}
2918-
dir := p.Dir
29192902
out, err := b.runOut(a, base.Cwd(), b.cCompilerEnv(), cmdargs...)
29202903

29212904
if len(out) > 0 {
@@ -2951,9 +2934,11 @@ func (b *Builder) gccld(a *Action, p *load.Package, objdir, outfile string, flag
29512934
save = append(save, line)
29522935
}
29532936
out = bytes.Join(save, nil)
2954-
if len(out) > 0 && (cfg.BuildN || cfg.BuildX) {
2955-
b.showOutput(nil, dir, p.ImportPath, b.processOutput(out))
2956-
}
2937+
}
2938+
// Note that failure is an expected outcome here, so we report output only
2939+
// in debug mode and don't report the error.
2940+
if cfg.BuildN || cfg.BuildX {
2941+
b.reportCmd(a, p, "", "", out, nil)
29572942
}
29582943
return err
29592944
}
@@ -3493,7 +3478,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
34933478
if pkgpath := gccgoPkgpath(p); pkgpath != "" {
34943479
cgoflags = append(cgoflags, "-gccgopkgpath="+pkgpath)
34953480
}
3496-
if !BuildToolchain.(gccgoToolchain).supportsCgoIncomplete(b) {
3481+
if !BuildToolchain.(gccgoToolchain).supportsCgoIncomplete(b, a) {
34973482
cgoflags = append(cgoflags, "-gccgo_define_cgoincomplete")
34983483
}
34993484
}
@@ -3990,18 +3975,11 @@ func (b *Builder) swigOne(a *Action, p *load.Package, file, objdir string, pcCFL
39903975
}
39913976

39923977
out, err := b.runOut(a, p.Dir, nil, "swig", args, file)
3993-
if err != nil {
3994-
if len(out) > 0 {
3995-
if bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo")) {
3996-
return "", "", errors.New("must have SWIG version >= 3.0.6")
3997-
}
3998-
// swig error
3999-
err = formatOutput(b.WorkDir, p.Dir, p.ImportPath, p.Desc(), b.processOutput(out))
4000-
}
4001-
return "", "", err
3978+
if err != nil && (bytes.Contains(out, []byte("-intgosize")) || bytes.Contains(out, []byte("-cgo"))) {
3979+
return "", "", errors.New("must have SWIG version >= 3.0.6")
40023980
}
4003-
if len(out) > 0 {
4004-
b.showOutput(a, p.Dir, p.Desc(), b.processOutput(out)) // swig warning
3981+
if err := b.reportCmd(a, p, "", "", out, err); err != nil {
3982+
return "", "", err
40053983
}
40063984

40073985
// If the input was x.swig, the output is x.go in the objdir.

src/cmd/go/internal/work/gc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ func (gcToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) er
464464
return nil
465465
}
466466
if err := packInternal(absAfile, absOfiles); err != nil {
467-
return formatOutput(b.WorkDir, p.Dir, p.ImportPath, p.Desc(), err.Error()+"\n")
467+
return b.reportCmd(a, nil, "", "", nil, err)
468468
}
469469
return nil
470470
}

src/cmd/go/internal/work/gccgo.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package work
66

77
import (
8+
"bytes"
89
"fmt"
910
"os"
1011
"os/exec"
@@ -243,12 +244,8 @@ func (tools gccgoToolchain) pack(b *Builder, a *Action, afile string, ofiles []s
243244
return b.run(a, p.Dir, p.ImportPath, nil, tools.ar(), arArgs, "rc", absAfile, absOfiles)
244245
}
245246

246-
if len(output) > 0 {
247-
// Show the output if there is any even without errors.
248-
b.showOutput(a, p.Dir, p.ImportPath, b.processOutput(output))
249-
}
250-
251-
return nil
247+
// Show the output if there is any even without errors.
248+
return b.reportCmd(a, nil, "", "", output, nil)
252249
}
253250

254251
func (tools gccgoToolchain) link(b *Builder, root *Action, out, importcfg string, allactions []*Action, buildmode, desc string) error {
@@ -617,7 +614,10 @@ type I cgo.Incomplete
617614

618615
// supportsCgoIncomplete reports whether the gccgo/GoLLVM compiler
619616
// being used supports cgo.Incomplete, which was added in GCC 13.
620-
func (tools gccgoToolchain) supportsCgoIncomplete(b *Builder) bool {
617+
//
618+
// This takes an Action only for output reporting purposes.
619+
// The result value is unrelated to the Action.
620+
func (tools gccgoToolchain) supportsCgoIncomplete(b *Builder, a *Action) bool {
621621
gccgoSupportsCgoIncompleteOnce.Do(func() {
622622
fail := func(err error) {
623623
fmt.Fprintf(os.Stderr, "cmd/go: %v\n", err)
@@ -650,14 +650,17 @@ func (tools gccgoToolchain) supportsCgoIncomplete(b *Builder) bool {
650650
}
651651
cmd := exec.Command(tools.compiler(), "-c", "-o", on, fn)
652652
cmd.Dir = tmpdir
653-
var buf strings.Builder
653+
var buf bytes.Buffer
654654
cmd.Stdout = &buf
655655
cmd.Stderr = &buf
656656
err = cmd.Run()
657-
if out := buf.String(); len(out) > 0 && cfg.BuildX {
658-
b.showOutput(nil, tmpdir, b.fmtcmd(tmpdir, "%s -c -o %s %s", tools.compiler(), on, fn), out)
659-
}
660657
gccgoSupportsCgoIncomplete = err == nil
658+
if cfg.BuildN || cfg.BuildX {
659+
// Show output. We always pass a nil err because errors are an
660+
// expected outcome in this case.
661+
desc := b.fmtcmd(tmpdir, "%s -c -o %s %s", tools.compiler(), on, fn)
662+
b.reportCmd(a, nil, desc, tmpdir, buf.Bytes(), nil)
663+
}
661664
})
662665
return gccgoSupportsCgoIncomplete
663666
}

src/cmd/go/testdata/script/list_pkgconfig_error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[!exec:pkg-config] skip 'test requires pkg-config tool'
33

44
! go list -export .
5-
stderr '^go build example:\n# pkg-config (.*\n)+pkg-config: exit status \d+$'
5+
stderr '^go build example:\n# pkg-config (.*\n)+Package .* not found$'
66

77
-- go.mod --
88
module example

0 commit comments

Comments
 (0)