Repository: docker/compose
File: pkg/compose/build_buildkit.go (Go)

Code before:

package compose

import (
	"context"
	"crypto/sha1"
	"fmt"
	"os"

	"github.com/docker/buildx/build"
	"github.com/docker/buildx/builder"
	_ "github.com/docker/buildx/driver/docker"           //nolint:blank-imports
	_ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
	_ "github.com/docker/buildx/driver/kubernetes"       //nolint:blank-imports
	_ "github.com/docker/buildx/driver/remote"           //nolint:blank-imports
	"github.com/docker/buildx/util/confutil"
	"github.com/docker/buildx/util/dockerutil"
	buildx "github.com/docker/buildx/util/progress"
	"github.com/docker/compose/v2/pkg/progress"
	"github.com/moby/buildkit/client"
)

func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, builderName string) (string, error) {
	if builderName == "" {
		builderName = os.Getenv("BUILDX_BUILDER")
	}
	b, err := builder.New(s.dockerCli, builder.WithName(builderName))
	if err != nil {
		return "", err
	}

	nodes, err := b.LoadNodes(ctx, false)
	if err != nil {
		return "", err
	}

	var response map[string]*client.SolveResponse
	if s.dryRun {
		response = s.dryRunBuildResponse(ctx, service, opts)
	} else {
		response, err = build.Build(ctx, nodes,
			map[string]build.Options{service: opts},
			dockerutil.NewClient(s.dockerCli),
			confutil.ConfigDir(s.dockerCli),
			buildx.WithPrefix(p, service, true))
		if err != nil {
			return "", WrapCategorisedComposeError(err, BuildFailure)
		}
	}

	for _, img := range response {

Review comment (suggestion):
Not critical, but I think it might be a bit cleaner/separation of concerns to do the fallback env lookup in the `cmd/` package parts and use the value as-is here

Code after:

package compose

import (
	"context"
	"crypto/sha1"
	"fmt"

	"github.com/docker/buildx/build"
	"github.com/docker/buildx/builder"
	_ "github.com/docker/buildx/driver/docker"           //nolint:blank-imports
	_ "github.com/docker/buildx/driver/docker-container" //nolint:blank-imports
	_ "github.com/docker/buildx/driver/kubernetes"       //nolint:blank-imports
	_ "github.com/docker/buildx/driver/remote"           //nolint:blank-imports
	"github.com/docker/buildx/util/confutil"
	"github.com/docker/buildx/util/dockerutil"
	buildx "github.com/docker/buildx/util/progress"
	"github.com/docker/compose/v2/pkg/progress"
	"github.com/moby/buildkit/client"
)

func (s *composeService) doBuildBuildkit(ctx context.Context, service string, opts build.Options, p *buildx.Printer, builderName string) (string, error) {
	b, err := builder.New(s.dockerCli, builder.WithName(builderName))
	if err != nil {
		return "", err
	}

	nodes, err := b.LoadNodes(ctx, false)
	if err != nil {
		return "", err
	}

	var response map[string]*client.SolveResponse
	if s.dryRun {
		response = s.dryRunBuildResponse(ctx, service, opts)
	} else {
		response, err = build.Build(ctx, nodes,
			map[string]build.Options{service: opts},
			dockerutil.NewClient(s.dockerCli),
			confutil.ConfigDir(s.dockerCli),
			buildx.WithPrefix(p, service, true))
		if err != nil {
			return "", WrapCategorisedComposeError(err, BuildFailure)
		}
	}

	for _, img := range response {
		if img == nil || len(img.ExporterResponse) == 0 {
			continue
		}
		digest, ok := img.ExporterResponse["containerimage.digest"]