From 4bd6284e79f0d25bb9c03aea082def78aa7e0513 Mon Sep 17 00:00:00 2001 From: Kazuma Watanabe Date: Sun, 21 Jan 2024 21:44:17 +0900 Subject: [PATCH] Fix a bug where auto-fixed code could not be retrieved via GetFile API (#1959) --- integrationtest/autofix/autofix_test.go | 5 ++ .../chdir_with_conflict/dir/.tflint.hcl | 3 + .../autofix/chdir_with_conflict/dir/main.tf | 4 + .../chdir_with_conflict/dir/main.tf.fixed | 4 + .../autofix/chdir_with_conflict/result.json | 85 +++++++++++++++++++ .../chdir_with_conflict/result_windows.json | 85 +++++++++++++++++++ plugin/server.go | 1 + tflint/runner.go | 4 +- tflint/runner_test.go | 1 - 9 files changed, 188 insertions(+), 4 deletions(-) create mode 100644 integrationtest/autofix/chdir_with_conflict/dir/.tflint.hcl create mode 100644 integrationtest/autofix/chdir_with_conflict/dir/main.tf create mode 100644 integrationtest/autofix/chdir_with_conflict/dir/main.tf.fixed create mode 100644 integrationtest/autofix/chdir_with_conflict/result.json create mode 100644 integrationtest/autofix/chdir_with_conflict/result_windows.json diff --git a/integrationtest/autofix/autofix_test.go b/integrationtest/autofix/autofix_test.go index 5275e404f..0b2d110d6 100644 --- a/integrationtest/autofix/autofix_test.go +++ b/integrationtest/autofix/autofix_test.go @@ -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", diff --git a/integrationtest/autofix/chdir_with_conflict/dir/.tflint.hcl b/integrationtest/autofix/chdir_with_conflict/dir/.tflint.hcl new file mode 100644 index 000000000..e19f589dd --- /dev/null +++ b/integrationtest/autofix/chdir_with_conflict/dir/.tflint.hcl @@ -0,0 +1,3 @@ +plugin "testing" { + enabled = true +} diff --git a/integrationtest/autofix/chdir_with_conflict/dir/main.tf b/integrationtest/autofix/chdir_with_conflict/dir/main.tf new file mode 100644 index 000000000..49c080361 --- /dev/null +++ b/integrationtest/autofix/chdir_with_conflict/dir/main.tf @@ -0,0 +1,4 @@ +// autofixed +resource "aws_instance" "autofixed_foo" { + instance_type = "[AUTO_FIXED]" +} diff --git a/integrationtest/autofix/chdir_with_conflict/dir/main.tf.fixed b/integrationtest/autofix/chdir_with_conflict/dir/main.tf.fixed new file mode 100644 index 000000000..123bf3892 --- /dev/null +++ b/integrationtest/autofix/chdir_with_conflict/dir/main.tf.fixed @@ -0,0 +1,4 @@ +# autofixed +resource "aws_instance" "autofixed_foo" { + instance_type = "t2.micro" # autofixed +} diff --git a/integrationtest/autofix/chdir_with_conflict/result.json b/integrationtest/autofix/chdir_with_conflict/result.json new file mode 100644 index 000000000..930f22d09 --- /dev/null +++ b/integrationtest/autofix/chdir_with_conflict/result.json @@ -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": [] +} diff --git a/integrationtest/autofix/chdir_with_conflict/result_windows.json b/integrationtest/autofix/chdir_with_conflict/result_windows.json new file mode 100644 index 000000000..bc3c05119 --- /dev/null +++ b/integrationtest/autofix/chdir_with_conflict/result_windows.json @@ -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": [] +} diff --git a/plugin/server.go b/plugin/server.go index e0a439878..3ea363aa5 100644 --- a/plugin/server.go +++ b/plugin/server.go @@ -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 } diff --git a/tflint/runner.go b/tflint/runner.go index 30f88ec91..8372285a4 100644 --- a/tflint/runner.go +++ b/tflint/runner.go @@ -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 } diff --git a/tflint/runner_test.go b/tflint/runner_test.go index 6fa2ad0c0..5348a307c 100644 --- a/tflint/runner_test.go +++ b/tflint/runner_test.go @@ -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": {