Skip to content

Commit 12af403

Browse files
perilloBryan C. Mills
authored and
Bryan C. Mills
committed
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 <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 5c69cb2 commit 12af403

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/cmd/go/internal/bug/bug.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"cmd/go/internal/base"
2222
"cmd/go/internal/cfg"
23+
"cmd/go/internal/envcmd"
2324
"cmd/go/internal/web"
2425
)
2526

@@ -90,17 +91,20 @@ func printEnvDetails(w io.Writer) {
9091
fmt.Fprintf(w, "### What operating system and processor architecture are you using (`go env`)?\n\n")
9192
fmt.Fprintf(w, "<details><summary><code>go env</code> Output</summary><br><pre>\n")
9293
fmt.Fprintf(w, "$ go env\n")
93-
goexe, err := os.Executable()
94-
if err != nil {
95-
goexe = filepath.Join(runtime.GOROOT(), "bin/go")
96-
}
97-
printCmdOut(w, "", goexe, "env")
94+
printGoEnv(w)
9895
printGoDetails(w)
9996
printOSDetails(w)
10097
printCDetails(w)
10198
fmt.Fprintf(w, "</pre></details>\n\n")
10299
}
103100

101+
func printGoEnv(w io.Writer) {
102+
env := envcmd.MkEnv()
103+
env = append(env, envcmd.ExtraEnvVars()...)
104+
env = append(env, envcmd.ExtraEnvVarsCostly()...)
105+
envcmd.PrintEnv(w, env)
106+
}
107+
104108
func printGoDetails(w io.Writer) {
105109
printCmdOut(w, "GOROOT/bin/go version: ", filepath.Join(runtime.GOROOT(), "bin/go"), "version")
106110
printCmdOut(w, "GOROOT/bin/go tool compile -V: ", filepath.Join(runtime.GOROOT(), "bin/go"), "tool", "compile", "-V")

src/cmd/go/internal/envcmd/env.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"go/build"
13+
"io"
1314
"os"
1415
"path/filepath"
1516
"runtime"
@@ -347,27 +348,32 @@ func runEnv(ctx context.Context, cmd *base.Command, args []string) {
347348
return
348349
}
349350

351+
PrintEnv(os.Stdout, env)
352+
}
353+
354+
// PrintEnv prints the environment variables to w.
355+
func PrintEnv(w io.Writer, env []cfg.EnvVar) {
350356
for _, e := range env {
351357
if e.Name != "TERM" {
352358
switch runtime.GOOS {
353359
default:
354-
fmt.Printf("%s=\"%s\"\n", e.Name, e.Value)
360+
fmt.Fprintf(w, "%s=\"%s\"\n", e.Name, e.Value)
355361
case "plan9":
356362
if strings.IndexByte(e.Value, '\x00') < 0 {
357-
fmt.Printf("%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''"))
363+
fmt.Fprintf(w, "%s='%s'\n", e.Name, strings.ReplaceAll(e.Value, "'", "''"))
358364
} else {
359365
v := strings.Split(e.Value, "\x00")
360-
fmt.Printf("%s=(", e.Name)
366+
fmt.Fprintf(w, "%s=(", e.Name)
361367
for x, s := range v {
362368
if x > 0 {
363-
fmt.Printf(" ")
369+
fmt.Fprintf(w, " ")
364370
}
365-
fmt.Printf("%s", s)
371+
fmt.Fprintf(w, "%s", s)
366372
}
367-
fmt.Printf(")\n")
373+
fmt.Fprintf(w, ")\n")
368374
}
369375
case "windows":
370-
fmt.Printf("set %s=%s\n", e.Name, e.Value)
376+
fmt.Fprintf(w, "set %s=%s\n", e.Name, e.Value)
371377
}
372378
}
373379
}

0 commit comments

Comments
 (0)