src/cmd/go/internal/load/pkg.go | 71 ++++++++++++++++++++++++----------------------------- src/cmd/go/internal/test/test.go | 2 +- src/cmd/go/internal/work/build.go | 3 ++- src/cmd/go/testdata/mod/rsc.io_fortune_v2_v2.0.0.txt | 6 ------ src/cmd/go/testdata/script/mod_build_versioned.txt | 16 ---------------- diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 187295b3459ae88f510ae3a5449525f5e00d45d6..228be07f2492a46237d6d8e6a42418598b485745 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -1178,44 +1178,6 @@ } var foldPath = make(map[string]string) -// DefaultExecName returns the default executable name of the given -// package. -func DefaultExecName(p *Package) string { - _, elem := filepath.Split(p.ImportPath) - if cfg.ModulesEnabled { - // NOTE(rsc): Using p.ImportPath instead of p.Dir - // makes sure we install a package in the root of a - // cached module directory as that package name - // not name@v1.2.3. - // Using p.ImportPath instead of p.Dir - // is probably correct all the time, - // even for non-module-enabled code, - // but I'm not brave enough to change the - // non-module behavior this late in the - // release cycle. Maybe for Go 1.12. - // See golang.org/issue/26869. - _, elem = pathpkg.Split(p.ImportPath) - - // If this is example.com/mycmd/v2, it's more useful to install it as mycmd than as v2. - // See golang.org/issue/24667. - isVersion := func(v string) bool { - if len(v) < 2 || v[0] != 'v' || v[1] < '1' || '9' < v[1] { - return false - } - for i := 2; i < len(v); i++ { - if c := v[i]; c < '0' || '9' < c { - return false - } - } - return true - } - if isVersion(elem) { - _, elem = pathpkg.Split(pathpkg.Dir(p.ImportPath)) - } - } - return elem -} - // load populates p using information from bp, err, which should // be the result of calling build.Context.Import. func (p *Package) load(stk *ImportStack, bp *build.Package, err error) { @@ -1256,7 +1218,38 @@ e := fmt.Sprintf("the %v command has moved; use %v instead.", p.ImportPath, newPath) p.Error = &PackageError{Err: e} return } - elem := DefaultExecName(p) + _, elem := filepath.Split(p.Dir) + if cfg.ModulesEnabled { + // NOTE(rsc): Using p.ImportPath instead of p.Dir + // makes sure we install a package in the root of a + // cached module directory as that package name + // not name@v1.2.3. + // Using p.ImportPath instead of p.Dir + // is probably correct all the time, + // even for non-module-enabled code, + // but I'm not brave enough to change the + // non-module behavior this late in the + // release cycle. Maybe for Go 1.12. + // See golang.org/issue/26869. + _, elem = pathpkg.Split(p.ImportPath) + + // If this is example.com/mycmd/v2, it's more useful to install it as mycmd than as v2. + // See golang.org/issue/24667. + isVersion := func(v string) bool { + if len(v) < 2 || v[0] != 'v' || v[1] < '1' || '9' < v[1] { + return false + } + for i := 2; i < len(v); i++ { + if c := v[i]; c < '0' || '9' < c { + return false + } + } + return true + } + if isVersion(elem) { + _, elem = pathpkg.Split(pathpkg.Dir(p.ImportPath)) + } + } full := cfg.BuildContext.GOOS + "_" + cfg.BuildContext.GOARCH + "/" + elem if cfg.BuildContext.GOOS != base.ToolGOOS || cfg.BuildContext.GOARCH != base.ToolGOARCH { // Install cross-compiled binaries to subdirectories of bin. diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go index 2eca627c9ae50722d2e925554ee704d28f790711..8dfb3df22d3b9ed4ba93d0b22d16b1550dbdac1a 100644 --- a/src/cmd/go/internal/test/test.go +++ b/src/cmd/go/internal/test/test.go @@ -805,7 +805,7 @@ var elem string if p.ImportPath == "command-line-arguments" { elem = p.Name } else { - elem = load.DefaultExecName(p) + _, elem = path.Split(p.ImportPath) } testBinary := elem + ".test" diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index d89ee899f095b5abf6afcbd8d91f6aa1d24a48df..145b87513a9166aecb9d9c7b7efd690425f3b1b3 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -10,6 +10,7 @@ "fmt" "go/build" "os" "os/exec" + "path" "path/filepath" "runtime" "strings" @@ -284,7 +285,7 @@ pkgs := load.PackagesForBuild(args) if len(pkgs) == 1 && pkgs[0].Name == "main" && cfg.BuildO == "" { - cfg.BuildO = load.DefaultExecName(pkgs[0]) + _, cfg.BuildO = path.Split(pkgs[0].ImportPath) cfg.BuildO += cfg.ExeSuffix } diff --git a/src/cmd/go/testdata/mod/rsc.io_fortune_v2_v2.0.0.txt b/src/cmd/go/testdata/mod/rsc.io_fortune_v2_v2.0.0.txt index 3acd6379311f643194e45a712464b4af4aed5fa6..cfa91f08a5d84ba07ffd96eef8580a43c455899b 100644 --- a/src/cmd/go/testdata/mod/rsc.io_fortune_v2_v2.0.0.txt +++ b/src/cmd/go/testdata/mod/rsc.io_fortune_v2_v2.0.0.txt @@ -13,9 +13,3 @@ func main() { println(quote.Hello()) } --- fortune_test.go -- -package main - -import "testing" - -func TestFortuneV2(t *testing.T) {} diff --git a/src/cmd/go/testdata/script/mod_build_versioned.txt b/src/cmd/go/testdata/script/mod_build_versioned.txt deleted file mode 100644 index eb081c9be1f0fb7b0932895fb491a470924c7c40..0000000000000000000000000000000000000000 --- a/src/cmd/go/testdata/script/mod_build_versioned.txt +++ /dev/null @@ -1,16 +0,0 @@ -env GO111MODULE=on - -go get -m rsc.io/fortune/v2 - -# The default executable name shouldn't be v2$exe -go build rsc.io/fortune/v2 -! exists v2$exe -exists fortune$exe - -# The default test binary name shouldn't be v2.test$exe -go test -c rsc.io/fortune/v2 -! exists v2.test$exe -exists fortune.test$exe - --- go.mod -- -module scratch