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

add terraform_comment_syntax rule #762

Merged
merged 4 commits into from
May 20, 2020

Conversation

bendrucker
Copy link
Member

This PR adds a terraform_comment_syntax rule that enforces the canonical # comment style as described in the Terraform syntax guide:

https://www.terraform.io/docs/configuration/syntax.html#comments

This was requested by a colleague who noticed that @TakeScoop's Terraform configuration contains a mix of # and // comments but that Terraform clearly recommends #.

Since this is an aesthetic preference, the rule is disabled by default.

In order to accomplish this, I've added a Files method to the runner which returns all files in the current module. I'm interested in some other syntax-oriented rules that would depend on that method, e.g. enforcing canonical block padding:

resource "foo" "bar" {
  attribute = "value"

  block {
    
  }
}

resource "foo" "baz" {
  # ...
}

@bendrucker bendrucker requested a review from wata727 May 20, 2020 03:53
Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@@ -237,6 +238,17 @@ func (r *Runner) File(path string) *hcl.File {
return r.files[path]
}

// Files returns the raw *hcl.File representation of all Terraform configuration in the module directory.
func (r *Runner) Files() map[string]*hcl.File {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@wata727
Copy link
Member

wata727 commented May 20, 2020

According to Terraform documentation:

Automatic configuration formatting tools may automatically transform // comments into # comments, since the double-slash style is not idiomatic.

After reading this, I wondered if terraform fmt would convert the comments automatically, but it doesn't seem so...
If Terraform convert comments with terraform fmt, we probably don't need this rule.

@bendrucker
Copy link
Member Author

Agreed, I think anything detectable by terraform fmt or terraform validate should be a non-goal for tflint.

I suspect that in the real world a lot of users (if not most) have a a mix of comment styles. Double slash is pretty much everywhere except for shells. If your editor has a syntax definition for Terraform/HCL, the keyboard shortcut for opening a comment should use # but without that definition it'll likely default to //.

Enforcing one comment style would be breaking for terraform fmt -check and currently fmt just modifies whitespace. So it seems like a big undertaking for visual consistency.

The language about auto-formatting in the docs is definitely interesting, and comes from here:

hashicorp/terraform@39579e8

Seems like Terraform 0.12 would have been the best time to introduce this, given that users were already significantly rewriting their configs. Very possible that was being considered at the time and it was cut from the release.

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.

2 participants