1
0
mirror of https://github.com/golang/go synced 2024-11-16 21:04:45 -07:00

cmd/go: clean up adding import path to command error

Currently, cmdError makes a somewhat fuzzy attempt to ensure the
package import path is part of the printed error, using a string
prefix check. Also, if it decides it does need to add the import path,
it prints it as a "go build" line, which could be misleading because
it can happen outside of "go build".

Clean up the whole code path by explicitly checking the provided error
description against Package.Desc(), and instead of emitting "go build"
in the error message, print it as "# importPath" just like we do in
the common case.

Change-Id: Idb61ac8ffd6920a3d2d282697f4d7d5555ebae0c
Reviewed-on: https://go-review.googlesource.com/c/go/+/534655
Auto-Submit: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
Austin Clements 2023-10-11 12:15:57 -04:00 committed by Gopher Robot
parent 9299547e4d
commit ab5bd15941
2 changed files with 15 additions and 13 deletions

View File

@ -2321,7 +2321,11 @@ func (b *Builder) reportCmd(a *Action, desc, dir string, cmdOut []byte, cmdErr e
out = cgoTypeSigRe.ReplaceAllString(out, "C.") out = cgoTypeSigRe.ReplaceAllString(out, "C.")
} }
err := &cmdError{desc, out, importPath} // Usually desc is already p.Desc(), but if not, signal cmdError.Error to
// add a line explicitly metioning the import path.
needsPath := importPath != "" && p != nil && desc != p.Desc()
err := &cmdError{desc, out, importPath, needsPath}
if cmdErr != nil { if cmdErr != nil {
// The command failed. Report the output up as an error. // The command failed. Report the output up as an error.
return err return err
@ -2360,21 +2364,19 @@ type cmdError struct {
desc string desc string
text string text string
importPath string importPath string
needsPath bool // Set if desc does not already include the import path
} }
func (e *cmdError) Error() string { func (e *cmdError) Error() string {
msg := "# " + e.desc + "\n" + e.text var msg string
if e.importPath != "" && !strings.HasPrefix(e.desc, e.importPath) { if e.needsPath {
// Ensure the import path is part of the message. We checked the prefix // Ensure the import path is part of the message.
// because desc can be a package ID, which may have text in addition to // Clearly distinguish the description from the import path.
// the import path. msg = fmt.Sprintf("# %s\n# [%s]\n", e.importPath, e.desc)
// } else {
// TODO(austin): Checking the prefix seems flimsy. reportCmd could msg = "# " + e.desc + "\n"
// instead check if desc != p.Desc() and leave a flag in cmdError to
// signal this code path.
msg = fmt.Sprintf("go build %s:\n%s", e.importPath, msg)
} }
return msg return msg + e.text
} }
func (e *cmdError) ImportPath() string { func (e *cmdError) ImportPath() string {

View File

@ -2,7 +2,7 @@
[!exec:pkg-config] skip 'test requires pkg-config tool' [!exec:pkg-config] skip 'test requires pkg-config tool'
! go list -export . ! go list -export .
stderr '^go build example:\n# pkg-config (.*\n)+Package .* not found' stderr '^# example\n# \[pkg-config .*\]\n(.*\n)*Package .* not found'
-- go.mod -- -- go.mod --
module example module example