Syntax highlighting for Jinja templated Terraform files
This is a very basic syntax highlighter (and formatting hack) for jinja wrappers to terraform code - you can use jinja to conditionally render terraform file contents, or dynamically render provisioners and providers within terraform.
Requirements
- Expects syntax highlighting
source.hcl.terraform
from Hashicorp Terraform extension.
- Expects syntax highlighting
source.jinja
from Jinja extension.
When installing through the VSCode Marketplace these should be installed automatically. Kindly verify these are enabled before raising issues.
Features
This is useful for syntax highlighting in projects where you want to abstract terraform with jinja and use a yaml to render terraform code based on user input.
For example; with jinja cli installed, and files as in examples/
folder, you could generate/update the main.tf dynamically as
jinja -d config.yml -f yaml main.tf.j2 --out generated_main.tf
It also adds a language capability called jinjaform
that can be used to configure custom formatting logic
By default, it associates all files with extensions .tf.j2
, .tf.jinja
, .tf.jinja2
, .tfvars.j2
, .tfvars.jinja
, .tfvars.jinja2
, .hcl.j2
, .hcl.jinja
, .hcl.jinja2
to the new language jinjaform
.
Assuming you want to format the file based on terraform
rules, and assuming you are only using {% if ... %}
, {% for ... %}
or {% with ... %}
and {% include ... %}
features from jinja, you could use Faster Format with CLI extension and set up the following language rule in your VSCode settings.json
{
// ...
"[jinjaform]": {
"editor.defaultFormatter": "djpai.faster-format-with-cli",
"djpai.format.command": "sed '/^$/N;/^\\n$/D' | sed -e 's/{%/#{%/g' | terraform fmt -write=false - | sed -e 's/#{%/{%/g'",
"djpai.format.mode": "inline_stdin"
}
// ...
}
We are using the djpai.format.command
setting provided by the extension to send the contents of our file to the terraform formatter.
In the command, we are using sed to remove any extraneous newlines from the file; comment and uncomment the jinja specific keywords that could cause syntax issues in terraform, and finally formatting with terraform fmt and reverting back changes.
You may have to tweak the regexes based on how you are leveraging jinja features when templating terraform.
[!NOTE]
- You can debug your format command through the OUTPUT Panel
View > Output
or Cmd/Ctrl + Shift + P > View: Toggle Output
in VSCode and selecting the fasterFormatWithCLI
channel.
- You may also need to
Developer: Set Log Level...
as Debug
to get more logs
Known Issues
- Nested jinja blocks will not respect highlighting
- No language server capabilities of terraform is possible since it requires custom clients and request forwarding, which is beyond the scope for now.
Refer VSCode/Embedded Languages, microsoft code samples and this SO answer to understand the complexity.
- Commenting is opinionated, since the syntax for commenting in jinja is different from terraform. By design, the comments for jinjaform uses terraform commenting
# inline
and /* block */
since the expectation is that it will be rendered into terraform for use.
Credits