go/packages: improve debug logging
Address a few irritating glitches in the go list debug logging. - Print a fully runnable command line, with args like "a" "b" "c" instead of [a b c]. - Include stderr in the debug logs for cases where the command fails. - Print the correct PWD environment var from cmd instead of cfg. Change-Id: I58e77b370baf8378a21377b81ee2ba5d21a557ab Reviewed-on: https://go-review.googlesource.com/c/163497 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
parent
2dc4ef2775
commit
f727befe75
|
@ -728,9 +728,6 @@ func golistargs(cfg *Config, words []string) []string {
|
||||||
|
|
||||||
// invokeGo returns the stdout of a go command invocation.
|
// invokeGo returns the stdout of a go command invocation.
|
||||||
func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
|
func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
|
||||||
if debug {
|
|
||||||
defer func(start time.Time) { log.Printf("%s for %v", time.Since(start), cmdDebugStr(cfg, args...)) }(time.Now())
|
|
||||||
}
|
|
||||||
stdout := new(bytes.Buffer)
|
stdout := new(bytes.Buffer)
|
||||||
stderr := new(bytes.Buffer)
|
stderr := new(bytes.Buffer)
|
||||||
cmd := exec.CommandContext(cfg.Context, "go", args...)
|
cmd := exec.CommandContext(cfg.Context, "go", args...)
|
||||||
|
@ -744,6 +741,12 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
|
||||||
cmd.Dir = cfg.Dir
|
cmd.Dir = cfg.Dir
|
||||||
cmd.Stdout = stdout
|
cmd.Stdout = stdout
|
||||||
cmd.Stderr = stderr
|
cmd.Stderr = stderr
|
||||||
|
if debug {
|
||||||
|
defer func(start time.Time) {
|
||||||
|
log.Printf("%s for %v, stderr: <<%s>>\n", time.Since(start), cmdDebugStr(cmd, args...), stderr)
|
||||||
|
}(time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
exitErr, ok := err.(*exec.ExitError)
|
exitErr, ok := err.(*exec.ExitError)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -777,12 +780,12 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
|
||||||
// be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS
|
// be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS
|
||||||
// is set.
|
// is set.
|
||||||
if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" {
|
if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" {
|
||||||
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cfg, args...), stderr)
|
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd, args...), stderr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// debugging
|
// debugging
|
||||||
if false {
|
if false {
|
||||||
fmt.Fprintf(os.Stderr, "%s stdout: <<%s>>\n", cmdDebugStr(cfg, args...), stdout)
|
fmt.Fprintf(os.Stderr, "%s stdout: <<%s>>\n", cmdDebugStr(cmd, args...), stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
return stdout, nil
|
return stdout, nil
|
||||||
|
@ -797,13 +800,17 @@ func containsGoFile(s []string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdDebugStr(cfg *Config, args ...string) string {
|
func cmdDebugStr(cmd *exec.Cmd, args ...string) string {
|
||||||
env := make(map[string]string)
|
env := make(map[string]string)
|
||||||
for _, kv := range cfg.Env {
|
for _, kv := range cmd.Env {
|
||||||
split := strings.Split(kv, "=")
|
split := strings.Split(kv, "=")
|
||||||
k, v := split[0], split[1]
|
k, v := split[0], split[1]
|
||||||
env[k] = v
|
env[k] = v
|
||||||
}
|
}
|
||||||
|
var quotedArgs []string
|
||||||
return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %v", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], args)
|
for _, arg := range args {
|
||||||
|
quotedArgs = append(quotedArgs, strconv.Quote(arg))
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %s", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], strings.Join(quotedArgs, " "))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue