File tree 2 files changed +55
-7
lines changed
2 files changed +55
-7
lines changed Original file line number Diff line number Diff line change @@ -78,13 +78,32 @@ func (r *TerraformRequiredVersionRule) Check(runner tflint.Runner) error {
78
78
exists = exists || ok
79
79
}
80
80
81
- if ! exists {
82
- return runner .EmitIssue (
83
- r ,
84
- `terraform "required_version" attribute is required` ,
85
- hcl.Range {},
86
- )
81
+ if exists {
82
+ return nil
83
+ }
84
+
85
+ var missingRange hcl.Range
86
+
87
+ if len (body .Blocks ) > 0 {
88
+ missingRange = body .Blocks [0 ].DefRange
89
+ } else {
90
+ // If there are no "terraform" blocks, create a hcl.Range for a file
91
+
92
+ // Grab a single filename from keys of files map
93
+ var filename string
94
+ for k := range files {
95
+ filename = k
96
+ break
97
+ }
98
+
99
+ missingRange = hcl.Range {
100
+ Filename : filename ,
101
+ }
87
102
}
88
103
89
- return nil
104
+ return runner .EmitIssue (
105
+ r ,
106
+ `terraform "required_version" attribute is required` ,
107
+ missingRange ,
108
+ )
90
109
}
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package rules
3
3
import (
4
4
"testing"
5
5
6
+ "github.com/hashicorp/hcl/v2"
6
7
"github.com/terraform-linters/tflint-plugin-sdk/helper"
7
8
)
8
9
@@ -25,6 +26,17 @@ terraform {}
25
26
{
26
27
Rule : NewTerraformRequiredVersionRule (),
27
28
Message : "terraform \" required_version\" attribute is required" ,
29
+ Range : hcl.Range {
30
+ Filename : "module.tf" ,
31
+ Start : hcl.Pos {
32
+ Line : 2 ,
33
+ Column : 1 ,
34
+ },
35
+ End : hcl.Pos {
36
+ Line : 2 ,
37
+ Column : 10 ,
38
+ },
39
+ },
28
40
},
29
41
},
30
42
},
@@ -53,6 +65,23 @@ terraform {
53
65
` ,
54
66
Expected : helper.Issues {},
55
67
},
68
+ {
69
+ Name : "no terraform block" ,
70
+ Content : `
71
+ locals {
72
+ foo = "bar"
73
+ }
74
+ ` ,
75
+ Expected : helper.Issues {
76
+ {
77
+ Rule : NewTerraformRequiredVersionRule (),
78
+ Message : "terraform \" required_version\" attribute is required" ,
79
+ Range : hcl.Range {
80
+ Filename : "module.tf" ,
81
+ },
82
+ },
83
+ },
84
+ },
56
85
}
57
86
58
87
rule := NewTerraformRequiredVersionRule ()
You can’t perform that action at this time.
0 commit comments