Skip to content

terraform TF020 misses a reference nested more than six levels deep in a block #5107

Closed

Description

@lex00

TF020 reports a local as "declared and never referenced" when its only reference sits several blocks down, such as the expression of a Cloud Armor rule's match { expr { ... } }. referencesInBody in lexicons/terraform/src/hcl/references.ts walks the hcl2json body and returns at depth > 6. hcl2json gives each nested block as an array of objects, so rule, match and expr use six levels and the expression string is the seventh, which is never scanned. The same reference inside an interpolation at the top level of the block is found, so string interpolation is not the cause. On kubernetes/k8s.io this gave TF020 for local.allowed_paths in infra/gcp/terraform/modules/oci-proxy/cloud-armor.tf, once for each root that calls the module.

Reproduce

Run with chant 0.124.95 from npm and @cdktn/hcl2json.

tree/mod/main.tf:

locals {
  shallow = ["a", "b"]
  deep    = ["/", "/v2/.*"]
}

resource "google_compute_security_policy" "p" {
  name = "p-${join("-", local.shallow)}"
  rule {
    action   = "deny(404)"
    priority = "1"
    match {
      expr {
        expression = "!request.path.matches('${join("|", local.deep)}')"
      }
    }
  }
}

tree/root/main.tf holds a terraform block with a backend and required_providers, and module "m" { source = "../mod" }.

chant audit tree --format json --tier all --fail-on none -o out.json

The report has one TF020, for local.deep, with entity root/module.m/locals. local.shallow is not reported.

Expected

No TF020 for local.deep. The walk reaches every string in the body, or its depth limit is high enough for the blocks providers nest, with a test at the depth of the Cloud Armor rule above.

Where

referencesInBody in lexicons/terraform/src/hcl/references.ts (the if (depth > 6) return; line). TF020 (lexicons/terraform/src/lint/post-synth/tf020.ts) is the only rule that reads this index today.

Found while reading kubernetes/k8s.io offline, for #5067. Tracked on #5105.

Metadata

Metadata

Assignees

No one assigned

    Labels

    epic:estate-read

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions