From c1d9d1f305df19c66e28a619e17f9f1d7563d977 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 25 May 2018 14:27:29 -0700 Subject: [PATCH] cmd/go: don't generate output for "go build -o /dev/null x.go" We look for "-o /dev/null", and, if found, pretend that there was no "-o" option and don't generate an action to create the final executable. We look for "go build x.go", and, if found, and if -o was not used, pretend that the user specified "-o x". Unfortunately, we were doing those in the wrong order, so that "go build -o /dev/null x.go" would first clear the "-o" option and then set it to "-o x". This CL flips the order so that the right thing happens. Fixes #25579 Change-Id: Ic9556ac0a57f7b45b685951bc96ba5ea4633b860 Reviewed-on: https://go-review.googlesource.com/114715 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/go/go_test.go | 11 +++++++++++ src/cmd/go/internal/work/build.go | 10 +++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index fb8846c710..a0fc72aac4 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -6325,3 +6325,14 @@ func TestCDAndGOPATHAreDifferent(t *testing.T) { testCDAndGOPATHAreDifferent(tg, cd, strings.ToLower(gopath)) } } + +// Issue 25579. +func TestGoBuildDashODevNull(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + tg.parallel() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + tg.run("build", "-o", os.DevNull, filepath.Join(tg.pwd(), "testdata", "src", "hello", "hello.go")) + tg.mustNotExist("hello") + tg.mustNotExist("hello.exe") +} diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index e5f0e624c3..5cb0c2431f 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -284,11 +284,6 @@ func runBuild(cmd *base.Command, args []string) { cfg.BuildO += cfg.ExeSuffix } - // Special case -o /dev/null by not writing at all. - if cfg.BuildO == os.DevNull { - cfg.BuildO = "" - } - // sanity check some often mis-used options switch cfg.BuildContext.Compiler { case "gccgo": @@ -311,6 +306,11 @@ func runBuild(cmd *base.Command, args []string) { pkgs = pkgsFilter(load.Packages(args)) + // Special case -o /dev/null by not writing at all. + if cfg.BuildO == os.DevNull { + cfg.BuildO = "" + } + if cfg.BuildO != "" { if len(pkgs) > 1 { base.Fatalf("go build: cannot use -o with multiple packages") -- 2.51.0