Skip to content

Commit 13b27b6

Browse files
committed
Fix build.
1 parent ef52a88 commit 13b27b6

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

pkg/plugins/builder/builder.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"context"
55
"embed"
66
"fmt"
7-
"log"
87
"os"
98
"path/filepath"
109
"runtime"
1110
"text/template"
1211

1312
"github.com/launchrctl/launchr"
13+
"github.com/launchrctl/launchr/pkg/cli"
14+
"github.com/launchrctl/launchr/pkg/log"
1415
)
1516

1617
const launchrPkg = "github.com/launchrctl/launchr"
@@ -85,7 +86,7 @@ func NewBuilder(opts *BuildOptions) (*Builder, error) {
8586

8687
// Build prepares build environment, generates go files and build the binary.
8788
func (b *Builder) Build(ctx context.Context) error {
88-
log.Printf("[INFO] Start building")
89+
cli.Println("Starting building %s", b.PkgName)
8990
// Prepare build environment dir and go executable.
9091
var err error
9192
b.env, err = newBuildEnvironment()
@@ -99,10 +100,10 @@ func (b *Builder) Build(ctx context.Context) error {
99100
_ = b.Close()
100101
}
101102
}()
102-
log.Printf("[DEBUG] Temporary folder: %s", b.env.wd)
103+
log.Debug("Temporary folder: %s", b.env.wd)
103104

104105
// Write files to dir and generate go mod.
105-
log.Printf("[INFO] Creating project files and fetching dependencies")
106+
cli.Println("Creating project files and fetching dependencies")
106107
b.env.SetEnv("CGO_ENABLE", "0")
107108
err = b.env.CreateModFile(ctx, b.BuildOptions)
108109
if err != nil {
@@ -138,9 +139,7 @@ func (b *Builder) Build(ctx context.Context) error {
138139
}
139140

140141
// Generate code for provided plugins.
141-
genArgs := []string{"generate", "./..."}
142-
cmdGen := b.env.NewCommand(ctx, b.env.Go(), genArgs...)
143-
err = b.env.RunCmd(ctx, cmdGen)
142+
err = b.runGen(ctx)
144143
if err != nil {
145144
return err
146145
}
@@ -152,19 +151,20 @@ func (b *Builder) Build(ctx context.Context) error {
152151
}
153152

154153
// Build the main go package.
155-
log.Printf("[INFO] Building Launchr")
154+
cli.Println("Building %s", b.PkgName)
156155
err = b.goBuild(ctx)
157156
if err != nil {
158157
return err
159158
}
160159

161-
log.Printf("[INFO] Build complete: %s", b.BuildOutput)
160+
cli.Println("Build complete: %s", b.BuildOutput)
162161
return nil
163162
}
164163

165164
// Close does cleanup after build.
166165
func (b *Builder) Close() error {
167166
if b.env != nil && !b.Debug {
167+
log.Debug("Cleaning build environment: %s", b.env.wd)
168168
return b.env.Close()
169169
}
170170
return nil
@@ -186,10 +186,6 @@ func (b *Builder) goBuild(ctx context.Context) error {
186186
args = append(args, "-ldflags", "-w -s", "-trimpath")
187187
}
188188
cmd := b.env.NewCommand(ctx, b.env.Go(), args...)
189-
cmd.Env = envFromOs()
190-
191-
log.Printf("[DEBUG] Go build command: %s", cmd)
192-
log.Printf("[DEBUG] Environment variables: %v", cmd.Env)
193189
err = b.env.RunCmd(ctx, cmd)
194190
if err != nil {
195191
return err
@@ -216,3 +212,15 @@ func (b *Builder) getBuildVersion(version *launchr.AppVersion) *launchr.AppVersi
216212

217213
return &bv
218214
}
215+
216+
func (b *Builder) runGen(ctx context.Context) error {
217+
genArgs := []string{"generate", "./..."}
218+
cmd := b.env.NewCommand(ctx, b.env.Go(), genArgs...)
219+
env := make(envVars, len(cmd.Env))
220+
copy(env, cmd.Env)
221+
// Exclude target platform information as it may break "go run".
222+
env.Unset("GOOS")
223+
env.Unset("GOARCH")
224+
cmd.Env = env
225+
return b.env.RunCmd(ctx, cmd)
226+
}

pkg/plugins/builder/environment.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"time"
1313

1414
"golang.org/x/mod/modfile"
15+
16+
"github.com/launchrctl/launchr/pkg/log"
1517
)
1618

1719
type envVars []string
@@ -42,6 +44,18 @@ func (a *envVars) Set(k string, v string) {
4244
*a = append(*a, fmt.Sprintf("%s=%s", k, v))
4345
}
4446

47+
func (a *envVars) Unset(k string) {
48+
if k == "" {
49+
return
50+
}
51+
for i := 0; i < len(*a); i++ {
52+
if strings.HasPrefix((*a)[i], k+"=") {
53+
*a = append((*a)[:i], (*a)[i+1:]...)
54+
return
55+
}
56+
}
57+
}
58+
4559
type buildEnvironment struct {
4660
wd string
4761
env envVars
@@ -142,6 +156,7 @@ func (env *buildEnvironment) Filepath(s string) string {
142156
func (env *buildEnvironment) NewCommand(ctx context.Context, command string, args ...string) *exec.Cmd {
143157
cmd := exec.CommandContext(ctx, command, args...)
144158
cmd.Dir = env.wd
159+
cmd.Env = env.env
145160
cmd.Stdout = os.Stdout
146161
cmd.Stderr = os.Stderr
147162
return cmd
@@ -158,6 +173,8 @@ func (env *buildEnvironment) execGoGet(ctx context.Context, args ...string) erro
158173
}
159174

160175
func (env *buildEnvironment) RunCmd(ctx context.Context, cmd *exec.Cmd) error {
176+
log.Debug("Executing shell: %s", cmd)
177+
log.Debug("Shell env variables: %s", cmd.Env)
161178
err := cmd.Start()
162179
if err != nil {
163180
return err

0 commit comments

Comments
 (0)