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 support for tflint-ignore-file annotations in JSON #2230

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

isobit
Copy link

@isobit isobit commented Feb 9, 2025

I have a use case where I'm generating .tf.json files for a bunch of different root modules; these .tf.json files define several locals for convenience, which may or may not be used by the root modules they're placed in. I wanted to be able to ignore the terraform_unused_declarations rule without disabling it globally, but I realized that there wasn't a way to do this since the annotation system didn't support JSON files at all. This change adds limited support for tflint-ignore-file annotations in a top-level comment property.

@isobit isobit force-pushed the 2025-02-09/ignore-file-annotation-json branch 4 times, most recently from b5d701c to b99c64d Compare February 9, 2025 01:55
cmd/inspect.go Show resolved Hide resolved
tflint/annotation.go Outdated Show resolved Hide resolved
@isobit isobit force-pushed the 2025-02-09/ignore-file-annotation-json branch from b99c64d to 7dab624 Compare February 10, 2025 19:51
@isobit isobit requested a review from wata727 February 10, 2025 19:57
@isobit isobit force-pushed the 2025-02-09/ignore-file-annotation-json branch 2 times, most recently from e171cc2 to dfdfd18 Compare February 10, 2025 20:49
```json
{
"//": {
"tflint-ignore-file": "aws_instance_invalid_type"
Copy link
Member

@bendrucker bendrucker Feb 10, 2025

Choose a reason for hiding this comment

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

Are there any other examples you can offer of this map comment structure being used? The documentation illustrates comment properties with string values. In that sense, this proposal clashes with the official guidance, as a JSON document that already contains a comment property (string) would be unable to conform to this object syntax.

Seems like // could be parsed equivalently to an HCL document with a first-line comment. These would be equivalent:

# tflint-ignore-file: foo
{
  "//": "tflint-ignore-file: foo"
}

Copy link
Author

@isobit isobit Feb 10, 2025

Choose a reason for hiding this comment

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

I have no examples of this being used "in the wild"; my reasoning for the map structure was only that, hypothetically, someone might want to be able to use the comment property to leave other information. It wasn't clear to me how that would work if the comment property was a string which was already occupied by the tflint-ignore-file annotation.

For example:

{
  "//": {
    "tflint-ignore-file": "foo",
    "_1": "we ignore foo because reasons",
    "_2": "this file does stuff"
  }
}

I'm realizing now though that the JSON spec may allow for multiple properties with the key //; that may be a cleaner way to have multiple comments, at the expense of being harder to parse here (encoding/json discards duplicate entries). I could look for a way of handling that though. E.g.

{
  "//": "this file does stuff",
  "//": "we ignore foo because reasons",
  "//": "tflint-ignore-file: foo"
}

I'm open to alternatives though, it might make sense to expect a escaped linefeed-delimited string or a list of strings. E.g.

{
  "//": "this file does stuff\nwe ignore foo because reasons\ntflint-ignore-file: foo"
}

or

{
  "//": [
    "this file does stuff",
    "we ignore foo because reasons",
    "tflint-ignore-file: foo"
  ]
}

Copy link
Member

Choose a reason for hiding this comment

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

This was my assumption, thanks. I'd treat it as a string then, with the same rules as HCL:

if !(token.Range.Start.Line == 1 && token.Range.Start.Column == 1) {

So if you have some existing comment property in the config, tflint-ignore-file would have to be prepended to that. Followed by whitespace, then any other comment content.

The map or list patterns are not inherently unreasonable, but TFLint tries to avoid creating Terraform patterns of its own.

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense to me; will update in a bit here.

Copy link
Member

Choose a reason for hiding this comment

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

The documentation illustrates comment properties with string values

I thought the same thing, but as mentioned in #2230 (comment), I thought a map would not be a bad choice to add other comments.

In fact, having looked at the HCL JSON parser, it doesn't seem to matter what the type of the comment value is:
https://github.com/hashicorp/hcl/blob/v2.23.0/json/structure.go#L55-L59

Personally, I'm concerned that comments like the following look like comments to the tflint-ignore-file annotation:

{
  "//": "tflint-ignore-file: aws_instance_invalid_type # This file is auto-generated by tools/terraform_gen.go. DO NOT EDIT",
}

I like the following style the most:

{
  "//": "tflint-ignore-file: aws_instance_invalid_type",
  "//": "This file is auto-generated by tools/terraform_gen.go. DO NOT EDIT"
}

However, given the difficulties involved when you have multiple keys with the same name, I believe we need to find some compromise.

docs/user-guide/annotations.md Outdated Show resolved Hide resolved
tflint/annotation.go Outdated Show resolved Hide resolved
tflint/annotation.go Outdated Show resolved Hide resolved
@isobit isobit force-pushed the 2025-02-09/ignore-file-annotation-json branch 2 times, most recently from 5aa0b5f to 8b15cf7 Compare February 11, 2025 03:40
@isobit isobit requested a review from bendrucker February 11, 2025 03:42
@isobit isobit force-pushed the 2025-02-09/ignore-file-annotation-json branch 2 times, most recently from 942c6fd to cf69671 Compare February 11, 2025 03:45
@isobit isobit force-pushed the 2025-02-09/ignore-file-annotation-json branch from cf69671 to bb1bf7c Compare February 11, 2025 03:46
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.

3 participants