diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 0bf10e2560a..f42ec50006a 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -317,6 +317,13 @@ 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 = pkgs[0].DefaultExecName() + cfg.BuildO += cfg.ExeSuffix + } + // sanity check some often mis-used options switch cfg.BuildContext.Compiler { case "gccgo": @@ -342,11 +349,16 @@ func runBuild(cmd *base.Command, args []string) { // Special case -o /dev/null by not writing at all. if cfg.BuildO == os.DevNull { cfg.BuildO = "" - } else if cfg.BuildO != "" { + } + + if cfg.BuildO != "" { // 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 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" { @@ -377,14 +389,6 @@ func runBuild(cmd *base.Command, args []string) { a := b.AutoAction(ModeInstall, depMode, p) b.Do(a) return - } else if len(pkgs) == 1 && pkgs[0].Name == "main" { - p := pkgs[0] - p.Target = p.DefaultExecName() - p.Target += cfg.ExeSuffix - p.Stale = true - a := b.AutoAction(ModeInstall, depMode, p) - b.Do(a) - return } a := &Action{Mode: "go build"}