diff --git a/modules/mesh-build-go/cmd/mesh-build-go/build.go b/modules/mesh-build-go/cmd/mesh-build-go/build.go index 4dbf22d2..746aa839 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/build.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/build.go @@ -40,7 +40,7 @@ type BuildResult struct { func (m *Module) buildTool() StepTool { return StepTool{ Name: "build_go_build", - Description: "Build one Go bundle artifact of a module of a fetched workspace with the build agent's recipe, by the " + + Description: "Build one Go bundle or archive artifact of a module of a fetched workspace with the build agent's recipe, by the " + "installed Go and no network, and pack it. Answers a step id at once; the step ends on its key with the " + "artifact's digest, size and the archive's path.", Input: map[string]any{ @@ -105,8 +105,11 @@ func (m *Module) build(ctx context.Context, args map[string]any) (any, error) { if art == nil { return nil, fmt.Errorf("the module %s has no artifact %q (it has %s)", manifest.Module, name, strings.Join(names, ", ")) } + if art.Kind == "archive" { + return m.buildArchive(ctx, workspace, module, dir, *art) + } if art.Kind != "bundle" || art.Language != "go" { - return nil, fmt.Errorf("the artifact %s of %s is a %s of language %q: this module builds Go bundles", name, manifest.Module, art.Kind, art.Language) + return nil, fmt.Errorf("the artifact %s of %s is a %s of language %q: this module builds Go bundles and archives", name, manifest.Module, art.Kind, art.Language) } if strings.TrimSpace(art.From) == "" { return nil, fmt.Errorf("the artifact %s of %s says nothing it is built from", name, manifest.Module) @@ -130,19 +133,30 @@ func (m *Module) build(ctx context.Context, args map[string]any) (any, error) { if r := m.Run(ctx, m.buildEnv(dir), "go", cmd...); r.Status != 0 || r.Err != "" { return nil, fmt.Errorf("go build of %s: %s", art.Name, causeLine(r.Err+"\n"+r.Stderr+"\n"+r.Stdout)) } - archive, digest, err := buildstep.Pack(out) + res, err := m.packInto(workspace, module, art.Name, out) if err != nil { - return nil, fmt.Errorf("packing %s: %w", art.Name, err) + return nil, err + } + res.Binary = binary + return res, nil +} + +// packInto packs a directory with mesh-sdk's Pack, as the build agent packs every artifact, and leaves the archive in +// the workspace's artifacts directory. +func (m *Module) packInto(workspace, module, artifact, root string) (BuildResult, error) { + archive, digest, err := buildstep.Pack(root) + if err != nil { + return BuildResult{}, fmt.Errorf("packing %s: %w", artifact, err) } artifacts := filepath.Join(m.Work, workspace, "artifacts") if err := os.MkdirAll(artifacts, 0o700); err != nil { - return nil, err + return BuildResult{}, err } - path := filepath.Join(artifacts, strings.ReplaceAll(module, "/", "_")+"."+art.Name+".tar.gz") + path := filepath.Join(artifacts, strings.ReplaceAll(module, "/", "_")+"."+artifact+".tar.gz") if err := os.WriteFile(path, archive, 0o600); err != nil { - return nil, err + return BuildResult{}, err } - return BuildResult{Artifact: art.Name, Binary: binary, Digest: digest, Size: len(archive), Path: path}, nil + return BuildResult{Artifact: artifact, Digest: digest, Size: len(archive), Path: path}, nil } // buildEnv is the environment of the compile: the build agent's (CGO_ENABLED=0, GOFLAGS) with the installed go diff --git a/modules/mesh-build-go/cmd/mesh-build-go/build_test.go b/modules/mesh-build-go/cmd/mesh-build-go/build_test.go index 181f7d87..fa369bb7 100644 --- a/modules/mesh-build-go/cmd/mesh-build-go/build_test.go +++ b/modules/mesh-build-go/cmd/mesh-build-go/build_test.go @@ -175,7 +175,7 @@ func TestTheArchiveHoldsOneExecutableAndTwoBuildsAreOneDigest(t *testing.T) { func TestAnArtifactOfAnotherLanguageFailsTheStepNamingIt(t *testing.T) { for artifact, want := range map[string]string{ `{"name":"tools","kind":"bundle","language":"typescript","entrypoints":["index.js"],"loads":["index.js"]}`: `language "typescript"`, - `{"name":"tools","kind":"archive","from":"files"}`: "archive", + `{"name":"tools","kind":"image","from":"files"}`: "image", } { m, states, calls := buildModule(t, manifestWith(artifact), "x") _, st := runBuild(t, m, states, "tools")