-
-
Notifications
You must be signed in to change notification settings - Fork 540
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
fix: Terraform validate - only run tf init if providers directory is missing in .terraform #509
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MaxymVlasov Can you think of any reason to check both dirs instead of only |
If a terraform state does not include providers, what then? |
I agree that a situation without modules is more common than modules without providers, but let's brainstorm first are there any modules that don't need providers |
Output-only modules. |
Maybe add something like |
Hmm, or ask the user to provide a comma-separated list of what paths should be checked, ie:
|
Might be a bit of overkilling as of quite rare use case though. Those who would want to provide a list of options, most probably were much more smarter and already use |
Maybe we can take one step back: Creating the "dummy" directory ".modules" was my short term fix, but that is a bit of a pain :-D |
@smelchior good idea, let's do it Here are my errors when no providers and modules provided ➜ time t validate -json
{
"format_version": "1.0",
"valid": false,
"error_count": 4,
"warning_count": 0,
"diagnostics": [
{
"severity": "error",
"summary": "Module not installed",
"detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
"range": {
"filename": "main.tf",
"start": {
"line": 75,
"column": 1,
"byte": 2450
},
"end": {
"line": 75,
"column": 23,
"byte": 2472
}
},
"snippet": {
"context": null,
"code": "module \"account_label\" {",
"start_line": 75,
"highlight_start_offset": 0,
"highlight_end_offset": 22,
"values": []
}
},
{
"severity": "error",
"summary": "Module not installed",
"detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
"range": {
"filename": "providers.tf",
"start": {
"line": 20,
"column": 1,
"byte": 388
},
"end": {
"line": 20,
"column": 21,
"byte": 408
}
},
"snippet": {
"context": null,
"code": "module \"email_label\" {",
"start_line": 20,
"highlight_start_offset": 0,
"highlight_end_offset": 20,
"values": []
}
},
{
"severity": "error",
"summary": "Module not installed",
"detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
"range": {
"filename": "providers.tf",
"start": {
"line": 15,
"column": 1,
"byte": 285
},
"end": {
"line": 15,
"column": 19,
"byte": 303
}
},
"snippet": {
"context": null,
"code": "module \"iam_roles\" {",
"start_line": 15,
"highlight_start_offset": 0,
"highlight_end_offset": 18,
"values": []
}
},
{
"severity": "error",
"summary": "Module not installed",
"detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
"range": {
"filename": "context.tf",
"start": {
"line": 23,
"column": 1,
"byte": 918
},
"end": {
"line": 23,
"column": 14,
"byte": 931
}
},
"snippet": {
"context": null,
"code": "module \"this\" {",
"start_line": 23,
"highlight_start_offset": 0,
"highlight_end_offset": 13,
"values": []
}
}
]
}
terraform validate -json 0.12s user 0.05s system 141% cpu 0.124 total Because it took only 0.12s, in case of a problem, that absolutely OK to do that check I suppose it could be done using next or little-bit changed function:
That code is mostly the same as what we need to execute then pre-commit-terraform/hooks/terraform_validate.sh Lines 117 to 139 in e1d78ae
As replace to pre-commit-terraform/hooks/terraform_validate.sh Lines 107 to 110 in e1d78ae
|
perfect, i will create a new PR and close this one afterwards :) |
As discussed, I made the change in #524 in a "simple" way, i think this should be sufficient to achieve what we want :-) |
Put an
x
into the box if that apply:Description of your changes
If a terraform states does not include modules, a
.terraform/modules
directory is not present. For these cases, the tf init is run every time the validate hook runs, which takes extra time + causes issues if e.g. a remote state is used which causes the init to fail.How can we test changes
I checked it with my repositories as described above, and the tf init did now only run if the .terraform/providers directly is not present.