From 6d4ae81af949db4b729bf60a84cbe65edf9e79e6 Mon Sep 17 00:00:00 2001
From: Laurin <laurin.martins@outlook.de>
Date: Sun, 12 Jul 2020 11:09:57 +0200
Subject: [PATCH] Fixed parsing of user defined linker flags

---
 harness/build.go    | 10 +++-------
 revel/build_test.go | 26 +++++++++++++++++++++-----
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/harness/build.go b/harness/build.go
index f3a45de6..2c216578 100644
--- a/harness/build.go
+++ b/harness/build.go
@@ -160,16 +160,15 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
 			if !contains(c.BuildFlags, "build") {
 				flags = []string{"build"}
 			}
-			flags = append(flags, c.BuildFlags...)
 			if !contains(flags, "-ldflags") {
 				ldflags := "-ldflags= " + versionLinkerFlags
-				// Add in build flags
+				// Add user defined build flags
 				for i := range c.BuildFlags {
-					ldflags += "-X '" + c.BuildFlags[i] + "'"
+					ldflags += " -X '" + c.BuildFlags[i] + "'"
 				}
 				flags = append(flags, ldflags)
 			}
-			if !contains(flags, "-tags") {
+			if !contains(flags, "-tags") && buildTags != "" {
 				flags = append(flags, "-tags", buildTags)
 			}
 			if !contains(flags, "-o") {
@@ -177,9 +176,6 @@ func Build(c *model.CommandConfig, paths *model.RevelContainer) (_ *App, err err
 			}
 		}
 
-		// Add in build flags
-		flags = append(flags, c.BuildFlags...)
-
 		// Note: It's not applicable for filepath.* usage
 		flags = append(flags, path.Join(paths.ImportPath, "app", "tmp"))
 
diff --git a/revel/build_test.go b/revel/build_test.go
index b1390667..f134fa07 100644
--- a/revel/build_test.go
+++ b/revel/build_test.go
@@ -1,19 +1,20 @@
 package main_test
 
 import (
-	"github.com/revel/cmd/model"
-	"github.com/revel/cmd/revel"
-	"github.com/revel/cmd/utils"
-	"github.com/stretchr/testify/assert"
 	"os"
 	"path/filepath"
 	"testing"
+
+	"github.com/revel/cmd/model"
+	main "github.com/revel/cmd/revel"
+	"github.com/revel/cmd/utils"
+	"github.com/stretchr/testify/assert"
 )
 
 // test the commands
 func TestBuild(t *testing.T) {
 	a := assert.New(t)
-	gopath := setup("revel-test-build",  a)
+	gopath := setup("revel-test-build", a)
 
 	t.Run("Build", func(t *testing.T) {
 		a := assert.New(t)
@@ -26,6 +27,21 @@ func TestBuild(t *testing.T) {
 		a.True(utils.Exists(filepath.Join(gopath, "build-test", "target")))
 	})
 
+	t.Run("Build-withFlags", func(t *testing.T) {
+		a := assert.New(t)
+		c := newApp("build-test-WithFlags", model.NEW, nil, a)
+		c.BuildFlags = []string{
+			"build-test-WithFlags/app.AppVersion=SomeValue",
+			"build-test-WithFlags/app.SomeOtherValue=SomeValue",
+		}
+		main.Commands[model.NEW].RunWith(c)
+		c.Index = model.BUILD
+		c.Build.TargetPath = filepath.Join(gopath, "build-test", "target")
+		c.Build.ImportPath = c.ImportPath
+		a.Nil(main.Commands[model.BUILD].RunWith(c), "Failed to run build-test-withFlags")
+		a.True(utils.Exists(filepath.Join(gopath, "build-test", "target")))
+	})
+
 	if !t.Failed() {
 		if err := os.RemoveAll(gopath); err != nil {
 			a.Fail("Failed to remove test path")