Skip to content

Parsing lookup(..., null) fails in 6.0.0 #183

Closed

Description

@ribejara-te

We use python-hcl2 to read Terraform code.

I can only replicate this exception in 6.0.0, if I downgrade to 5.1.1 it works just fine.
I'm using Python 3.13 in both cases.


When we parse a file similar to:

Details
resource "foo" "bar" {
  dynamic "foo" {
    for_each = var.foo
    content {
      foo = lookup(foo.value, "bar", null)
    }
  }
}

It throws the following exception:

Details
Traceback (most recent call last):
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 124, in _call_userfunc
    return f(children)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/hcl2/transformer.py", line 118, in function_call
    [self.to_tf_inline(arg) for arg in args[1] if arg is not Discard]
     ~~~~~~~~~~~~~~~~~^^^^^
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/hcl2/transformer.py", line 347, in to_tf_inline
    raise RuntimeError(f"Invalid type to convert to inline HCL: {type(value)}")
RuntimeError: Invalid type to convert to inline HCL: <class 'NoneType'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/hcl2/api.py", line 15, in load
    return loads(file.read(), with_meta=with_meta)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/hcl2/api.py", line 29, in loads
    return DictTransformer(with_meta=with_meta).transform(tree)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 161, in transform
    res = list(self._transform_children([tree]))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 156, in _transform_tree
    children = list(self._transform_children(tree.children))
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 146, in _transform_children
    res = self._transform_tree(c)
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 157, in _transform_tree
    return self._call_userfunc(tree, children)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/Users/ribejara/.local/share/uv/tools/stacks/lib/python3.13/site-packages/lark/visitors.py", line 128, in _call_userfunc
    raise VisitError(tree.data, tree, e)
lark.exceptions.VisitError: Error trying to process rule "function_call":

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

If we remove the lookup(..., null) line from our code it works.
If we remove just the final null attribute it also works.

Activity

  1. added
    bug
    on Jan 20, 2025
  2. 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