Skip to content

Parsing a for loop fails if it includes a conditional is used to compare to null in 6.0.0 #185

Closed

Description

@xRokco

Hi,

Since the upgrade to 6.0.0 the following Terraform fails to parse:

locals {
  loop = { for k, v in data.aws_iam_policy_document.inline_policy : k => v if v.statement != null }
}

This appears to be related to the v.statement != null part. The following hypotheticals all work:

if v.statement != "null"
if v.statement != 1
if v.statement != []
if v.statement

Also, the symbols don't matter, tried with ==, >, etc and they all similarly fail if compared with null.

A workaround for the moment is this sort of thing:

locals {
  loop = { for k, v in data.aws_iam_policy_document.inline_policy : k => v if can(coalesce(v.statement)) }
}

coalesce() will error if v.statement is null, which gets caught by can() to evaluate as false. However, this will also evaluate to false if v.statement is the empty string, so it's not identical to != null.

Below is the full stacktrace:

Traceback (most recent call last):
  File "/Users/mattcarrick/repos/lc-39/azure-sso/bin/unique.py", line 14, in <module>
    for local in hcl2.load(file)["locals"]:
                 ^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/hcl2/api.py", line 15, in load
    return loads(file.read(), with_meta=with_meta)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/hcl2/api.py", line 29, in loads
    return DictTransformer(with_meta=with_meta).transform(tree)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 161, in transform
    return self._transform_tree(tree)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 157, in _transform_tree
    return self._call_userfunc(tree, children)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/lark/visitors.py", line 128, in _call_userfunc
    raise VisitError(tree.data, tree, e)
lark.exceptions.VisitError: Error trying to process rule "binary_term":

Invalid type to convert to inline HCL: <class 'NoneType'>

Activity

  1. added
    bug
    on Jan 21, 2025
  2. kkozik-amplify commented on Jan 21, 2025

    @kkozik-amplify
    Collaborator

    Hey @xRokco, thanks for taking time to report this.
    The issue was already fixed in #184 and we're hoping to release it at the end of this week

  3. xRokco commented on Jan 21, 2025

    @xRokco
    Author

    Ah, my bad for not seeing it. Cheers!

  4. added a commit that references this issue on Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions