question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

meta: Product-agnostic HCL syntax highlighting

See original GitHub issue

Problem Statement

Most editors, plugins, extensions (including this Terraform VS Code extension), or websites wanting displaying files with some flavour of HCL (such as Terraform language) need a way of highlighting these files.

The fundamental problem of highlighting involves answering two (related) questions:

  • which files to highlight
    • e.g. highlight *.tf as Terraform configuration
  • how to highlight the file that should be highlighted
    • e.g. sequence of bytes between 0 and 10 on line 3 is an attribute name

Often times implementations start by supporting a single flavour of HCL, such as Terraform and many end there, or just decide to treat different HCL flavours as equivalent, e.g. highlight Nomad job spec with the same set of rules as Terraform configuration.

This creates a few challenges as described below.

Differences between Terraform versions

Terraform language contains a number of components which change between versions. For example function names, or block types. Product-specific highlighting often hard-codes such components, which makes it version-specific.

For example alltrue or anytrue functions were added in Terraform 0.14.0, but are highlighted differently (in this VS Code extension) than other functions introduced earlier (such as lower or max):

Screenshot 2021-05-10 at 15 12 40

Other HCL-based languages

Reusing the same implementation which is tailored for one product (Terraform) results in inaccurate and incomplete highlighting.

While the underlying parser is the same and components available within HCL2 are the same, the highlighting differs in small subtle ways. This results in inconsistent and suboptimal experience across HashiCorp products.

For example Packer template highlighted with Terraform VS Code extension:

Screenshot 2021-05-10 at 15 27 02

or Nomad job spec

Screenshot 2021-05-10 at 15 35 45

User Impact

All users of VS Code Terraform extension would benefit from more accurate highlighting of Terraform configurations.

At the time of writing there’s close to 18 000 public repositories on GitHub which are detected as majority HCL (with close to 1M files across them). All of these files in these repositories could be highlighted more accurately.

Visitors of all HashiCorp .io product sites contain snippets of HCL configurations which would benefit from more accurate highlighting.

Expected User Experience

  • Users of the VS Code Terraform extension are provided with files highlighted consistently and correctly for any Terraform 0.12+ compatible configuration.
  • Users displaying Terraform configuration on GitHub see it highlighted consistently and correctly for any Terraform 0.12+ compatible configuration
  • Visitors of terraform.io and other HashiCorp .io websites see snippets of HCL2 code highlighted accurately

Proposal

  • create new repository, e.g. hashicorp/hcl-grammar to host the HCL grammar
  • implement a generator which generates TextMate JSON grammar (initially) for product-agnostic HCL
  • commit generated grammar to the repository
  • create tests to cover the generated grammar, e.g. via https://github.com/PanAeon/vscode-tmgrammar-test
  • use this grammar in the Terraform extension to highlight *.tf files (while assuming that any richer highlighting experience can be provided via LS)
  • switch GitHub’s linguist to this grammar for *.tf, *.hcl and other known extensions

Blocking Questions (Optional)

  • Is generating syntax grammars feasible? A prototype would likely need to be created to answer this question.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:41
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
jpograncommented, Mar 14, 2022

I can’t explain more at the moment, but this is something we’re actively working on and will have more information in the coming weeks.

0reactions
github-actions[bot]commented, Jun 27, 2022

I’m going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - meta: Product-agnostic HCL syntax highlighting -
meta : Product-agnostic HCL syntax highlighting. ... files with some flavour of HCL (such as Terraform language) need a way of highlighting these...
Read more >
HashiCorp HCL - Visual Studio Marketplace
This extension provides HCL syntax highlighting for files that use .hcl as the file extension, for example: Nomad job specification · Packer ...
Read more >
HCL Technologies Annual Report 2021-22
Key products under market sub-segments. • Client and Digital Experience: Commerce, Unica, Discover. • Pro and Low Code Platform:.
Read more >
Terraform Dynamic Blocks with Examples - CloudBolt Software
Dynamic block flowchart using an input variable. The key parts of the code are highlighted below for the inbound_ports local variable and the...
Read more >
Terraform AWS Cloud: Sane Infrastructure Management - Toptal
Terraform is platform-agnostic; you can use it to manage bare metal servers ... You write Terraform configuration code in *.tf files using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found