Commit 0c14345c authored by Jess Frazelle's avatar Jess Frazelle Committed by Russ Cox

cmd/go: ensure pkgsFilter is run before build

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: default avatarRuss Cox <rsc@golang.org>
parent d85a3535
...@@ -2045,6 +2045,16 @@ func TestGoTestMutexprofileDashOControlsBinaryLocation(t *testing.T) { ...@@ -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") 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) { func TestGoTestDashCDashOControlsBinaryLocation(t *testing.T) {
tg := testgo(t) tg := testgo(t)
defer tg.cleanup() defer tg.cleanup()
......
...@@ -310,6 +310,8 @@ func runBuild(cmd *base.Command, args []string) { ...@@ -310,6 +310,8 @@ func runBuild(cmd *base.Command, args []string) {
depMode = ModeInstall depMode = ModeInstall
} }
pkgs = pkgsFilter(load.Packages(args))
if cfg.BuildO != "" { if cfg.BuildO != "" {
if len(pkgs) > 1 { if len(pkgs) > 1 {
base.Fatalf("go build: cannot use -o with multiple packages") base.Fatalf("go build: cannot use -o with multiple packages")
...@@ -325,8 +327,6 @@ func runBuild(cmd *base.Command, args []string) { ...@@ -325,8 +327,6 @@ func runBuild(cmd *base.Command, args []string) {
return return
} }
pkgs = pkgsFilter(load.Packages(args))
a := &Action{Mode: "go build"} a := &Action{Mode: "go build"}
for _, p := range pkgs { for _, p := range pkgs {
a.Deps = append(a.Deps, b.AutoAction(ModeBuild, depMode, p)) a.Deps = append(a.Deps, b.AutoAction(ModeBuild, depMode, p))
......
...@@ -123,6 +123,12 @@ func buildModeInit() { ...@@ -123,6 +123,12 @@ func buildModeInit() {
case "exe": case "exe":
pkgsFilter = pkgsMain pkgsFilter = pkgsMain
ldBuildmode = "exe" 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": case "pie":
if cfg.BuildRace { if cfg.BuildRace {
base.Fatalf("-buildmode=pie not supported when -race is enabled") base.Fatalf("-buildmode=pie not supported when -race is enabled")
......
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