From 549f2e86a5d0daa1c4b88f409ff9051f7c257b14 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 2 Dec 2024 07:59:52 -0300 Subject: [PATCH] fix: improve zig build --- internal/builders/zig/build.go | 13 ++++++++++--- internal/builders/zig/build_test.go | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/internal/builders/zig/build.go b/internal/builders/zig/build.go index c8543dd0a86..126a2d333b9 100644 --- a/internal/builders/zig/build.go +++ b/internal/builders/zig/build.go @@ -3,6 +3,7 @@ package zig import ( "errors" "fmt" + "os" "os/exec" "path/filepath" "slices" @@ -128,9 +129,6 @@ func (b *Builder) WithDefaults(build config.Build) (config.Build, error) { // Build implements build.Builder. func (b *Builder) Build(ctx *context.Context, build config.Build, options api.Options) error { - prefix := filepath.Dir(options.Path) - options.Path = filepath.Join(prefix, "bin", options.Name) - t := options.Target.(Target) a := &artifact.Artifact{ Type: artifact.Binary, @@ -160,6 +158,7 @@ func (b *Builder) Build(ctx *context.Context, build config.Build, options api.Op return err } + prefix := filepath.Join("zig-out", t.Target) command := []string{ zigbin, build.Command, @@ -199,6 +198,14 @@ func (b *Builder) Build(ctx *context.Context, build config.Build, options api.Op log.WithField("cmd", command).Info(s) } + if err := os.MkdirAll(filepath.Dir(options.Path), 0o755); err != nil { + return err + } + realPath := filepath.Join(build.Dir, prefix, "bin", options.Name) + if err := gio.Copy(realPath, options.Path); err != nil { + return err + } + // TODO: move this to outside builder for both go and zig modTimestamp, err := tpl.Apply(build.ModTimestamp) if err != nil { diff --git a/internal/builders/zig/build_test.go b/internal/builders/zig/build_test.go index f6089009d4e..0768d605ec3 100644 --- a/internal/builders/zig/build_test.go +++ b/internal/builders/zig/build_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/goreleaser/goreleaser/v2/internal/artifact" "github.com/goreleaser/goreleaser/v2/internal/testctx" "github.com/goreleaser/goreleaser/v2/internal/testlib" api "github.com/goreleaser/goreleaser/v2/pkg/build" @@ -149,6 +150,7 @@ func TestBuild(t *testing.T) { }, Builds: []config.Build{ { + ID: "default", Dir: "./testdata/proj/", ModTimestamp: fmt.Sprintf("%d", modTime.Unix()), BuildDetails: config.BuildDetails{ @@ -177,7 +179,20 @@ func TestBuild(t *testing.T) { require.Len(t, bins, 1) bin := bins[0] - require.Equal(t, filepath.Join(dist, "proj-aarch64-macos", "bin", "proj"), filepath.FromSlash(bin.Path)) + require.Equal(t, artifact.Artifact{ + Name: "proj", + Path: filepath.Join(dist, "proj-aarch64-macos", "proj"), + Goos: "darwin", + Goarch: "arm64", + Target: "aarch64-macos", + Type: artifact.Binary, + Extra: artifact.Extras{ + artifact.ExtraBinary: "proj", + artifact.ExtraBuilder: "zig", + artifact.ExtraExt: "", + artifact.ExtraID: "default", + }, + }, *bin) require.FileExists(t, bin.Path) fi, err := os.Stat(bin.Path)