terraform-linters / tflint

A Pluggable Terraform Linter
Mozilla Public License 2.0
4.96k stars 357 forks source link

Single files are ignored on Windows when the path begins with `.\` #1412

Closed cmellongoempyrean closed 2 years ago

cmellongoempyrean commented 2 years ago

When run on this directory, tflint correctly identifies issues in an individual file:

λ tflint .\bootstrap\organization\
1 issue(s) found:

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

  on bootstrap\organization\org.tf line 7:
   7: // set up a KMS key for Control Tower
   8: resource "aws_kms_key" "control_tower_key" {

Reference: https://github.com/terraform-linters/tflint/blob/v0.35.0/docs/rules/terraform_comment_syntax.md

When run on a single file, however, it only identifies issues when the path does not have a leading dot:

λ tflint bootstrap\organization\org.tf
1 issue(s) found:

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

  on bootstrap\organization\org.tf line 7:
   7: // set up a KMS key for Control Tower
   8: resource "aws_kms_key" "control_tower_key" {

Reference: https://github.com/terraform-linters/tflint/blob/v0.35.0/docs/rules/terraform_comment_syntax.md
λ tflint .\bootstrap\organization\org.tf

Version

λ tflint -v
TFLint version 0.35.0
+ ruleset.aws (0.14.0)
λ terraform -v
Terraform v1.2.2
on windows_amd64
bendrucker commented 2 years ago

Can you share debug logs?

https://github.com/terraform-linters/tflint/blob/master/README.md

Separately, the scope of a Terraform module is a directory. Passing individual files may lead to unexpected errors. Syntax rules like comment_syntax are fine but anything structural, eg required_providers is likely to break when not called against a complete module.

The ability to pass files instead of modules is likely to be removed in a future release with a move to a -chdir option to match Terraform.

cmellongoempyrean commented 2 years ago

There's some info in the debug logs that I would rather not paste so I created a minimal repo at this gist instead: https://gist.github.com/cmellongoempyrean/add2e9d93bc9cf050eebc543bed16e6d

Understood about the individual file vs the module concept. I noticed this because the leading dot syntax is how paths get tab completed in powershell on Windows, and I was experimenting with only linting files that had changed in pull requests. I will need to change my methodology to be more sophisticated, running it on modules that contain changed files instead.

wata727 commented 2 years ago

This seems to be a bug about file path normalization. If you pass a file path, TFLint will only report issues that match the passed file path from the inspection results for that directory. https://github.com/terraform-linters/tflint/blob/v0.37.0/tflint/runner.go#L408-L423

This issue occurs here because .\org.tf and org.tf cannot be identified as the same file. This can be reproduced on Linux.