Skip to content

Document the IR and support the Terraform 1.x configuration language - #2

Merged
be-thomas merged 4 commits into
mainfrom
feat/ir-docs-tf-1x
Oct 26, 2026
Merged

be-thomas merged 4 commits into
mainfrom
feat/ir-docs-tf-1x

Conversation

@be-thomas

Copy link
Copy Markdown
Owner

What changed

IR schema 0.2 (additive over 0.1)

  • Graph gains terraform metadata, preserved blocks, features and min_version.
  • Variable / Output gain the post-1.0 fields (nullable, ephemeral, deprecated, type) plus attributes / blocks catch-alls, so settings the IR does not model are no longer dropped.
  • Local, Module and Provider gain file / line provenance.
  • Nested blocks record file as well as line; JSON-derived blocks carry the enclosing file only.

Parser

  • ParseDir / ParseFiles / ParseString accept native HCL and .tf.json, chosen by suffix.
  • One top-level schema recovers JSON block types and labels; bodies below the top level stay flat because HCL cannot distinguish a nested block from an object property there.
  • moved, import, removed, check, ephemeral, action and unknown future blocks are preserved instead of being dropped. Preserved blocks never become nodes and never create edges.
  • terraform settings are extracted into typed metadata: required_version, required_providers, experiments, backend, cloud, provider_meta.
  • The parser file is split into parser.go, body.go, json.go, preserve.go, schema.go, terraform.go. The native path keeps its previous structure.

pkg/tfversion

  • Registry of 25 language features covering Terraform 1.0–1.17, each with a detection rule and canonical since release.
  • Detectors that can fire on older configuration are marked unreliable and never raise min_version.
  • ParseConstraint understands =, !=, >, >=, <, <=, ~> and comma lists; invalid input never fails a parse.

Emitters

  • New emitters.WarnUnsupported reports preserved blocks, terraform backend/cloud/provider_meta, conditions and variable validations with stable W_* codes.
  • Called by both emitters immediately after the warning collector is created. Generated TypeScript is byte-for-byte unchanged.

Docs

  • New docs/IR.md (full IR reference) and docs/TERRAFORM-VERSIONS.md (support matrix, release history, feature registry, next-release checklist).
  • Updated docs/ARCHITECTURE.md, docs/CONFORMANCE.md, docs/ROADMAP.md, README.md.

Why

pkg/ir is the contract between every parser and every emitter, but it had no reference. Separately, every top-level construct added after Terraform 1.0 was silently discarded, which contradicted the project rule that unsupported constructs warn rather than disappear.

Verification

gofmt -l .                                  # clean
go vet ./...                                # clean
go test ./...                               # pass
go test -race ./pkg/...                     # pass
OPENIAC_CORPUS=1 go test ./pkg/parsers/hcl -run TestParseConformanceCorpus -count=1
make build && make wasm
make conformance-verify                     # 853 fixtures, 6471 files

Manual end-to-end: the same module written as .tf and .tf.json (moved, import, removed, check, ephemeral, action, validation, output precondition, backend "s3") converts with exit 0, emits one coded warning per construct, and produces identical TypeScript apart from the source-comment path.

Notes for review

  • preserved is deliberately separate from Resources: both emitters treat every non-data resource entry as managed.
  • .tf.json cannot express nested block lines; documented in docs/IR.md and docs/TERRAFORM-VERSIONS.md.
  • Unreliable feature detectors still appear in features but do not affect min_version.

@be-thomas be-thomas left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the native HCL and .tf.json parser paths, the IR 0.2 semantics, pkg/tfversion detection and the emitter warning path. go test ./... and go run ./cmd/openiac-conformance verify pass on this head.

This pull request changes 6532 files, which is past GitHub's 3000-file diff limit, so the reviews API rejects inline comments on pkg/ and docs/ paths with Path could not be resolved. The findings are therefore attached inline to the head commit a0299e9; the summaries below index them.

  • pkg/parsers/hcl/body.go:76 — nested blocks in a .tf.json body stay in attributes, so the CDK emitter drops them (for example versioning and ingress) while the same .tf renders them.
  • pkg/tfversion/features.go:214 — configuration_aliases is counted as a reference, so any module that aliases a provider reports provider_requirements_expressions and min_version=1.17.0.
  • pkg/ir/types.go:111 — the File doc comment says nested blocks leave file empty, but the parser sets it (and line) on every native nested block.
  • docs/IR.md:311 — says HasNode filters variable, local and module references out of the graph; the code keeps them as edges, as the document's own complete example shows.

Two behavioural issues and two documentation/comment statements that contradict the code.

Schema 0.2 adds terraform metadata, preserved top-level blocks, detected
language features and min_version. The parser now reads .tf.json alongside
native HCL and preserves moved, import, removed, check, ephemeral, action and
unknown top-level blocks instead of dropping them. pkg/tfversion maps detected
features and required_version constraints to the oldest release that accepts
the input.

Emitters report every construct they cannot convert through stable W_* codes,
including preserved blocks, backend and cloud settings, conditions and variable
validations. Generated TypeScript is unchanged.

Adds docs/IR.md as the IR reference and docs/TERRAFORM-VERSIONS.md covering
support for Terraform 1.0 through 1.17, release history and the feature
registry.
Recover JSON flat map and list attributes as nested blocks in both emitters
whenever the mapping declares that block type, so .tf.json and native HCL
generate the same TypeScript. Declare the Pulumi security group ingress and
egress blocks so the two targets agree on the schema.

Restrict provider_requirements_expressions to variable and local references.
configuration_aliases writes a provider address such as aws.west, which has
been valid since aliases existed and must not raise min_version to 1.17.

Correct the Block.File doc comment and the HasNode rule in docs/IR.md, and
state precisely how emitters recover flat JSON blocks.

@be-thomas be-thomas left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second pass against 5da16a5 (35 files).

The four findings from the first pass are resolved; each is marked inline at the line that now carries the fix.

Re-checked for regressions:

  • Native HCL output is unchanged: emitting the 853 conformance corpus input directories with the pre-fix and post-fix library produced byte-identical files and identical warning lists for both CDK and Pulumi.
  • go test ./... passes.
  • UnflattenBlocks is order-stable (sorted names), nil-safe, and recurses through nested block schemas; both emitters guard a nil mapping.

Two new defects and one low-priority behaviour change are noted inline: expression parity is not covered and currently does not hold, an empty block array is treated as an unmapped attribute, and a native block plus a same-named attribute renders twice.

Comment thread pkg/emitters/blocks.go Resolved
Comment thread pkg/tfversion/features.go Resolved
Comment thread pkg/ir/types.go Resolved
Comment thread docs/IR.md Resolved
Comment thread pkg/emitters/json_parity_test.go Resolved
Comment thread pkg/emitters/blocks.go Resolved
Comment thread pkg/emitters/awscdk/emitter.go Resolved
A .tf.json string that is exactly one interpolation, such as
"${var.port}", is evaluated by Terraform to the inner expression's
value and type, not to a string. The parser now keeps the bare
expression, so a recovered block renders the same TypeScript as the
native form instead of wrapping it in a string template.

UnflattenBlocks now treats an empty array as an absent block, matching
native HCL with no such block, and leaves an attribute alone when a
block of the same type already exists natively, so invalid input is
reported rather than rendered twice.

Docs describe the interpolation rule.

@be-thomas be-thomas left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Focused pass on 636ff1c, the follow-up to the previous review.

Checked and working:

  • singleInterpolation keeps the bare inner expression only for a string that is exactly one interpolation. Verified ${var.x}, ${ var.x }, ${lookup(var.m, "k", {a = 1})}, nested braces inside string literals (${replace(var.s, "}", "")}), $${var.x} (still quoted), multi-part and prefixed/suffixed templates (still quoted), and malformed ${var.x, ${} and ${var.x}} (fall back to the quoted template without panicking). Refs stay correct: data.aws_ami.ubuntu, module.vpc, local.a, and multiple refs.
  • Expression parity now holds. Native from_port = var.port and cidr_blocks = [aws_vpc.v.cidr_block] render byte-identically from .tf.json, as do multi-block resources (s3 versioning/cors/lifecycle/website in order), repeated ingress blocks, object and array shapes, presence blocks, lambda environment and dynamodb attributes. An empty block array matches an absent native block with no warning.
  • blockShape leaves a list containing a scalar as an attribute, and nested mapping recursion consumes nested flat blocks correctly.
  • go test ./... passes.

Two findings are inline: the empty-array rule changes native HCL output in the corpus, and the duplicate-attribute guard does not take effect in the Pulumi emitter.

Comment thread pkg/emitters/blocks.go Resolved
Comment thread pkg/emitters/pulumi/emitter.go Resolved
A body that gives the same name as both a block and an attribute is
invalid Terraform. UnflattenBlocks now consumes the attribute instead of
leaving it for the emitter to render, so Pulumi no longer emits a
duplicate object key, and reports the name so both emitters warn about
it.

An empty array for a block type means the block is absent in either
syntax. That rule now applies to native HCL as well, since the IR cannot
tell a native attribute from a recovered JSON one, and it is documented
alongside the JSON block recovery.
@be-thomas
be-thomas merged commit 24ee613 into main Oct 26, 2026
1 check passed
@be-thomas
be-thomas deleted the feat/ir-docs-tf-1x branch October 26, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant