Repository navigation
Closed
Labels
enhancementNew feature or request
Description
Context
We use this library to parse and modify Terraform files. We do this by reading a particular resource using this library, then using the data from that to write a new resource back to the file in the same place, removing the old one (using with_meta to know where it was). However, since no comments are output, we end up removing any that were present inside the original block.
Possible Enhancement
If possible, it would be really helpful to optionally include parsed comments in the output from loads/load.
For example, if I parse a resource like this:
resource "aws_ec2_tag" "example" {
# This is a comment
resource_id = aws_vpn_connection.example.transit_gateway_attachment_id
key = "Name"
value = "Hello World"
}I currently get the following dictionary representation:
{
"resource": [
{
"aws_ec2_tag": {
"example": {
"resource_id": "${aws_vpn_connection.example.transit_gateway_attachment_id}",
"key": "Name",
"value": "Hello World",
"__start_line__": 1,
"__end_line__": 6
}
}
}
]
}But maybe comment data could be added alongside it, something like:
{
"resource": [
{
"aws_ec2_tag": {
"example": {
"resource_id": "${aws_vpn_connection.example.transit_gateway_attachment_id}",
"key": "Name",
"value": "Hello World",
"__start_line__": 1,
"__end_line__": 6,
"__comments__": [
{
"__start_line__": 2,
"__end_line__": 2,
"text": "This is a comment"
}
]
}
}
}
]
}Reacted by Leslie Lam, Ryan Delaney, Marek Kuczynski, jaredhinzecs, Celeste Gambardella, Diogo Lemos, Bow Archer, JackyCCChen, Leletir and Thayne McCombs
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
IButskhrikidze commentedon Jun 20, 2023
Seems would be helpful for some cases but requires making some keys reserved.
for instance, what if comments will be key itself?
Arianna2028 commentedon Aug 16, 2023
Yeah, I guess it would mean the
__comments__key would be reserved. Isn't this technically true for__start_line__and__end_line__already as well, though?Alternatively, maybe
__comments__could be renamed to something with characters that are not considered valid in Terraform, so there is no possible conflict? I believe symbols are not considered valid syntax, so that might work.jaredhinzecs commentedon Sep 13, 2024
This would be a very useful feature for my Team. Currently, we have existing Terraform file structures we require people to duplicate and lightly modify select fields for various deployments. We use comments within these files to help segment logical blocks of code and provide context to readers. Presently, I have created a script to generate these file structures, but it wouldn't pass in a code review due to the comments being deleted. I'd love if this library allowed users to maintain the entire layout of whichever file is loaded which includes comments AND blank lines (which may not be commented). Formatting can be handled by other tooling imo. Though, maintaining overall format wouldn't be a bad idea either. I just wouldn't let that hold up the implementation of comment and blank line retention.
decoded4620 commentedon Mar 21, 2025
Are there any planned contributions or initiatives to support comments? We are making use of this library to augment terraform via automated process - and the current terraform has copious comments - which of course all are removed once the file is regenerated. Some of the comments are important - which we'd rather not manage in a Notion Page or elsewhere / e.g. README since this terraform code is used in multiple environments.
Fix comment serialization: multi-token NL_OR_COMMENT and classificati…
Add adjacent comment support to hq BlockView and AttributeView queries (
Fix comment serialization: multi-token NL_OR_COMMENT and classificati…
Add adjacent comment support to hq BlockView and AttributeView queries (
kkozik-amplify commentedon Apr 7, 2026
Fixed in the latest release:
https://github.com/amplify-education/python-hcl2/releases/tag/v8.1.0
Fix comment serialization: multi-token NL_OR_COMMENT and classificati…
Add adjacent comment support to hq BlockView and AttributeView queries (