Skip to content

HclParser might fail if you use if as property name #6446

@greg-at-moderne

Description

@greg-at-moderne

What version of OpenRewrite are you using?

Current main = 9e52e99

What is the smallest, simplest way to reproduce the problem?

The following test when added to HclParserTest:

    @Test
    void usingIf() {
        rewriteRun(
          hcl(
            """
            resource "not_a_real_one_for_sure" deny {
              if = "a"
            }
            """
          )
        );
    }

fails with:

Caused by: org.openrewrite.hcl.HclParsingException: Syntax error in file.tf at line 2:2 mismatched input 'if' expecting {'null', '}', Identifier}.
	at org.openrewrite.hcl.HclParser$ForwardingErrorListener.syntaxError(HclParser.java:108)
	... 26 more
Caused by: org.antlr.v4.runtime.InputMismatchException
	at org.antlr.v4.runtime.DefaultErrorStrategy.recoverInline(DefaultErrorStrategy.java:485)
	at org.antlr.v4.runtime.Parser.match(Parser.java:208)
	at org.openrewrite.hcl.internal.grammar.HCLParser.blockExpr(HCLParser.java:1111)
	... 21 more

while I think this is a valid HCL source.
Terraform doesn't seem to have a problem with it.

OSS Repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghclparsertest providedAlready replicated with a unit test, using JUnit pioneer's ExpectedToFail

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions