aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorHiroshi Ioka <hirochachacha@gmail.com>2017-09-19 18:18:09 +0900
committerIan Lance Taylor <iant@golang.org>2017-09-20 03:54:16 +0000
commitfb54abe9ce3cbec6d464c238406b05502cb34eeb (patch)
tree596ae2eb647e4c2b0b1d023ede6c85d5a55099b7 /src/cmd
parent88ced021907fb96d5609a3c63db0d9738bf0ac2b (diff)
downloadgo-fb54abe9ce3cbec6d464c238406b05502cb34eeb.tar.xz
all: correct location of go tool
In general, there are no guarantee that `go` command exist on $PATH. This CL tries to get `go` command from $GOROOT/bin instead. There are three kinds of code we should handle: For normal code, the CL implements goCmd() or goCmdName(). For unit tests, the CL uses testenv.GoTool() or testenv.GoToolPath(). For integration tests, the CL sets PATH=$GOROOT/bin:$PATH in cmd/dist. Note that make.bash sets PATH=$GOROOT/bin:$PATH in the build process. So this change is only useful when we use toolchain manually. Updates #21875 Change-Id: I963b9f22ea732dd735363ececde4cf94a5db5ca2 Reviewed-on: https://go-review.googlesource.com/64650 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/api/goapi.go14
-rw-r--r--src/cmd/api/goapi_test.go3
-rw-r--r--src/cmd/api/run.go15
-rw-r--r--src/cmd/dist/test.go8
-rw-r--r--src/cmd/go/go_test.go11
-rw-r--r--src/cmd/link/internal/ld/nooptcgolink_test.go2
-rw-r--r--src/cmd/trace/pprof.go16
7 files changed, 61 insertions, 8 deletions
diff --git a/src/cmd/api/goapi.go b/src/cmd/api/goapi.go
index 936f9e5511..8cc78c01ed 100644
--- a/src/cmd/api/goapi.go
+++ b/src/cmd/api/goapi.go
@@ -27,6 +27,18 @@ import (
"strings"
)
+func goCmd() string {
+ var exeSuffix string
+ if runtime.GOOS == "windows" {
+ exeSuffix = ".exe"
+ }
+ path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
+ if _, err := os.Stat(path); err == nil {
+ return path
+ }
+ return "go"
+}
+
// Flags
var (
checkFile = flag.String("c", "", "optional comma-separated filename(s) to check API against")
@@ -127,7 +139,7 @@ func main() {
if flag.NArg() > 0 {
pkgNames = flag.Args()
} else {
- stds, err := exec.Command("go", "list", "std").Output()
+ stds, err := exec.Command(goCmd(), "list", "std").Output()
if err != nil {
log.Fatal(err)
}
diff --git a/src/cmd/api/goapi_test.go b/src/cmd/api/goapi_test.go
index 0d00f6a297..3c4e50a21a 100644
--- a/src/cmd/api/goapi_test.go
+++ b/src/cmd/api/goapi_test.go
@@ -9,6 +9,7 @@ import (
"flag"
"fmt"
"go/build"
+ "internal/testenv"
"io/ioutil"
"os"
"os/exec"
@@ -163,7 +164,7 @@ func TestSkipInternal(t *testing.T) {
}
func BenchmarkAll(b *testing.B) {
- stds, err := exec.Command("go", "list", "std").Output()
+ stds, err := exec.Command(testenv.GoToolPath(b), "list", "std").Output()
if err != nil {
b.Fatal(err)
}
diff --git a/src/cmd/api/run.go b/src/cmd/api/run.go
index 20cddb704b..219776cae4 100644
--- a/src/cmd/api/run.go
+++ b/src/cmd/api/run.go
@@ -14,8 +14,21 @@ import (
"os"
"os/exec"
"path/filepath"
+ "runtime"
)
+func goCmd() string {
+ var exeSuffix string
+ if runtime.GOOS == "windows" {
+ exeSuffix = ".exe"
+ }
+ path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
+ if _, err := os.Stat(path); err == nil {
+ return path
+ }
+ return "go"
+}
+
var goroot string
func main() {
@@ -25,7 +38,7 @@ func main() {
log.Fatal("No $GOROOT set.")
}
- out, err := exec.Command("go", "tool", "api",
+ out, err := exec.Command(goCmd(), "tool", "api",
"-c", file("go1", "go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6", "go1.7", "go1.8", "go1.9"),
"-next", file("next"),
"-except", file("except")).CombinedOutput()
diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go
index e9b4ca0fb2..ae7f25cad9 100644
--- a/src/cmd/dist/test.go
+++ b/src/cmd/dist/test.go
@@ -85,6 +85,14 @@ type distTest struct {
}
func (t *tester) run() {
+ var exeSuffix string
+ if goos == "windows" {
+ exeSuffix = ".exe"
+ }
+ if _, err := os.Stat(filepath.Join(gobin, "go"+exeSuffix)); err == nil {
+ os.Setenv("PATH", fmt.Sprintf("%s%c%s", gobin, os.PathListSeparator, os.Getenv("PATH")))
+ }
+
slurp, err := exec.Command("go", "env", "CGO_ENABLED").Output()
if err != nil {
log.Fatalf("Error running go env CGO_ENABLED: %v", err)
diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go
index eaa2d0f68d..1a47b72083 100644
--- a/src/cmd/go/go_test.go
+++ b/src/cmd/go/go_test.go
@@ -91,20 +91,25 @@ func TestMain(m *testing.M) {
if race.Enabled {
args = append(args, "-race")
}
- out, err := exec.Command("go", args...).CombinedOutput()
+ gotool, err := testenv.GoTool()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, err)
+ os.Exit(2)
+ }
+ out, err := exec.Command(gotool, args...).CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "building testgo failed: %v\n%s", err, out)
os.Exit(2)
}
- out, err = exec.Command("go", "env", "GOROOT").CombinedOutput()
+ out, err = exec.Command(gotool, "env", "GOROOT").CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "could not find testing GOROOT: %v\n%s", err, out)
os.Exit(2)
}
testGOROOT = strings.TrimSpace(string(out))
- out, err = exec.Command("go", "env", "CC").CombinedOutput()
+ out, err = exec.Command(gotool, "env", "CC").CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "could not find testing CC: %v\n%s", err, out)
os.Exit(2)
diff --git a/src/cmd/link/internal/ld/nooptcgolink_test.go b/src/cmd/link/internal/ld/nooptcgolink_test.go
index 1df29652b2..e019a39bf7 100644
--- a/src/cmd/link/internal/ld/nooptcgolink_test.go
+++ b/src/cmd/link/internal/ld/nooptcgolink_test.go
@@ -22,7 +22,7 @@ func TestNooptCgoBuild(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(dir)
- cmd := exec.Command("go", "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out"))
+ cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags=-N -l", "-o", filepath.Join(dir, "a.out"))
cmd.Dir = filepath.Join(runtime.GOROOT(), "src", "runtime", "testdata", "testprogcgo")
out, err := cmd.CombinedOutput()
if err != nil {
diff --git a/src/cmd/trace/pprof.go b/src/cmd/trace/pprof.go
index 40803ac5f9..47be2a6d1c 100644
--- a/src/cmd/trace/pprof.go
+++ b/src/cmd/trace/pprof.go
@@ -15,10 +15,24 @@ import (
"net/http"
"os"
"os/exec"
+ "path/filepath"
+ "runtime"
"github.com/google/pprof/profile"
)
+func goCmd() string {
+ var exeSuffix string
+ if runtime.GOOS == "windows" {
+ exeSuffix = ".exe"
+ }
+ path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
+ if _, err := os.Stat(path); err == nil {
+ return path
+ }
+ return "go"
+}
+
func init() {
http.HandleFunc("/io", serveSVGProfile(pprofIO))
http.HandleFunc("/block", serveSVGProfile(pprofBlock))
@@ -147,7 +161,7 @@ func serveSVGProfile(prof func(w io.Writer) error) http.HandlerFunc {
return
}
svgFilename := blockf.Name() + ".svg"
- if output, err := exec.Command("go", "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil {
+ if output, err := exec.Command(goCmd(), "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil {
http.Error(w, fmt.Sprintf("failed to execute go tool pprof: %v\n%s", err, output), http.StatusInternalServerError)
return
}