-
Notifications
You must be signed in to change notification settings - Fork 10
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
Formatting support for HCL #98
Comments
This is the place to ask 😁 What kind of HCL are you writing? For example are you writing Packer or Boundary, etc. In this first release we wanted to see where the need was before moving in one direction or another. |
atm I'm working with a combination of |
We could implement a product-agnostic formatting via |
ahhhh, that's ringing some bells actually. I remember asking for generic HCL formatting a year or two back and referencing the existing help me understand a bit if you can: if they're all HCL2 ( to be clear, as much as I want a general-purpose formatter, I do not like the idea of having a low-level formatter that can easily conflict with the higher-level tool-specific formatters. |
The application-specific formatting tools should typically be a superset of the generic HCL formatting, by which I mean that running Taking Terraform's formatting command as an example, what it's doing is:
A generic HCL format is essentially the same but without the step 2, and so running generic HCL format after running There may be other reasons that this wouldn't be desirable beyond my knowledge -- for example, I don't know how formatters provided for the same file by several different VSCode extensions interact with one another -- but at least from a pure HCL perspective it seems plausible that we could offer a generic subset of formatting in the generic extension without causing troubles for application-specific formatting done elsewhere. Generic formatting probably also applies generic syntax checking, which could perhaps allow for some basic "red squiggles" support in the generic HCL extension, for the subset of errors that HCL itself detects. (Over in hashicorp/hcl#362 I previously discussed some similar tradeoffs about generic vs. application-specific validation. What I said there is still broadly true, but it might be that the generic HCL extension is sufficient motivation to revisit the priority tradeoff I mentioned there and offer a generic HCL format / validate as an installable package. It's not for me to say whether that would be the best approach to integrating this into this extension, though; I expect there are some other interesting strategies to consider such as compiling the relevant subset of HCL to portable WebAssembly and embedding it directly inside the extension. 🤔 ) To more directly answer the question "isn't it all just HCL?": unlike some other formats like JSON and YAML, a HCL file is more like a program to be executed than a data structure to be parsed, and so there's considerably more application-level interpretation to be done than you might be accustomed to with other grammars. HCL is designed as a toolkit for building languages rather than as a language in its own right, but it's true that a bunch of the existing HCL-based languages aren't doing that much above what HCL itself offers, aside from defining their expected block types and attributes. The languages that allow for e.g. creating relationships between declared objects via expressions, or writing "libraries" like Terraform's modules, will tend to bend HCL in more complicated ways than where HCL is being used mainly just as a serialization of a flat data structure. To be specific, I would expect the Terraform Language, the Packer Language and the Waypoint Language to all eventually benefit from application-specific extensions with their own formatters, but something like Vault's policy language or Consul's agent configuration files would probably suffice with a generic HCL extension and generic formatter. |
man, I love that you take the time to write out detailed responses like this @apparentlymart
this resonates with me perfectly, and a lack of a stable, supported
this also tracks for the most part, although I'm not entirely sure I can think of a good example. that is, HCL is still a syntax/language spec from what I understand, so I'm curious if you have a concrete example of something
so, given this, I guess where that leaves me is the thought that
also, I strongly agree with your interpretation here that phew, that got sorta meta~ |
or maybe, very specific to this issue, the better takeaway could be to focus on creating/enabling well-supported extensions for the high-level tooling and not wasting time on adding format support to this one? ¯\_(ツ)_/¯ |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as outdated.
This comment was marked as outdated.
Looking for a formatting tool for hcl files, I came across this: https://marketplace.visualstudio.com/items?itemName=fredwangwang.vscode-hcl-format it seems to rely on a |
Hi, will |
I did try to use https://marketplace.visualstudio.com/items?itemName=fredwangwang.vscode-hcl-format to format terragrunt.hcl files but even by downloading the binary it didn't work, at the end I end up using https://marketplace.visualstudio.com/items?itemName=jkillian.custom-local-formatters with the conf below: {
"[hcl]": {
"editor.defaultFormatter": "jkillian.custom-local-formatters",
},
"customLocalFormatters.formatters": [
{
"command": "terragrunt hclfmt",
"languages": ["hcl"]
}
],
} So to auto format you would need to press |
https://marketplace.visualstudio.com/items?itemName=jkillian.custom-local-formatters is great, but the command is expected to act like a filter, with input on stdin. So the relevant {
"[hcl]": {
"editor.defaultFormatter": "jkillian.custom-local-formatters",
},
"customLocalFormatters.formatters": [
{
"command": "terraform fmt -",
"languages": [
"hcl"
]
}
]
} |
It is true that That said as was already mentioned above at length, |
I'm here because of the new native |
When |
Borrowing the idea of vim modelines, what if we were to allow for a comment near the top of HCL files that provided hints about the specific type/flavor of the file? Something like |
@mdepot The problem is not in detecting flavour/product - that can already be done relatively easily by reading the file extensions (such as The problem is in scoping - there is potentially infinite number of HCL-based languages and it seems unrealistic that a single tool/extension would be able to format them all reliably/accurately. Product-specific problems should have product specific solutions. I'm not implying that there can't be formatting support in vscode-hcl extension but it would likely be unable to format configuration according to product-specific rules. |
I use the following solution with the "emeraldwalk.runonsave": {
"commands": [
{
"match": "\\.tf*",
"cmd": "terraform fmt ${fileDirname}"
},
{
"match": "\\.hcl",
"cmd": "terragrunt hclfmt ${fileDirname}"
}
]
}, |
historically I'd used vscode's
"files.associations": {}
setting to tiehcl
files to the terraform extension to get simple formatting support, like so:if there isn't already a plan to add formatting support to this extension I'd like to formally request one!
The text was updated successfully, but these errors were encountered: