terraform-linters / tflint

A Pluggable Terraform Linter
Mozilla Public License 2.0
4.73k stars 349 forks source link

tflint does not recognise ".tf" files at the root of the folder #2059

Closed alexpilon666 closed 3 weeks ago

alexpilon666 commented 3 weeks ago

Summary

We use Terraspace instead of native Terraform. This gives us the ability to use generic configurations that are made available to all of our stacks, from a single source: /config/terraform/<file_name>.tf. In this folder we have a common.tf with a bunch of locals that have their value generated by Terraspace upon initialization, and then referenced in our various stacks; those locals are ignored by tflint and instead we have hundreds of Error: Reference to undeclared local value errors when running tflint when executing our pre-commit hooks.

I've tried looking at a rule that we could disable for this, but I can't find anything.

I had previously opened an issue when Hashicorp updated their VSCode extension, which started flagging the same thing as errors everywhere. They said we could disable something called "Enhanced Validation", which when we did started working properly, REF: https://github.com/hashicorp/vscode-terraform/issues/1584

But around the same time as the extension starting throwing the errors, we had to disable our tflint pre-commit hook because it would always return the above-mentioned hundreds of errors.

Command

tflint --config=GIT_WORKING_DIR/.tflint.hcl

Terraform Configuration

#config/terraform/common.tf
locals {
  environment = "dev"
}

#app/stacks/test/locals.tf
locals {
  test=local.environment
}

TFLint Configuration

rule "terraform_required_version" {
    enabled = false
}

rule "terraform_required_providers" {
    enabled = false
}

plugin "azurerm" {
    enabled = true
    version = "0.26.0"
    source = "github.com/terraform-linters/tflint-ruleset-azurerm"
}

Output

Terraform validate with tflint...........................................Failed
- hook id: terraform_tflint
- exit code: 1

Command 'tflint --init' successfully done:
Plugin "azurerm" is already installed

TFLint in app/stacks/cbc-profilage/:
Failed to prepare rule checking; main.tf:5,17-34: Reference to undeclared local value; A local value with the name "environment" has not been declared., and 8 other diagnostic(s):

Error: Reference to undeclared local value

  on main.tf line 5, in module "resource_group":
   5:   environment = local.environment

A local value with the name "environment" has not been declared.

TFLint Version

0.50.1

Terraform Version

1.8.4

Operating System

bendrucker commented 3 weeks ago

TFLint validates Terraform modules. If you use another tool that takes some other input and turns it into valid Terraform modules, TFLint is inherently not going to handle that. This is not a bug.