VladRassokhin / intellij-hcl

HCL language support for IntelliJ platform based IDEs
Apache License 2.0
244 stars 47 forks source link

Better support for Terraform 0.13 syntax in .tf.erb files #314

Open Justin-W opened 4 years ago

Justin-W commented 4 years ago

Thank you for opening an issue. In this template paragraph text could be removed, however please retain headers.

Prerequisites

Installation details

intellij-hcl plugin version: 0.7.10

Terraform v0.13.3

RubyMine 2020.2.2 Build #RM-202.7319.53, built on September 14, 2020 Runtime version: 11.0.8+10-b944.31 x86_64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 10.15.6 GC: ParNew, ConcurrentMarkSweep Memory: 1654M Cores: 4 Registry: documentation.show.toolbar=true Non-Bundled Plugins: org.intellij.plugins.hcl, name.kropp.intellij.makefile, com.intellij.plugin.adernov.powershell

Terraform Configuration Files

Sample ERB:

<%- (data[:storage_containers]||[]).each do |bucket| -%>

resource "google_storage_bucket" "<%= bucket[:name] %>" {
  provider = <%= data[:tf_provider] %>.abc-<%= data[:tf_provider_name] %>
  name = "<%= bucket[:name] %>"
<%- if bucket[:location] -%>
  location = "<%= bucket[:location] %>"
<%- end -%>
}
<%- if bucket[:acl] -%>

resource "google_storage_bucket_acl" "<%= bucket[:name] %>-acl" {
  provider = <%= data[:tf_provider] %>.abc-<%= data[:tf_provider_name] %>
  bucket = google_storage_bucket.<%= bucket[:name] %>.name

  predefined_acl = "<%= bucket[:acl] %>"
}
<%- end -%>
<%- end -%>

Sample HCL generated by the above ERB:

resource "google_storage_bucket" "mybucket" {
  provider = google.abc-p1
  name = "mybucket"
  location = "us-east1"
}

resource "google_storage_bucket_acl" "mybucket-acl" {
  provider = google.abc-p1
  bucket = google_storage_bucket.mybucket.name

  predefined_acl = "projectPrivate"
}

Exception

I didn't see any exceptions logged to the IDE's Event Log.

Expected Behavior

What should have happened?

  1. The IDE's Editor window should correctly parse the HCL resource, data source, output, etc. declarations embedded within the ERB.
  2. In the 'Terraform Config' tab of the IDE's Structure panel, the ERB's structure should be displayed as:

Actual Behavior

What actually happened?

  1. The IDE's Editor window shows error message " expected, got '.'" on the 4th line of the sample ERB fragment above, after this part of the line: provider =. Apparently, removing the wrapping double quotes (which were required by TF v0.11, but which are invalid in TF v0.13) from around the value of the "provider" attribute (changing it from a String literal into an un-quoted Terraform expression) causes parsing problems for the HCL plugin.
  2. In the 'Terraform Config' tab of the IDE's Structure panel, the ERB's structure is incorrectly displayed as:

Note that everything after the first provider attribute is incorrectly indented, and all resources past the first (incorrectly parsed) resource are missing from the structure.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. Open a '*.tf.erb' file like the sample above.
  2. Open the 'Terraform Config' tab of the IDE's Structure panel.

Note that changing ERB line 4 (provider = <%= data[:tf_provider] %>.abc-<%= data[:tf_provider_name] %>) to either of the following fixes the Structure panel's hierarchy display, but at the cost of making the ERB generate invalid HCL:

Previously, before migrating our ERBs from generating TF v0.11-compatible HCL to TF v0.13-compatible HCL, the HCL plugin was able to parse the ERBs mostly correctly, even despite the various if/else, each loop, and <%= ... %> expression blocks scattered throughout the ERBs.

After migrating to TF v0.13-compatible HCL, the HCL plugin is nearly useless for working with the migrated ERB files (even though they generate perfectly valid v0.13-compatible HCL). Everything after the first provider attribute in the file is effectively unparsed, and therefore doesn't work properly with the IDE's code folding, syntax highlighting, Structure panel, and other features which are dependent on correct parsing of the file's AST and code structure.

It seems like much of the plugin's regressions in this area are related to the removal of most of the "wrapping" double-quotes around HCL expressions, and the transition of many Interpolated Strings to first-class expressions.