1
0
mirror of https://github.com/golang/go synced 2024-11-24 04:40:24 -07:00

cmd/go: do not write output when -o is not specified, but folder with same name exists

Fixes #31296

Change-Id: Ib8850fe22749ca0bf268614ba045ffe3fc68f5cc
Reviewed-on: https://go-review.googlesource.com/c/go/+/171057
Run-TryBot: Daniel Theophanes <kardianos@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Daniel Theophanes 2019-04-06 09:50:28 -07:00
parent c46ebec322
commit 081bc62d72
2 changed files with 11 additions and 0 deletions

View File

@ -283,6 +283,8 @@ func runBuild(cmd *base.Command, args []string) {
pkgs := load.PackagesForBuild(args)
explicitO := len(cfg.BuildO) > 0
if len(pkgs) == 1 && pkgs[0].Name == "main" && cfg.BuildO == "" {
cfg.BuildO = load.DefaultExecName(pkgs[0].ImportPath)
cfg.BuildO += cfg.ExeSuffix
@ -320,6 +322,9 @@ func runBuild(cmd *base.Command, args []string) {
// write all main packages to that directory.
// Otherwise require only a single package be built.
if fi, err := os.Stat(cfg.BuildO); err == nil && fi.IsDir() {
if !explicitO {
base.Fatalf("go build: build output %q already exists and is a directory", cfg.BuildO)
}
a := &Action{Mode: "go build"}
for _, p := range pkgs {
if p.Name != "main" {

View File

@ -7,6 +7,9 @@ go build -o $WORK/bin ./cmd/c1 ./cmd/c2
! go build -o $WORK/bin ./pkg1 ./pkg1
stderr 'no main packages'
! go build ./cmd/c1
stderr 'already exists and is a directory'
-- go.mod --
module exmod
@ -25,3 +28,6 @@ package pkg1
-- pkg2/pkg2.go --
package pkg2
-- c1$exe/keep.txt --
Create c1 directory.