gruntwork-io / pre-commit

A collection of pre-commit hooks used by Gruntwork tools
https://gruntwork.io/
Apache License 2.0
484 stars 155 forks source link

.tflint.hcl is rules are ignored #102

Closed brycelowe closed 10 months ago

brycelowe commented 1 year ago

Describe the bug We utilize a monorepo for our terraform configurations and we have integrated into our CI pipline precommit checks with the tflint precommit plugin. What I've noticed is that ever since the release of chdir my .tflint.hcl file is no longer effective. It looks like because tflint is actually changing the current working directory (https://github.com/terraform-linters/tflint/blob/0e58cd19e334b6c1edfc4ba6d9f4778a28a3342e/cmd/inspect.go#L35C20-L35C20 and https://github.com/terraform-linters/tflint/blob/0e58cd19e334b6c1edfc4ba6d9f4778a28a3342e/cmd/cli.go#L178) and then loading the config file (https://github.com/terraform-linters/tflint/blob/0e58cd19e334b6c1edfc4ba6d9f4778a28a3342e/cmd/inspect.go#L110). The result is tflint failing on rules that I've called out as disabled in my base .tflint.hcl file.

To Reproduce

mkdir -pv tflint-demo/module
pushd tflint-demo >/dev/null
git init

cat <<EOF > .pre-commit-config.yaml
---
repos:
  - repo: https://github.com/gruntwork-io/pre-commit
    rev: v0.1.22
    hooks:
    - id: terragrunt-hclfmt
    - id: tflint
      args:
        - "--config=.tflint.hcl"
EOF

cat <<EOF > .tflint.hcl
rule "terraform_required_providers" {
  enabled = false
}
EOF

pushd module >/dev/null

cat <<EOF > main.tf
resource "null_resource" "foo" {
  triggers = {
    "coffee" = "sandwich"
  }
}
EOF

popd >/dev/null

git add .
pre-commit install
pre-commit run

Expected behavior I'd expect the .tflint.hcl file to be honored from the repo root directory like it used to, but the functionality of tflint has changed.

Nice to have

cat < .pre-commit-config.yaml

repos:

cat < .tflint.hcl rule "terraform_required_providers" { enabled = false } EOF

pushd module >/dev/null

cat < main.tf resource "null_resource" "foo" { triggers = { "coffee" = "sandwich" } } EOF

popd >/dev/null

git add . pre-commit install pre-commit run tflint-demo tflint-demo/module Initialized empty Git repository in /Users/blowe/project-scratch-space/tflint-demo/.git/ pre-commit installed at .git/hooks/pre-commit Terragrunt hclfmt........................................................Passed tflint...................................................................Failed

1 issue(s) found:

Warning: Missing version constraint for provider "null" in required_providers (terraform_required_providers)

on module/main.tf line 1: 1: resource "null_resource" "foo" {

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.4.0/docs/rules/terraform_required_providers.md


- [X] Screenshots
<img width="864" alt="image" src="https://github.com/gruntwork-io/pre-commit/assets/106990495/2c31d82d-a296-4e68-8aaa-cacc017659cf">

**Additional context**
I realize this isn't a gruntwork problem pre-se, but there are other pre-commit hooks that do handle this.
thegreatsunra commented 1 year ago

We are encountering this issue as well, where the .tflint file in the root of a project used to be read in and used by pre-commit for files in subfolders, but now that config is ignored.

We confirmed that moving the base .tflint file into any affected project subfolder remedied the issue for files in that specific folder. Not a solution, per se, but another data point that chdir is at work here.

plaformsre commented 1 year ago

Experiencing the same issue as @brycelowe .

Hope this gets fixed soon as we cannot keep copying .tflint.hcl into so many subdirectories.

brycelowe commented 1 year ago

I proposed a fix but I haven't heard anything on my PR.