]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: do not write output when -o is not specified, but folder with same name exists
authorDaniel Theophanes <kardianos@gmail.com>
Sat, 6 Apr 2019 16:50:28 +0000 (09:50 -0700)
committerDaniel Theophanes <kardianos@gmail.com>
Mon, 8 Apr 2019 17:21:02 +0000 (17:21 +0000)
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>
src/cmd/go/internal/work/build.go
src/cmd/go/testdata/script/build_multi_main.txt

index 96b37444447f535787785210391f5762dc0bfa20..15faf578f89b3567182f5a16905737cb04dac76a 100644 (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" {
index 734e8d88d2881184c7aaceeced18c5fa43351576..89fe2bec13e95a02bd0ed5b460daaa4f3cd213df 100644 (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.