feat(build): honor configured main package path
This commit is contained in:
parent
72bb94e355
commit
83a50b684d
4 changed files with 45 additions and 2 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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...)
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue