-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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 fmt
removing resource name when % in value
#35417
Comments
Note: cannot run `terraform fmt` on the HCL yet[1] [1]: hashicorp/terraform#35417
Hi @jacobbednarz! Thanks for reporting this. I was able to reproduce the problem using files on disk too. I created a directory with a file containing the following content: resource "foo" "%[1]s" {} After running resource "foo" {} I agree that this is incorrect behavior, but since Therefore I think the correct behavior here would be to return the following error message:
From reading the code, I think what we have here is a historical accident: when we originally wrote the In the meantime we've introduced into All of the My first suggested fix would be to change the following code so that it calls Terraform's own logic for loading and decoding the configuration for a single file: terraform/internal/command/fmt.go Lines 187 to 194 in c19118f
That would then raise exactly the error I described above. However, we should consider carefully whether this would be a breaking change, since it will make With all of that said, it does seem like something is wrong in The rewrite that terraform/internal/command/fmt.go Lines 319 to 322 in c19118f
Therefore it seems like either I've not yet proven it with running code, but I suspect this is a buggy interaction between an edge case of the main IIRC in some cases HCL decodes a quoted literal as a sequence of separate literal tokens just due to a quirk of how the tokenizer works, and so it might be interpreting When such an expression gets evaluated normally that quirk is invisible: the two literals just get concatenated together and the result is the single string But If I'm right about how the tokenizer is dealing with this, then we could improve it by making that logic tolerate zero or more literal strings between the quotes, and concatenate their results together in the same way that expression evaluation would. That solution would not have any compatibility implications and would make the system behave as in "Expected Behavior" in the original issue, but does not address the more general problem that I suppose the most ideal thing would therefore to be make both of the changes I described: the first one would make the |
Thanks for the deep dive into this and I'm largely onboard with the line of thinking here. I'll leave the detective work up of isolating the correct boundaries to someone more knowledgeable in the interactions at play here however, I do have some thoughts on introducing some change to move this forward. I totally agree that fixing this (whatever shape that may take) given the behaviour now is confusing. The bit that I'd like to see a bit more flexibility in is how strict we are with the validation. The reason this came about for me is that we're moving our test configurations (the HCL used in assertions) to their own dedicated For me, a good middle ground would be separating these two issues (strict validation, potentially missing labels/parser issues) and addressing them individually which in my view, also gets a nice in between where the example syntax like this can be used but also allow operators to be stricter with their validation if they would like to. I would put the stricter validation behind a new flag -- my very creative suggestion being |
Hi @jacobbednarz, I was reflecting on what I'd said here overnight and independently reached a conclusion that I think you will find more agreeable, if I'm understanding your comment correctly. In my comment yesterday I was thinking back to the earliest versions of modern We resolved that with the decision that the tool would do nothing unless the input was already valid syntax, as I was describing yesterday. However, in retrospect my yesterday comment proposed to apply that rule too strongly: the Terraform-specific formatting rules cannot create HCL-level syntax ambiguity because Terraform's language is built in terms of HCL's grammar. Only HCL grammar features contribute to the indentation and alignment decisions, and so Terraform's own concerns cannot have such a broad impact on the larger document. Therefore I think the rule should be:
Since what you shared is valid HCL syntax despite being invalid Terraform syntax, the above rules would cause the behavior you expected in this particular case. We can get there by fixing the bug I pointed out in However, I do still want to caution that |
That all makes total sense to me! If we get any wilder with the templating, i.e. outside of just passing in string values, we're probably best off moving to a full templating solution anyway. |
Terraform Version
Terraform Configuration Files
No configuration needed
Debug Output
Expected Behavior
Output should be
resource "foo" "%[1]s" {}
Actual Behavior
resource "foo" {}
Steps to Reproduce
echo 'resource "foo" "%[1]s" {}' | terraform fmt -
Additional Context
I suspect this may be related to string directive expressions which are used for evaluating conditionals however, I think
%[...]s
shouldn't be an catching and getting removed here as it is a valid Go template placeholder without an overlap in HCL (at least to my knowledge).References
No response
The text was updated successfully, but these errors were encountered: