Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform_docs: unable to specify relative PATH to config file #225

Closed
7adityaraj opened this issue Sep 14, 2021 · 26 comments · Fixed by #244 or #260
Closed

terraform_docs: unable to specify relative PATH to config file #225

7adityaraj opened this issue Sep 14, 2021 · 26 comments · Fixed by #244 or #260
Assignees
Labels
bug_with_workaround Something isn't working but there is a workaround estimate/4h Need 4 hours to be done hook/terraform_docs Bash hook

Comments

@7adityaraj
Copy link

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
@MaxymVlasov MaxymVlasov added bug Something isn't working area/local_installation estimate/2h Need 2 hours to be done hook/terraform_docs Bash hook and removed area/docker labels Sep 14, 2021
@MaxymVlasov MaxymVlasov added this to the Bug and docs fixes milestone Sep 14, 2021
@MaxymVlasov
Copy link
Collaborator

Hi, @7adityaraj .
I think it can be fixed by #155. You can try clone repo, checkout to PR branch, and use pre-commit try-repo (more details) to check is it works for you.

After, check, please describe is it help you.

@7adityaraj
Copy link
Author

Hi @MaxymVlasov,

Thank you for the suggestions.

I tried with commit ID as below, but it didn't work. May be I am still missing something

repos:
    - repo : https://github.com/jans-forks/pre-commit-terraform
      rev  : 39d811f
      hooks:
       - id: terraform_docs
         files: (\.tf)$
         exclude: >
          (?x)^(
              examples/.*$|
              modules/alb/.*$|
              modules/nlb/.*$|
              modules/elb/.*$|
              modules/elb-tcp/.*$|
              modules/simple/.*$|
              modules/alb-wi/.*$
          )$
         args:
          - --config=precommit/.terraform-docs.yml
          - md
>> running precommit from ./precommit/.pre-commit-config.yaml
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 1.47s
- exit code: 1

WORKING with: "--config=precommit/.terraform-docs.yml"
Adding arg
WORKING with: "md"
Adding arg
WORKING with: "modules/core/ecr.tf"
WORKING with: "config.tf"
WORKING with: "modules/core/locals.tf"
WORKING with: "main.tf"
WORKING with: "modules/core/task.tf"
WORKING with: "outputs.tf"
WORKING with: "modules/core/autoscaling.tf"
WORKING with: "modules/core/ecs_lb.tf"
WORKING with: "modules/core/data.tf"
WORKING with: "variables.tf"
WORKING with: "modules/core/outputs.tf"
WORKING with: "modules/core/variables.tf"
Error: config file precommit/.terraform-docs.yml not found

@MaxymVlasov
Copy link
Collaborator

Good, at least now it show right path to config file in error.

@7adityaraj , using this fork, try change first arg to

- --config=__GIT_WORKING_DIR__/precommit/.terraform-docs.yml

@7adityaraj
Copy link
Author

Many thanks @MaxymVlasov,

It works when I pass the full path of the yml file. Also, I figure out with your help, it does work with the previous release tag 1.50.0 when I provide the full path of yml file. Is this expected or do we need to fill a bug for it.

@MaxymVlasov
Copy link
Collaborator

This is exepted behavior.

I will clarify docs about that.

@MaxymVlasov MaxymVlasov added documentation Improvements or additions to documentation estimate/1h Need 1 hour to be done and removed bug Something isn't working estimate/2h Need 2 hours to be done labels Sep 14, 2021
@MaxymVlasov
Copy link
Collaborator

MaxymVlasov commented Sep 14, 2021

And __GIT_WORKING_DIR__ - is not full path, it's argument that should be passed as is

@7adityaraj

@MaxymVlasov
Copy link
Collaborator

Like this one
Screenshot_20210915-002933.png

@7adityaraj
Copy link
Author

@MaxymVlasov,

It didn't work that way.

Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 1.55s
- exit code: 1

WORKING with: "--config=__GIT_WORKING_DIR__/precommit/.terraform-docs.yml"
Adding arg
WORKING with: "md"
Adding arg
WORKING with: "modules/core/ecr.tf"
WORKING with: "config.tf"
WORKING with: "modules/core/locals.tf"
WORKING with: "main.tf"
WORKING with: "modules/core/task.tf"
WORKING with: "outputs.tf"
WORKING with: "modules/core/autoscaling.tf"
WORKING with: "modules/core/ecs_lb.tf"
WORKING with: "modules/core/data.tf"
WORKING with: "variables.tf"
WORKING with: "modules/core/outputs.tf"
WORKING with: "modules/core/variables.tf"
Error: config file __GIT_WORKING_DIR__/precommit/.terraform-docs.yml not found
Terraform docs...........................................................Passed
- hook id: terraform_docs
- duration: 2.75s

WORKING with: "--config=/Users/user1/Documents/GitHub/terraform-mod-ecs-service/precommit/.terraform-docs.yml"
Adding arg
WORKING with: "md"
Adding arg
WORKING with: "modules/core/ecr.tf"
WORKING with: "config.tf"
WORKING with: "modules/core/locals.tf"
WORKING with: "main.tf"
WORKING with: "modules/core/task.tf"
WORKING with: "outputs.tf"
WORKING with: "modules/core/autoscaling.tf"
WORKING with: "modules/core/ecs_lb.tf"
WORKING with: "modules/core/data.tf"
WORKING with: "variables.tf"
WORKING with: "modules/core/outputs.tf"
WORKING with: "modules/core/variables.tf"

@MaxymVlasov
Copy link
Collaborator

Is it work if use tag 1.50 from this repo?

repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform 
  rev: v1.50.0 
  hooks: 
  - id: terraform_docs
    args:
    - --config=__GIT_WORKING_DIR__/precommit/.terraform-docs.yml
    - md

@7adityaraj
Copy link
Author

no luck.

 - repo: https://github.com/antonbabenko/pre-commit-terraform
      rev: v1.50.0
      hooks:
       - id: terraform_docs
         args:
          - --config=__GIT_WORKING_DIR__/precommit/.terraform-docs.yml
          - md
         exclude: >
          (?x)^(
              examples/.*$|
              modules/alb/.*$|
              modules/nlb/.*$|
              modules/elb/.*$|
              modules/elb-tcp/.*$|
              modules/alb-wi/.*$|
              modules/alb/.*$|
              modules/simple/.*$
          )$
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.02s
- exit code: 1

getopt: unrecognized option '--config'

@MaxymVlasov MaxymVlasov changed the title unable to pass config file from pre-commit terraform_docs: unable to specify relative PATH to config file Sep 14, 2021
@MaxymVlasov MaxymVlasov added estimate/4h Need 4 hours to be done bug_with_workaround Something isn't working but there is a workaround and removed area/local_installation estimate/1h Need 1 hour to be done labels Sep 14, 2021
@MaxymVlasov
Copy link
Collaborator

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)

@7adityaraj
Copy link
Author

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

@MaxymVlasov
Copy link
Collaborator

reminder: Need to document TFlint relative path usage

Already done

@MaxymVlasov MaxymVlasov removed the documentation Improvements or additions to documentation label Sep 29, 2021
@tpdownes
Copy link

tpdownes commented Oct 4, 2021

Why is this function doing such convolute stuff for functionality that is built-in to terraform-docs?

It basically does

terraform-docs markdown --config .tfdocs-markdown.yaml ${terraform_module_folder}

where the config file looks like:

formatter: markdown

output:
  file: README.md
  mode: inject
  template: |-
    <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
    {{ .Content }}
    <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

@antonbabenko
Copy link
Owner

@tpdownes In short, the terraform_docs hook in pre-commit-terraform used to handle a variety of versions of Terraform starting from versions before 0.12 (yes, it was a few years ago) and terraform-docs has been evolving.

We are improving it all the time.

@7adityaraj
Copy link
Author

@antonbabenko, @MaxymVlasov sorry for the delay.
I have tried new changes with version 1.52.0 and unfortunately, it is not working for me.
When I provide the full path then it works.

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.52.0
    hooks:
      - id: terraform_docs
        args: [
          --args=--sort-by required,
          #--args=--config=/Users/user1/Documents/Github/terraform-mod-ecs-service/precommit/.terraform-docs.yml
          --args=--config=./precommit/.terraform-docs.yml
          #--args=--config=.terraform-docs.yml
        ]
$ ls -la precommit/
.pre-commit-config.yaml
.terraform-docs.yml
>> running precommit from ./precommit/.pre-commit-config.yaml
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 1.06s
- exit code: 1

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


Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 1.09s
- exit code: 1

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

running from directory where yaml file exists.

└─ $ pre-commit run --all-files --verbose
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.96s
- exit code: 1

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

When I add a full path, all seems to be fine. I have compared the changes from 1.50.0 to 1.52.0 and which should be helpful but somehow there seems an issue.

I am not sure what I am misisng. I would appreciate any feedbacks.

@MaxymVlasov
Copy link
Collaborator

MaxymVlasov commented Oct 21, 2021

@7adityaraj you missing that it will be in 1.53.0 :)

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

@7adityaraj
Copy link
Author

Oops my bad, thank you for the quick response.

@7adityaraj
Copy link
Author

Hello @MaxymVlasov,

Tried with latest SHA but have the same error.

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 2973f85f3ba5f8f5b4610cce9b01a7ce26510c84
    hooks:
      - id: terraform_docs
        args: [
          --args=--sort-by required,
          --args=--config=.terraform-docs.yml,
          #--args=--config=./precommit/.terraform-docs.yml
        ]
└─ $ pre-commit run --all-files --verbose
[INFO] Initializing environment for https://github.com/antonbabenko/pre-commit-terraform.
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.84s
- exit code: 1

Error: config file .terraform-docs.yml not found
>> running precommit from ./precommit/.pre-commit-config.yaml
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.88s
- exit code: 1
Error: config file ./.terraform-docs.yml not found

@MaxymVlasov
Copy link
Collaborator

@7adityaraj

running precommit from ./precommit/.pre-commit-config.yaml
Error: config file ./.terraform-docs.yml not found

Try to specify the relative path from the repo root.

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 2973f85f3ba5f8f5b4610cce9b01a7ce26510c84
    hooks:
      - id: terraform_docs
        args:
          - --args=--sort-by required
          - --args=--config=precommit/.terraform-docs.yml

@7adityaraj
Copy link
Author

Hi @MaxymVlasov,
Thank you checking.

I have tried all possible ways but it still returns an error. Following the above, one more time:

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 2973f85f3ba5f8f5b4610cce9b01a7ce26510c84
    hooks:
      - id: terraform_docs
        args:
          - --args=--sort-by required
          - --args=--config=precommit/.terraform-docs.yml
─ $ pre-commit run -c precommit/.pre-commit-config.yaml --all-files --verbose
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.99s
- exit code: 1

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


└─ $ pre-commit run -c precommit/.pre-commit-config.yaml --all-files --verbose
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.86s
- exit code: 1

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

└─ $ cd precommit/
└─ $ pre-commit run -c .pre-commit-config.yaml --all-files --verbose
Terraform docs...........................................................Failed
- hook id: terraform_docs
- duration: 0.88s
- exit code: 1

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

@MaxymVlasov
Copy link
Collaborator

@7adityaraj can you try this one please?

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 41329df414df04a78d06c228e2fbfd2aedc1c5ea
    hooks:
      - id: terraform_docs
        args:
          - --args=--sort-by required
          - --args=--config=precommit/.terraform-docs.yml

@MaxymVlasov
Copy link
Collaborator

If it works, then I successfully fixed a bug introduced in #244 and will prepare PR with fix

@7adityaraj
Copy link
Author

Hi @MaxymVlasov,

Many thanks, yes it works. I tried with all behaviour and it all works.

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 41329df414df04a78d06c228e2fbfd2aedc1c5ea
    hooks:
      - id: terraform_docs
        args:
          - --args=--sort-by required
          - --args=--config=precommit/.terraform-docs.yml
└─ $ pre-commit run -c precommit/.pre-commit-config.yaml --all-files --verbose
[INFO] Initializing environment for https://github.com/antonbabenko/pre-commit-terraform.
Terraform docs...........................................................Passed
- hook id: terraform_docs
- duration: 2.19s

1 --sort-by required
1 --config=precommit/.terraform-docs.yml

@MaxymVlasov
Copy link
Collaborator

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

@7adityaraj
Copy link
Author

@MaxymVlasov thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug_with_workaround Something isn't working but there is a workaround estimate/4h Need 4 hours to be done hook/terraform_docs Bash hook
Projects
None yet
4 participants