Skip to content

Commit

Permalink
Ignore module issues that are not valid expressions (#1969)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker authored Feb 5, 2024
1 parent 5ed807d commit 4c461b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func (s *GRPCServer) EvaluateExpr(expr hcl.Expression, opts sdk.EvaluateExprOpti
}

// EmitIssue stores an issue in the server based on passed rule, message, and location.
// It attempts to detect whether the issue range represents an expression and emits it based on that context.
// However, some ranges may be syntactically valid but not actually represent an expression.
// In these cases, the "expression" is still provided as context and the client should ignore any errors when attempting to evaluate it.
func (s *GRPCServer) EmitIssue(rule sdk.Rule, message string, location hcl.Range, fixable bool) (bool, error) {
// If the issue range represents an expression, it is emitted based on that context.
// This is required to emit issues in called modules.
Expand Down
10 changes: 7 additions & 3 deletions tflint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,18 @@ func (r *Runner) listModuleVars(expr hcl.Expression) []*moduleVariable {
return ret
}

// listVarRefs returns the references in the expression.
// If the expression is not a valid expression, it returns an empty map.
func listVarRefs(expr hcl.Expression) map[string]addrs.InputVariable {
ret := map[string]addrs.InputVariable{}
refs, diags := lang.ReferencesInExpr(expr)

if diags.HasErrors() {
// Maybe this is bug
panic(diags)
// If we cannot determine the references in the expression, it is likely a valid HCL expression, but not a valid Terraform expression.
// The declaration range of a block with no labels is its name, which is syntactically valid as an HCL expression, but is not a valid Terraform reference.
return ret
}

ret := map[string]addrs.InputVariable{}
for _, ref := range refs {
if varRef, ok := ref.Subject.(addrs.InputVariable); ok {
ret[varRef.String()] = varRef
Expand Down
5 changes: 5 additions & 0 deletions tflint/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,11 @@ func Test_listVarRefs(t *testing.T) {
"var.tags": {Name: "tags"},
},
},
{
Name: "invalid expression",
Expr: "my_block",
Expected: map[string]addrs.InputVariable{},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 4c461b2

Please sign in to comment.