From 114cf34748937abe887433cb6dc5c0131aaff411 Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Fri, 1 Mar 2024 11:31:04 +0100 Subject: [PATCH] fix panic for partial provider defined functions in tuple expressions (`[ provider:: ]`) When parsing invalid provider defined functions, a `nil` expression is returned. While there are diagnostics that correctly tell us about invalid configuration, in the editor we still need to continue working with the invalid configuration. This fix allows us to call `Variables()` on expressions that may have a nested nil in a tuple somewhere. We do this e.g. when [collecting reference origins](https://github.com/hashicorp/hcl-lang/blob/4db48f4b5961120c51b11216d040c095c0fd4a76/decoder/expr_any_ref_origins.go#L174) and instead of digging into the expressions at hand and deciding whether to call `.Variables()` or not, fixing it at the source seems more useful. --- hclsyntax/expression.go | 4 +++- hclsyntax/variables_test.go | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hclsyntax/expression.go b/hclsyntax/expression.go index c4a353c4..d191fbc9 100644 --- a/hclsyntax/expression.go +++ b/hclsyntax/expression.go @@ -1133,7 +1133,9 @@ type TupleConsExpr struct { func (e *TupleConsExpr) walkChildNodes(w internalWalkFunc) { for _, expr := range e.Exprs { - w(expr) + if expr != nil { + w(expr) + } } } diff --git a/hclsyntax/variables_test.go b/hclsyntax/variables_test.go index c841d94a..e6c7aaee 100644 --- a/hclsyntax/variables_test.go +++ b/hclsyntax/variables_test.go @@ -293,6 +293,15 @@ func TestVariables(t *testing.T) { }, }, }, + { + // happens e.g. when parsing an invalid [ provider:: ] + &TupleConsExpr{ + Exprs: []Expression{ + nil, + }, + }, + nil, + }, } for _, test := range tests {