Skip to content

Commit 4bd6284

Browse files
authored
Fix a bug where auto-fixed code could not be retrieved via GetFile API (#1959)
1 parent cc6168c commit 4bd6284

File tree

9 files changed

+188
-4
lines changed

9 files changed

+188
-4
lines changed

integrationtest/autofix/autofix_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ func TestIntegration(t *testing.T) {
7070
Command: "./tflint --chdir=dir --format json --fix",
7171
Dir: "chdir",
7272
},
73+
{
74+
Name: "--chdir with conflict",
75+
Command: "./tflint --chdir=dir --format json --fix",
76+
Dir: "chdir_with_conflict",
77+
},
7378
{
7479
Name: "--filter",
7580
Command: "./tflint --format json --fix --filter=main.tf",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugin "testing" {
2+
enabled = true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// autofixed
2+
resource "aws_instance" "autofixed_foo" {
3+
instance_type = "[AUTO_FIXED]"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# autofixed
2+
resource "aws_instance" "autofixed_foo" {
3+
instance_type = "t2.micro" # autofixed
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "terraform_autofix_comment",
6+
"severity": "error",
7+
"link": ""
8+
},
9+
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
10+
"range": {
11+
"filename": "dir/main.tf",
12+
"start": {
13+
"line": 1,
14+
"column": 1
15+
},
16+
"end": {
17+
"line": 2,
18+
"column": 1
19+
}
20+
},
21+
"callers": []
22+
},
23+
{
24+
"rule": {
25+
"name": "aws_instance_example_type",
26+
"severity": "error",
27+
"link": ""
28+
},
29+
"message": "instance type is [AUTO_FIXED]",
30+
"range": {
31+
"filename": "dir/main.tf",
32+
"start": {
33+
"line": 3,
34+
"column": 19
35+
},
36+
"end": {
37+
"line": 3,
38+
"column": 33
39+
}
40+
},
41+
"callers": []
42+
},
43+
{
44+
"rule": {
45+
"name": "aws_instance_autofix_conflict",
46+
"severity": "error",
47+
"link": ""
48+
},
49+
"message": "instance type is [AUTO_FIXED]",
50+
"range": {
51+
"filename": "dir/main.tf",
52+
"start": {
53+
"line": 3,
54+
"column": 19
55+
},
56+
"end": {
57+
"line": 3,
58+
"column": 33
59+
}
60+
},
61+
"callers": []
62+
},
63+
{
64+
"rule": {
65+
"name": "terraform_autofix_comment",
66+
"severity": "error",
67+
"link": ""
68+
},
69+
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
70+
"range": {
71+
"filename": "dir/main.tf",
72+
"start": {
73+
"line": 3,
74+
"column": 30
75+
},
76+
"end": {
77+
"line": 4,
78+
"column": 1
79+
}
80+
},
81+
"callers": []
82+
}
83+
],
84+
"errors": []
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"issues": [
3+
{
4+
"rule": {
5+
"name": "terraform_autofix_comment",
6+
"severity": "error",
7+
"link": ""
8+
},
9+
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
10+
"range": {
11+
"filename": "dir\\main.tf",
12+
"start": {
13+
"line": 1,
14+
"column": 1
15+
},
16+
"end": {
17+
"line": 2,
18+
"column": 1
19+
}
20+
},
21+
"callers": []
22+
},
23+
{
24+
"rule": {
25+
"name": "aws_instance_example_type",
26+
"severity": "error",
27+
"link": ""
28+
},
29+
"message": "instance type is [AUTO_FIXED]",
30+
"range": {
31+
"filename": "dir\\main.tf",
32+
"start": {
33+
"line": 3,
34+
"column": 19
35+
},
36+
"end": {
37+
"line": 3,
38+
"column": 33
39+
}
40+
},
41+
"callers": []
42+
},
43+
{
44+
"rule": {
45+
"name": "aws_instance_autofix_conflict",
46+
"severity": "error",
47+
"link": ""
48+
},
49+
"message": "instance type is [AUTO_FIXED]",
50+
"range": {
51+
"filename": "dir\\main.tf",
52+
"start": {
53+
"line": 3,
54+
"column": 19
55+
},
56+
"end": {
57+
"line": 3,
58+
"column": 33
59+
}
60+
},
61+
"callers": []
62+
},
63+
{
64+
"rule": {
65+
"name": "terraform_autofix_comment",
66+
"severity": "error",
67+
"link": ""
68+
},
69+
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
70+
"range": {
71+
"filename": "dir\\main.tf",
72+
"start": {
73+
"line": 3,
74+
"column": 30
75+
},
76+
"end": {
77+
"line": 4,
78+
"column": 1
79+
}
80+
},
81+
"callers": []
82+
}
83+
],
84+
"errors": []
85+
}

plugin/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (s *GRPCServer) GetFile(name string) (*hcl.File, error) {
7777
return file, nil
7878
}
7979
// If the file is not found in the current module, it may be in other modules (e.g. root module).
80+
log.Printf(`[DEBUG] The file "%s" is not found in the current module. Fall back to global caches.`, name)
8081
return s.files[name], nil
8182
}
8283

tflint/runner.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,7 @@ func (r *Runner) File(path string) *hcl.File {
215215
func (r *Runner) Files() map[string]*hcl.File {
216216
result := make(map[string]*hcl.File)
217217
for name, file := range r.TFConfig.Module.Files {
218-
if filepath.Dir(name) == filepath.Clean(r.TFConfig.Module.SourceDir) {
219-
result[name] = file
220-
}
218+
result[name] = file
221219
}
222220
return result
223221
}

tflint/runner_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ func Test_RunnerFiles(t *testing.T) {
292292
runner := TestRunner(t, map[string]string{
293293
"main.tf": "",
294294
})
295-
runner.TFConfig.Module.Files["child/main.tf"] = &hcl.File{}
296295

297296
expected := map[string]*hcl.File{
298297
"main.tf": {

0 commit comments

Comments
 (0)