You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Trying to construct HCL which uses blocks, the output of yq in HCL mode is fragile as to whether the input strings contain certain characters.
Two input files, moved-simple.tf and moved-modules.tf
moved-simple.tf:
moved {
from="OLD"to="NEW"
}
moved-modules.tf:
moved {
from="module[\"OLD\"].foo"to="module[\"NEW\"].foo"
}
Using a command of yq -phcl -ojson | yq -pjson -ohcl to check round-tripping, we see some issues.
% yq < moved-simple.tf -phcl -ojson | yq -pjson -ohclmoved { from = OLD to = NEW}
% yq < moved-modules.tf -phcl -ojson | yq -pjson -ohclmoved {}moved = { from = "module[\"OLD\"].foo" to = "module[\"NEW\"].foo"}
You can see that in the simple case, we get a block output and round-trip fine. In the case which differs only in the string content of the data input, we no longer round-trip fine, and get both a block (empty) and a key/value assignment of an object.
Version of yq: v4.52.2
Operating system: linux
Installed via: go install
Additional context
Add any other context about the problem here.
I constructed some moved blocks from a terraform state pull, using xargs and shell echo. I had hoped to avoid the fragile shell and use yq instead but can't figure out how we signal when input is supposed to be block vs object assignment and can't make it reliable in the presence of the sort of strings encountered in a terraform state file.
IMO, It may not be possible to reliably convert between HCL and other types unless you define a very arbitrary format. See hcl2json for another attempt to convert HCL to a JSON format, but I think it would also struggle with conversion back to HCL
Version of yq: v4.52.4
Operating system: osx
Installed via: homebrew
Activity
phene commentedon Feb 26, 2026
Yeah, blocks vs kv maps are problematic
foo.tf:yq -phcl -oyml foo.tf | yq -pyml -ohclAnd conversion to JSON runs into this issue
IMO, It may not be possible to reliably convert between HCL and other types unless you define a very arbitrary format. See hcl2json for another attempt to convert HCL to a JSON format, but I think it would also struggle with conversion back to HCL
Version of yq: v4.52.4
Operating system: osx
Installed via: homebrew