Skip to content

Commit f727bef

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]>
1 parent 2dc4ef2 commit f727bef

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

go/packages/golist.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -728,9 +728,6 @@ func golistargs(cfg *Config, words []string) []string {
728728

729729
// invokeGo returns the stdout of a go command invocation.
730730
func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
731-
if debug {
732-
defer func(start time.Time) { log.Printf("%s for %v", time.Since(start), cmdDebugStr(cfg, args...)) }(time.Now())
733-
}
734731
stdout := new(bytes.Buffer)
735732
stderr := new(bytes.Buffer)
736733
cmd := exec.CommandContext(cfg.Context, "go", args...)
@@ -744,6 +741,12 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
744741
cmd.Dir = cfg.Dir
745742
cmd.Stdout = stdout
746743
cmd.Stderr = stderr
744+
if debug {
745+
defer func(start time.Time) {
746+
log.Printf("%s for %v, stderr: <<%s>>\n", time.Since(start), cmdDebugStr(cmd, args...), stderr)
747+
}(time.Now())
748+
}
749+
747750
if err := cmd.Run(); err != nil {
748751
exitErr, ok := err.(*exec.ExitError)
749752
if !ok {
@@ -777,12 +780,12 @@ func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
777780
// be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS
778781
// is set.
779782
if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" {
780-
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cfg, args...), stderr)
783+
fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd, args...), stderr)
781784
}
782785

783786
// debugging
784787
if false {
785-
fmt.Fprintf(os.Stderr, "%s stdout: <<%s>>\n", cmdDebugStr(cfg, args...), stdout)
788+
fmt.Fprintf(os.Stderr, "%s stdout: <<%s>>\n", cmdDebugStr(cmd, args...), stdout)
786789
}
787790

788791
return stdout, nil
@@ -797,13 +800,17 @@ func containsGoFile(s []string) bool {
797800
return false
798801
}
799802

800-
func cmdDebugStr(cfg *Config, args ...string) string {
803+
func cmdDebugStr(cmd *exec.Cmd, args ...string) string {
801804
env := make(map[string]string)
802-
for _, kv := range cfg.Env {
805+
for _, kv := range cmd.Env {
803806
split := strings.Split(kv, "=")
804807
k, v := split[0], split[1]
805808
env[k] = v
806809
}
810+
var quotedArgs []string
811+
for _, arg := range args {
812+
quotedArgs = append(quotedArgs, strconv.Quote(arg))
813+
}
807814

808-
return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %v", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], args)
815+
return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %s", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], strings.Join(quotedArgs, " "))
809816
}

0 commit comments

Comments
 (0)