Skip to content

Null as Identifier in HCL #5780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 25, 2025
Merged

Null as Identifier in HCL #5780

merged 5 commits into from
Jul 25, 2025

Conversation

MBoegers
Copy link
Contributor

What's changed?

null is a valid key for objects and variables in HCL. We blocked it as syntactically error.

What's your motivation?

allow basic setups as

terraform {
  required_providers {
    null = {
      source = "hashicorp/null"
      version = "=3.2.3" 
    }
  }
}

Anything in particular you'd like reviewers to focus on?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

null seems to be common in TF

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jul 21, 2025
@MBoegers MBoegers self-assigned this Jul 21, 2025
@MBoegers MBoegers added the bug Something isn't working label Jul 21, 2025
@MBoegers MBoegers requested a review from greg-at-moderne July 21, 2025 14:29
github-actions[bot]

This comment was marked as outdated.

@MBoegers MBoegers force-pushed the null-as-attribute branch from d6da229 to dafc9b8 Compare July 22, 2025 12:31
github-actions[bot]

This comment was marked as outdated.

@MBoegers
Copy link
Contributor Author

I know we usually don't force push, I just neede to get the PR displaying right

@MBoegers MBoegers marked this pull request as ready for review July 22, 2025 13:21
Comment on lines 21 to 23
attribute
: Identifier ASSIGN expression
: (Identifier | NULL) ASSIGN expression
;
Copy link
Member

Choose a reason for hiding this comment

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

Somewhat surprised not to see any corresponding change in the tree package here around:

class Attribute implements BodyContent, Expression {

Is that just a problem with my expectations? Could you help explain why such a change wasn't needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Token NULL here is not the OOP null as no object. In the G4 the NULL is a special identifier with the value "null" and will be parsed into a Hcl.Identifier with the name="null".

The NULL-Token is only used in 2 locations to selectively allow "null" as an identifier in these locations (as object keys and variable names). This way we still reject corrupt files at the parser level when null is used in a different location or no identifier is provided.

You see no change in the Hcl.Tree because none is needed to represent an Hcl.Identifier with value "null". And I choose the minimal change.
We could think about mirroring this setup with a specialization of Hcl.Identifier to explicitly model "null" as an identifier in these cases.
I think the gain would be very low, as null is simply valid in these cases, and the risk of introducing new LST elements is higher.

For example:

terraform {
required_providers {
  null = {
    source = "hashicorp/null"
    version = "=3.2.3"
  }
}

is parsed into (sorry for no TreeVisitingPrinter):
grafik

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While writing these lines, I realize that NULL is probably not a great name for the Token.

@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Jul 22, 2025
@timtebeek timtebeek changed the title Null as attribute Null as attribute in HCL Jul 22, 2025
@MBoegers MBoegers changed the title Null as attribute in HCL Null as Identifier in HCL Jul 23, 2025
@MBoegers MBoegers requested a review from timtebeek July 23, 2025 15:08
Copy link
Contributor

@greg-at-moderne greg-at-moderne left a comment

Choose a reason for hiding this comment

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

The g4 change and related changes look fine to me.

I am only slightly concerned with the changes to the JsonPath* classes. It's hard to tell for sure, but I hope they are only whitespace change as there's no reason for these to be altered. Github's "Hide Whitespace" option in the review interface hides only some of them, but they all look trivial.

@MBoegers
Copy link
Contributor Author

agree they look strange let me reset to main

@MBoegers
Copy link
Contributor Author

@greg-at-moderne the answer is, I executd the ./gradlew rewrite-hcl:generateAntlrSources task which also generated the HCL internal JSON grammar. rolled back because its a disturbing change

github-actions[bot]

This comment was marked as outdated.

@timtebeek timtebeek merged commit 4bead32 into main Jul 25, 2025
2 checks passed
@timtebeek timtebeek deleted the null-as-attribute branch July 25, 2025 11:28
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jul 25, 2025
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
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants