pre-commit-terraform: terraform_docs: unable to specify relative PATH to config file

Describe the bug

I looked over the below block on how tf docs pick the config file. https://github.com/terraform-docs/terraform-docs/blob/3d84cad448378bc36dfef856a319ff6a43d7752f/internal/cli/run.go#L133

v.AddConfigPath(r.rootDir)              // first look at module root
	v.AddConfigPath(r.rootDir + "/.config") // then .config/ folder at module root
	v.AddConfigPath(".")                    // then current directory
	v.AddConfigPath(".config")              // then .config/ folder at current directory
	v.AddConfigPath("$HOME/.tfdocs.d")      // and finally $HOME/.tfdocs.d/

	if err := v.ReadInConfig(); err != nil {
		var perr *os.PathError
		if errors.As(err, &perr) {
			return fmt.Errorf("config file %s not found", config.File)
		}

I have my configuration files under precommit dir as below

user: Github/terraform-mod-ecs-service
└─ $ ls -la precommit/
 .
 ..
 .pre-commit-config.yaml
 .terraform-docs.yml
 Dockerfile
 docker-compose-multibranch.yml
 entrypoint.sh
 gitleaks-config.toml

my precommit config is:

#  pre-commit run --all-files --verbose
repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.50.0
    hooks:
      - id: terraform_docs
        args: [
          '--args=--sort-by required',
          '--args=--config=.terraform-docs.yml'
          #'--args=--config=precommit/.terraform-docs.yml'
        ]
        exclude: >
          (?x)^(
              examples/.*$
          )$
user: Github/terraform-mod-ecs-service
└─ $ pre-commit run -c ./precommit/.pre-commit-config.yaml --all-files --verbose
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 1.75s
- exit code: 1

Error: config file .terraform-docs.yml not found
user: terraform-mod-ecs-service/precommit
└─ $ pre-commit run -c .pre-commit-config.yaml --all-files --verbose
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 1.67s
- exit code: 1

Error: config file .terraform-docs.yml not found

Please let us know what behaviour you expected and how terraform-docs diverged from that behaviour.

.terraform-docs.yml should be picked as config for tf-docs or someone can suggest if there is other way.

Adding yml file to each directory where I have readme with tf-docs hooks works. But I would like to have only one yml file to the precommit dir if possible.

Thank you in advance.

How can we reproduce it?

with above steps.

Environment information

└─ $ pre-commit autoupdate
Updating https://github.com/antonbabenko/pre-commit-terraform ... already up to date.
└─ $ pre-commit --v
pre-commit 2.11.1

- terraform-docs version (use `terraform-docs --version`):  terraform-docs version v0.14.1 darwin/amd64
- Go version (if you manually built. use `go version`): go version go1.16.2 darwin/amd64
- OS (e.g. Windows, MacOS): Darwin 20.6.0 Darwin Kernel Version 20.6.0

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26 (1 by maintainers)

Most upvoted comments

That really looks like a bug. Depends on #155 merge.

  • reminder: Need to document TFlint relative path usage

Thank you, @7adityaraj ! I will fix this in ~3-4 weeks. If it too long - you can try prepare fix based on #155 and send PR (don’t forget mention here if you’ll start contibuting)

Yay! v1.53.0 will be released this week, jfyi

@7adityaraj you missing that it will be in 1.53.0 😃

You can use the latest commit SHA from master branch, for tests proposes

thank you @MaxymVlasov and will wait for the next release. I am using a workaround as have a copy of yml file in each readme directory with tag 1.50.0