Repository navigation
Document the IR and support the Terraform 1.x configuration language - #2
Conversation
be-thomas
left a comment
There was a problem hiding this comment.
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.jsonbody stay inattributes, so the CDK emitter drops them (for exampleversioningandingress) while the same.tfrenders them.pkg/tfversion/features.go:214—configuration_aliasesis counted as a reference, so any module that aliases a provider reportsprovider_requirements_expressionsandmin_version=1.17.0.pkg/ir/types.go:111— theFiledoc comment says nested blocks leavefileempty, but the parser sets it (andline) on every native nested block.docs/IR.md:311— saysHasNodefilters 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.
a0299e9 to
5da16a5
Compare
be-thomas
left a comment
There was a problem hiding this comment.
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.UnflattenBlocksis 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.
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
left a comment
There was a problem hiding this comment.
Focused pass on 636ff1c, the follow-up to the previous review.
Checked and working:
singleInterpolationkeeps 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).Refsstay correct:data.aws_ami.ubuntu,module.vpc,local.a, and multiple refs.- Expression parity now holds. Native
from_port = var.portandcidr_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. blockShapeleaves 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.
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.
What changed
IR schema 0.2 (additive over 0.1)
Graphgainsterraformmetadata,preservedblocks,featuresandmin_version.Variable/Outputgain the post-1.0 fields (nullable,ephemeral,deprecated,type) plusattributes/blockscatch-alls, so settings the IR does not model are no longer dropped.Local,ModuleandProvidergainfile/lineprovenance.fileas well asline; JSON-derived blocks carry the enclosing file only.Parser
ParseDir/ParseFiles/ParseStringaccept native HCL and.tf.json, chosen by suffix.moved,import,removed,check,ephemeral,actionand unknown future blocks are preserved instead of being dropped. Preserved blocks never become nodes and never create edges.terraformsettings are extracted into typed metadata:required_version,required_providers,experiments,backend,cloud,provider_meta.parser.go,body.go,json.go,preserve.go,schema.go,terraform.go. The native path keeps its previous structure.pkg/tfversionsincerelease.min_version.ParseConstraintunderstands=,!=,>,>=,<,<=,~>and comma lists; invalid input never fails a parse.Emitters
emitters.WarnUnsupportedreports preserved blocks,terraformbackend/cloud/provider_meta, conditions and variable validations with stableW_*codes.Docs
docs/IR.md(full IR reference) anddocs/TERRAFORM-VERSIONS.md(support matrix, release history, feature registry, next-release checklist).docs/ARCHITECTURE.md,docs/CONFORMANCE.md,docs/ROADMAP.md,README.md.Why
pkg/iris 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
Manual end-to-end: the same module written as
.tfand.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
preservedis deliberately separate fromResources: both emitters treat every non-data resource entry as managed..tf.jsoncannot express nested block lines; documented indocs/IR.mdanddocs/TERRAFORM-VERSIONS.md.featuresbut do not affectmin_version.