Skip to content

Commit

Permalink
Fix a bug where auto-fixed code could not be retrieved via GetFile API (
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Jan 21, 2024
1 parent cc6168c commit 4bd6284
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 4 deletions.
5 changes: 5 additions & 0 deletions integrationtest/autofix/autofix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ func TestIntegration(t *testing.T) {
Command: "./tflint --chdir=dir --format json --fix",
Dir: "chdir",
},
{
Name: "--chdir with conflict",
Command: "./tflint --chdir=dir --format json --fix",
Dir: "chdir_with_conflict",
},
{
Name: "--filter",
Command: "./tflint --format json --fix --filter=main.tf",
Expand Down
3 changes: 3 additions & 0 deletions integrationtest/autofix/chdir_with_conflict/dir/.tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugin "testing" {
enabled = true
}
4 changes: 4 additions & 0 deletions integrationtest/autofix/chdir_with_conflict/dir/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// autofixed
resource "aws_instance" "autofixed_foo" {
instance_type = "[AUTO_FIXED]"
}
4 changes: 4 additions & 0 deletions integrationtest/autofix/chdir_with_conflict/dir/main.tf.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# autofixed
resource "aws_instance" "autofixed_foo" {
instance_type = "t2.micro" # autofixed
}
85 changes: 85 additions & 0 deletions integrationtest/autofix/chdir_with_conflict/result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"issues": [
{
"rule": {
"name": "terraform_autofix_comment",
"severity": "error",
"link": ""
},
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
"range": {
"filename": "dir/main.tf",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 2,
"column": 1
}
},
"callers": []
},
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is [AUTO_FIXED]",
"range": {
"filename": "dir/main.tf",
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 33
}
},
"callers": []
},
{
"rule": {
"name": "aws_instance_autofix_conflict",
"severity": "error",
"link": ""
},
"message": "instance type is [AUTO_FIXED]",
"range": {
"filename": "dir/main.tf",
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 33
}
},
"callers": []
},
{
"rule": {
"name": "terraform_autofix_comment",
"severity": "error",
"link": ""
},
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
"range": {
"filename": "dir/main.tf",
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 4,
"column": 1
}
},
"callers": []
}
],
"errors": []
}
85 changes: 85 additions & 0 deletions integrationtest/autofix/chdir_with_conflict/result_windows.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"issues": [
{
"rule": {
"name": "terraform_autofix_comment",
"severity": "error",
"link": ""
},
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
"range": {
"filename": "dir\\main.tf",
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 2,
"column": 1
}
},
"callers": []
},
{
"rule": {
"name": "aws_instance_example_type",
"severity": "error",
"link": ""
},
"message": "instance type is [AUTO_FIXED]",
"range": {
"filename": "dir\\main.tf",
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 33
}
},
"callers": []
},
{
"rule": {
"name": "aws_instance_autofix_conflict",
"severity": "error",
"link": ""
},
"message": "instance type is [AUTO_FIXED]",
"range": {
"filename": "dir\\main.tf",
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 33
}
},
"callers": []
},
{
"rule": {
"name": "terraform_autofix_comment",
"severity": "error",
"link": ""
},
"message": "Use \"# autofixed\" instead of \"// autofixed\"",
"range": {
"filename": "dir\\main.tf",
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 4,
"column": 1
}
},
"callers": []
}
],
"errors": []
}
1 change: 1 addition & 0 deletions plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (s *GRPCServer) GetFile(name string) (*hcl.File, error) {
return file, nil
}
// If the file is not found in the current module, it may be in other modules (e.g. root module).
log.Printf(`[DEBUG] The file "%s" is not found in the current module. Fall back to global caches.`, name)
return s.files[name], nil
}

Expand Down
4 changes: 1 addition & 3 deletions tflint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ func (r *Runner) File(path string) *hcl.File {
func (r *Runner) Files() map[string]*hcl.File {
result := make(map[string]*hcl.File)
for name, file := range r.TFConfig.Module.Files {
if filepath.Dir(name) == filepath.Clean(r.TFConfig.Module.SourceDir) {
result[name] = file
}
result[name] = file
}
return result
}
Expand Down
1 change: 0 additions & 1 deletion tflint/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ func Test_RunnerFiles(t *testing.T) {
runner := TestRunner(t, map[string]string{
"main.tf": "",
})
runner.TFConfig.Module.Files["child/main.tf"] = &hcl.File{}

expected := map[string]*hcl.File{
"main.tf": {
Expand Down

0 comments on commit 4bd6284

Please sign in to comment.