summaryrefslogtreecommitdiff
path: root/src/cmd/go
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/go')
-rw-r--r--src/cmd/go/build.go122
-rw-r--r--src/cmd/go/clean.go2
-rw-r--r--src/cmd/go/discovery.go1
-rw-r--r--src/cmd/go/doc.go66
-rw-r--r--src/cmd/go/env.go1
-rw-r--r--src/cmd/go/go11.go10
-rw-r--r--src/cmd/go/help.go8
-rw-r--r--src/cmd/go/main.go37
-rwxr-xr-xsrc/cmd/go/mkdoc.sh1
-rw-r--r--src/cmd/go/pkg.go7
-rw-r--r--src/cmd/go/run.go1
-rwxr-xr-xsrc/cmd/go/test.bash39
-rw-r--r--src/cmd/go/test.go29
-rw-r--r--src/cmd/go/testdata/errmsg/x.go3
-rw-r--r--src/cmd/go/testdata/errmsg/x1_test.go3
-rw-r--r--src/cmd/go/testdata/errmsg/x_test.go3
-rw-r--r--src/cmd/go/vcs.go2
-rw-r--r--src/cmd/go/vet.go2
18 files changed, 223 insertions, 114 deletions
diff --git a/src/cmd/go/build.go b/src/cmd/go/build.go
index 38fc43ef1..025b258bf 100644
--- a/src/cmd/go/build.go
+++ b/src/cmd/go/build.go
@@ -20,6 +20,7 @@ import (
"path/filepath"
"regexp"
"runtime"
+ "strconv"
"strings"
"sync"
"time"
@@ -40,9 +41,13 @@ build writes the resulting executable to output.
Otherwise build compiles the packages but discards the results,
serving only as a check that the packages can be built.
-The -o flag specifies the output file name. If not specified, the
-name is packagename.a (for a non-main package) or the base
-name of the first source file (for a main package).
+The -o flag specifies the output file name. If not specified, the
+output file name depends on the arguments and derives from the name
+of the package, such as p.a for package p, unless p is 'main'. If
+the package is main and file names are provided, the file name
+derives from the first file name mentioned, such as f1 for 'go build
+f1.go f2.go'; with no files provided ('go build'), the output file
+name is the base name of the containing directory.
The build flags are shared by the build, install, run, and test commands:
@@ -53,6 +58,9 @@ The build flags are shared by the build, install, run, and test commands:
-p n
the number of builds that can be run in parallel.
The default is the number of CPUs available.
+ -race
+ enable data race detection.
+ Supported only on linux/amd64, darwin/amd64 and windows/amd64.
-v
print the names of packages as they are compiled.
-work
@@ -60,20 +68,22 @@ The build flags are shared by the build, install, run, and test commands:
do not delete it when exiting.
-x
print the commands.
- -race
- enable data race detection.
- Supported only on linux/amd64, darwin/amd64 and windows/amd64.
-ccflags 'arg list'
- arguments to pass on each 5c, 6c, or 8c compiler invocation
+ arguments to pass on each 5c, 6c, or 8c compiler invocation.
-compiler name
- name of compiler to use, as in runtime.Compiler (gccgo or gc)
+ name of compiler to use, as in runtime.Compiler (gccgo or gc).
-gccgoflags 'arg list'
- arguments to pass on each gccgo compiler/linker invocation
+ arguments to pass on each gccgo compiler/linker invocation.
-gcflags 'arg list'
- arguments to pass on each 5g, 6g, or 8g compiler invocation
+ arguments to pass on each 5g, 6g, or 8g compiler invocation.
+ -installsuffix suffix
+ a suffix to use in the name of the package installation directory,
+ in order to keep output separate from default builds.
+ If using the -race flag, the install suffix is automatically set to race
+ or, if set explicitly, has _race appended to it.
-ldflags 'flag list'
- arguments to pass on each 5l, 6l, or 8l linker invocation
+ arguments to pass on each 5l, 6l, or 8l linker invocation.
-tags 'tag list'
a list of build tags to consider satisfied during the build.
See the documentation for the go/build package for
@@ -153,6 +163,7 @@ func addBuildFlags(cmd *Command) {
cmd.Flag.BoolVar(&buildA, "a", false, "")
cmd.Flag.BoolVar(&buildN, "n", false, "")
cmd.Flag.IntVar(&buildP, "p", buildP, "")
+ cmd.Flag.StringVar(&buildContext.InstallSuffix, "installsuffix", "", "")
cmd.Flag.BoolVar(&buildV, "v", false, "")
cmd.Flag.BoolVar(&buildX, "x", false, "")
cmd.Flag.BoolVar(&buildWork, "work", false, "")
@@ -391,7 +402,9 @@ var (
func (b *builder) init() {
var err error
- b.print = fmt.Print
+ b.print = func(a ...interface{}) (int, error) {
+ return fmt.Fprint(os.Stderr, a...)
+ }
b.actionCache = make(map[cacheKey]*action)
b.mkdirCache = make(map[string]bool)
@@ -1188,8 +1201,8 @@ var cgoLine = regexp.MustCompile(`\[[^\[\]]+\.cgo1\.go:[0-9]+\]`)
// run runs the command given by cmdline in the directory dir.
// If the command fails, run prints information about the failure
// and returns a non-nil error.
-func (b *builder) run(dir string, desc string, cmdargs ...interface{}) error {
- out, err := b.runOut(dir, desc, cmdargs...)
+func (b *builder) run(dir string, desc string, env []string, cmdargs ...interface{}) error {
+ out, err := b.runOut(dir, desc, env, cmdargs...)
if len(out) > 0 {
if desc == "" {
desc = b.fmtcmd(dir, "%s", strings.Join(stringList(cmdargs...), " "))
@@ -1221,10 +1234,10 @@ func (b *builder) processOutput(out []byte) string {
// runOut runs the command given by cmdline in the directory dir.
// It returns the command output and any errors that occurred.
-func (b *builder) runOut(dir string, desc string, cmdargs ...interface{}) ([]byte, error) {
+func (b *builder) runOut(dir string, desc string, env []string, cmdargs ...interface{}) ([]byte, error) {
cmdline := stringList(cmdargs...)
if buildN || buildX {
- b.showcmd(dir, "%s", strings.Join(cmdline, " "))
+ b.showcmd(dir, "%s", joinUnambiguously(cmdline))
if buildN {
return nil, nil
}
@@ -1237,7 +1250,7 @@ func (b *builder) runOut(dir string, desc string, cmdargs ...interface{}) ([]byt
cmd.Stdout = &buf
cmd.Stderr = &buf
cmd.Dir = dir
- cmd.Env = envForDir(cmd.Dir)
+ cmd.Env = mergeEnvLists(env, envForDir(cmd.Dir))
err := cmd.Run()
// cmd.Run will fail on Unix if some other process has the binary
@@ -1289,7 +1302,25 @@ func (b *builder) runOut(dir string, desc string, cmdargs ...interface{}) ([]byt
return buf.Bytes(), err
}
- panic("unreachable")
+}
+
+// joinUnambiguously prints the slice, quoting where necessary to make the
+// output unambiguous.
+// TODO: See issue 5279. The printing of commands needs a complete redo.
+func joinUnambiguously(a []string) string {
+ var buf bytes.Buffer
+ for i, s := range a {
+ if i > 0 {
+ buf.WriteByte(' ')
+ }
+ q := strconv.Quote(s)
+ if s == "" || strings.Contains(s, " ") || len(q) > len(s)+2 {
+ buf.WriteString(q)
+ } else {
+ buf.WriteString(s)
+ }
+ }
+ return buf.String()
}
// mkdir makes the named directory.
@@ -1439,13 +1470,13 @@ func (gcToolchain) gc(b *builder, p *Package, obj string, importArgs []string, g
args = append(args, mkAbs(p.Dir, f))
}
- output, err = b.runOut(p.Dir, p.ImportPath, args)
+ output, err = b.runOut(p.Dir, p.ImportPath, nil, args)
return ofile, output, err
}
func (gcToolchain) asm(b *builder, p *Package, obj, ofile, sfile string) error {
sfile = mkAbs(p.Dir, sfile)
- return b.run(p.Dir, p.ImportPath, tool(archChar+"a"), "-I", obj, "-o", ofile, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, sfile)
+ return b.run(p.Dir, p.ImportPath, nil, tool(archChar+"a"), "-I", obj, "-o", ofile, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, sfile)
}
func (gcToolchain) pkgpath(basedir string, p *Package) string {
@@ -1458,7 +1489,7 @@ func (gcToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles []s
for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(objDir, f))
}
- return b.run(p.Dir, p.ImportPath, tool("pack"), "grcP", b.work, mkAbs(objDir, afile), absOfiles)
+ return b.run(p.Dir, p.ImportPath, nil, tool("pack"), "grcP", b.work, mkAbs(objDir, afile), absOfiles)
}
func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action, mainpkg string, ofiles []string) error {
@@ -1477,14 +1508,14 @@ func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action,
swigDirs[sd] = true
}
}
- return b.run(".", p.ImportPath, tool(archChar+"l"), "-o", out, importArgs, swigArg, buildLdflags, mainpkg)
+ return b.run(".", p.ImportPath, nil, tool(archChar+"l"), "-o", out, importArgs, swigArg, buildLdflags, mainpkg)
}
func (gcToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
inc := filepath.Join(goroot, "pkg", fmt.Sprintf("%s_%s", goos, goarch))
cfile = mkAbs(p.Dir, cfile)
args := stringList(tool(archChar+"c"), "-F", "-V", "-w", "-I", objdir, "-I", inc, "-o", ofile, buildCcflags, "-D", "GOOS_"+goos, "-D", "GOARCH_"+goarch, cfile)
- return b.run(p.Dir, p.ImportPath, args)
+ return b.run(p.Dir, p.ImportPath, nil, args)
}
// The Gccgo toolchain.
@@ -1516,7 +1547,7 @@ func (gccgoToolchain) gc(b *builder, p *Package, obj string, importArgs []string
args = append(args, mkAbs(p.Dir, f))
}
- output, err = b.runOut(p.Dir, p.ImportPath, args)
+ output, err = b.runOut(p.Dir, p.ImportPath, nil, args)
return ofile, output, err
}
@@ -1527,7 +1558,7 @@ func (gccgoToolchain) asm(b *builder, p *Package, obj, ofile, sfile string) erro
defs = append(defs, `-D`, `GOPKGPATH="`+pkgpath+`"`)
}
defs = append(defs, b.gccArchArgs()...)
- return b.run(p.Dir, p.ImportPath, "gccgo", "-I", obj, "-o", ofile, defs, sfile)
+ return b.run(p.Dir, p.ImportPath, nil, "gccgo", "-I", obj, "-o", ofile, defs, sfile)
}
func (gccgoToolchain) pkgpath(basedir string, p *Package) string {
@@ -1542,7 +1573,7 @@ func (gccgoToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles
for _, f := range ofiles {
absOfiles = append(absOfiles, mkAbs(objDir, f))
}
- return b.run(p.Dir, p.ImportPath, "ar", "cru", mkAbs(objDir, afile), absOfiles)
+ return b.run(p.Dir, p.ImportPath, nil, "ar", "cru", mkAbs(objDir, afile), absOfiles)
}
func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []*action, mainpkg string, ofiles []string) error {
@@ -1584,7 +1615,7 @@ func (tools gccgoToolchain) ld(b *builder, p *Package, out string, allactions []
if usesCgo && goos == "linux" {
ldflags = append(ldflags, "-Wl,-E")
}
- return b.run(".", p.ImportPath, "gccgo", "-o", out, ofiles, "-Wl,-(", ldflags, "-Wl,-)", buildGccgoflags)
+ return b.run(".", p.ImportPath, nil, "gccgo", "-o", out, ofiles, "-Wl,-(", ldflags, "-Wl,-)", buildGccgoflags)
}
func (gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
@@ -1596,7 +1627,7 @@ func (gccgoToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) er
defs = append(defs, `-D`, `GOPKGPATH="`+pkgpath+`"`)
}
// TODO: Support using clang here (during gccgo build)?
- return b.run(p.Dir, p.ImportPath, "gcc", "-Wall", "-g",
+ return b.run(p.Dir, p.ImportPath, nil, "gcc", "-Wall", "-g",
"-I", objdir, "-I", inc, "-o", ofile, defs, "-c", cfile)
}
@@ -1632,11 +1663,11 @@ func (b *builder) libgcc(p *Package) (string, error) {
// print function to capture the command-line. This
// let's us assign it to $LIBGCC and produce a valid
// buildscript for cgo packages.
- b.print = func(a ...interface{}) (n int, err error) {
+ b.print = func(a ...interface{}) (int, error) {
return fmt.Fprint(&buf, a...)
}
}
- f, err := b.runOut(p.Dir, p.ImportPath, gccCmd, "-print-libgcc-file-name")
+ f, err := b.runOut(p.Dir, p.ImportPath, nil, gccCmd, "-print-libgcc-file-name")
if err != nil {
return "", fmt.Errorf("gcc -print-libgcc-file-name: %v (%s)", err, f)
}
@@ -1659,12 +1690,12 @@ func (b *builder) libgcc(p *Package) (string, error) {
// gcc runs the gcc C compiler to create an object from a single C file.
func (b *builder) gcc(p *Package, out string, flags []string, cfile string) error {
cfile = mkAbs(p.Dir, cfile)
- return b.run(p.Dir, p.ImportPath, b.gccCmd(p.Dir), flags, "-o", out, "-c", cfile)
+ return b.run(p.Dir, p.ImportPath, nil, b.gccCmd(p.Dir), flags, "-o", out, "-c", cfile)
}
// gccld runs the gcc linker to create an executable from a set of object files
func (b *builder) gccld(p *Package, out string, flags []string, obj []string) error {
- return b.run(p.Dir, p.ImportPath, b.gccCmd(p.Dir), "-o", out, obj, flags)
+ return b.run(p.Dir, p.ImportPath, nil, b.gccCmd(p.Dir), "-o", out, obj, flags)
}
// gccCmd returns a gcc command line prefix
@@ -1745,7 +1776,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, gccfiles []string) (outGo,
cgoLDFLAGS := stringList(envList("CGO_LDFLAGS"), p.CgoLDFLAGS)
if pkgs := p.CgoPkgConfig; len(pkgs) > 0 {
- out, err := b.runOut(p.Dir, p.ImportPath, "pkg-config", "--cflags", pkgs)
+ out, err := b.runOut(p.Dir, p.ImportPath, nil, "pkg-config", "--cflags", pkgs)
if err != nil {
b.showOutput(p.Dir, "pkg-config --cflags "+strings.Join(pkgs, " "), string(out))
b.print(err.Error() + "\n")
@@ -1754,7 +1785,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, gccfiles []string) (outGo,
if len(out) > 0 {
cgoCFLAGS = append(cgoCFLAGS, strings.Fields(string(out))...)
}
- out, err = b.runOut(p.Dir, p.ImportPath, "pkg-config", "--libs", pkgs)
+ out, err = b.runOut(p.Dir, p.ImportPath, nil, "pkg-config", "--libs", pkgs)
if err != nil {
b.showOutput(p.Dir, "pkg-config --libs "+strings.Join(pkgs, " "), string(out))
b.print(err.Error() + "\n")
@@ -1791,6 +1822,16 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, gccfiles []string) (outGo,
cgoflags = append(cgoflags, "-import_syscall=false")
}
+ // Update $CGO_LDFLAGS with p.CgoLDFLAGS.
+ var cgoenv []string
+ if len(cgoLDFLAGS) > 0 {
+ flags := make([]string, len(cgoLDFLAGS))
+ for i, f := range cgoLDFLAGS {
+ flags[i] = strconv.Quote(f)
+ }
+ cgoenv = []string{"CGO_LDFLAGS=" + strings.Join(flags, " ")}
+ }
+
if _, ok := buildToolchain.(gccgoToolchain); ok {
cgoflags = append(cgoflags, "-gccgo")
if pkgpath := gccgoPkgpath(p); pkgpath != "" {
@@ -1798,7 +1839,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, gccfiles []string) (outGo,
}
objExt = "o"
}
- if err := b.run(p.Dir, p.ImportPath, cgoExe, "-objdir", obj, cgoflags, "--", cgoCFLAGS, p.CgoFiles); err != nil {
+ if err := b.run(p.Dir, p.ImportPath, cgoenv, cgoExe, "-objdir", obj, cgoflags, "--", cgoCFLAGS, p.CgoFiles); err != nil {
return nil, nil, err
}
outGo = append(outGo, gofiles...)
@@ -1883,7 +1924,7 @@ func (b *builder) cgo(p *Package, cgoExe, obj string, gccfiles []string) (outGo,
if p.Standard && p.ImportPath == "runtime/cgo" {
cgoflags = append(cgoflags, "-dynlinker") // record path to dynamic linker
}
- if err := b.run(p.Dir, p.ImportPath, cgoExe, "-objdir", obj, "-dynimport", dynobj, "-dynout", importC, cgoflags); err != nil {
+ if err := b.run(p.Dir, p.ImportPath, nil, cgoExe, "-objdir", obj, "-dynimport", dynobj, "-dynout", importC, cgoflags); err != nil {
return nil, nil, err
}
@@ -2006,7 +2047,7 @@ func (b *builder) swigOne(p *Package, file, obj string, cxx bool, intgosize stri
args = append(args, "-c++")
}
- if out, err := b.runOut(p.Dir, p.ImportPath, "swig", args, file); err != nil {
+ if out, err := b.runOut(p.Dir, p.ImportPath, nil, "swig", args, file); err != nil {
if len(out) > 0 {
if bytes.Contains(out, []byte("Unrecognized option -intgosize")) {
return "", "", errors.New("must have SWIG version >= 2.0.9\n")
@@ -2044,7 +2085,7 @@ func (b *builder) swigOne(p *Package, file, obj string, cxx bool, intgosize stri
cxxlib = []string{"-lstdc++"}
}
ldflags := stringList(osldflags[goos], cxxlib)
- b.run(p.Dir, p.ImportPath, b.gccCmd(p.Dir), "-o", soname, gccObj, ldflags)
+ b.run(p.Dir, p.ImportPath, nil, b.gccCmd(p.Dir), "-o", soname, gccObj, ldflags)
return obj + goFile, cObj, nil
}
@@ -2083,6 +2124,9 @@ func raceInit() {
buildGcflags = append(buildGcflags, "-race")
buildLdflags = append(buildLdflags, "-race")
buildCcflags = append(buildCcflags, "-D", "RACE")
- buildContext.InstallTag = "race"
+ if buildContext.InstallSuffix != "" {
+ buildContext.InstallSuffix += "_"
+ }
+ buildContext.InstallSuffix += "race"
buildContext.BuildTags = append(buildContext.BuildTags, "race")
}
diff --git a/src/cmd/go/clean.go b/src/cmd/go/clean.go
index ba600d3bb..8345c9af1 100644
--- a/src/cmd/go/clean.go
+++ b/src/cmd/go/clean.go
@@ -106,6 +106,8 @@ func clean(p *Package) {
if cleaned[p] {
return
}
+ cleaned[p] = true
+
if p.Dir == "" {
errorf("can't load package: %v", p.Error)
return
diff --git a/src/cmd/go/discovery.go b/src/cmd/go/discovery.go
index d9f930867..047834050 100644
--- a/src/cmd/go/discovery.go
+++ b/src/cmd/go/discovery.go
@@ -48,7 +48,6 @@ func parseMetaGoImports(r io.Reader) (imports []metaImport) {
})
}
}
- return
}
// attrValue returns the attribute value for the case-insensitive key
diff --git a/src/cmd/go/doc.go b/src/cmd/go/doc.go
index 0297b7602..df82ab45b 100644
--- a/src/cmd/go/doc.go
+++ b/src/cmd/go/doc.go
@@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// DO NOT EDIT THIS FILE. GENERATED BY mkdoc.sh.
+// Edit the documentation in other files and rerun mkdoc.sh to generate this one.
+
/*
Go is a tool for managing Go source code.
@@ -56,9 +59,13 @@ build writes the resulting executable to output.
Otherwise build compiles the packages but discards the results,
serving only as a check that the packages can be built.
-The -o flag specifies the output file name. If not specified, the
-name is packagename.a (for a non-main package) or the base
-name of the first source file (for a main package).
+The -o flag specifies the output file name. If not specified, the
+output file name depends on the arguments and derives from the name
+of the package, such as p.a for package p, unless p is 'main'. If
+the package is main and file names are provided, the file name
+derives from the first file name mentioned, such as f1 for 'go build
+f1.go f2.go'; with no files provided ('go build'), the output file
+name is the base name of the containing directory.
The build flags are shared by the build, install, run, and test commands:
@@ -69,6 +76,9 @@ The build flags are shared by the build, install, run, and test commands:
-p n
the number of builds that can be run in parallel.
The default is the number of CPUs available.
+ -race
+ enable data race detection.
+ Supported only on linux/amd64, darwin/amd64 and windows/amd64.
-v
print the names of packages as they are compiled.
-work
@@ -76,20 +86,22 @@ The build flags are shared by the build, install, run, and test commands:
do not delete it when exiting.
-x
print the commands.
- -race
- enable data race detection.
- Supported only on linux/amd64, darwin/amd64 and windows/amd64.
-ccflags 'arg list'
- arguments to pass on each 5c, 6c, or 8c compiler invocation
+ arguments to pass on each 5c, 6c, or 8c compiler invocation.
-compiler name
- name of compiler to use, as in runtime.Compiler (gccgo or gc)
+ name of compiler to use, as in runtime.Compiler (gccgo or gc).
-gccgoflags 'arg list'
- arguments to pass on each gccgo compiler/linker invocation
+ arguments to pass on each gccgo compiler/linker invocation.
-gcflags 'arg list'
- arguments to pass on each 5g, 6g, or 8g compiler invocation
+ arguments to pass on each 5g, 6g, or 8g compiler invocation.
+ -installsuffix suffix
+ a suffix to use in the name of the package installation directory,
+ in order to keep output separate from default builds.
+ If using the -race flag, the install suffix is automatically set to race
+ or, if set explicitly, has _race appended to it.
-ldflags 'flag list'
- arguments to pass on each 5l, 6l, or 8l linker invocation
+ arguments to pass on each 5l, 6l, or 8l linker invocation.
-tags 'tag list'
a list of build tags to consider satisfied during the build.
See the documentation for the go/build package for
@@ -358,6 +370,7 @@ Usage:
go run [build flags] gofiles... [arguments...]
Run compiles and runs the main package comprising the named Go source files.
+If no files are named, it compiles and runs all non-test Go source files.
For more about build flags, see 'go help build'.
@@ -385,6 +398,9 @@ the file pattern "*_test.go". These additional files can contain test functions
benchmark functions, and example functions. See 'go help testfunc' for more.
Each listed package causes the execution of a separate test binary.
+Test files that declare a package with the suffix "_test" will be compiled as a
+separate package, and then linked and run with the main test binary.
+
By default, go test needs no arguments. It compiles and tests the package
with source in the current directory, including tests, and runs the tests.
@@ -394,6 +410,7 @@ non-test installation.
In addition to the build flags, the flags handled by 'go test' itself are:
-c Compile the test binary to pkg.test but do not run it.
+ (Where pkg is the last element of the package's import path.)
-i
Install packages that are dependencies of the test.
@@ -569,7 +586,7 @@ a revision control system.
A few common code hosting sites have special syntax:
- BitBucket (Mercurial)
+ Bitbucket (Git, Mercurial)
import "bitbucket.org/user/project"
import "bitbucket.org/user/project/sub/directory"
@@ -679,6 +696,11 @@ Description of testing flags
The 'go test' command takes both flags that apply to 'go test' itself
and flags that apply to the resulting test binary.
+Several of the flags control profiling and write an execution profile
+suitable for "go tool pprof"; run "go tool pprof help" for more
+information. The --alloc_space, --alloc_objects, and --show_bytes
+options of pprof control how the information is presented.
+
The following flags are recognized by the 'go test' command and
control the execution of any test:
@@ -691,17 +713,18 @@ control the execution of any test:
Print memory allocation statistics for benchmarks.
-benchtime t
- Run enough iterations of each benchmark to take t, specified
- as a time.Duration (for example, -benchtime 1h30s).
- The default is 1 second (1s).
+ Run enough iterations of each benchmark to take t, specified
+ as a time.Duration (for example, -benchtime 1h30s).
+ The default is 1 second (1s).
-blockprofile block.out
Write a goroutine blocking profile to the specified file
when all tests are complete.
-blockprofilerate n
- Control the detail provided in goroutine blocking profiles by setting
- runtime.BlockProfileRate to n. See 'godoc runtime BlockProfileRate'.
+ Control the detail provided in goroutine blocking profiles by
+ calling runtime.SetBlockProfileRate with n.
+ See 'godoc runtime SetBlockProfileRate'.
The profiler aims to sample, on average, one blocking event every
n nanoseconds the program spends blocked. By default,
if -test.blockprofile is set without this flag, all blocking events
@@ -716,8 +739,8 @@ control the execution of any test:
Write a CPU profile to the specified file before exiting.
-memprofile mem.out
- Write a memory profile to the specified file when all tests
- are complete.
+ Write a memory profile to the specified file after all tests
+ have passed.
-memprofilerate n
Enable more precise (and expensive) memory profiles by setting
@@ -743,10 +766,11 @@ control the execution of any test:
exhaustive tests.
-timeout t
- If a test runs longer than t, panic.
+ If a test runs longer than t, panic.
-v
- Verbose output: log all tests as they are run.
+ Verbose output: log all tests as they are run. Also print all
+ text from Log and Logf calls even if the test succeeds.
The test binary, called pkg.test where pkg is the name of the
directory containing the package sources, can be invoked directly
diff --git a/src/cmd/go/env.go b/src/cmd/go/env.go
index 6cab37b48..00e03e9bd 100644
--- a/src/cmd/go/env.go
+++ b/src/cmd/go/env.go
@@ -42,6 +42,7 @@ func mkEnv() []envVar {
{"GOHOSTOS", runtime.GOOS},
{"GOOS", goos},
{"GOPATH", os.Getenv("GOPATH")},
+ {"GORACE", os.Getenv("GORACE")},
{"GOROOT", goroot},
{"GOTOOLDIR", toolDir},
}
diff --git a/src/cmd/go/go11.go b/src/cmd/go/go11.go
new file mode 100644
index 000000000..8a434dfed
--- /dev/null
+++ b/src/cmd/go/go11.go
@@ -0,0 +1,10 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build go1.1
+
+package main
+
+// Test that go1.1 tag above is included in builds. main.go refers to this definition.
+const go11tag = true
diff --git a/src/cmd/go/help.go b/src/cmd/go/help.go
index 6d2bd7dbb..c70a25fdd 100644
--- a/src/cmd/go/help.go
+++ b/src/cmd/go/help.go
@@ -20,13 +20,13 @@ denotes the package in that directory.
Otherwise, the import path P denotes the package found in
the directory DIR/src/P for some DIR listed in the GOPATH
-environment variable (see 'go help gopath').
+environment variable (see 'go help gopath').
If no import paths are given, the action applies to the
package in the current directory.
The special import path "all" expands to all package directories
-found in all the GOPATH trees. For example, 'go list all'
+found in all the GOPATH trees. For example, 'go list all'
lists all the packages on the local system.
The special import path "std" is like all but expands to just the
@@ -68,7 +68,7 @@ a revision control system.
A few common code hosting sites have special syntax:
- BitBucket (Mercurial)
+ Bitbucket (Git, Mercurial)
import "bitbucket.org/user/project"
import "bitbucket.org/user/project/sub/directory"
@@ -232,7 +232,7 @@ Here's an example directory layout:
bar.a (installed package object)
Go searches each directory listed in GOPATH to find source code,
-but new packages are always downloaded into the first directory
+but new packages are always downloaded into the first directory
in the list.
`,
}
diff --git a/src/cmd/go/main.go b/src/cmd/go/main.go
index bf1dad40f..3180dbeed 100644
--- a/src/cmd/go/main.go
+++ b/src/cmd/go/main.go
@@ -108,6 +108,7 @@ func setExitStatus(n int) {
}
func main() {
+ _ = go11tag
flag.Usage = usage
flag.Parse()
log.SetFlags(0)
@@ -129,8 +130,11 @@ func main() {
fmt.Fprintf(os.Stderr, "warning: GOPATH set to GOROOT (%s) has no effect\n", gopath)
} else {
for _, p := range filepath.SplitList(gopath) {
- if strings.Contains(p, "~") && runtime.GOOS != "windows" {
- fmt.Fprintf(os.Stderr, "go: GOPATH entry cannot contain shell metacharacter '~': %q\n", p)
+ // Note: using HasPrefix instead of Contains because a ~ can appear
+ // in the middle of directory elements, such as /tmp/git-1.8.2~rc3
+ // or C:\PROGRA~1. Only ~ as a path prefix has meaning to the shell.
+ if strings.HasPrefix(p, "~") {
+ fmt.Fprintf(os.Stderr, "go: GOPATH entry cannot start with shell metacharacter '~': %q\n", p)
os.Exit(2)
}
if build.IsLocalImport(p) {
@@ -189,6 +193,9 @@ var documentationTemplate = `// Copyright 2011 The Go Authors. All rights reser
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
+// DO NOT EDIT THIS FILE. GENERATED BY mkdoc.sh.
+// Edit the documentation in other files and rerun mkdoc.sh to generate this one.
+
/*
{{range .}}{{if .Short}}{{.Short | capitalize}}
@@ -386,16 +393,26 @@ func runOut(dir string, cmdargs ...interface{}) []byte {
// child will be faster.
func envForDir(dir string) []string {
env := os.Environ()
- for i, kv := range env {
- if strings.HasPrefix(kv, "PWD=") {
- env[i] = "PWD=" + dir
- return env
- }
- }
// Internally we only use rooted paths, so dir is rooted.
// Even if dir is not rooted, no harm done.
- env = append(env, "PWD="+dir)
- return env
+ return mergeEnvLists([]string{"PWD=" + dir}, env)
+}
+
+// mergeEnvLists merges the two environment lists such that
+// variables with the same name in "in" replace those in "out".
+func mergeEnvLists(in, out []string) []string {
+NextVar:
+ for _, inkv := range in {
+ k := strings.SplitAfterN(inkv, "=", 2)[0]
+ for i, outkv := range out {
+ if strings.HasPrefix(outkv, k) {
+ out[i] = inkv
+ continue NextVar
+ }
+ }
+ out = append(out, inkv)
+ }
+ return out
}
// matchPattern(pattern)(name) reports whether
diff --git a/src/cmd/go/mkdoc.sh b/src/cmd/go/mkdoc.sh
index 7768baeb6..12fd7ba3e 100755
--- a/src/cmd/go/mkdoc.sh
+++ b/src/cmd/go/mkdoc.sh
@@ -3,6 +3,7 @@
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
+go install # So the next line will produce updated documentation.
go help documentation > doc.go
gofmt -w doc.go
diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go
index 7fc61fd86..b33d800bf 100644
--- a/src/cmd/go/pkg.go
+++ b/src/cmd/go/pkg.go
@@ -72,6 +72,7 @@ type Package struct {
imports []*Package
deps []*Package
gofiles []string // GoFiles+CgoFiles+TestGoFiles+XTestGoFiles files, absolute paths
+ sfiles []string
allgofiles []string // gofiles + IgnoredGoFiles, absolute paths
target string // installed file for this package (may be executable)
fake bool // synthesized package
@@ -366,6 +367,12 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
}
sort.Strings(p.gofiles)
+ p.sfiles = stringList(p.SFiles)
+ for i := range p.sfiles {
+ p.sfiles[i] = filepath.Join(p.Dir, p.sfiles[i])
+ }
+ sort.Strings(p.sfiles)
+
p.allgofiles = stringList(p.IgnoredGoFiles)
for i := range p.allgofiles {
p.allgofiles[i] = filepath.Join(p.Dir, p.allgofiles[i])
diff --git a/src/cmd/go/run.go b/src/cmd/go/run.go
index b50569363..91bdc1be2 100644
--- a/src/cmd/go/run.go
+++ b/src/cmd/go/run.go
@@ -16,6 +16,7 @@ var cmdRun = &Command{
Short: "compile and run Go program",
Long: `
Run compiles and runs the main package comprising the named Go source files.
+If no files are named, it compiles and runs all non-test Go source files.
For more about build flags, see 'go help build'.
diff --git a/src/cmd/go/test.bash b/src/cmd/go/test.bash
index 460061a11..e2264a46e 100755
--- a/src/cmd/go/test.bash
+++ b/src/cmd/go/test.bash
@@ -11,18 +11,19 @@ ok=true
unset GOPATH
unset GOBIN
-# Test that error messages have file:line information
-# at beginning of line.
-for i in testdata/errmsg/*.go
-do
- # TODO: |cat should not be necessary here but is.
- ./testgo test $i 2>&1 | cat >err.out || true
- if ! grep -q "^$i:" err.out; then
- echo "$i: missing file:line in error message"
- cat err.out
- ok=false
- fi
-done
+# Test that error messages have file:line information at beginning of
+# the line. Also test issue 4917: that the error is on stderr.
+d=$(mktemp -d -t testgoXXX)
+fn=$d/err.go
+echo "package main" > $fn
+echo 'import "bar"' >> $fn
+./testgo run $fn 2>$d/err.out || true
+if ! grep -q "^$fn:" $d/err.out; then
+ echo "missing file:line in error message"
+ cat $d/err.out
+ ok=false
+fi
+rm -r $d
# Test local (./) imports.
testlocal() {
@@ -51,7 +52,7 @@ testlocal() {
ok=false
fi
- rm -f err.out hello.out hello
+ rm -f hello.out hello
# Test that go install x.go fails.
if ./testgo install "testdata/$local/easy.go" >/dev/null 2>&1; then
@@ -183,7 +184,7 @@ fi
# issue 4186. go get cannot be used to download packages to $GOROOT
# Test that without GOPATH set, go get should fail
-d=$(mktemp -d -t testgo)
+d=$(mktemp -d -t testgoXXX)
mkdir -p $d/src/pkg
if GOPATH= GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then
echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with $GOPATH unset'
@@ -191,7 +192,7 @@ if GOPATH= GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch
fi
rm -rf $d
# Test that with GOPATH=$GOROOT, go get should fail
-d=$(mktemp -d -t testgo)
+d=$(mktemp -d -t testgoXXX)
mkdir -p $d/src/pkg
if GOPATH=$d GOROOT=$d ./testgo get -d code.google.com/p/go.codereview/cmd/hgpatch ; then
echo 'go get code.google.com/p/go.codereview/cmd/hgpatch should not succeed with GOPATH=$GOROOT'
@@ -200,7 +201,7 @@ fi
rm -rf $d
# issue 3941: args with spaces
-d=$(mktemp -d -t testgo)
+d=$(mktemp -d -t testgoXXX)
cat >$d/main.go<<EOF
package main
var extern string
@@ -226,7 +227,7 @@ rm -f strings.prof strings.test
# issue 4568. test that symlinks don't screw things up too badly.
old=$(pwd)
-d=$(mktemp -d -t testgo)
+d=$(mktemp -d -t testgoXXX)
mkdir -p $d/src
(
ln -s $d $d/src/dir1
@@ -247,7 +248,7 @@ fi
rm -rf $d
# issue 4515.
-d=$(mktemp -d -t testgo)
+d=$(mktemp -d -t testgoXXX)
mkdir -p $d/src/example/a $d/src/example/b $d/bin
cat >$d/src/example/a/main.go <<EOF
package main
@@ -280,7 +281,7 @@ unset GOPATH
rm -rf $d
# issue 4773. case-insensitive collisions
-d=$(mktemp -d -t testgo)
+d=$(mktemp -d -t testgoXXX)
export GOPATH=$d
mkdir -p $d/src/example/a $d/src/example/b
cat >$d/src/example/a/a.go <<EOF
diff --git a/src/cmd/go/test.go b/src/cmd/go/test.go
index 3132ab210..b1db16f77 100644
--- a/src/cmd/go/test.go
+++ b/src/cmd/go/test.go
@@ -50,6 +50,9 @@ the file pattern "*_test.go". These additional files can contain test functions
benchmark functions, and example functions. See 'go help testfunc' for more.
Each listed package causes the execution of a separate test binary.
+Test files that declare a package with the suffix "_test" will be compiled as a
+separate package, and then linked and run with the main test binary.
+
By default, go test needs no arguments. It compiles and tests the package
with source in the current directory, including tests, and runs the tests.
@@ -59,6 +62,7 @@ non-test installation.
In addition to the build flags, the flags handled by 'go test' itself are:
-c Compile the test binary to pkg.test but do not run it.
+ (Where pkg is the last element of the package's import path.)
-i
Install packages that are dependencies of the test.
@@ -81,6 +85,11 @@ var helpTestflag = &Command{
The 'go test' command takes both flags that apply to 'go test' itself
and flags that apply to the resulting test binary.
+Several of the flags control profiling and write an execution profile
+suitable for "go tool pprof"; run "go tool pprof help" for more
+information. The --alloc_space, --alloc_objects, and --show_bytes
+options of pprof control how the information is presented.
+
The following flags are recognized by the 'go test' command and
control the execution of any test:
@@ -93,17 +102,18 @@ control the execution of any test:
Print memory allocation statistics for benchmarks.
-benchtime t
- Run enough iterations of each benchmark to take t, specified
- as a time.Duration (for example, -benchtime 1h30s).
- The default is 1 second (1s).
+ Run enough iterations of each benchmark to take t, specified
+ as a time.Duration (for example, -benchtime 1h30s).
+ The default is 1 second (1s).
-blockprofile block.out
Write a goroutine blocking profile to the specified file
when all tests are complete.
-blockprofilerate n
- Control the detail provided in goroutine blocking profiles by setting
- runtime.BlockProfileRate to n. See 'godoc runtime BlockProfileRate'.
+ Control the detail provided in goroutine blocking profiles by
+ calling runtime.SetBlockProfileRate with n.
+ See 'godoc runtime SetBlockProfileRate'.
The profiler aims to sample, on average, one blocking event every
n nanoseconds the program spends blocked. By default,
if -test.blockprofile is set without this flag, all blocking events
@@ -118,8 +128,8 @@ control the execution of any test:
Write a CPU profile to the specified file before exiting.
-memprofile mem.out
- Write a memory profile to the specified file when all tests
- are complete.
+ Write a memory profile to the specified file after all tests
+ have passed.
-memprofilerate n
Enable more precise (and expensive) memory profiles by setting
@@ -145,10 +155,11 @@ control the execution of any test:
exhaustive tests.
-timeout t
- If a test runs longer than t, panic.
+ If a test runs longer than t, panic.
-v
- Verbose output: log all tests as they are run.
+ Verbose output: log all tests as they are run. Also print all
+ text from Log and Logf calls even if the test succeeds.
The test binary, called pkg.test where pkg is the name of the
directory containing the package sources, can be invoked directly
diff --git a/src/cmd/go/testdata/errmsg/x.go b/src/cmd/go/testdata/errmsg/x.go
deleted file mode 100644
index 60f5b6e98..000000000
--- a/src/cmd/go/testdata/errmsg/x.go
+++ /dev/null
@@ -1,3 +0,0 @@
-package foo
-
-import "bar"
diff --git a/src/cmd/go/testdata/errmsg/x1_test.go b/src/cmd/go/testdata/errmsg/x1_test.go
deleted file mode 100644
index eb1a6798c..000000000
--- a/src/cmd/go/testdata/errmsg/x1_test.go
+++ /dev/null
@@ -1,3 +0,0 @@
-package foo_test
-
-import "bar"
diff --git a/src/cmd/go/testdata/errmsg/x_test.go b/src/cmd/go/testdata/errmsg/x_test.go
deleted file mode 100644
index 60f5b6e98..000000000
--- a/src/cmd/go/testdata/errmsg/x_test.go
+++ /dev/null
@@ -1,3 +0,0 @@
-package foo
-
-import "bar"
diff --git a/src/cmd/go/vcs.go b/src/cmd/go/vcs.go
index b99579441..39881a6dc 100644
--- a/src/cmd/go/vcs.go
+++ b/src/cmd/go/vcs.go
@@ -641,7 +641,7 @@ func oldGoogleCode(match map[string]string) error {
}
// bitbucketVCS determines the version control system for a
-// BitBucket repository, by using the BitBucket API.
+// Bitbucket repository, by using the Bitbucket API.
func bitbucketVCS(match map[string]string) error {
if err := noVCSSuffix(match); err != nil {
return err
diff --git a/src/cmd/go/vet.go b/src/cmd/go/vet.go
index eb0b89cca..503e16362 100644
--- a/src/cmd/go/vet.go
+++ b/src/cmd/go/vet.go
@@ -32,6 +32,6 @@ func runVet(cmd *Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package,
// not to packages in subdirectories.
- run(tool("vet"), relPaths(pkg.gofiles))
+ run(tool("vet"), relPaths(stringList(pkg.gofiles, pkg.sfiles)))
}
}