Skip to content

Allow 'if' as HCL attribute name#6473

Draft
Gautam-aman wants to merge 11 commits intoopenrewrite:mainfrom
Gautam-aman:fix/hcl-if-as-property
Draft

Allow 'if' as HCL attribute name#6473
Gautam-aman wants to merge 11 commits intoopenrewrite:mainfrom
Gautam-aman:fix/hcl-if-as-property

Conversation

@Gautam-aman
Copy link
Copy Markdown

@Gautam-aman Gautam-aman commented Dec 31, 2025

Fixes parsing failure when if is used as an attribute name in HCL.

Terraform allows keywords as attribute names, but the current grammar only accepts Identifier, causing valid HCL to fail parsing.

This change allows if in attribute position only and adds a regression test.

Signed-off-by: Aman Gautam <amangautam2128@gmail.com>
@timtebeek
Copy link
Copy Markdown
Member

timtebeek commented Dec 31, 2025

Thanks for looking into this @Gautam-aman ; are you sure you pushed all the changes you were after? As I'm seeing no reference that identifierLike, and a failure in running the tests.

@timtebeek timtebeek marked this pull request as draft December 31, 2025 17:40
Signed-off-by: Aman Gautam <amangautam2128@gmail.com>
@Gautam-aman Gautam-aman marked this pull request as ready for review December 31, 2025 18:31
@Gautam-aman
Copy link
Copy Markdown
Author

This PR is now ready for review.

The grammar and visitor changes allow if to be used as an HCL attribute key
(e.g. if = true) while preserving its role in conditional and for expressions.

Local build and tests pass:

  • :rewrite-hcl:compileJava
  • :rewrite-hcl:test

Happy to address any feedback.

Comment thread rewrite-hcl/build.gradle.kts Outdated

identifierLike
: Identifier
| IF
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@greg-at-moderne any idea whether if is the only keyword that would need special handling? Or could there be others too?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That's a good point. I think it's not the only one.
Another real-life example is in, see this real-world mention:
https://github.com/terraform-aws-modules/terraform-aws-fsx/blob/ac8fe39a0d2428c30be6ed96a2d13026e0306e41/examples/lustre/main.tf#L73

So I think eventually we need to allow all (or at least nearly all) keywords.

Having said that, I think this PR is a net improvement.
@Gautam-aman - would you like to continue the effort and change the impl to support keywords too, not if specifically?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As a potentially larger change we can optionally rework the parser to specify where it should expect identifiers using boolean fields as also seen on

I've found Claude to be quite efficient to pick up this work, so you might not want to tackle all of that manually.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, that makes sense.
I agree the broader keyword handling and parser rework would be a larger change. For this PR, I’d prefer to keep the scope limited to if so it remains small and safe.

I’m happy to look into a follow-up PR for broader keyword support or parser refactoring if that would be useful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's more that your current change would likely result in an out-of-place keyword IF and LST element, where an identifier is expected. Let me try quickly if I can change your test to show that issue.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for checking that makes sense.
If the test exposes an unintended case where IF is accepted outside identifier positions, I’m happy to adjust the implementation accordingly once we see the result.

Comment thread rewrite-hcl/src/main/java/org/openrewrite/hcl/internal/HclParserVisitor.java Outdated
Gautam-aman and others added 2 commits January 1, 2026 01:06
…/JsonPathParserVisitor.java

Co-authored-by: Tim te Beek <timtebeek@gmail.com>
…erVisitor.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@Gautam-aman
Copy link
Copy Markdown
Author

All review feedback has been addressed and the PR is ready.
Thanks for taking a look!

@Gautam-aman Gautam-aman requested a review from timtebeek January 2, 2026 17:32
@Gautam-aman
Copy link
Copy Markdown
Author

Thanks for the review and merge , appreciate it!

@timtebeek
Copy link
Copy Markdown
Member

Thanks! There's one open question that I'd like @greg-at-moderne to weigh in on; please don't resolve that comment before then:

Mostly I'm wondering if the fix here is too specific for the problem that was reported, whereas I think there might be similar cases that we'd want to tackle here as well.

@Gautam-aman
Copy link
Copy Markdown
Author

I agree it’s worth considering whether this should be generalized beyond the reported case. I’m happy to wait for @greg-at-moderne’s input and adjust the approach if needed.

Let me know how you’d like to proceed.

@greg-at-moderne greg-at-moderne self-requested a review January 7, 2026 10:12
@timtebeek timtebeek marked this pull request as draft March 30, 2026 11:39
@timtebeek timtebeek removed their request for review March 30, 2026 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hcl parser

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

HclParser might fail if you use if as property name

3 participants