From f727befe758c04ce68d52abc6e69ad111e2c6797 Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Fri, 22 Feb 2019 15:48:23 -0500 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Dmitri Shuralyov --- go/packages/golist.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/go/packages/golist.go b/go/packages/golist.go index 2fee7fb1..e053b5e9 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -728,9 +728,6 @@ func golistargs(cfg *Config, words []string) []string { // invokeGo returns the stdout of a go command invocation. 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) stderr := new(bytes.Buffer) 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.Stdout = stdout 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 { exitErr, ok := err.(*exec.ExitError) if !ok { @@ -777,12 +780,12 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) { // be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS // is set. 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 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 @@ -797,13 +800,17 @@ func containsGoFile(s []string) bool { return false } -func cmdDebugStr(cfg *Config, args ...string) string { +func cmdDebugStr(cmd *exec.Cmd, args ...string) string { env := make(map[string]string) - for _, kv := range cfg.Env { + for _, kv := range cmd.Env { split := strings.Split(kv, "=") k, v := split[0], split[1] env[k] = v } + var quotedArgs []string + for _, arg := range args { + quotedArgs = append(quotedArgs, strconv.Quote(arg)) + } - return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %v", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], args) + return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %s", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], strings.Join(quotedArgs, " ")) }