mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 01:53:42 +08:00 
			
		
		
		
	bake: fix context from target platform matching
Signed-off-by: Arran Walker <arran.walker@fiveturns.org>
This commit is contained in:
		
							
								
								
									
										16
									
								
								bake/bake.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								bake/bake.go
									
									
									
									
									
								
							@@ -9,7 +9,6 @@ import (
 | 
				
			|||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
	"regexp"
 | 
						"regexp"
 | 
				
			||||||
	"slices"
 | 
						"slices"
 | 
				
			||||||
	"sort"
 | 
					 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
@@ -515,8 +514,8 @@ func (c Config) loadLinks(name string, t *Target, m map[string]*Target, o map[st
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if len(t.Platforms) > 1 && len(t2.Platforms) > 1 {
 | 
								if len(t.Platforms) > 1 && len(t2.Platforms) > 1 {
 | 
				
			||||||
				if !sliceEqual(t.Platforms, t2.Platforms) {
 | 
									if !isSubset(t.Platforms, t2.Platforms) {
 | 
				
			||||||
					return errors.Errorf("target %s can't be used by %s because it is defined for different platforms %v and %v", target, name, t2.Platforms, t.Platforms)
 | 
										return errors.Errorf("target %s can't be used by %s because its platforms %v are not a subset of %v", target, name, t.Platforms, t2.Platforms)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -1516,14 +1515,9 @@ func sanitizeTargetName(target string) string {
 | 
				
			|||||||
	return strings.ReplaceAll(target, ".", "_")
 | 
						return strings.ReplaceAll(target, ".", "_")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func sliceEqual(s1, s2 []string) bool {
 | 
					func isSubset(s1, s2 []string) bool {
 | 
				
			||||||
	if len(s1) != len(s2) {
 | 
						for _, item := range s1 {
 | 
				
			||||||
		return false
 | 
							if !slices.Contains(s2, item) {
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	sort.Strings(s1)
 | 
					 | 
				
			||||||
	sort.Strings(s2)
 | 
					 | 
				
			||||||
	for i := range s1 {
 | 
					 | 
				
			||||||
		if s1[i] != s2[i] {
 | 
					 | 
				
			||||||
			return false
 | 
								return false
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -910,7 +910,28 @@ func TestReadContextFromTargetInvalidPlatforms(t *testing.T) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	_, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil, &EntitlementConf{})
 | 
						_, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil, &EntitlementConf{})
 | 
				
			||||||
	require.Error(t, err)
 | 
						require.Error(t, err)
 | 
				
			||||||
	require.Contains(t, err.Error(), "defined for different platforms")
 | 
						require.Contains(t, err.Error(), "are not a subset of")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestReadContextFromTargetSubsetPlatforms(t *testing.T) {
 | 
				
			||||||
 | 
						ctx := context.TODO()
 | 
				
			||||||
 | 
						fp := File{
 | 
				
			||||||
 | 
							Name: "docker-bake.hcl",
 | 
				
			||||||
 | 
							Data: []byte(`
 | 
				
			||||||
 | 
							target "mid" {
 | 
				
			||||||
 | 
								output = ["foo"]
 | 
				
			||||||
 | 
								platforms = ["linux/amd64", "linux/riscv64", "linux/arm64"]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							target "app" {
 | 
				
			||||||
 | 
								contexts = {
 | 
				
			||||||
 | 
									bar: "target:mid"
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								platforms = ["linux/amd64", "linux/arm64"]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							`),
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						_, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, []string{}, nil, &EntitlementConf{})
 | 
				
			||||||
 | 
						require.NoError(t, err)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestReadTargetsDefault(t *testing.T) {
 | 
					func TestReadTargetsDefault(t *testing.T) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user