Skip to content

Conversation

rorychatterton
Copy link
Contributor

@rorychatterton rorychatterton commented Sep 25, 2025

Updated ParseExpression to support parsing any json files in addition to the existing .tf, .tf.json, and .hcl file extensions.

What changed?

  • Modified ParseExpression to accept any .json file extension, not just .tf.json
  • Updated error message to include .json as a valid extension
  • Added comments clarifying that calling functions are responsible for file validation
  • Added test cases for:
    • Regular .json files
    • .tflint.json configuration files
    • Invalid JSON content (verifies proper error handling)
    • Invalid file extensions

Why make this change?

This issue is introducing the ability to define configuration as JSON (.tflint.json) in addition to HCL (.tflint.hcl). The ParseExpression function needs to support parsing these JSON configuration files.

There are only two callers of this function:

  1. fromproto.Expression - Receives expressions from both Terraform files AND TFLint config files (including .tflint.json), requiring .json support
  2. getExprFromRange in plugin/server.go - Only processes Terraform files (.tf and .tf.json) as these are the only files loaded into runner.TFConfig.Module.Files

The calling functions already validate that files are hcl and should be processed, and the JSON parser will return appropriate diagnostics if the content is not valid HCL-compatible JSON.

Design Decision

I chose to accept any .json extension rather than specifically adding .tflint.json because:

  • The calling functions already perform appropriate file filtering
  • Users may name config files differently (e.g., --config=myconfig.json)
  • The JSON parser will properly reject invalid content with diagnostics

Question for reviewers:

Are we comfortable accepting any .json file at this level, or would you prefer we restrict to specific patterns like .tflint.json and restrict configuration inputs to only accept .tflint.json on the tflint server side?

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.

Are we comfortable accepting any .json file at this level, or would you prefer we restrict to specific patterns like .tflint.json and restrict configuration inputs to only accept .tflint.json on the tflint server side?

Any .json file would be fine. hclext.ParseExpression is a wrapper around hclsyntax.ParseExpression and json.ParseExpression, and the extension restriction is just because that's what we were expecting at the time.

Considering that new files will be supported at the plugin level like this, it would be better to accept as many files as possible.

Co-authored-by: Kazuma Watanabe <[email protected]>
@rorychatterton
Copy link
Contributor Author

Cool, thanks

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.

Nice!

@wata727 wata727 merged commit 702731c into terraform-linters:master Sep 27, 2025
4 checks passed
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