From 12af403624baa87700fe05db3e21c26e410871fd Mon Sep 17 00:00:00 2001 From: Manlio Perillo Date: Tue, 27 Apr 2021 14:48:20 +0200 Subject: [PATCH] cmd/go/internal/bug: use envcmd instead of go env Add the printGoEnv function to print the go environment variables, using the envcmd package instead of invoking go env. Add the PrintEnv function to the envcmd package, to avoid duplicating code. Updates #45803 Change-Id: I38d5b936c0ebb16e741ffbee4309b95d6d0ecc6c Reviewed-on: https://go-review.googlesource.com/c/go/+/314230 Reviewed-by: Bryan C. Mills Reviewed-by: Michael Matloob Run-TryBot: Bryan C. Mills TryBot-Result: Go Bot --- src/cmd/go/internal/bug/bug.go | 14 +++++++++----- src/cmd/go/internal/envcmd/env.go | 20 +++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/cmd/go/internal/bug/bug.go b/src/cmd/go/internal/bug/bug.go index df63c579d98..307527c695c 100644 --- a/src/cmd/go/internal/bug/bug.go +++ b/src/cmd/go/internal/bug/bug.go @@ -20,6 +20,7 @@ import ( "cmd/go/internal/base" "cmd/go/internal/cfg" + "cmd/go/internal/envcmd" "cmd/go/internal/web" ) @@ -90,17 +91,20 @@ func printEnvDetails(w io.Writer) { fmt.Fprintf(w, "### What operating system and processor architecture are you using (`go env`)?\n\n") fmt.Fprintf(w, "
go env Output
\n")
 	fmt.Fprintf(w, "$ go env\n")
-	goexe, err := os.Executable()
-	if err != nil {
-		goexe = filepath.Join(runtime.GOROOT(), "bin/go")
-	}
-	printCmdOut(w, "", goexe, "env")
+	printGoEnv(w)
 	printGoDetails(w)
 	printOSDetails(w)
 	printCDetails(w)
 	fmt.Fprintf(w, "
\n\n") } +func printGoEnv(w io.Writer) { + env := envcmd.MkEnv() + env = append(env, envcmd.ExtraEnvVars()...) + env = append(env, envcmd.ExtraEnvVarsCostly()...) + envcmd.PrintEnv(w, env) +} + func printGoDetails(w io.Writer) { printCmdOut(w, "GOROOT/bin/go version: ", filepath.Join(runtime.GOROOT(), "bin/go"), "version") printCmdOut(w, "GOROOT/bin/go tool compile -V: ", filepath.Join(runtime.GOROOT(), "bin/go"), "tool", "compile", "-V") diff --git a/src/cmd/go/internal/envcmd/env.go b/src/cmd/go/internal/envcmd/env.go index aad5d704e58..8dbb8af1e75 100644 --- a/src/cmd/go/internal/envcmd/env.go +++ b/src/cmd/go/internal/envcmd/env.go @@ -10,6 +10,7 @@ import ( "encoding/json" "fmt" "go/build" + "io" "os" "path/filepath" "runtime" @@ -347,27 +348,32 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) { return } + PrintEnv(os.Stdout, env) +} + +// PrintEnv prints the environment variables to w. +func PrintEnv(w io.Writer, env []cfg.EnvVar) { for _, e := range env { if e.Name != "TERM" { switch runtime.GOOS { default: - fmt.Printf("%s=\"%s\"\n", e.Name, e.Value) + fmt.Fprintf(w, "%s=\"%s\"\n", e.Name, e.Value) case "plan9": if strings.IndexByte(e.Value, '\x00') < 0 { - fmt.Printf("%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''")) + fmt.Fprintf(w, "%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''")) } else { v := strings.Split(e.Value, "\x00") - fmt.Printf("%s=(", e.Name) + fmt.Fprintf(w, "%s=(", e.Name) for x, s := range v { if x > 0 { - fmt.Printf(" ") + fmt.Fprintf(w, " ") } - fmt.Printf("%s", s) + fmt.Fprintf(w, "%s", s) } - fmt.Printf(")\n") + fmt.Fprintf(w, ")\n") } case "windows": - fmt.Printf("set %s=%s\n", e.Name, e.Value) + fmt.Fprintf(w, "set %s=%s\n", e.Name, e.Value) } } }