mirror of
				https://gitea.com/Lydanne/buildx.git
				synced 2025-11-04 01:53:42 +08:00 
			
		
		
		
	buildflags: handle unknown values from cty
Update the buildflags cty code to handle unknown values. When hcl decodes a value with an invalid variable name, it appends a diagnostic for the error and then returns an unknown value so it can continue processing the file and finding more errors. The iteration code has now been changed to use a rangefunc from go 1.23 and it skips empty or unknown values. Empty values are valid when they are skipped and unknown values will have a diagnostic for itself. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
		@@ -2,8 +2,10 @@ package bake
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"reflect"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	hcl "github.com/hashicorp/hcl/v2"
 | 
			
		||||
	"github.com/stretchr/testify/require"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -647,7 +649,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
 | 
			
		||||
	require.Equal(t, []string{"default", "key=path/to/key"}, stringify(c.Targets[0].SSH))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHCLAttrsCapsuleTypeVars(t *testing.T) {
 | 
			
		||||
func TestHCLAttrsCapsuleType_ObjectVars(t *testing.T) {
 | 
			
		||||
	dt := []byte(`
 | 
			
		||||
	variable "foo" {
 | 
			
		||||
		default = "bar"
 | 
			
		||||
@@ -716,6 +718,52 @@ func TestHCLAttrsCapsuleTypeVars(t *testing.T) {
 | 
			
		||||
	require.Equal(t, []string{"id=oci,src=/local/secret"}, stringify(web.Secrets))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHCLAttrsCapsuleType_MissingVars(t *testing.T) {
 | 
			
		||||
	dt := []byte(`
 | 
			
		||||
	target "app" {
 | 
			
		||||
		attest = [
 | 
			
		||||
			"type=sbom,disabled=${SBOM}",
 | 
			
		||||
		]
 | 
			
		||||
 | 
			
		||||
		cache-from = [
 | 
			
		||||
			{ type = "registry", ref = "user/app:${FOO1}" },
 | 
			
		||||
      "type=local,src=path/to/cache:${FOO2}",
 | 
			
		||||
		]
 | 
			
		||||
 | 
			
		||||
		cache-to = [
 | 
			
		||||
			{ type = "local", dest = "path/to/${BAR}" },
 | 
			
		||||
		]
 | 
			
		||||
 | 
			
		||||
		output = [
 | 
			
		||||
			{ type = "oci", dest = "../${OUTPUT}.tar" },
 | 
			
		||||
		]
 | 
			
		||||
 | 
			
		||||
		secret = [
 | 
			
		||||
			{ id = "mysecret", src = "/local/${SECRET}" },
 | 
			
		||||
		]
 | 
			
		||||
 | 
			
		||||
		ssh = [
 | 
			
		||||
			{ id = "key", paths = ["path/to/${SSH_KEY}"] },
 | 
			
		||||
		]
 | 
			
		||||
	}
 | 
			
		||||
	`)
 | 
			
		||||
 | 
			
		||||
	var diags hcl.Diagnostics
 | 
			
		||||
	_, err := ParseFile(dt, "docker-bake.hcl")
 | 
			
		||||
	require.ErrorAs(t, err, &diags)
 | 
			
		||||
 | 
			
		||||
	re := regexp.MustCompile(`There is no variable named "([\w\d_]+)"`)
 | 
			
		||||
	var actual []string
 | 
			
		||||
	for _, diag := range diags {
 | 
			
		||||
		if m := re.FindStringSubmatch(diag.Error()); m != nil {
 | 
			
		||||
			actual = append(actual, m[1])
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	require.ElementsMatch(t,
 | 
			
		||||
		[]string{"SBOM", "FOO1", "FOO2", "BAR", "OUTPUT", "SECRET", "SSH_KEY"},
 | 
			
		||||
		actual)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestHCLMultiFileAttrs(t *testing.T) {
 | 
			
		||||
	dt := []byte(`
 | 
			
		||||
		variable "FOO" {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user