Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore module issues that are not valid expressions #1969

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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