Skip to content

Commit 0f098db

Browse files
committed
add warning
Signed-off-by: Areeb Ahmed <areebahmed0709@gmail.com>
1 parent 8e6656f commit 0f098db

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

bake/bake.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/moby/buildkit/client/llb"
3333
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
3434
"github.com/pkg/errors"
35+
"github.com/sirupsen/logrus"
3536
"github.com/zclconf/go-cty/cty"
3637
"github.com/zclconf/go-cty/cty/convert"
3738
)
@@ -1281,7 +1282,25 @@ func (t *Target) GetName(ectx *hcl.EvalContext, block *hcl.Block, loadDeps func(
12811282
return value.AsString(), nil
12821283
}
12831284

1285+
func warnDuplicateCacheExports(m map[string]*Target) {
1286+
seen := map[string][]string{}
1287+
for _, name := range slices.Sorted(maps.Keys(m)) {
1288+
t := m[name]
1289+
for _, entry := range t.CacheTo {
1290+
key := entry.String()
1291+
seen[key] = append(seen[key], name)
1292+
}
1293+
}
1294+
for _, key := range slices.Sorted(maps.Keys(seen)) {
1295+
names := seen[key]
1296+
if len(names) > 1 {
1297+
logrus.Warnf("multiple targets write to the same cache export %q: %s", key, strings.Join(names, ", "))
1298+
}
1299+
}
1300+
}
1301+
12841302
func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Options, error) {
1303+
warnDuplicateCacheExports(m)
12851304
m2 := make(map[string]build.Options, len(m))
12861305
for k, v := range m {
12871306
bo, err := toBuildOpt(v, inp)

bake/bake_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/docker/buildx/util/buildflags"
1313
"github.com/moby/buildkit/util/entitlements"
14+
"github.com/sirupsen/logrus"
1415
"github.com/stretchr/testify/assert"
1516
"github.com/stretchr/testify/require"
1617
)
@@ -2491,3 +2492,46 @@ func stringify[V fmt.Stringer](values []V) []string {
24912492
sort.Strings(s)
24922493
return s
24932494
}
2495+
2496+
func TestWarnDuplicateCacheExports(t *testing.T) {
2497+
var buf strings.Builder
2498+
logrus.SetOutput(&buf)
2499+
logrus.SetLevel(logrus.WarnLevel)
2500+
t.Cleanup(func() { logrus.SetOutput(os.Stderr) })
2501+
2502+
t.Run("duplicate cache-to", func(t *testing.T) {
2503+
buf.Reset()
2504+
m := map[string]*Target{
2505+
"app": {
2506+
CacheTo: buildflags.CacheOptions{
2507+
{Type: "registry", Attrs: map[string]string{"ref": "registry.example.com/cache/shared"}},
2508+
},
2509+
},
2510+
"worker": {
2511+
CacheTo: buildflags.CacheOptions{
2512+
{Type: "registry", Attrs: map[string]string{"ref": "registry.example.com/cache/shared"}},
2513+
},
2514+
},
2515+
}
2516+
warnDuplicateCacheExports(m)
2517+
require.Contains(t, buf.String(), "registry.example.com/cache/shared")
2518+
})
2519+
2520+
t.Run("no duplicate", func(t *testing.T) {
2521+
buf.Reset()
2522+
m := map[string]*Target{
2523+
"app": {
2524+
CacheTo: buildflags.CacheOptions{
2525+
{Type: "registry", Attrs: map[string]string{"ref": "registry.example.com/cache/app"}},
2526+
},
2527+
},
2528+
"worker": {
2529+
CacheTo: buildflags.CacheOptions{
2530+
{Type: "registry", Attrs: map[string]string{"ref": "registry.example.com/cache/worker"}},
2531+
},
2532+
},
2533+
}
2534+
warnDuplicateCacheExports(m)
2535+
require.Empty(t, buf.String())
2536+
})
2537+
}

0 commit comments

Comments
 (0)