这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@walteh
Copy link
Owner

@walteh walteh commented Aug 5, 2025

Summary

  • add resolver to lazily evaluate HCL vars/env with cycle detection
  • allow HCL vars and env blocks to reference each other recursively
  • extend HCL E2E test and add unit tests for recursive resolution

Testing

  • go test ./...

Prompt

Task 9: Recursive Resolution Support for HCL Variables

Purpose:
Enable recursive and referential evaluation of HCL expressions in vars and env, such that later values can refer to previously declared values, including through expression functions. This allows building rich, composable configurations similar to Terraform or Docker Bake.


Requirements:

  • HCL variable and env blocks must support referencing other vars.X and env.X keys.
  • Recursive references must be resolved lazily at runtime, not during initial decode.
  • Evaluation must support both string interpolation ("Hello, ${vars.NAME}") and function composition (upper(vars.GREETING)).
  • Self-references must be detected and produce an error (e.g., NAME = "${vars.NAME}").

Scope:

  • Applies to both global and task-scoped vars and env blocks.
  • Evaluation order must respect declaration order in HCL but allow resolution of later values as long as their dependencies are resolvable.

Implementation Hints (Do Not Copy Code):

  • Variables must be stored as hcl.Expression during parsing.
  • During task execution, a recursive evaluation context (e.g., EvalContext) must resolve values on-demand.
  • Use memoization to cache evaluated values and avoid duplicate computation.
  • Add cycle detection to prevent infinite loops in indirect self-references.

Validation:

  • Unit test with:

    vars {
      GREETING = "Hello, ${vars.NAME}!"
      NAME = "BOB"
      UPPER_GREETING = upper(vars.GREETING)
    }

    Assert:

    • GREETING == "Hello, BOB!"
    • UPPER_GREETING == "HELLO, BOB!"
  • Test ordering independence:

    vars {
      FINAL = upper(vars.INTERMEDIATE)
      INTERMEDIATE = "${vars.BASE} + ok"
      BASE = "yup"
    }

    Assert: FINAL == "YUP + OK"

  • Test invalid cyclic reference:

    vars {
      LOOP = "${vars.LOOP}"
    }

    Should fail with error indicating cyclic variable reference.

  • Extend HCLE2ETest to include realistic example that uses multiple layers of recursive expression resolution.


By completing this task, the HCL loader and runtime will fully support dynamic configuration patterns, improving flexibility and parity with advanced DSLs like Terraform.


https://chatgpt.com/codex/tasks/task_e_68922f2b906483309838d8c558bc399a

@walteh walteh merged commit c84f532 into codex/hcl Aug 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants