Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bake: skip empty attributes #2832

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,9 @@ type arrValue[B any] interface {
func parseArrValue[T any, PT arrValue[T]](s []string) ([]*T, error) {
outputs := make([]*T, 0, len(s))
for _, text := range s {
if text == "" {
continue
}
output := new(T)
if err := PT(output).UnmarshalText([]byte(text)); err != nil {
return nil, err
Expand All @@ -1594,7 +1597,9 @@ func parseArrValue[T any, PT arrValue[T]](s []string) ([]*T, error) {
func parseCacheArrValues(s []string) ([]*buildflags.CacheOptionsEntry, error) {
outs := make([]*buildflags.CacheOptionsEntry, 0, len(s))
for _, in := range s {
if !strings.Contains(in, "=") {
if in == "" {
continue
} else if !strings.Contains(in, "=") {
// This is ref only format. Each field in the CSV is its own entry.
fields, err := csvvalue.Fields(in, nil)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,26 @@
})
}

// https://github.com/docker/buildx/pull/428
// https://github.com/docker/buildx/issues/2822
func TestEmptyAttribute(t *testing.T) {
fp := File{
Name: "docker-bake.hcl",
Data: []byte(`
target "app" {
output = [""]
}
`),
}

ctx := context.TODO()

m, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, nil, nil)
require.Equal(t, 1, len(m))
require.Len(t, m["app"].Outputs, 0)

Check failure on line 2038 in bake/bake_test.go

View workflow job for this annotation

GitHub Actions / test-unit (ubuntu-24.04)

Failed: bake/TestEmptyAttribute

=== RUN TestEmptyAttribute bake_test.go:2038: Error Trace: /home/runner/work/buildx/buildx/bake/bake_test.go:2038 Error: "[]" should have 0 item(s), but has 1 Test: TestEmptyAttribute --- FAIL: TestEmptyAttribute (0.00s)

Check failure on line 2038 in bake/bake_test.go

View workflow job for this annotation

GitHub Actions / test-unit (macos-14)

Failed: bake/TestEmptyAttribute

=== RUN TestEmptyAttribute bake_test.go:2038: Error Trace: /Users/runner/work/buildx/buildx/bake/bake_test.go:2038 Error: "[]" should have 0 item(s), but has 1 Test: TestEmptyAttribute --- FAIL: TestEmptyAttribute (0.00s)

Check failure on line 2038 in bake/bake_test.go

View workflow job for this annotation

GitHub Actions / test-unit (windows-2022)

Failed: bake/TestEmptyAttribute

=== RUN TestEmptyAttribute bake_test.go:2038: Error Trace: D:/a/buildx/buildx/bake/bake_test.go:2038 Error: "[]" should have 0 item(s), but has 1 Test: TestEmptyAttribute --- FAIL: TestEmptyAttribute (0.00s)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails as expected: https://github.com/docker/buildx/actions/runs/12120057362/job/33787924177?pr=2832#step:6:1326

=== FAIL: bake TestEmptyAttribute (0.00s)
    bake_test.go:2038: 
        	Error Trace:	/home/runner/work/buildx/buildx/bake/bake_test.go:2038
        	Error:      	"[]" should have 0 item(s), but has 1
        	Test:       	TestEmptyAttribute

require.NoError(t, err)
}

func stringify[V fmt.Stringer](values []V) []string {
s := make([]string, len(values))
for i, v := range values {
Expand Down
5 changes: 4 additions & 1 deletion util/buildflags/attests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ func CanonicalizeAttest(attestType string, in string) string {
}

func ParseAttests(in []string) ([]*controllerapi.Attest, error) {
out := []*controllerapi.Attest{}
var out []*controllerapi.Attest
found := map[string]struct{}{}
for _, in := range in {
if in == "" {
continue
}
in := in
attest, err := ParseAttest(in)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion util/buildflags/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ func (e *CacheOptionsEntry) validate(gv interface{}) error {
}

func ParseCacheEntry(in []string) ([]*controllerapi.CacheOptionsEntry, error) {
outs := make([]*controllerapi.CacheOptionsEntry, 0, len(in))
var outs []*controllerapi.CacheOptionsEntry
for _, in := range in {
if in == "" {
continue
}
var out CacheOptionsEntry
if err := out.UnmarshalText([]byte(in)); err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions util/buildflags/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ func ParseContextNames(values []string) (map[string]string, error) {
}
result := make(map[string]string, len(values))
for _, value := range values {
if value == "" {
continue
}
kv := strings.SplitN(value, "=", 2)
if len(kv) != 2 {
return nil, errors.Errorf("invalid context value: %s, expected key=value", value)
Expand Down
6 changes: 5 additions & 1 deletion util/buildflags/cty.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,9 @@ func unmarshalTextFallback[V encoding.TextUnmarshaler](in cty.Value, v V, inErr

// Conversion was successful. Use UnmarshalText on the string and return any
// errors associated with that.
return v.UnmarshalText([]byte(conv.AsString()))
convstr := conv.AsString()
if convstr == "" {
return nil
}
return v.UnmarshalText([]byte(convstr))
}
3 changes: 3 additions & 0 deletions util/buildflags/entitlements.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import "github.com/moby/buildkit/util/entitlements"
func ParseEntitlements(in []string) ([]entitlements.Entitlement, error) {
out := make([]entitlements.Entitlement, 0, len(in))
for _, v := range in {
if v == "" {
continue
}
e, err := entitlements.Parse(v)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions util/buildflags/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ func ParseExports(inp []string) ([]*controllerapi.ExportEntry, error) {
return nil, nil
}
for _, s := range inp {
if s == "" {
continue
}
var out ExportEntry
if err := out.UnmarshalText([]byte(s)); err != nil {
return nil, err
Expand All @@ -153,6 +156,9 @@ func ParseAnnotations(inp []string) (map[exptypes.AnnotationKey]string, error) {

annotations := make(map[exptypes.AnnotationKey]string)
for _, inp := range inp {
if inp == "" {
continue
}
k, v, ok := strings.Cut(inp, "=")
if !ok {
return nil, errors.Errorf("invalid annotation %q, expected key=value", inp)
Expand Down
3 changes: 3 additions & 0 deletions util/buildflags/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func (s *Secret) UnmarshalText(text []byte) error {
func ParseSecretSpecs(sl []string) ([]*controllerapi.Secret, error) {
fs := make([]*controllerapi.Secret, 0, len(sl))
for _, v := range sl {
if v == "" {
continue
}
s, err := parseSecret(v)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions util/buildflags/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func ParseSSHSpecs(sl []string) ([]*controllerapi.SSH, error) {
}

for _, s := range sl {
if s == "" {
continue
}
var out SSH
if err := out.UnmarshalText([]byte(s)); err != nil {
return nil, err
Expand Down
Loading