Skip to content

Commit f19be66

Browse files
authored
Fix panic when enabling Terraform rules by CLI (#1373)
1 parent e795ff7 commit f19be66

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

tflint/runner.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,12 @@ func (r *Runner) WithExpressionContext(expr hcl.Expression, proc func() error) e
475475
// DecodeRuleConfig extracts the rule's configuration into the given value
476476
func (r *Runner) DecodeRuleConfig(ruleName string, val interface{}) error {
477477
if rule, exists := r.config.Rules[ruleName]; exists {
478+
// If you enable the rule through the CLI instead of the file, its hcl.Body will be nil.
479+
if rule.Body == nil {
480+
return errors.New("This rule cannot be enabled with the `--enable-rule` option because it lacks the required configuration")
481+
}
478482
diags := gohcl.DecodeBody(rule.Body, nil, val)
479483
if diags.HasErrors() {
480-
// HACK: If you enable the rule through the CLI instead of the file, its hcl.Body will not contain valid range.
481-
// @see https://github.com/hashicorp/hcl/blob/v2.5.0/merged.go#L132-L135
482-
if rule.Body.MissingItemRange().Filename == "<empty>" {
483-
return errors.New("This rule cannot be enabled with the `--enable-rule` option because it lacks the required configuration")
484-
}
485484
return diags
486485
}
487486
}

tflint/runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ func Test_DecodeRuleConfig_emptyBody(t *testing.T) {
11791179
cfg.Rules["test"] = &RuleConfig{
11801180
Name: "test",
11811181
Enabled: true,
1182-
Body: hcl.EmptyBody(),
1182+
Body: nil,
11831183
}
11841184

11851185
runner := TestRunnerWithConfig(t, map[string]string{}, cfg)

0 commit comments

Comments
 (0)