aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/work/build.go
diff options
context:
space:
mode:
authorDmitri Shuralyov <dmitshur@golang.org>2019-03-21 13:31:32 -0400
committerDmitri Shuralyov <dmitshur@golang.org>2019-03-22 19:28:22 +0000
commit94563de87fad642677ffc62a4a82766597e39123 (patch)
tree335e6b247706bdc997255a957a42bc3a15a0ae3e /src/cmd/go/internal/work/build.go
parent1e83369ca002218389e81235ed96d2bb509bb779 (diff)
downloadgo-94563de87fad642677ffc62a4a82766597e39123.tar.gz
go-94563de87fad642677ffc62a4a82766597e39123.zip
cmd/go: fix the default build output name for versioned binaries
This change is a re-apply of the reverted CL 140863 with changes to address issue #30821. Specifically, path.Split continues to be used to split the '/'-separated import path, rather than filepath.Split. Document the algorithm for how the default executable name is determined in DefaultExecName. Rename a variable returned from os.Stat from bs to fi for consistency. CL 140863 factored out the logic to determine the default executable name from the Package.load method into a DefaultExecName function, and started using it in more places to avoid having to re-implement the logic everywhere it's needed. Most previous callers already computed the default executable name based on the import path. The load.Package method, before CL 140863, was the exception, in that it used the p.Dir value in GOPATH mode instead. There was a NOTE(rsc) comment that it should be equivalent to use import path, but it was too late in Go 1.11 cycle to risk implementing that change. This is part 1, a more conservative change for backporting to Go 1.12.2, and it keeps the original behavior of splitting on p.Dir in GOPATH mode. Part 2 will address the NOTE(rsc) comment and modify behavior in Package.load to always use DefaultExecName which splits the import path rather than directory. It is intended to be included in Go 1.13. Fixes #27283 (again) Updates #26869 Fixes #30821 Change-Id: Ib1ebb95acba7c85c24e3a55c40cdf48405af34f3 Reviewed-on: https://go-review.googlesource.com/c/go/+/167503 Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Diffstat (limited to 'src/cmd/go/internal/work/build.go')
-rw-r--r--src/cmd/go/internal/work/build.go10
1 files changed, 4 insertions, 6 deletions
diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go
index 21abd5ef5b..eac027e09e 100644
--- a/src/cmd/go/internal/work/build.go
+++ b/src/cmd/go/internal/work/build.go
@@ -10,7 +10,6 @@ import (
"go/build"
"os"
"os/exec"
- "path"
"path/filepath"
"runtime"
"strings"
@@ -285,7 +284,7 @@ func runBuild(cmd *base.Command, args []string) {
pkgs := load.PackagesForBuild(args)
if len(pkgs) == 1 && pkgs[0].Name == "main" && cfg.BuildO == "" {
- _, cfg.BuildO = path.Split(pkgs[0].ImportPath)
+ cfg.BuildO = load.DefaultExecName(pkgs[0].ImportPath)
cfg.BuildO += cfg.ExeSuffix
}
@@ -320,14 +319,13 @@ func runBuild(cmd *base.Command, args []string) {
// If the -o name exists and is a directory, then
// write all main packages to that directory.
// Otherwise require only a single package be built.
- if bs, err := os.Stat(cfg.BuildO); err == nil && bs.IsDir() {
+ if fi, err := os.Stat(cfg.BuildO); err == nil && fi.IsDir() {
a := &Action{Mode: "go build"}
for _, p := range pkgs {
if p.Name != "main" {
continue
}
- _, elem := path.Split(p.ImportPath)
- p.Target = filepath.Join(cfg.BuildO, elem)
+ p.Target = filepath.Join(cfg.BuildO, load.DefaultExecName(p.ImportPath))
p.Target += cfg.ExeSuffix
p.Stale = true
p.StaleReason = "build -o flag in use"
@@ -540,7 +538,7 @@ func InstallPackages(patterns []string, pkgs []*load.Package) {
if len(patterns) == 0 && len(pkgs) == 1 && pkgs[0].Name == "main" {
// Compute file 'go build' would have created.
// If it exists and is an executable file, remove it.
- _, targ := filepath.Split(pkgs[0].ImportPath)
+ targ := load.DefaultExecName(pkgs[0].ImportPath)
targ += cfg.ExeSuffix
if filepath.Join(pkgs[0].Dir, targ) != pkgs[0].Target { // maybe $GOBIN is the current directory
fi, err := os.Stat(targ)