Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce autofix #1755

Merged
merged 1 commit into from
Jun 13, 2023
Merged

Introduce autofix #1755

merged 1 commit into from
Jun 13, 2023

Conversation

wata727
Copy link
Member

@wata727 wata727 commented May 2, 2023

Fixes #266
See also terraform-linters/tflint-plugin-sdk#254
See also terraform-linters/tflint-ruleset-terraform#93

This PR introduces autofix feature. Running tflint --fix will automatically fix issues as possible. Below is an example of autofix in the terraform_comment_syntax rule:

$ cat main.tf
// EC2
resource "aws_instance" "foo" {
  instance_type = "t2.micro"
}

$ tflint --fix
1 issue(s) found:

Warning: [Fixed] Single line comments should begin with # (terraform_comment_syntax)

  on main.tf line 1:
   1: // EC2
   2: resource "aws_instance" "foo" {

$ cat main.tf
# EC2
resource "aws_instance" "foo" {
  instance_type = "t2.micro"
}

In order for autofix to work, the rule that emits the issue must support autofix. This should take advantage of the EmitIssueWithFix API added in the latest SDK version.

Technically, rewriting of files by autofix is completed on the plugin side. Once the rewrite is complete, the plugin sends the changed file per rule to the host server (TFLint) and stores the changes in-memory. TFLint finally writes to files when all inspections are completed successfully. See terraform-linters/tflint-plugin-sdk#254 for more information on plugin-to-host server communication and autofixer implementation.

@wata727 wata727 force-pushed the introduce_autofix branch 7 times, most recently from 55d6124 to db4a355 Compare May 8, 2023 15:45
@wata727 wata727 force-pushed the introduce_autofix branch 6 times, most recently from 1db6e91 to a0662f0 Compare May 20, 2023 07:28
@wata727 wata727 force-pushed the introduce_autofix branch 5 times, most recently from 3a5141e to 366b38c Compare June 2, 2023 17:40
@wata727 wata727 merged commit ec11eee into master Jun 13, 2023
11 checks passed
@wata727 wata727 deleted the introduce_autofix branch June 13, 2023 14:02
@nitrocode
Copy link

Wow!!! Nice work @wata727 ! Thank you

@davenicoll
Copy link

@bendrucker can you please create a new release? thanks

@wata727
Copy link
Member Author

wata727 commented Jun 14, 2023

I'm planning on adding a few more changes in the next release. The next release will come out in this month.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

feat: Add a --fix option to auto fix small issues like eslint
4 participants