hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.06k stars 3.32k forks source link

hcl2template: recursively evaluate local variables #13039

Closed lbajolet-hashicorp closed 3 months ago

lbajolet-hashicorp commented 3 months ago

The logic for evaluating local variables used to rely on their definition order, with some edge cases. Typically locals blocks define multiple local variables, which don't necessarily appear in the same order at evaluation as within the template, leading to inconsistent behaviour, as the order in which those are added to the list of local variables is non-deterministic.

To avoid this problem, we change how local variables are evaluated, and we're adopting a workflow similar to datasources, where the local variables first build a list of direct dependencies. Then when evaluation happens, we evaluate all the dependencies recursively for each local variable, which takes care of this issue.

As with Datasources, we add a cap to the recursion: 10. I.e. if the evaluation of a single variable provokes an infinite recursion, we stop at that point and return an error to the user, telling them to fix their template.

Closes: #13018

lbajolet-hashicorp commented 3 months ago

I think this is a good approach to this issue and not something users would expect given that Terraform locals honor the dependencies. That said we need to update the documentation for our locals as the current documentation reads

 The locals block is read as a map. Maps are not sorted, and therefore the evaluation time is not deterministic.

To avoid that, singular local blocks should be used instead. These will be evaluated in the order they are defined, and the evaluation order and time will always be the same.

I'm not sure I see where those docs are in the repo, where did you see this?

github-actions[bot] commented 2 months ago

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.