Skip to content

hq: .[select()] syntax fails — path tokenizer splits on dot before bracket #290

@kkozik-amplify

Description

@kkozik-amplify

Description

module.[select(.source | contains("docker_application"))] produces a syntax error:

{"error": "query_syntax", "message": "Invalid segment name before [select(): ''"}

The ~[select()] tilde syntax works correctly: module~[select(.source | contains("docker_application"))].

The simple .[] jq alias also works: module.[].

Root Cause

Two issues in hcl2/query/path.py:

  1. Line 45 — The .[] jq alias replacement (path_str.replace(".[]", "[*]")) only handles the exact literal .[], not .[select(...)] or .[*].

  2. Line 159_split_path() treats . as a segment separator without looking ahead. When . is immediately followed by [, it splits there, producing an empty segment name '' before the [select(...)] bracket. Then _extract_select() (line 211) validates seg_name = part[:0] → empty string → raises QuerySyntaxError.

Expected Behavior

module.[select(...)] should work the same as module~[select(...)] — the . before [ should be treated as jq-style bracket application, not as a path separator.

Workaround

Use the tilde syntax: module~[select(.source | contains("docker_application"))]

Suggested Fix

In _split_path(), when a . is immediately followed by [, don't treat it as a separator — either consume it as part of the bracket syntax or apply the same wildcard expansion that .[] gets. Existing tests for .[select()] patterns are missing in test/unit/query/test_path.py.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginternal-testingFound during internal test runsqueryhcl2.query() API, DocumentView, NodeView

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions