From 83a50b684da57c6d54b99acdfd9df071dca63042 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 10:58:30 +0000 Subject: [PATCH] feat(build): honor configured main package path --- cmd/build/cmd_project.go | 1 + pkg/build/build.go | 2 ++ pkg/build/builders/go.go | 8 ++++++-- pkg/build/builders/go_test.go | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/cmd/build/cmd_project.go b/cmd/build/cmd_project.go index f4f6f46..f176561 100644 --- a/cmd/build/cmd_project.go +++ b/cmd/build/cmd_project.go @@ -115,6 +115,7 @@ func runProjectBuild(ctx context.Context, buildType string, ciMode bool, targets // Create build config for the builder cfg := &build.Config{ FS: filesystem, + Project: buildConfig.Project, ProjectDir: projectDir, OutputDir: outputDir, Name: binaryName, diff --git a/pkg/build/build.go b/pkg/build/build.go index ae0281c..05d1b84 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -71,6 +71,8 @@ type Artifact struct { type Config struct { // FS is the medium used for file operations. FS io.Medium + // Project holds build-time project metadata. + Project Project // ProjectDir is the root directory of the project. ProjectDir string // OutputDir is where build artifacts are placed. diff --git a/pkg/build/builders/go.go b/pkg/build/builders/go.go index 8cec471..421070b 100644 --- a/pkg/build/builders/go.go +++ b/pkg/build/builders/go.go @@ -108,8 +108,12 @@ func (b *GoBuilder) buildTarget(ctx context.Context, cfg *build.Config, target b // Add output path args = append(args, "-o", outputPath) - // Add the project directory as the build target (current directory) - args = append(args, ".") + // Build the configured main package path, defaulting to the project root. + mainPackage := cfg.Project.Main + if mainPackage == "" { + mainPackage = "." + } + args = append(args, mainPackage) // Set up environment. env := append([]string{}, cfg.Env...) diff --git a/pkg/build/builders/go_test.go b/pkg/build/builders/go_test.go index d48518f..942cb42 100644 --- a/pkg/build/builders/go_test.go +++ b/pkg/build/builders/go_test.go @@ -382,6 +382,42 @@ func TestGo_GoBuilderBuild_Good(t *testing.T) { assert.Contains(t, args, ".") }) + t.Run("builds the configured main package path", func(t *testing.T) { + projectDir := setupGoTestProject(t) + err := ax.MkdirAll(ax.Join(projectDir, "cmd", "myapp"), 0755) + require.NoError(t, err) + err = ax.WriteFile(ax.Join(projectDir, "cmd", "myapp", "main.go"), []byte("package main\n\nfunc main() {}\n"), 0644) + require.NoError(t, err) + + outputDir := t.TempDir() + logPath := ax.Join(t.TempDir(), "go-build-args.log") + t.Setenv("GO_BUILD_LOG_FILE", logPath) + + builder := NewGoBuilder() + cfg := &build.Config{ + FS: io.Local, + ProjectDir: projectDir, + OutputDir: outputDir, + Name: "mainpackage", + } + cfg.Project.Main = "./cmd/myapp" + targets := []build.Target{ + {OS: runtime.GOOS, Arch: runtime.GOARCH}, + } + + artifacts, err := builder.Build(context.Background(), cfg, targets) + require.NoError(t, err) + require.Len(t, artifacts, 1) + + content, err := ax.ReadFile(logPath) + require.NoError(t, err) + + args := strings.Split(strings.TrimSpace(string(content)), "\n") + require.NotEmpty(t, args) + assert.Contains(t, args, "./cmd/myapp") + assert.NotContains(t, args, ".") + }) + t.Run("creates output directory if missing", func(t *testing.T) { projectDir := setupGoTestProject(t) outputDir := ax.Join(t.TempDir(), "nested", "output")