Skip to content

Commit

Permalink
fix: be more graceful when trying to access the value of an invalid e…
Browse files Browse the repository at this point in the history
…xpression template for templates with a prefix
  • Loading branch information
ansgarm committed Mar 4, 2024
1 parent 2adeb39 commit 4763cd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions hclsyntax/expression_template.go
Expand Up @@ -33,6 +33,11 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
marks := make(cty.ValueMarks)

for _, part := range e.Parts {
if part == nil {
// we silently ignore nil parts as they only occur for configuration that is already known to be invalid
continue
}

partVal, partDiags := part.Value(ctx)
diags = append(diags, partDiags...)

Expand Down
11 changes: 11 additions & 0 deletions hclsyntax/expression_template_test.go
Expand Up @@ -439,6 +439,17 @@ trim`,
}

func TestTemplateExprGracefulValue(t *testing.T) {
// we don't care about diags since we know it's invalid config
expr, _ := ParseTemplate([]byte(`prefix${provider::}`), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})

got, _ := expr.Value(nil) // this should not panic

if !got.RawEquals(cty.StringVal("prefix")) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, cty.NilVal)
}
}

func TestTemplateExprWrappedGracefulValue(t *testing.T) {
// we don't care about diags since we know it's invalid config
expr, _ := ParseTemplate([]byte(`${provider::}`), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})

Expand Down

0 comments on commit 4763cd0

Please sign in to comment.