diff --git a/interpreter/attributes.go b/interpreter/attributes.go index 5c8107ab..0d13d08f 100644 --- a/interpreter/attributes.go +++ b/interpreter/attributes.go @@ -16,6 +16,7 @@ package interpreter import ( "fmt" + "strings" "github.com/google/cel-go/common/containers" "github.com/google/cel-go/common/types" @@ -301,7 +302,14 @@ func (a *absoluteAttribute) Resolve(vars Activation) (any, error) { } } } - return nil, missingAttribute(a.String()) + var attrNames strings.Builder + for i, nm := range a.namespaceNames { + if i != 0 { + attrNames.WriteString(", ") + } + attrNames.WriteString(nm) + } + return nil, missingAttribute(attrNames.String()) } type conditionalAttribute struct { @@ -436,7 +444,9 @@ func (a *maybeAttribute) AddQualifier(qual Qualifier) (Attribute, error) { } } // Next, ensure the most specific variable / type reference is searched first. - a.attrs = append([]NamespacedAttribute{a.fac.AbsoluteAttribute(qual.ID(), augmentedNames...)}, a.attrs...) + if len(augmentedNames) != 0 { + a.attrs = append([]NamespacedAttribute{a.fac.AbsoluteAttribute(qual.ID(), augmentedNames...)}, a.attrs...) + } return a, nil } @@ -1288,7 +1298,7 @@ func (e *resolutionError) Error() string { return fmt.Sprintf("index out of bounds: %v", e.missingIndex) } if e.missingAttribute != "" { - return fmt.Sprintf("no such attribute: %s", e.missingAttribute) + return fmt.Sprintf("no such attribute(s): %s", e.missingAttribute) } return "invalid attribute" } diff --git a/interpreter/attributes_test.go b/interpreter/attributes_test.go index 023aa784..0735b88d 100644 --- a/interpreter/attributes_test.go +++ b/interpreter/attributes_test.go @@ -689,7 +689,7 @@ func TestAttributesOptional(t *testing.T) { varName: "a", quals: []any{}, vars: map[string]any{}, - err: errors.New("no such attribute: id: 1, names: [a]"), + err: errors.New("no such attribute(s): a"), }, } for i, tst := range tests { diff --git a/interpreter/interpreter_test.go b/interpreter/interpreter_test.go index 91de37b5..3d2c9f17 100644 --- a/interpreter/interpreter_test.go +++ b/interpreter/interpreter_test.go @@ -1400,6 +1400,19 @@ var ( expr: `1/0 in [1, 2, 3]`, err: `division by zero`, }, + { + name: "list_index_error", + expr: `mylistundef[0]`, + unchecked: true, + err: `no such attribute(s): mylistundef`, + }, + { + name: "pkg_list_index_error", + container: "goog", + expr: `pkg.mylistundef[0]`, + unchecked: true, + err: `no such attribute(s): goog.pkg.mylistundef, pkg.mylistundef`, + }, } )