feat: add Terraform/HCL language support (#199)#514
Open
stk0vrfl0w wants to merge 1 commit into
Open
Conversation
Adds full .tf / .hcl parsing to code-review-graph, closing issue tirth8205#199. | Terraform construct | Node kind | Name | |----------------------------|-----------|--------------------| | resource "type" "name" | Class | resource.type.name | | data "type" "name" | Class | data.type.name | | module "name" | Class | module.name | | variable "name" | Function | var.name | | output "name" | Function | output.name | | locals { key = ... } | Function | local.key | | provider "name" | Function | provider.name | | terraform { ... } | (skipped) | | Edge types emitted: - CONTAINS — file -> every node - IMPORTS_FROM — file -> module source path (resolved when possible) - REFERENCES — cross-block value references via variable_expr+get_attr chains _hcl_variable_refs / _hcl_ref_target resolve dotted expression chains: - var.x, local.x, module.x, data.type.name -> named graph targets - resource references (aws_vpc.main.id) -> resource.aws_vpc.main - Built-in / block-local roots suppressed (no spurious edges): _HCL_REF_PREFIXES includes each, count, self, path, terraform _HCL_RECURSE_TYPES covers function_call + function_arguments (so var refs inside length(var.x) are extracted) and quoted_template + template_interpolation (so "${var.x}" interpolations are extracted). _hcl_dynamic_iterator_name() extracts the iterator symbol from any dynamic block (defaulting to the block label; respecting the optional iterator = <ident> override). _walk_hcl_expressions accepts a local_names: frozenset[str] scope that accumulates iterator symbols across nested dynamic blocks, suppressing spurious resource.<iter>.* edges from expressions like setting.value[...] or origin_group.key. Covers all three cases from the Terraform dynamic-blocks documentation: - Default iterator (block label = iterator symbol) - Custom iterator via iterator = <ident> - Multi-level nested dynamic blocks - code_review_graph/parser.py — extension map, type dicts, 10+ module-level helpers, 4 class methods, _walk_hcl_expressions scope-aware iterator tracking - tests/fixtures/sample.tf — 243-line fixture covering resources, data sources, modules, variables, outputs, locals, providers, for_each chaining, function-call refs, template interpolations, lifecycle/replace_triggered_by, and all three dynamic block cases - tests/test_multilang.py — TestHCLParsing: 36 tests, all passing - docs/FEATURES.md — HCL/Terraform added to language list - docs/LLM-OPTIMIZED-REFERENCE.md — HCL/Terraform added to <section name="languages"> Recognised and fully parsed: Terraform and OpenTofu (.tf, .hcl). OpenTofu is a direct fork of Terraform with identical block schemas. The following tools also use .hcl files but with different top-level block schemas. Their files will be detected as language="hcl" by extension, but their block types are absent from _HCL_BLOCK_CFG and will produce a File node only (no resources, variables, or edges): - Terragrunt (terragrunt.hcl, terragrunt.stack.hcl) blocks: include, dependency, dependencies, remote_state, generate, inputs, unit, stack - Packer (*.pkr.hcl) blocks: source, build, packer - Nomad (*.hcl, *.nomad) blocks: job, group, task, service, network - Vault (*.hcl policy files) blocks: path - Consul (*.hcl config files) blocks: datacenter, acl, server, and others - Waypoint (waypoint.hcl) blocks: app, build, deploy, release - Boundary (*.hcl) blocks: controller, worker, listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds full .tf / .hcl parsing to code-review-graph, addressing issue #199.
Edge types emitted:
_hcl_variable_refs / _hcl_ref_target resolve dotted expression chains:
_HCL_RECURSE_TYPES covers function_call + function_arguments (so var refs inside length(var.x) are extracted) and quoted_template + template_interpolation (so "${var.x}" interpolations are extracted).
_hcl_dynamic_iterator_name() extracts the iterator symbol from any dynamic block (defaulting to the block label; respecting the optional iterator = override). _walk_hcl_expressions accepts a local_names: frozenset[str] scope that accumulates iterator symbols across nested dynamic blocks, suppressing spurious resource..* edges from expressions like setting.value[...] or origin_group.key.
Covers all three cases from the Terraform dynamic-blocks documentation:
Default iterator (block label = iterator symbol)
Custom iterator via iterator =
Multi-level nested dynamic blocks
code_review_graph/parser.py — extension map, type dicts, 10+ module-level helpers, 4 class methods, _walk_hcl_expressions scope-aware iterator tracking
tests/fixtures/sample.tf — 243-line fixture covering resources, data sources, modules, variables, outputs, locals, providers, for_each chaining, function-call refs, template interpolations, lifecycle/replace_triggered_by, and all three dynamic block cases
tests/test_multilang.py — TestHCLParsing: 36 tests, all passing
docs/FEATURES.md — HCL/Terraform added to language list
docs/LLM-OPTIMIZED-REFERENCE.md — HCL/Terraform added to
Recognised and fully parsed: Terraform and OpenTofu (.tf, .hcl). OpenTofu is a direct fork of Terraform with identical block schemas.
The following tools also use .hcl files but with different top-level block schemas. Their files will be detected as language="hcl" by extension, but their block types are absent from _HCL_BLOCK_CFG and will produce a File node only (no resources, variables, or edges):
blocks: source, build, packer
blocks: job, group, task, service, network
blocks: path
blocks: datacenter, acl, server, and others
blocks: app, build, deploy, release
blocks: controller, worker, listener