]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: ensure pkgsFilter is run before build
authorJess Frazelle <me@jessfraz.com>
Mon, 17 Jul 2017 21:00:35 +0000 (17:00 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 29 Nov 2017 16:54:25 +0000 (16:54 +0000)
Return an error when a user passes -o and -buildmode=exe to build a package
without a main.

Fixes #20017.

Change-Id: I07d49c75e7088a96f00afe18c9faa842c5d71afb
Reviewed-on: https://go-review.googlesource.com/49371
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/work/build.go
src/cmd/go/internal/work/init.go
src/cmd/go/testdata/src/not_main/not_main.go [new file with mode: 0644]

index d756814f7bc99cf2ed93b1726bd7b745612e7f91..3507b12a03cc25200854a8b460dc0ff147b6f8fc 100644 (file)
@@ -2045,6 +2045,16 @@ func TestGoTestMutexprofileDashOControlsBinaryLocation(t *testing.T) {
        tg.wantExecutable("myerrors.test"+exeSuffix, "go test -mutexprofile -o myerrors.test did not create myerrors.test")
 }
 
+func TestGoBuildNonMain(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       // TODO: tg.parallel()
+       tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
+       tg.runFail("build", "-buildmode=exe", "-o", "not_main"+exeSuffix, "not_main")
+       tg.grepStderr("-buildmode=exe requires exactly one main package", "go build with -o and -buildmode=exe should on a non-main package should throw an error")
+       tg.mustNotExist("not_main" + exeSuffix)
+}
+
 func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
        tg := testgo(t)
        defer tg.cleanup()
index 6ae2ca35cf35540c158f66f8e6712888726ff545..57b7b00879121a6ca1acf919ff773e23a3bb1c9b 100644 (file)
@@ -310,6 +310,8 @@ func runBuild(cmd *base.Command, args []string) {
                depMode = ModeInstall
        }
 
+       pkgs = pkgsFilter(load.Packages(args))
+
        if cfg.BuildO != "" {
                if len(pkgs) > 1 {
                        base.Fatalf("go build: cannot use -o with multiple packages")
@@ -325,8 +327,6 @@ func runBuild(cmd *base.Command, args []string) {
                return
        }
 
-       pkgs = pkgsFilter(load.Packages(args))
-
        a := &Action{Mode: "go build"}
        for _, p := range pkgs {
                a.Deps = append(a.Deps, b.AutoAction(ModeBuild, depMode, p))
index 0e17286cf68921debb9a7011874c441d2ff20f2e..7f894f5c6dce20c2019044fea34505ebe1ceb877 100644 (file)
@@ -123,6 +123,12 @@ func buildModeInit() {
        case "exe":
                pkgsFilter = pkgsMain
                ldBuildmode = "exe"
+               // Set the pkgsFilter to oneMainPkg if the user passed a specific binary output
+               // and is using buildmode=exe for a better error message.
+               // See issue #20017.
+               if cfg.BuildO != "" {
+                       pkgsFilter = oneMainPkg
+               }
        case "pie":
                if cfg.BuildRace {
                        base.Fatalf("-buildmode=pie not supported when -race is enabled")
diff --git a/src/cmd/go/testdata/src/not_main/not_main.go b/src/cmd/go/testdata/src/not_main/not_main.go
new file mode 100644 (file)
index 0000000..75a397c
--- /dev/null
@@ -0,0 +1,3 @@
+package not_main
+
+func F() {}