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

.pre-commit-config.yaml: Limit terragrunt validate to folders with terragrunt.hcl files #4

Merged
merged 3 commits into from
Nov 6, 2024

Conversation

Wayneoween
Copy link
Contributor

@Wayneoween Wayneoween commented Oct 31, 2024

I ran into the issue, that freshly checked out, the pre-commit configuration failed with this already known issue for me:

🛰️ ❯ pre-commit run --all-files
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
mixed line ending........................................................Passed
Terragrunt fmt...........................................................Passed
Terragrunt validate......................................................Failed
- hook id: terragrunt_validate
- exit code: 1

19:57:52.689 ERROR  stat ./terragrunt.hcl: no such file or directory
19:57:52.689 ERROR  Unable to determine underlying exit code, so Terragrunt will exit with error code 1

It appears to me that if you give any directory without a terragrunt.hcl file to terragrunt_validate this error occurs.

The issue seems to stem from the fact that there apparently needs to be a terragrunt.hcl file everywhere (although in antonbabenko/pre-commit-terraform#191 it already looks like it should only run where a .hcl file exists.

Additionally please look at this comment in the same issue where it seems like root.hcl is a bad name if we want the file to be validated.

I used the versions in this repo and also the newest ones available (5.0.0 and 1.96.1 respectively) with not difference in behaviour.

This MR appears to me not as the best solution, but maybe its a solution to get pre-commit to cleanly run through?

@simonhops simonhops self-assigned this Nov 6, 2024
@simonhops
Copy link
Collaborator

First of all: The problem is verified.

This is a good and working fix for the problem. Thank you for contributing! I simplified the regex a little bit, but the change is in the same nature.

When there is an error in root.hcl the terragrunt_validate pre-commit hook will find it when evaluating the root modules which include the root.hcl. Therefore exluding the file in the terragrunt_validate regex doesn't mean it will not check the root.hcl.

I read the linked issues and comments. There is a good reason for naming the top-level Terragrunt file root.hcl instead of terragrunt.hcl. Running terragrunt run-all plan in the environments folder runs without errors. This means every root module can be planed with one command. When naming this file terragrunt.hcl Terragrunt identifies this file as the entrypoint for a root module itself and tries to create a plan. This plan then fails.

Nonetheless this error when using terragrunt run-all isn't obvious and easily searchable. I added more context to the README for clarification.

@simonhops simonhops merged commit 570b888 into makandra:main Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants