Commit 081bc62d authored by Daniel Theophanes's avatar Daniel Theophanes

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: default avatarJay Conrod <jayconrod@google.com>
parent c46ebec3
......@@ -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" {
......
......@@ -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.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment