From 1cb1ee018b216b5fd3d4f2ce8b8c99bf2569c859 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 3 Jun 2022 19:26:16 +0200 Subject: [PATCH] build: enhance warning message when no output specified Signed-off-by: CrazyMax --- bake/bake.go | 5 +++++ build/build.go | 22 ++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 0c06969d..e2f04802 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -332,6 +332,7 @@ func (c Config) loadLinks(name string, t *Target, m map[string]*Target, o map[st return err } t2.Outputs = nil + t2.linked = true m[target] = t2 } if err := c.loadLinks(target, t2, m, o, visited); err != nil { @@ -528,6 +529,9 @@ type Target struct { NetworkMode *string `json:"-" hcl:"-"` NoCacheFilter []string `json:"no-cache-filter,omitempty" hcl:"no-cache-filter,optional"` // IMPORTANT: if you add more fields here, do not forget to update newOverrides and README. + + // linked is a private field to mark a target used as a linked one + linked bool } func (t *Target) normalize() { @@ -869,6 +873,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { NoCacheFilter: t.NoCacheFilter, Pull: pull, NetworkMode: networkMode, + Linked: t.linked, } platforms, err := platformutil.Parse(t.Platforms) diff --git a/build/build.go b/build/build.go index ab241af9..2020d939 100644 --- a/build/build.go +++ b/build/build.go @@ -2,6 +2,7 @@ package build import ( "bufio" + "bytes" "context" "crypto/rand" "encoding/hex" @@ -75,6 +76,9 @@ type Options struct { Tags []string Target string Ulimits *opts.UlimitOpt + + // Linked marks this target as exclusively linked (not requested by the user). + Linked bool } type Inputs struct { @@ -630,11 +634,21 @@ func Build(ctx context.Context, drivers []DriverInfo, opt map[string]Options, do } if noMobyDriver != nil && !noDefaultLoad() { - for _, opt := range opt { - if len(opt.Exports) == 0 { - logrus.Warnf("No output specified for %s driver. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load", noMobyDriver.Factory().Name()) - break + var noOutputTargets []string + for name, opt := range opt { + if !opt.Linked && len(opt.Exports) == 0 { + noOutputTargets = append(noOutputTargets, name) + } + } + if len(noOutputTargets) > 0 { + var warnNoOutputBuf bytes.Buffer + warnNoOutputBuf.WriteString("No output specified ") + if len(noOutputTargets) == 1 && noOutputTargets[0] == "default" { + warnNoOutputBuf.WriteString(fmt.Sprintf("with %s driver", noMobyDriver.Factory().Name())) + } else { + warnNoOutputBuf.WriteString(fmt.Sprintf("for %s target(s) with %s driver", strings.Join(noOutputTargets, ", "), noMobyDriver.Factory().Name())) } + logrus.Warnf("%s. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load", warnNoOutputBuf.String()) } }