terraform-linters / tflint

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

Allow use of `--chdir` and `--recursive` together #2051

Closed rdimartino closed 4 months ago

rdimartino commented 6 months ago

Introduction

Terraform allows using both the -chdir and -recursive flags, e.g. terraform -chdir=infra/ fmt -check -recursive

I expected to be able to do something similar with TFLint, but got an error

Failed to find workspaces; cannot use --recursive and --chdir at the same time

I am running my formatters/linters from the project root for other tools but am having trouble doing that with TFLint.

My directory structure looks similar to

▷ app/
▷ tmp/
▷ node_modules/
▽ infra/
  main.tf
  .tflint.hcl
  ▽ modules/
    ▽ deployment/
    main.tf
    outputs.tf
    variables.tf

The specifics are not important, but the general problem is that running tflint --chdir infra/ seems to only check infra/main.tf, but running tflint --recursive ends up wasting a ton of time scanning node_modules/, tmp/, and other large directories without any terraform configuration.

As a work-around, I can cd into the infra/ directory before running tflint --recursive, but I would expect this tool to work similarly to terraform and allow me to specify --chdir infra/ and --recursive to tell TFLint to start recursing from the infra/ directory.

Proposal

Allow the use of both --chdir and --recursive together.

Recursion should start from the directory specified by the --chdir flag.

References

https://github.com/terraform-linters/tflint/issues/1304#issuecomment-1907277718

https://developer.hashicorp.com/terraform/cli/commands/fmt#recursive

wata727 commented 6 months ago

Related discussions: https://github.com/terraform-linters/tflint/discussions/1920 https://github.com/terraform-linters/tflint/discussions/1960

If terraform fmt can use --chdir and --recursive together, I support this proposal in terms of interface consistency.